@@ -10,7 +10,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
1010
1111import Statistics # for _mean_promote
1212
13- using Base. Checked: checked_add, checked_sub, checked_div
13+ import Base. Checked: checked_neg, checked_add, checked_sub, checked_div
1414
1515using Base: @pure
1616
3333# "special" typealiases
3434 # Q and N typealiases are exported in separate source files
3535# Functions
36- scaledual
36+ scaledual,
37+ wrapping_neg, wrapping_add, wrapping_sub,
38+ saturating_neg, saturating_add, saturating_sub
3739
3840include (" utilities.jl" )
3941
@@ -166,6 +168,47 @@ floattype(::Type{Base.TwicePrecision{T}}) where T<:Union{Float16,Float32} = wide
166168
167169float (x:: FixedPoint ) = convert (floattype (x), x)
168170
171+ # wrapping arithmetic
172+ wrapping_neg (x:: X ) where {X <: FixedPoint } = X (- x. i, 0 )
173+ wrapping_add (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i + y. i, 0 )
174+ wrapping_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i - y. i, 0 )
175+
176+ # saturating arithmetic
177+ function saturating_neg (x:: X ) where {X <: FixedPoint }
178+ r, f = Base. Checked. sub_with_overflow (zero (X). i, x. i)
179+ ifelse (f, ifelse (x. i < 0 , typemax (X), typemin (X)), X (r, 0 ))
180+ end
181+ function saturating_add (x:: X , y:: X ) where {X <: FixedPoint }
182+ r, f = Base. Checked. add_with_overflow (x. i, y. i)
183+ ifelse (f, ifelse (y. i < 0 , typemin (X), typemax (X)), X (r, 0 ))
184+ end
185+ function saturating_sub (x:: X , y:: X ) where {X <: FixedPoint }
186+ r, f = Base. Checked. sub_with_overflow (x. i, y. i)
187+ ifelse (f, ifelse (y. i < 0 , typemax (X), typemin (X)), X (r, 0 ))
188+ end
189+
190+ # checked arithmetic
191+ checked_neg (x:: X ) where {X <: FixedPoint } = X (checked_neg (x. i), 0 )
192+ checked_add (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_add (x. i, y. i), 0 )
193+ checked_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_sub (x. i, y. i), 0 )
194+
195+ # default arithmetic
196+ const DEFAULT_ARITHMETIC = :wrapping
197+
198+ for (op, name) in ((:- , :neg ), )
199+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
200+ @eval begin
201+ $ op (x:: X ) where {X <: FixedPoint } = $ f (x)
202+ end
203+ end
204+ for (op, name) in ((:+ , :add ), (:- , :sub ))
205+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
206+ @eval begin
207+ $ op (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x, y)
208+ end
209+ end
210+
211+
169212function minmax (x:: X , y:: X ) where {X <: FixedPoint }
170213 a, b = minmax (reinterpret (x), reinterpret (y))
171214 X (a,0 ), X (b,0 )
@@ -183,12 +226,12 @@ for f in (:(==), :<, :<=, :div, :fld, :fld1)
183226 $ f (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x. i, y. i)
184227 end
185228end
186- for f in (:- , : ~ , :abs )
229+ for f in (:~ , :abs )
187230 @eval begin
188231 $ f (x:: X ) where {X <: FixedPoint } = X ($ f (x. i), 0 )
189232 end
190233end
191- for f in (:+ , : - , : rem , :mod , :mod1 , :min , :max )
234+ for f in (:rem , :mod , :mod1 , :min , :max )
192235 @eval begin
193236 $ f (x:: X , y:: X ) where {X <: FixedPoint } = X ($ f (x. i, y. i), 0 )
194237 end
0 commit comments