Skip to content

Commit

Permalink
Update for 0.7 and 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed Aug 15, 2018
1 parent 21b5793 commit 0f038e3
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 67 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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())'
12 changes: 12 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.6
Compat 0.33
julia 0.7
9 changes: 5 additions & 4 deletions benchmark/benchmark.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using SLEEF
using BenchmarkTools
using JLD, DataStructures
using Printf

const RETUNE = false
const VERBOSE = true
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 3 additions & 10 deletions src/SLEEF.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)))
Expand Down
4 changes: 2 additions & 2 deletions src/log.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 2 additions & 6 deletions src/priv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions src/trig.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions test/accuracy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
40 changes: 20 additions & 20 deletions test/exceptional.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 0f038e3

Please sign in to comment.