Skip to content

Commit

Permalink
Make CRlibm work on Windows (#13)
Browse files Browse the repository at this point in the history
* __init__ correctly modifies global use_MPFR

* Remove __init__; now works on Windows

* Refine Windows warning
  • Loading branch information
dpsanders authored Dec 9, 2016
1 parent a47599c commit b61bf03
Showing 1 changed file with 54 additions and 48 deletions.
102 changes: 54 additions & 48 deletions src/CRlibm.jl
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@

__precompile__(true)
module CRlibm

using Compat

unixpath = "../deps/src/crlibm-1.0beta4/libcrlibm"
const libcrlibm = joinpath(dirname(@__FILE__), unixpath)

# check from Diercxk.jl (3-clause BSD license):

use_MPFR = false
function setup_CRlibm(use_MPFR=false)

function __init__()
# Ensure library is available.
if (Libdl.dlopen_e(libcrlibm) == C_NULL)
warn("CRlibm not properly installed; falling back to use MPFR.
This is currently the default on Windows. The package should still
work correctly, but will be slower.")
wrap_MPFR()

use_MPFR = true
if use_MPFR
println("CRlibm will shadow MPFR.")
shadow_MPFR()
else
wrap_CRlibm()
end

wrap_generic_fallbacks()
end

export tanpi, atanpi

# All functions in crlibm except pow, according to section 0.4 of the PDF manual
# (page 8); source: ./deps/src/crlibm1.0beta4/docs/latex/0_getting-started.tex,
# section "Currently available functions"

const function_names = split("""exp expm1 log log1p log2 log10
sin cos tan asin acos atan
sinh cosh sinpi cospi tanpi atanpi
""")


const functions = map(Symbol, function_names)


## Generate wrappers of CRlibm shared library:

# Aiming for functions of the form
# cos(x::Float64, ::RoundingMode{:RoundUp}) = ccall((:cos, libcrlibm), Float64, (Float64,), x)


const MPFR_function_names = split("""exp expm1 log log1p log2 log10
sin cos tan asin acos atan
sinh cosh
""")

const MPFR_functions = map(Symbol, MPFR_function_names)


function wrap_MPFR()
# stopgap until included in Base
Expand Down Expand Up @@ -85,6 +53,7 @@ function wrap_MPFR()

end


function wrap_CRlibm()

for f in functions
Expand All @@ -108,6 +77,7 @@ function wrap_CRlibm()

end


function shadow_MPFR()
for f in functions

Expand Down Expand Up @@ -148,17 +118,53 @@ function wrap_generic_fallbacks()
end


wrap_MPFR()

if !use_MPFR
wrap_CRlibm()
else
shadow_MPFR()
end
export tanpi, atanpi

# All functions in crlibm except pow, according to section 0.4 of the PDF manual
# (page 8); source: ./deps/src/crlibm1.0beta4/docs/latex/0_getting-started.tex,
# section "Currently available functions"

const function_names = split("""exp expm1 log log1p log2 log10
sin cos tan asin acos atan
sinh cosh sinpi cospi tanpi atanpi
""")


const functions = map(Symbol, function_names)


## Generate wrappers of CRlibm shared library:

# Aiming for functions of the form
# cos(x::Float64, ::RoundingMode{:RoundUp}) = ccall((:cos, libcrlibm), Float64, (Float64,), x)


const MPFR_function_names = split("""exp expm1 log log1p log2 log10
sin cos tan asin acos atan
sinh cosh
""")

const MPFR_functions = map(Symbol, MPFR_function_names)

wrap_generic_fallbacks()


unixpath = "../deps/src/crlibm-1.0beta4/libcrlibm"
const libcrlibm = joinpath(dirname(@__FILE__), unixpath)


use_MPFR = false

# Ensure library is available:
if (Libdl.dlopen_e(libcrlibm) == C_NULL)
warn("CRlibm is falling back to use MPFR; it will have
the same functionality, but with slower execution.
This is currently the only option on Windows.")

use_MPFR = true
end

setup_CRlibm(use_MPFR)


end # module
Expand Down

0 comments on commit b61bf03

Please sign in to comment.