11module FixedPointNumbers
22
3- import Base: == , < , <= , - , + , * , / , ~ , isapprox,
3+ # arithmetic functions (operators) are imported in "arithmetic.jl"
4+ import Base: == , < , <= , ~ , isapprox,
45 convert, promote_rule, print, show, bitstring, abs, decompose,
56 isnan, isinf, isfinite, isinteger,
67 zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, reinterpret,
78 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,
910 signed, unsigned, copysign, flipsign, signbit,
1011 length
1112
1213import Random: Random, AbstractRNG, SamplerType, rand!
1314
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-
1715using Base: @pure
1816
1917"""
@@ -26,7 +24,6 @@ of fraction bits.
2624"""
2725abstract type FixedPoint{T <: Integer , f} <: Real end
2826
29-
3027export
3128 FixedPoint,
3229 Fixed,
@@ -35,12 +32,18 @@ export
3532# "special" typealiases
3633 # Q and N typealiases are exported in separate source files
3734# 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
4447
4548include (" utilities.jl" )
4649
@@ -58,16 +61,18 @@ signbits(::Type{X}) where {T, X <: FixedPoint{T}} = T <: Unsigned ? 0 : 1
5861nbitsint (:: Type{X} ) where {X <: FixedPoint } = bitwidth (X) - nbitsfrac (X) - signbits (X)
5962
6063# 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)
6265wrapping_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = x % X
6366saturating_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = clamp (x, X)
6467checked_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _convert (X, x)
68+ unchecked_mul (x:: Real , :: Type{X} ) where {X <: FixedPoint } = x % X
6569
6670# 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)
6872wrapping_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
6973saturating_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
7074checked_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
75+ unchecked_rem (x:: Real , :: Type{X} ) where {X <: FixedPoint } = _rem (x, X)
7176
7277# constructor-style conversions
7378(:: 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
325330end
326331checked_mod (x:: X , y:: X ) where {X <: FixedPoint } = checked_rem (x, y, RoundDown)
327332
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-
353333function minmax (x:: X , y:: X ) where {X <: FixedPoint }
354334 a, b = minmax (reinterpret (x), reinterpret (y))
355335 X (a,0 ), X (b,0 )
0 commit comments