Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Float32 and Float16 #31

Merged
merged 6 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ os:
- osx

julia:
- 0.7
- 1.0
- 1.3
- nightly

matrix:
allow_failures:
- julia: nightly

notifications:
email: false
# uncomment the following lines to override the default test script
Expand Down
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
environment:
matrix:
- julia_version: 0.7
- julia_version: 1
- julia_version: 1.3
- julia_version: nightly

platform:
- x86 # 32-bit
- x64 # 64-bit

# # Uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia_version: nightly
# Uncomment the following lines to allow failures on nightly julia
# (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia_version: nightly

branches:
only:
Expand Down
24 changes: 11 additions & 13 deletions src/CRlibm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ function setup(use_MPFR=false)
the same functionality, but with slower execution.
This is currently the only option on Windows.")

use_MPFR = true
use_MPFR = true
end

wrap_MPFR()

if use_MPFR
println("CRlibm is shadowing MPFR.")
@info "CRlibm is shadowing MPFR."
shadow_MPFR()
else
wrap_CRlibm()
Expand Down Expand Up @@ -92,13 +92,13 @@ function wrap_CRlibm()
mode = :(::RoundingMode{$mode})

@eval $f(x::Float64, $mode) = ccall(($fname, libcrlibm), Float64, (Float64,), x)

end

@eval $f(x::Float64) = $f(x, RoundNearest)

# Specialise for Float32 and Float16 to get the other IEEE FP types
# working transparently as well
@eval $f(x::Float16, r::RoundingMode) = Float16(($f)(Float64(x), r), r)
@eval $f(x::Float32, r::RoundingMode) = Float32(($f)(Float64(x), r), r)
end

end


Expand All @@ -116,16 +116,16 @@ function shadow_MPFR()

mode2 = Symbol("Round", string(mode))

@eval function $f(x::Float64, $mode1)
setprecision(BigFloat, 53) do
Float64(($f)(BigFloat(x), $mode2), $mode2)
for T in (Float16, Float32, Float64)
@eval function $f(x::$T, $mode1)
setprecision(BigFloat, precision($T)) do
$T(($f)(BigFloat(x), $mode2), $mode2)
end
end
end
# use the functions that were previously defined for BigFloat
end

@eval $f(x::BigFloat) = $f(x, RoundNearest)

end
end

Expand All @@ -137,8 +137,6 @@ function wrap_generic_fallbacks()
@eval $f(x::Real, r::RoundingMode) = ($f)(float(x), r)
@eval $f(x::Real) = $f(x, RoundNearest)
end


end


Expand Down
17 changes: 17 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
using CRlibm
using Test

# Float64
@test CRlibm.cos(0.5, RoundDown) == 0.8775825618903726
@test CRlibm.cos(0.5, RoundUp) == 0.8775825618903728
@test CRlibm.cos(0.5, RoundNearest) == cos(0.5) == 0.8775825618903728
@test CRlibm.cos(1.6, RoundToZero) == -0.029199522301288812
@test CRlibm.cos(1.6, RoundDown) == -0.029199522301288815
@test CRlibm.cos(0.5) == CRlibm.cos(0.5, RoundNearest)

# Float32
@test CRlibm.cos(0.5f0, RoundDown) == 0.87758255f0
@test CRlibm.cos(0.5f0, RoundUp) == 0.8775826f0
@test CRlibm.cos(0.5f0, RoundNearest) == cos(0.5f0) == 0.87758255f0
@test CRlibm.cos(1.6f0, RoundToZero) == -0.029199544f0
@test CRlibm.cos(1.6f0, RoundDown) == -0.029199546f0
@test CRlibm.cos(0.5f0) == CRlibm.cos(0.5f0, RoundNearest)

# Float16
@test CRlibm.cos(Float16(0.5), RoundDown) == Float16(0.8774)
@test CRlibm.cos(Float16(0.5), RoundUp) == Float16(0.878)
@test CRlibm.cos(Float16(0.5), RoundNearest) == cos(Float16(0.5)) == Float16(0.8774)
@test CRlibm.cos(Float16(1.6), RoundToZero) == Float16(-0.02881)
@test CRlibm.cos(Float16(1.6), RoundDown) == Float16(-0.02882)
@test CRlibm.cos(Float16(0.5)) == CRlibm.cos(Float16(0.5), RoundNearest)

function my_eps(prec::Int)
ldexp(eps(Float64), 53-prec)
end
Expand Down