11__precompile__ (true )
22module DecFP
33
4- using Compat
5-
6- if ! (VERSION < v " 0.7.0-DEV.3026" )
7- using Printf
8- end
9-
10- if ! (VERSION < v " 0.7.0-DEV.2813" )
11- using Unicode
12- end
4+ using Compat, Compat. Printf, Compat. Unicode
135
146export Dec32, Dec64, Dec128, @d_str , @d32_str , @d64_str , @d128_str
157
@@ -20,15 +12,18 @@ const _buffer = fill(0x00, 1024)
2012import Base. promote_rule
2113import Base. Grisu. DIGITS
2214
15+ const rounding = Ref {Ptr{Cuint}} ()
16+ const flags = Ref {Ptr{Cuint}} ()
17+ # rounding modes, from bid_functions.h
18+
19+ const rounding_c2j = [RoundNearest, RoundDown, RoundUp, RoundToZero, RoundFromZero]
20+ const rounding_j2c = Dict {RoundingMode, UInt32} ([(rounding_c2j[i], Cuint (i- 1 )) for i in 1 : length (rounding_c2j)])
21+
2322# global pointers and dicts must be initialized at runtime (via __init__)
2423function __init__ ()
25- global const rounding = cglobal ((:__bid_IDEC_glbround , libbid), Cuint) # rounding mode
26- global const flags = cglobal ((:__bid_IDEC_glbflags , libbid), Cuint) # exception status
27- unsafe_store! (flags, 0 )
28-
29- # rounding modes, from bid_functions.h
30- global const rounding_c2j = [RoundNearest, RoundDown, RoundUp, RoundToZero, RoundFromZero]
31- global const rounding_j2c = Dict {RoundingMode, UInt32} ([(rounding_c2j[i], Cuint (i- 1 )) for i in 1 : length (rounding_c2j)])
24+ global rounding[] = cglobal ((:__bid_IDEC_glbround , libbid), Cuint) # rounding mode
25+ global flags[] = cglobal ((:__bid_IDEC_glbflags , libbid), Cuint) # exception status
26+ unsafe_store! (flags[], 0 )
3227end
3328
3429# status flags from bid_functions.h:
@@ -42,8 +37,8 @@ const INEXACT = 0x20
4237bidsym (w,s... ) = string (" __bid" , w, " _" , s... )
4338
4439abstract type DecimalFloatingPoint <: AbstractFloat end
45- Base. rounding (:: Type{T} ) where {T<: DecimalFloatingPoint } = rounding_c2j[unsafe_load (rounding)+ 1 ]
46- Base. setrounding (:: Type{T} , r:: RoundingMode ) where {T<: DecimalFloatingPoint } = unsafe_store! (rounding, rounding_j2c[r])
40+ Base. rounding (:: Type{T} ) where {T<: DecimalFloatingPoint } = rounding_c2j[unsafe_load (rounding[] )+ 1 ]
41+ Base. setrounding (:: Type{T} , r:: RoundingMode ) where {T<: DecimalFloatingPoint } = unsafe_store! (rounding[] , rounding_j2c[r])
4742
4843for w in (32 ,64 ,128 )
4944 BID = Symbol (string (" Dec" ,w))
@@ -343,15 +338,15 @@ macro d128_str(s, flags...) parse(Dec128, s) end
343338
344339# clear exception flags and return x
345340function nox (x)
346- unsafe_store! (flags, 0 )
341+ unsafe_store! (flags[] , 0 )
347342 return x
348343end
349344
350345# check exception flags in mask & throw, otherwise returning x;
351346# always clearing exceptions
352347function xchk (x, args... ; mask:: Integer = 0x3f )
353- f = unsafe_load (flags)
354- unsafe_store! (flags, 0 )
348+ f = unsafe_load (flags[] )
349+ unsafe_store! (flags[] , 0 )
355350 if f & mask != 0
356351 f & INEXACT != 0 && throw (InexactError (args... ))
357352 f & OVERFLOW != 0 && throw (OverflowError (args... ))
@@ -364,8 +359,8 @@ function xchk(x, args...; mask::Integer=0x3f)
364359end
365360
366361function xchk (x, exc:: Type{E} , args... ; mask:: Integer = 0x3f ) where {E<: Exception }
367- f = unsafe_load (flags)
368- unsafe_store! (flags, 0 )
362+ f = unsafe_load (flags[] )
363+ unsafe_store! (flags[] , 0 )
369364 f & mask != 0 && throw (exc (args... ))
370365 return x
371366end
0 commit comments