Skip to content

Commit 65adaad

Browse files
authored
Merge pull request #53 from stevengj/jq/0.7
0.7 compat
2 parents ea95d2d + 4c09191 commit 65adaad

File tree

3 files changed

+22
-35
lines changed

3 files changed

+22
-35
lines changed

deps/build.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BinDeps
1+
using BinDeps, Compat.Libdl
22

33
vers = "20U1"
44

src/DecFP.jl

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
__precompile__(true)
22
module 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

146
export Dec32, Dec64, Dec128, @d_str, @d32_str, @d64_str, @d128_str
157

@@ -20,15 +12,18 @@ const _buffer = fill(0x00, 1024)
2012
import Base.promote_rule
2113
import 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__)
2423
function __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)
3227
end
3328

3429
# status flags from bid_functions.h:
@@ -42,8 +37,8 @@ const INEXACT = 0x20
4237
bidsym(w,s...) = string("__bid", w, "_", s...)
4338

4439
abstract 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

4843
for 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
345340
function nox(x)
346-
unsafe_store!(flags, 0)
341+
unsafe_store!(flags[], 0)
347342
return x
348343
end
349344

350345
# check exception flags in mask & throw, otherwise returning x;
351346
# always clearing exceptions
352347
function 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)
364359
end
365360

366361
function 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
371366
end

test/runtests.jl

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
using DecFP
1+
using DecFP, Compat.Test, Compat.Printf
22

3-
@static if VERSION < v"0.7.0-DEV.2005"
4-
using Base.Test
5-
else
6-
using Test
7-
end
8-
if !(VERSION < v"0.7.0-DEV.3026")
9-
using Printf
10-
end
113
if !(VERSION < v"0.7.0-DEV.1592")
124
using Base.MathConstants
135
end
146

15-
@test unsafe_load(DecFP.flags) == 0
7+
@test unsafe_load(DecFP.flags[]) == 0
168

179
import DecFP.isnanstr
1810
@test isnanstr("nan") && isnanstr(" +NAN") && isnanstr("-NaN") && !isnanstr("nano")
@@ -181,7 +173,7 @@ for T in (Dec32, Dec64, Dec128)
181173
@test typeof((xd+yd*im)*pi) == Complex{T}
182174
end
183175

184-
@test unsafe_load(DecFP.flags) == 0
176+
@test unsafe_load(DecFP.flags[]) == 0
185177

186178
# issue #37
187179
@test reinterpret(UInt128, Dec128(1.5)) == 0x303e000000000000000000000000000f

0 commit comments

Comments
 (0)