Skip to content

Commit

Permalink
Merge pull request #53 from stevengj/jq/0.7
Browse files Browse the repository at this point in the history
0.7 compat
  • Loading branch information
quinnj authored Jan 21, 2018
2 parents ea95d2d + 4c09191 commit 65adaad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deps/build.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using BinDeps
using BinDeps, Compat.Libdl

vers = "20U1"

Expand Down
41 changes: 18 additions & 23 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
__precompile__(true)
module DecFP

using Compat

if !(VERSION < v"0.7.0-DEV.3026")
using Printf
end

if !(VERSION < v"0.7.0-DEV.2813")
using Unicode
end
using Compat, Compat.Printf, Compat.Unicode

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

Expand All @@ -20,15 +12,18 @@ const _buffer = fill(0x00, 1024)
import Base.promote_rule
import Base.Grisu.DIGITS

const rounding = Ref{Ptr{Cuint}}()
const flags = Ref{Ptr{Cuint}}()
# rounding modes, from bid_functions.h

const rounding_c2j = [RoundNearest, RoundDown, RoundUp, RoundToZero, RoundFromZero]
const rounding_j2c = Dict{RoundingMode, UInt32}([(rounding_c2j[i], Cuint(i-1)) for i in 1:length(rounding_c2j)])

# global pointers and dicts must be initialized at runtime (via __init__)
function __init__()
global const rounding = cglobal((:__bid_IDEC_glbround, libbid), Cuint) # rounding mode
global const flags = cglobal((:__bid_IDEC_glbflags, libbid), Cuint) # exception status
unsafe_store!(flags, 0)

# rounding modes, from bid_functions.h
global const rounding_c2j = [RoundNearest, RoundDown, RoundUp, RoundToZero, RoundFromZero]
global const rounding_j2c = Dict{RoundingMode, UInt32}([(rounding_c2j[i], Cuint(i-1)) for i in 1:length(rounding_c2j)])
global rounding[] = cglobal((:__bid_IDEC_glbround, libbid), Cuint) # rounding mode
global flags[] = cglobal((:__bid_IDEC_glbflags, libbid), Cuint) # exception status
unsafe_store!(flags[], 0)
end

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

abstract type DecimalFloatingPoint <: AbstractFloat end
Base.rounding(::Type{T}) where {T<:DecimalFloatingPoint} = rounding_c2j[unsafe_load(rounding)+1]
Base.setrounding(::Type{T}, r::RoundingMode) where {T<:DecimalFloatingPoint} = unsafe_store!(rounding, rounding_j2c[r])
Base.rounding(::Type{T}) where {T<:DecimalFloatingPoint} = rounding_c2j[unsafe_load(rounding[])+1]
Base.setrounding(::Type{T}, r::RoundingMode) where {T<:DecimalFloatingPoint} = unsafe_store!(rounding[], rounding_j2c[r])

for w in (32,64,128)
BID = Symbol(string("Dec",w))
Expand Down Expand Up @@ -343,15 +338,15 @@ macro d128_str(s, flags...) parse(Dec128, s) end

# clear exception flags and return x
function nox(x)
unsafe_store!(flags, 0)
unsafe_store!(flags[], 0)
return x
end

# check exception flags in mask & throw, otherwise returning x;
# always clearing exceptions
function xchk(x, args...; mask::Integer=0x3f)
f = unsafe_load(flags)
unsafe_store!(flags, 0)
f = unsafe_load(flags[])
unsafe_store!(flags[], 0)
if f & mask != 0
f & INEXACT != 0 && throw(InexactError(args...))
f & OVERFLOW != 0 && throw(OverflowError(args...))
Expand All @@ -364,8 +359,8 @@ function xchk(x, args...; mask::Integer=0x3f)
end

function xchk(x, exc::Type{E}, args...; mask::Integer=0x3f) where {E<:Exception}
f = unsafe_load(flags)
unsafe_store!(flags, 0)
f = unsafe_load(flags[])
unsafe_store!(flags[], 0)
f & mask != 0 && throw(exc(args...))
return x
end
Expand Down
14 changes: 3 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
using DecFP
using DecFP, Compat.Test, Compat.Printf

@static if VERSION < v"0.7.0-DEV.2005"
using Base.Test
else
using Test
end
if !(VERSION < v"0.7.0-DEV.3026")
using Printf
end
if !(VERSION < v"0.7.0-DEV.1592")
using Base.MathConstants
end

@test unsafe_load(DecFP.flags) == 0
@test unsafe_load(DecFP.flags[]) == 0

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

@test unsafe_load(DecFP.flags) == 0
@test unsafe_load(DecFP.flags[]) == 0

# issue #37
@test reinterpret(UInt128, Dec128(1.5)) == 0x303e000000000000000000000000000f
Expand Down

0 comments on commit 65adaad

Please sign in to comment.