@@ -11,7 +11,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox,
1111
1212import Statistics # for _mean_promote
1313
14- using Base. Checked: checked_add, checked_sub, checked_div
14+ import Base. Checked: checked_neg, checked_add, checked_sub, checked_div
1515
1616using Base: @pure
1717
3434# "special" typealiases
3535 # Q and N typealiases are exported in separate source files
3636# Functions
37- scaledual
37+ scaledual,
38+ wrapping_neg, wrapping_add, wrapping_sub,
39+ saturating_neg, saturating_add, saturating_sub
3840
3941include (" utilities.jl" )
4042
@@ -179,6 +181,49 @@ floattype(::Type{Base.TwicePrecision{T}}) where T<:Union{Float16,Float32} = wide
179181
180182float (x:: FixedPoint ) = convert (floattype (x), x)
181183
184+ # wrapping arithmetic
185+ wrapping_neg (x:: X ) where {X <: FixedPoint } = X (- x. i, 0 )
186+ wrapping_add (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i + y. i, 0 )
187+ wrapping_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (x. i - y. i, 0 )
188+
189+ # saturating arithmetic
190+ saturating_neg (x:: X ) where {X <: FixedPoint } = X (~ min (x. i - true , x. i), 0 )
191+ saturating_neg (x:: X ) where {X <: FixedPoint{<:Unsigned} } = zero (X)
192+
193+ function saturating_add (x:: X , y:: X ) where {X <: FixedPoint }
194+ r, f = Base. Checked. add_with_overflow (x. i, y. i)
195+ ifelse (f, ifelse (y. i < 0 , typemin (X), typemax (X)), X (r, 0 ))
196+ end
197+ saturating_add (x:: X , y:: X ) where {X <: FixedPoint{<:Unsigned} } = X (x. i + min (~ x. i, y. i), 0 )
198+
199+ function saturating_sub (x:: X , y:: X ) where {X <: FixedPoint }
200+ r, f = Base. Checked. sub_with_overflow (x. i, y. i)
201+ ifelse (f, ifelse (y. i < 0 , typemax (X), typemin (X)), X (r, 0 ))
202+ end
203+ saturating_sub (x:: X , y:: X ) where {X <: FixedPoint{<:Unsigned} } = X (x. i - min (x. i, y. i), 0 )
204+
205+ # checked arithmetic
206+ checked_neg (x:: X ) where {X <: FixedPoint } = X (checked_neg (x. i), 0 )
207+ checked_add (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_add (x. i, y. i), 0 )
208+ checked_sub (x:: X , y:: X ) where {X <: FixedPoint } = X (checked_sub (x. i, y. i), 0 )
209+
210+ # default arithmetic
211+ const DEFAULT_ARITHMETIC = :wrapping
212+
213+ for (op, name) in ((:- , :neg ), )
214+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
215+ @eval begin
216+ $ op (x:: X ) where {X <: FixedPoint } = $ f (x)
217+ end
218+ end
219+ for (op, name) in ((:+ , :add ), (:- , :sub ))
220+ f = Symbol (DEFAULT_ARITHMETIC, :_ , name)
221+ @eval begin
222+ $ op (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x, y)
223+ end
224+ end
225+
226+
182227function minmax (x:: X , y:: X ) where {X <: FixedPoint }
183228 a, b = minmax (reinterpret (x), reinterpret (y))
184229 X (a,0 ), X (b,0 )
@@ -228,12 +273,12 @@ for f in (:(==), :<, :<=, :div, :fld, :fld1)
228273 $ f (x:: X , y:: X ) where {X <: FixedPoint } = $ f (x. i, y. i)
229274 end
230275end
231- for f in (:- , : ~ , :abs )
276+ for f in (:~ , :abs )
232277 @eval begin
233278 $ f (x:: X ) where {X <: FixedPoint } = X ($ f (x. i), 0 )
234279 end
235280end
236- for f in (:+ , : - , : rem , :mod , :mod1 , :min , :max )
281+ for f in (:rem , :mod , :mod1 , :min , :max )
237282 @eval begin
238283 $ f (x:: X , y:: X ) where {X <: FixedPoint } = X ($ f (x. i, y. i), 0 )
239284 end
0 commit comments