From 0f038e3b676d142e4dbcb30a1e953fadc84ca77d Mon Sep 17 00:00:00 2001 From: Mus Date: Wed, 15 Aug 2018 12:49:36 -0400 Subject: [PATCH] Update for 0.7 and 1.0 --- .travis.yml | 7 ++----- Project.toml | 12 ++++++++++++ README.md | 2 +- REQUIRE | 3 +-- benchmark/benchmark.jl | 9 +++++---- src/SLEEF.jl | 13 +++---------- src/log.jl | 4 ++-- src/priv.jl | 8 ++------ src/trig.jl | 6 +++--- test/accuracy.jl | 13 ++++++------- test/exceptional.jl | 40 ++++++++++++++++++++-------------------- test/runtests.jl | 14 +++++++------- 12 files changed, 64 insertions(+), 67 deletions(-) create mode 100644 Project.toml diff --git a/.travis.yml b/.travis.yml index 4d4f294..06c2a26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,14 +4,11 @@ os: - linux - osx julia: - - 0.6 + - 0.7 + - 1 - nightly notifications: email: false -# uncomment the following lines to override the default test script -#script: -# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi - # - julia -e 'versioninfo(); Pkg.clone(pwd()); Pkg.build("SLEEF"); Pkg.test("SLEEF"; coverage=true)' after_success: # push coverage results - julia -e 'cd(Pkg.dir("SLEEF")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(Codecov.process_folder())' diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..ad4f02b --- /dev/null +++ b/Project.toml @@ -0,0 +1,12 @@ +name = "SLEEF" +uuid = "3e6341c9-01f6-5312-b33f-f8894a2e817a" +license = "MIT" +repo = "https://github.com/musm/SLEEF.jl.git" +version = "v0.5.0" + +[extras] +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" + +[targets] +test = ["Test","Printf"] diff --git a/README.md b/README.md index f96ac97..dc25b03 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ julia> SLEEF.exp(3f0) The available functions include (within 1 ulp) ```julia -sin, cos, tan, asin, acos, atan, atan2, sincos, sinh, cosh, tanh, +sin, cos, tan, asin, acos, atan, sincos, sinh, cosh, tanh, asinh, acosh, atanh, log, log2, log10, log1p, ilogb, exp, exp2, exp10, expm1, ldexp, cbrt, pow ``` diff --git a/REQUIRE b/REQUIRE index d3506f8..859ad46 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1 @@ -julia 0.6 -Compat 0.33 +julia 0.7 diff --git a/benchmark/benchmark.jl b/benchmark/benchmark.jl index f248704..495d991 100644 --- a/benchmark/benchmark.jl +++ b/benchmark/benchmark.jl @@ -1,6 +1,7 @@ using SLEEF using BenchmarkTools using JLD, DataStructures +using Printf const RETUNE = false const VERBOSE = true @@ -110,17 +111,17 @@ end println("Running micro benchmarks...") results = run(suite; verbose=VERBOSE, seconds = 2) -print_with_color(:blue, "Benchmarks: median ratio SLEEF/Base\n") +printstyled("Benchmarks: median ratio SLEEF/Base\n", color = :blue) for f in keys(micros) - print_with_color(:magenta, string(f)) + printstyled(string(f) color = :magenta) for T in test_types println() print("time: ", ) tratio = ratio(median(results["SLEEF"][f][string(T)]), median(results["Base"][f][string(T)])).time tcolor = tratio > 3 ? :red : tratio < 1.5 ? :green : :blue - print_with_color(tcolor, @sprintf("%.2f",tratio), " ", string(T)) + printstyled(@sprintf("%.2f",tratio), " ", string(T), color = tcolor) if DETAILS - print_with_color(:blue, "details SLEEF/Base\n") + printstyled("details SLEEF/Base\n", color=:blue) println(results["SLEEF"][f][string(T)]) println(results["Base"][f][string(T)]) println() diff --git a/src/SLEEF.jl b/src/SLEEF.jl index 9bca72e..6249c71 100644 --- a/src/SLEEF.jl +++ b/src/SLEEF.jl @@ -1,20 +1,13 @@ -__precompile__() - module SLEEF -# export sin, cos, tan, asin, acos, atan, atan2, sincos, sinh, cosh, tanh, +# export sin, cos, tan, asin, acos, atan, sincos, sinh, cosh, tanh, # asinh, acosh, atanh, log, log2, log10, log1p, ilogb, exp, exp2, exp10, expm1, ldexp, cbrt, pow # fast variants (within 3 ulp) # export sin_fast, cos_fast, tan_fast, sincos_fast, asin_fast, acos_fast, atan_fast, atan2_fast, log_fast, cbrt_fast -using Base.Math: @horner, exponent_bias, exponent_mask, significand_bits, IEEEFloat, exponent_raw_max +using Base.Math: uinttype, @horner, exponent_bias, exponent_mask, significand_bits, IEEEFloat, exponent_raw_max -if VERSION < v"0.7.0-DEV.1430" - using Base.Math: fpinttype -else - using Base: uinttype -end ## constants const MLN2 = 6.931471805599453094172321214581765680755001343602552541206800094933936219696955e-01 # log(2) @@ -113,7 +106,7 @@ for func in (:sin, :cos, :tan, :asin, :acos, :atan, :sinh, :cosh, :tanh, end end -for func in (:atan2, :hypot) +for func in (:atan, :hypot) @eval begin $func(y::Real, x::Real) = $func(promote(float(y), float(x))...) $func(a::Float16, b::Float16) = Float16($func(Float32(a), Float32(b))) diff --git a/src/log.jl b/src/log.jl index b764d8b..8f4ca1a 100644 --- a/src/log.jl +++ b/src/log.jl @@ -109,7 +109,7 @@ Compute the natural logarithm of `x`. The inverse of the natural logarithm is the natural expoenential function `exp(x)` """ function log(d::T) where {T<:Union{Float32,Float64}} - o = d < realmin(T) + o = d < floatmin(T) o && (d *= T(Int64(1) << 32) * T(Int64(1) << 32)) e = ilogb2k(d * T(1.0/0.75)) @@ -179,7 +179,7 @@ Compute the natural logarithm of `x`. The inverse of the natural logarithm is the natural expoenential function `exp(x)` """ function log_fast(d::T) where {T<:Union{Float32,Float64}} - o = d < realmin(T) + o = d < floatmin(T) o && (d *= T(Int64(1) << 32) * T(Int64(1) << 32)) e = ilogb2k(d * T(1.0/0.75)) diff --git a/src/priv.jl b/src/priv.jl index 0a0f8f2..5652cdb 100644 --- a/src/priv.jl +++ b/src/priv.jl @@ -50,11 +50,7 @@ end end @inline function ldexp3k(x::T, e::Int) where {T<:Union{Float32,Float64}} - @static if VERSION < v"0.7.0-DEV.1430" - reinterpret(T, reinterpret(Unsigned, x) + (Int64(e) << significand_bits(T)) % fpinttype(T)) - else - reinterpret(T, reinterpret(Unsigned, x) + (Int64(e) << significand_bits(T)) % uinttype(T)) - end + reinterpret(T, reinterpret(Unsigned, x) + (Int64(e) << significand_bits(T)) % uinttype(T)) end # threshold values for `ilogbk` @@ -325,7 +321,7 @@ end end @inline function logk(d::T) where {T<:Union{Float32,Float64}} - o = d < realmin(T) + o = d < floatmin(T) o && (d *= T(Int64(1) << 32) * T(Int64(1) << 32)) e = ilogb2k(d * T(1.0/0.75)) diff --git a/src/trig.jl b/src/trig.jl index cb6b51e..e81f2fe 100644 --- a/src/trig.jl +++ b/src/trig.jl @@ -764,11 +764,11 @@ const under_atan2(::Type{Float64}) = 5.5626846462680083984e-309 const under_atan2(::Type{Float32}) = 2.9387372783541830947f-39 """ - atan2(x, y) + atan(x, y) Compute the inverse tangent of `x/y`, using the signs of both `x` and `y` to determine the quadrant of the return value. """ -function atan2(x::T, y::T) where {T<:Union{Float32,Float64}} +function atan(x::T, y::T) where {T<:Union{Float32,Float64}} abs(y) < under_atan2(T) && (x *= T(Int64(1) << 53); y *= T(Int64(1) << 53)) r = T(atan2k(Double(abs(x)), Double(y))) @@ -791,7 +791,7 @@ end Compute the inverse tangent of `x/y`, using the signs of both `x` and `y` to determine the quadrant of the return value. """ -function atan2_fast(x::T, y::T) where {T<:Union{Float32,Float64}} +function atan_fast(x::T, y::T) where {T<:Union{Float32,Float64}} r = atan2k_fast(abs(x), y) r = flipsign(r, y) if isinf(y) || y == 0 diff --git a/test/accuracy.jl b/test/accuracy.jl index d9acacb..778ac0d 100644 --- a/test/accuracy.jl +++ b/test/accuracy.jl @@ -48,15 +48,14 @@ IntF(::Type{Float32}) = Int32 tol = 4 test_acc(T, fun_table, xx, tol) - - sin_sincos_fast(x) = SLEEF.sincos_fast(x)[1] - cos_sincos_fast(x) = SLEEF.sincos_fast(x)[2] + global sin_sincos_fast(x) = (SLEEF.sincos_fast(x))[1] + global cos_sincos_fast(x) = (SLEEF.sincos_fast(x))[2] fun_table = Dict(sin_sincos_fast => Base.sin, cos_sincos_fast => Base.cos) tol = 4 test_acc(T, fun_table, xx, tol) - sin_sincos(x) = SLEEF.sincos(x)[1] - cos_sincos(x) = SLEEF.sincos(x)[2] + global sin_sincos(x) = (SLEEF.sincos(x))[1] + global cos_sincos(x) = (SLEEF.sincos(x))[2] fun_table = Dict(sin_sincos => Base.sin, cos_sincos => Base.cos) tol = 1 test_acc(T, fun_table, xx, tol) @@ -88,11 +87,11 @@ IntF(::Type{Float32}) = Int32 xx4 = map(Tuple{T,T}, [zip(-100:0.51:100, -100:0.52:100)...]) xx = vcat(xx1, xx2, xx3, xx4) - fun_table = Dict(SLEEF.atan2_fast => Base.atan2) + fun_table = Dict(SLEEF.atan_fast => Base.atan) tol = 2.5 test_acc(T, fun_table, xx, tol) - fun_table = Dict(SLEEF.atan2 => Base.atan2) + fun_table = Dict(SLEEF.atan => Base.atan) tol = 1 test_acc(T, fun_table, xx, tol) diff --git a/test/exceptional.jl b/test/exceptional.jl index c27b18d..c74ccd2 100644 --- a/test/exceptional.jl +++ b/test/exceptional.jl @@ -1,87 +1,87 @@ @testset "exceptional $T" for T in (Float32, Float64) -@testset "exceptional $xatan2" for xatan2 in (SLEEF.atan2_fast, SLEEF.atan2) +@testset "exceptional $xatan" for xatan in (SLEEF.atan_fast, SLEEF.atan) - @test xatan2(T(0.0), T(-0.0)) === T(pi) - @test xatan2(T(-0.0), T(-0.0)) === -T(pi) - @test ispzero(xatan2(T(0.0), T(0.0))) - @test isnzero(xatan2(T(-0.0), T(0.0))) - @test xatan2( T(Inf), -T(Inf)) === T(3*pi/4) - @test xatan2(-T(Inf), -T(Inf)) === T(-3*pi/4) - @test xatan2( T(Inf), T(Inf)) === T(pi/4) - @test xatan2(-T(Inf), T(Inf)) === T(-pi/4) + @test xatan(T(0.0), T(-0.0)) === T(pi) + @test xatan(T(-0.0), T(-0.0)) === -T(pi) + @test ispzero(xatan(T(0.0), T(0.0))) + @test isnzero(xatan(T(-0.0), T(0.0))) + @test xatan( T(Inf), -T(Inf)) === T(3*pi/4) + @test xatan(-T(Inf), -T(Inf)) === T(-3*pi/4) + @test xatan( T(Inf), T(Inf)) === T(pi/4) + @test xatan(-T(Inf), T(Inf)) === T(-pi/4) y = T(0.0) xa = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5] for x in xa - @test xatan2(y,x) === T(pi) + @test xatan(y,x) === T(pi) end y = T(-0.0) xa = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5] for x in xa - @test xatan2(y,x) === T(-pi) + @test xatan(y,x) === T(-pi) end ya = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5] xa = T[T(0.0), T(-0.0)] for x in xa, y in ya - @test xatan2(y,x) === T(-pi/2) + @test xatan(y,x) === T(-pi/2) end ya = T[100000.5, 100000, 3, 2.5, 2, 1.5, 1.0, 0.5] xa = T[T(0.0), T(-0.0)] for x in xa, y in ya - @test xatan2(y,x) === T(pi/2) + @test xatan(y,x) === T(pi/2) end y = T(Inf) xa = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5, -0.0, +0.0, 0.5, 1.5, 2.0, 2.5, 3.0, 100000, 100000.5] for x in xa - @test xatan2(y,x) === T(pi/2) + @test xatan(y,x) === T(pi/2) end y = T(-Inf) xa = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5, -0.0, +0.0, 0.5, 1.5, 2.0, 2.5, 3.0, 100000, 100000.5] for x in xa - @test xatan2(y,x) === T(-pi/2) + @test xatan(y,x) === T(-pi/2) end ya = T[0.5, 1.5, 2.0, 2.5, 3.0, 100000, 100000.5] x = T(Inf) for y in ya - @test ispzero(xatan2(y,x)) + @test ispzero(xatan(y,x)) end ya = T[-0.5, -1.5, -2.0, -2.5, -3.0, -100000, -100000.5] x = T(Inf) for y in ya - @test isnzero(xatan2(y,x)) + @test isnzero(xatan(y,x)) end ya = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5, -0.0, +0.0, 0.5, 1.5, 2.0, 2.5, 3.0, 100000, 100000.5, NaN] x = T(NaN) for y in ya - @test isnan(xatan2(y,x)) + @test isnan(xatan(y,x)) end y = T(NaN) xa = T[-100000.5, -100000, -3, -2.5, -2, -1.5, -1.0, -0.5, -0.0, +0.0, 0.5, 1.5, 2.0, 2.5, 3.0, 100000, 100000.5, NaN] for x in xa - @test isnan(xatan2(y,x)) + @test isnan(xatan(y,x)) end -end # denormal/nonumber atan2 +end # denormal/nonumber atan @testset "exceptional xpow" begin diff --git a/test/runtests.jl b/test/runtests.jl index 4def00e..8a2c243 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,8 +1,8 @@ using SLEEF -using Compat -using Compat.Test +using Test +using Printf -using Base.Math.significand_bits +using Base.Math: significand_bits isnzero(x::T) where {T <: AbstractFloat} = signbit(x) ispzero(x::T) where {T <: AbstractFloat} = !signbit(x) @@ -63,19 +63,19 @@ countulp(x::T, y::T) where {T <: AbstractFloat} = countulp(T, x, y) # get rid off annoying warnings from overwritten function macro nowarn(expr) quote - stderr = STDERR + _stderr = stderr tmp = tempname() stream = open(tmp, "w") redirect_stderr(stream) result = $(esc(expr)) - redirect_stderr(stderr) + redirect_stderr(_stderr) close(stream) result end end # overide domain checking that base adheres to -using Base.MPFR.ROUNDING_MODE +using Base.MPFR: ROUNDING_MODE for f in (:sin, :cos, :tan, :asin, :acos, :atan, :asinh, :acosh, :atanh, :log, :log10, :log2, :log1p) @eval begin import Base.$f @@ -111,7 +111,7 @@ function test_acc(T, fun_table, xx, tol; debug = false, tol_debug = 5) end rmean = rmean / length(xx) - t = @test trunc(rmax, 1) <= tol + t = @test trunc(rmax, digits=1) <= tol fmtxloc = isa(xmax, Tuple) ? string('(', join((@sprintf("%.5f", x) for x in xmax), ", "), ')') : @sprintf("%.5f", xmax) println(rpad(strip_module_name(xfun), 18, " "), ": max ", @sprintf("%f", rmax),