Skip to content

Commit c64a623

Browse files
committed
Optimize clamp
1 parent 56760f3 commit c64a623

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/FixedPointNumbers.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
44
convert, promote_rule, show, isinteger, abs, decompose,
55
isnan, isinf, isfinite,
66
zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, sizeof, reinterpret,
7-
float, trunc, round, floor, ceil, bswap,
7+
float, trunc, round, floor, ceil, bswap, clamp,
88
div, fld, rem, mod, mod1, fld1, min, max, minmax,
99
rand, length
1010

@@ -173,6 +173,9 @@ end
173173

174174
bswap(x::X) where {X <: FixedPoint} = sizeof(X) == 1 ? x : X(bswap(x.i), 0)
175175

176+
clamp(x::X, lo::X, hi::X) where {X <: FixedPoint} = X(clamp(x.i, lo.i, hi.i), 0)
177+
clamp(x, ::Type{X}) where {X <: FixedPoint} = clamp(x, typemin(X), typemax(X)) % X
178+
176179
for f in (:zero, :oneunit, :one, :eps, :rawone, :rawtype, :floattype)
177180
@eval begin
178181
$f(x::FixedPoint) = $f(typeof(x))

test/fixed.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,19 @@ end
399399
@test bswap(Q0f15(0.5)) === reinterpret(Q0f15, signed(0x0040))
400400
end
401401

402+
@testset "clamp" begin
403+
@test clamp(0.5Q0f7, -0.8Q0f7, 0.8Q0f7) === 0.5Q0f7
404+
@test clamp(0.5Q0f7, 0.75Q0f7, 0.8Q0f7) === 0.75Q0f7
405+
@test clamp(0.5Q0f7, -0.8Q0f7, 0.25Q0f7) === 0.25Q0f7
406+
@test clamp(0.5, -0.8Q0f7, 0.8Q0f7) === 0.5
407+
@test clamp(0.5f0, 0.75Q0f7, 0.8Q0f7) === 0.75f0
408+
@test clamp(0.5Q0f15, -0.8Q0f7, 0.25Q0f7) === 0.25Q0f15
409+
@test clamp(0.5Q0f7, -Inf, Inf) === 0.5
410+
@test clamp(0.5, Q0f7) === 0.5Q0f7
411+
@test clamp(-1.5f0, Q0f7) === -1.0Q0f7
412+
@test clamp(1.5Q1f6, Q0f7) === 0.992Q0f7
413+
end
414+
402415
@testset "Promotion within Fixed" begin
403416
@test @inferred(promote(Q0f7(0.25), Q0f7(0.75))) ===
404417
(Q0f7(0.25), Q0f7(0.75))

test/normed.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,19 @@ end
318318
@test minmax(N0f8(0.8), N0f8(0.2)) === (N0f8(0.2), N0f8(0.8))
319319
end
320320

321+
@testset "clamp" begin
322+
@test clamp(0.5N0f8, 0.2N0f8, 0.8N0f8) === 0.5N0f8
323+
@test clamp(0.5N0f8, 0.6N0f8, 0.8N0f8) === 0.6N0f8
324+
@test clamp(0.5N0f8, 0.2N0f8, 0.4N0f8) === 0.4N0f8
325+
@test clamp(0.5, 0.2N0f8, 0.8N0f8) === 0.5
326+
@test clamp(0.5f0, 0.6N0f8, 0.8N0f8) === 0.6f0
327+
@test clamp(0.5N0f16, 0.2N0f8, 0.4N0f8) === 0.4N0f16
328+
@test clamp(0.6N0f8, -Inf, Inf) === 0.6
329+
@test clamp(0.5, N0f8) === 0.5N0f8
330+
@test clamp(-1.0f0, N0f8) === 0.0N0f8
331+
@test clamp(2.0N1f7, N0f8) === 1.0N0f8
332+
end
333+
321334
@testset "unit range" begin
322335
@test length(N0f8(0):N0f8(1)) == 2
323336
@test length(N0f8(1):N0f8(0)) == 0

0 commit comments

Comments
 (0)