@@ -12,7 +12,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
1212import Statistics # for _mean_promote
1313import Random: Random, AbstractRNG, SamplerType, rand!
1414
15- using Base. Checked: checked_add, checked_sub, checked_div
15+ import Base. Checked: checked_neg, checked_add, checked_sub, checked_div
1616
1717using Base: @pure
1818
3535# "special" typealiases
3636 # Q and N typealiases are exported in separate source files
3737# Functions
38- scaledual
38+ scaledual,
39+ wrapping_neg, wrapping_add, wrapping_sub,
40+ saturating_neg, saturating_add, saturating_sub
3941
4042include (" utilities.jl" )
4143
@@ -180,6 +182,45 @@ floattype(::Type{Base.TwicePrecision{T}}) where T<:Union{Float16,Float32} = wide
180182
181183float (x:: FixedPoint ) = convert (floattype (x), x)
182184
185+ # wrapping arithmetic
186+ wrapping_neg (x:: X ) where {X <: FixedPoint } = X (- x. i, 0 )
187+ wrapping_add (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i + y. i, 0 )
188+ wrapping_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i - y. i, 0 )
189+
190+ # saturating arithmetic
191+ saturating_neg (x:: X ) where {X <: FixedPoint } = X (~ min (x. i - true , x. i), 0 )
192+ saturating_neg (x:: X ) where {X <: FixedPoint{<:Unsigned} } = zero (X)
193+
194+ saturating_add (x:: X , y:: X ) where {X <: FixedPoint } =
195+ X (x. i + ifelse (x. i < 0 , max (y. i, typemin (x. i) - x. i), min (y. i, typemax (x. i) - x. i)), 0 )
196+ saturating_add (x:: X , y:: X ) where {X <: FixedPoint{<:Unsigned} } = X (x. i + min (~ x. i, y. i), 0 )
197+
198+ saturating_sub (x:: X , y:: X ) where {X <: FixedPoint } =
199+ X (x. i - ifelse (x. i < 0 , min (y. i, x. i - typemin (x. i)), max (y. i, x. i - typemax (x. i))), 0 )
200+ saturating_sub (x:: X , y:: X ) where {X <: FixedPoint{<:Unsigned} } = X (x. i - min (x. i, y. i), 0 )
201+
202+ # checked arithmetic
203+ checked_neg (x:: X ) where {X <: FixedPoint } = X (checked_neg (x. i), 0 )
204+ checked_add (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_add (x. i, y. i), 0 )
205+ checked_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_sub (x. i, y. i), 0 )
206+
207+ # default arithmetic
208+ const DEFAULT_ARITHMETIC = :wrapping
209+
210+ for (op, name) in ((:- , :neg ), )
211+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
212+ @eval begin
213+ $ op (x:: X ) where {X <: FixedPoint } = $ f (x)
214+ end
215+ end
216+ for (op, name) in ((:+ , :add ), (:- , :sub ))
217+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
218+ @eval begin
219+ $ op (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x, y)
220+ end
221+ end
222+
223+
183224function minmax (x:: X , y:: X ) where {X <: FixedPoint }
184225 a, b = minmax (reinterpret (x), reinterpret (y))
185226 X (a,0 ), X (b,0 )
@@ -229,12 +270,12 @@ for f in (:(==), :<, :<=, :div, :fld, :fld1)
229270 $ f (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x. i, y. i)
230271 end
231272end
232- for f in (:- , : ~ , :abs )
273+ for f in (:~ , :abs )
233274 @eval begin
234275 $ f (x:: X ) where {X <: FixedPoint } = X ($ f (x. i), 0 )
235276 end
236277end
237- for f in (:+ , : - , : rem , :mod , :mod1 , :min , :max )
278+ for f in (:rem , :mod , :mod1 , :min , :max )
238279 @eval begin
239280 $ f (x:: X , y:: X ) where {X <: FixedPoint } = X ($ f (x. i, y. i), 0 )
240281 end
0 commit comments