@@ -40,26 +40,11 @@ function nox(x)
40
40
unsafe_store! (flags[], 0 )
41
41
return x
42
42
end
43
+
43
44
# Check exception flags in mask & throw, otherwise returning x;
44
- # always clearing exceptions. These are macros so that
45
+ # always clearing exceptions. This is a macros so that
45
46
# the error message is only evaluated if an exception occurs.
46
-
47
- macro xchk (x, args... )
48
- mask= 0x3f
49
- if ! isempty (args) && Meta. isexpr (args[end ], :(= )) && args[end ]. args[1 ] == :mask # mask=... keyword at end
50
- mask = esc (args[end ]. args[2 ])
51
- args = args[1 : end - 1 ]
52
- end
53
- quote
54
- ret = $ (esc (x))
55
- f = unsafe_load (flags[])
56
- unsafe_store! (flags[], 0 )
57
- f & $ mask != 0 && xchk_throw (f, $ (map (esc,args)... ))
58
- ret
59
- end
60
- end
61
-
62
- macro xchk1 (x, exc, args... )
47
+ macro xchk (x, exc, args... )
63
48
mask= 0x3f
64
49
if ! isempty (args) && Meta. isexpr (args[end ], :(= )) && args[end ]. args[1 ] == :mask # mask=... keyword at end
65
50
mask = esc (args[end ]. args[2 ])
@@ -74,18 +59,6 @@ macro xchk1(x, exc, args...)
74
59
end
75
60
end
76
61
77
- # separate this exception throwing code into a function to avoid
78
- # inlining it over and over in the @xchk macro
79
- function xchk_throw (f, args... )
80
- f & INEXACT != 0 && throw (InexactError (args... ))
81
- f & OVERFLOW != 0 && throw (OverflowError (args... ))
82
- f & DIVBYZERO != 0 && throw (DivideError ())
83
- f & INVALID != 0 && throw (DomainError (args... ))
84
- f & UNDERFLOW != 0 && error (" underflow" )
85
- f & UNNORMAL != 0 && error (" unnormal" )
86
- @assert false # this should be unreachable
87
- end
88
-
89
62
# ############################################################################
90
63
91
64
const rounding = Ref {Ptr{Cuint}} ()
@@ -167,7 +140,7 @@ for w in (32,64,128)
167
140
if isnan (x) && ! isnanstr (s)
168
141
throw (ArgumentError (" invalid number format $s " ))
169
142
end
170
- return @xchk1 (x, InexactError, :parse , $ BID, s)
143
+ return @xchk (x, InexactError, :parse , $ BID, s)
171
144
end
172
145
173
146
$ BID (x:: AbstractString ) = parse ($ BID, x)
@@ -233,7 +206,7 @@ for w in (32,64,128)
233
206
n = length (DIGITS) - 1
234
207
end
235
208
# rounded = round(x * exp10($BID(n)), RoundNearestTiesAway)
236
- rounded = @xchk1 (ccall (($ (bidsym (w," round_integral_nearest_away" )), libbid), $ BID, ($ BID,), x * exp10 ($ BID (n))), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
209
+ rounded = @xchk (ccall (($ (bidsym (w," round_integral_nearest_away" )), libbid), $ BID, ($ BID,), x * exp10 ($ BID (n))), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
237
210
if rounded == 0
238
211
DIGITS[1 ] = UInt8 (' 0' )
239
212
return Int32 (1 ), Int32 (1 ), signbit (x)
@@ -306,7 +279,7 @@ for w in (32,64,128)
306
279
307
280
Base. nextfloat (x:: $BID ) = nox (_nextfloat (x))
308
281
Base. prevfloat (x:: $BID ) = nox (_prevfloat (x))
309
- Base. eps (x:: $BID ) = ifelse (isfinite (x), @xchk (nextfloat (x) - x, " $($ BID) value overflow" , mask= OVERFLOW), $ (_parse (T, " NaN" )))
282
+ Base. eps (x:: $BID ) = ifelse (isfinite (x), @xchk (nextfloat (x) - x, OverflowError, " $($ BID) value overflow" , mask= OVERFLOW), $ (_parse (T, " NaN" )))
310
283
311
284
# the meaning of the exponent is different than for binary FP: it is 10^n, not 2^n:
312
285
# Base.exponent(x::$BID) = nox(ccall(($(bidsym(w,"ilogb")), libbid), Cint, ($BID,), x))
@@ -322,11 +295,11 @@ for w in (32,64,128)
322
295
end
323
296
324
297
for f in (:exp ,:log ,:sin ,:cos ,:tan ,:asin ,:acos ,:atan ,:sinh ,:cosh ,:tanh ,:asinh ,:acosh ,:atanh ,:log1p ,:expm1 ,:log10 ,:log2 ,:exp2 ,:exp10 ,:lgamma ,:sqrt ,:cbrt ,:abs )
325
- @eval Base.$ f (x:: $BID ) = @xchk (ccall (($ (bidsym (w,f)), libbid), $ BID, ($ BID,), x), " invalid operation ' $( $ f) ' on $( $ BID) " , mask= INVALID)
298
+ @eval Base.$ f (x:: $BID ) = @xchk (ccall (($ (bidsym (w,f)), libbid), $ BID, ($ BID,), x), DomainError, x , mask= INVALID)
326
299
end
327
300
328
301
for (f,c) in ((:gamma ," tgamma" ), (:- ," negate" ), (:round ," nearbyint" ))
329
- @eval Base.$ f (x:: $BID ) = @xchk (ccall (($ (bidsym (w,c)), libbid), $ BID, ($ BID,), x), " invalid operation ' $( $ c) ' on $( $ BID) " , mask= INVALID)
302
+ @eval Base.$ f (x:: $BID ) = @xchk (ccall (($ (bidsym (w,c)), libbid), $ BID, ($ BID,), x), DomainError, x , mask= INVALID)
330
303
end
331
304
332
305
for (f,c) in ((:(== )," quiet_equal" ), (:> ," quiet_greater" ), (:< ," quiet_less" ), (:(>= ), " quiet_greater_equal" ), (:(<= ), " quiet_less_equal" ))
@@ -358,7 +331,7 @@ for w in (32,64,128)
358
331
@eval promote_rule (:: Type{$BID} , :: Type{$BID′} ) = $ BID
359
332
end
360
333
if w != w′
361
- @eval Base. convert (:: Type{$BID} , x:: $BID′ ) = @xchk1 (ccall (($ (string (" __bid" ,w′," _to_" ," bid" ,w)), libbid), $ BID, ($ BID′,), x), InexactError, :convert , $ BID, x, mask= INEXACT)
334
+ @eval Base. convert (:: Type{$BID} , x:: $BID′ ) = @xchk (ccall (($ (string (" __bid" ,w′," _to_" ," bid" ,w)), libbid), $ BID, ($ BID′,), x), InexactError, :convert , $ BID, x, mask= INEXACT)
362
335
end
363
336
364
337
# promote binary*decimal -> decimal, for consistency with other operations above
@@ -379,12 +352,12 @@ for w in (32,64,128)
379
352
for (i′, i′str) in ((" Int$w′ " , " int$w′ " ), (" UInt$w′ " , " uint$w′ " ))
380
353
Ti′ = eval (Symbol (i′))
381
354
@eval begin
382
- Base. trunc (:: Type{$Ti′} , x:: $BID ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xint" )), libbid), $ Ti′, ($ BID,), x), InexactError, :trunc , $ BID, x, mask= INVALID | OVERFLOW)
383
- Base. floor (:: Type{$Ti′} , x:: $BID ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xfloor" )), libbid), $ Ti′, ($ BID,), x), InexactError, :floor , $ BID, x, mask= INVALID | OVERFLOW)
384
- Base. ceil (:: Type{$Ti′} , x:: $BID ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xceil" )), libbid), $ Ti′, ($ BID,), x), InexactError, :ceil , $ BID, x, mask= INVALID | OVERFLOW)
385
- Base. round (:: Type{$Ti′} , x:: $BID ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xrnint" )), libbid), $ Ti′, ($ BID,), x), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
386
- Base. round (:: Type{$Ti′} , x:: $BID , :: RoundingMode{:NearestTiesAway} ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xrninta" )), libbid), $ Ti′, ($ BID,), x), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
387
- Base. convert (:: Type{$Ti′} , x:: $BID ) = @xchk1 (ccall (($ (bidsym (w," to_" ,i′str," _xfloor" )), libbid), $ Ti′, ($ BID,), x), InexactError, :convert , $ BID, x)
355
+ Base. trunc (:: Type{$Ti′} , x:: $BID ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xint" )), libbid), $ Ti′, ($ BID,), x), InexactError, :trunc , $ BID, x, mask= INVALID | OVERFLOW)
356
+ Base. floor (:: Type{$Ti′} , x:: $BID ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xfloor" )), libbid), $ Ti′, ($ BID,), x), InexactError, :floor , $ BID, x, mask= INVALID | OVERFLOW)
357
+ Base. ceil (:: Type{$Ti′} , x:: $BID ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xceil" )), libbid), $ Ti′, ($ BID,), x), InexactError, :ceil , $ BID, x, mask= INVALID | OVERFLOW)
358
+ Base. round (:: Type{$Ti′} , x:: $BID ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xrnint" )), libbid), $ Ti′, ($ BID,), x), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
359
+ Base. round (:: Type{$Ti′} , x:: $BID , :: RoundingMode{:NearestTiesAway} ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xrninta" )), libbid), $ Ti′, ($ BID,), x), InexactError, :round , $ BID, x, mask= INVALID | OVERFLOW)
360
+ Base. convert (:: Type{$Ti′} , x:: $BID ) = @xchk (ccall (($ (bidsym (w," to_" ,i′str," _xfloor" )), libbid), $ Ti′, ($ BID,), x), InexactError, :convert , $ BID, x)
388
361
Base.$ (Symbol (" $Ti′ " ))(x:: $BID ) = convert ($ Ti′, x)
389
362
end
390
363
end
0 commit comments