1
1
module FixedPointNumbers
2
2
3
- import Base: == , < , <= , - , + , * , / , ~ , isapprox,
3
+ # arithmetic functions (operators) are imported in "arithmetic.jl"
4
+ import Base: == , < , <= , ~ , isapprox,
4
5
convert, promote_rule, print, show, bitstring, abs, decompose,
5
6
isnan, isinf, isfinite, isinteger,
6
7
zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, reinterpret,
7
8
big, rationalize, float, trunc, round, floor, ceil, bswap, clamp,
8
- div, fld, cld, rem, mod, mod1, fld1, min, max, minmax,
9
+ mod1, fld1, min, max, minmax,
9
10
signed, unsigned, copysign, flipsign, signbit,
10
11
length
11
12
12
13
import Random: Random, AbstractRNG, SamplerType, rand!
13
14
14
- import Base. Checked: checked_neg, checked_abs, checked_add, checked_sub, checked_mul,
15
- checked_div, checked_fld, checked_cld, checked_rem, checked_mod
16
-
17
15
using Base: @pure
18
16
19
17
"""
@@ -26,7 +24,6 @@ of fraction bits.
26
24
"""
27
25
abstract type FixedPoint{T <: Integer , f} <: Real end
28
26
29
-
30
27
export
31
28
FixedPoint,
32
29
Fixed,
@@ -35,12 +32,18 @@ export
35
32
# "special" typealiases
36
33
# Q and N typealiases are exported in separate source files
37
34
# Functions
38
- scaledual,
39
- wrapping_neg, wrapping_abs, wrapping_add, wrapping_sub, wrapping_mul,
40
- wrapping_div, wrapping_fld, wrapping_cld, wrapping_rem, wrapping_mod,
41
- saturating_neg, saturating_abs, saturating_add, saturating_sub, saturating_mul,
42
- saturating_div, saturating_fld, saturating_cld, saturating_rem, saturating_mod,
43
- wrapping_fdiv, saturating_fdiv, checked_fdiv
35
+ scaledual
36
+
37
+ include (" arithmetic.jl" )
38
+
39
+ import . FixedPointArithmetic
40
+ import . FixedPointArithmetic: Wrapping, Saturating, Checked, Unchecked
41
+
42
+ for modname in (:Wrapping , :Saturating , :Checked , :Unchecked )
43
+ for name in names (getproperty (FixedPointArithmetic, modname))
44
+ @eval import .$ modname: $ name
45
+ end
46
+ end
44
47
45
48
include (" utilities.jl" )
46
49
@@ -58,16 +61,18 @@ signbits(::Type{X}) where {T, X <: FixedPoint{T}} = T <: Unsigned ? 0 : 1
58
61
nbitsint (:: Type{X} ) where {X <: FixedPoint } = bitwidth (X) - nbitsfrac (X) - signbits (X)
59
62
60
63
# construction using the (approximate) intended value, i.e., N0f8
61
- * (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _convert (X, x)
64
+ Base.: * (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _convert (X, x)
62
65
wrapping_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = x % X
63
66
saturating_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = clamp (x, X)
64
67
checked_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _convert (X, x)
68
+ unchecked_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = x % X
65
69
66
70
# type modulus
67
- rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
71
+ Base . rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
68
72
wrapping_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
69
73
saturating_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
70
74
checked_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
75
+ unchecked_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
71
76
72
77
# constructor-style conversions
73
78
(:: Type{X} )(x:: X ) where {X <: FixedPoint } = x
@@ -325,31 +330,6 @@ function checked_rem(x::X, y::X, r::RoundingMode = RoundToZero) where {T, X <: F
325
330
end
326
331
checked_mod (x:: X , y:: X ) where {X <: FixedPoint } = checked_rem (x, y, RoundDown)
327
332
328
- # default arithmetic
329
- const DEFAULT_ARITHMETIC = :wrapping
330
-
331
- for (op, name) in ((:- , :neg ), (:abs , :abs ))
332
- f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
333
- @eval begin
334
- $ op (x:: X ) where {X <: FixedPoint } = $ f (x)
335
- end
336
- end
337
- for (op, name) in ((:+ , :add ), (:- , :sub ), (:* , :mul ))
338
- f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
339
- @eval begin
340
- $ op (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x, y)
341
- end
342
- end
343
- # force checked arithmetic
344
- / (x:: X , y:: X ) where {X <: FixedPoint } = checked_fdiv (x, y)
345
- div (x:: X , y:: X , r:: RoundingMode = RoundToZero) where {X <: FixedPoint } = checked_div (x, y, r)
346
- fld (x:: X , y:: X ) where {X <: FixedPoint } = checked_div (x, y, RoundDown)
347
- cld (x:: X , y:: X ) where {X <: FixedPoint } = checked_div (x, y, RoundUp)
348
- rem (x:: X , y:: X ) where {X <: FixedPoint } = checked_rem (x, y, RoundToZero)
349
- rem (x:: X , y:: X , :: RoundingMode{:Down} ) where {X <: FixedPoint } = checked_rem (x, y, RoundDown)
350
- rem (x:: X , y:: X , :: RoundingMode{:Up} ) where {X <: FixedPoint } = checked_rem (x, y, RoundUp)
351
- mod (x:: X , y:: X ) where {X <: FixedPoint } = checked_rem (x, y, RoundDown)
352
-
353
333
function minmax (x:: X , y:: X ) where {X <: FixedPoint }
354
334
a, b = minmax (reinterpret (x), reinterpret (y))
355
335
X (a,0 ), X (b,0 )
0 commit comments