|
| 1 | +__precompile__(true) |
1 | 2 | module DecFP |
2 | 3 |
|
3 | 4 | using Compat |
@@ -52,6 +53,7 @@ for w in (32,64,128) |
52 | 53 | $BID(x) = convert($BID, x) |
53 | 54 | Base.reinterpret(::Type{$BID}, x::$Ti) = new(x) |
54 | 55 | end |
| 56 | + @eval $BID(x::Rational{T}) where {T} = convert($BID, x) |
55 | 57 | end |
56 | 58 |
|
57 | 59 | # quickly check whether s begins with "±nan" |
@@ -118,7 +120,7 @@ for w in (32,64,128) |
118 | 120 | DIGITS[1] = UInt8('0') |
119 | 121 | return Int32(1), Int32(1), signbit(x) |
120 | 122 | end |
121 | | - ccall(($(bidsym(w,"to_string")), libbid), Void, (Ptr{UInt8}, $BID), _buffer, rounded) |
| 123 | + ccall(($(bidsym(w,"to_string")), libbid), Cvoid, (Ptr{UInt8}, $BID), _buffer, rounded) |
122 | 124 | trailing_zeros = 0 |
123 | 125 | i = 2 |
124 | 126 | while _buffer[i] != UInt8('E') |
@@ -164,7 +166,7 @@ for w in (32,64,128) |
164 | 166 | # rounded = round(x * exp10($BID(n - 1 - normalized_exponent)), RoundNearestTiesAway) |
165 | 167 | rounded = xchk(ccall(($(bidsym(w,"round_integral_nearest_away")), libbid), $BID, ($BID,), x * exp10($BID(n - 1 - normalized_exponent))), InexactError, :round, $BID, x, mask=INVALID | OVERFLOW) |
166 | 168 | rounded_exponent = nox(ccall(($(bidsym(w,"ilogb")), libbid), Cint, ($BID,), rounded)) |
167 | | - ccall(($(bidsym(w,"to_string")), libbid), Void, (Ptr{UInt8}, $BID), _buffer, rounded) |
| 169 | + ccall(($(bidsym(w,"to_string")), libbid), Cvoid, (Ptr{UInt8}, $BID), _buffer, rounded) |
168 | 170 | i = 2 |
169 | 171 | while _buffer[i] != UInt8('E') |
170 | 172 | DIGITS[i - 1] = _buffer[i] |
@@ -217,6 +219,7 @@ for w in (32,64,128) |
217 | 219 | bT = string("binary",sizeof(Tf)*8) |
218 | 220 | @eval begin |
219 | 221 | Base.convert(::Type{$Tf}, x::$BID) = nox(ccall(($(bidsym(w,"to_",bT)), libbid), $Tf, ($BID,), x)) |
| 222 | + Base.$(Symbol("$Tf"))(x::$BID) = convert($Tf, x) |
220 | 223 | Base.convert(::Type{$BID}, x::$Tf) = nox(ccall(($(string("__",bT,"_to_","bid",w)), libbid), $BID, ($Tf,), x)) |
221 | 224 | end |
222 | 225 | end |
@@ -264,12 +267,14 @@ for w in (32,64,128) |
264 | 267 | 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) |
265 | 268 | 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) |
266 | 269 | Base.convert(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",i′str,"_xfloor")), libbid), $Ti′, ($BID,), x), InexactError, :convert, $BID, x) |
| 270 | + Base.$(Symbol("$Ti′"))(x::$BID) = convert($Ti′, x) |
267 | 271 | end |
268 | 272 | end |
269 | 273 | end |
270 | 274 |
|
271 | 275 | @eval Base.bswap(x::$BID) = reinterpret($BID, bswap(x.x)) |
272 | 276 | @eval Base.convert(::Type{Float16}, x::$BID) = convert(Float16, convert(Float32, x)) |
| 277 | + @eval Base.Float16(x::$BID) = convert(Float16, x) |
273 | 278 | @eval Base.reinterpret(::Type{$Ti}, x::$BID) = x.x |
274 | 279 | end # widths w |
275 | 280 |
|
|
0 commit comments