forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath_libm.j
65 lines (56 loc) · 2.34 KB
/
math_libm.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
libm = dlopen("libm")
macro vectorize(f)
quote
($f)(x::Vector) = [ ($f)(x[i]) | i=1:length(x) ]
($f)(x::Matrix) = [ ($f)(x[i,j]) | i=1:size(x,1), j=1:size(x,2) ]
end
end
for f = {:sin, :cos, :tan, :sinh, :cosh, :tanh, :asin, :acos, :atan, :log,
:log2, :log10, :log1p, :logb, :exp, :exp2, :expm1, :erf, :erfc,
:sqrt, :cbrt, :ceil, :floor, :nearbyint, :round, :rint, :trunc}
@eval begin
($f)(x::Float64) = ccall(dlsym(libm,$string(f)), Float64, (Float64,), x)
($f)(x::Float32) = ccall(dlsym(libm,$strcat(string(f),"f")), Float32, (Float32,), x)
($f)(x::Real) = ($f)(convert(Float64,x))
@vectorize $f
end
end
ipart(x) = trunc(x)
fpart(x) = x - trunc(x)
for f = {:isinf, :isnan}
@eval begin
($f)(x::Float64) = (0 != ccall(dlsym(libm,$string(f)), Int32, (Float64,), x))
($f)(x::Float32) = ($f)(float64(x))
($f)(x::Int) = false
@vectorize $f
end
end
for f = {:lrint, :lround, :ilogb}
@eval begin
($f)(x::Float64) = ccall(dlsym(libm,$string(f)), Int32, (Float64,), x)
($f)(x::Float32) = ccall(dlsym(libm,$strcat(string(f),"f")), Int32, (Float32,), x)
@vectorize $f
end
end
abs(x::Float64) = ccall(dlsym(libm, :fabs), Float64, (Float64,), x)
abs(x::Float32) = ccall(dlsym(libm, :fabsf), Float32, (Float32,), x)
@vectorize abs
for f = {:atan2, :pow, :fmod, :copysign, :hypot, :fmin, :fmax, :fdim}
@eval begin
($f)(x::Float64, y::Float64) = ccall(dlsym(libm,$string(f)),
Float64,
(Float64, Float64,),
x, y)
($f)(x::Float32, y::Float32) = ccall(dlsym(libm,$strcat(string(f),"f")),
Float32,
(Float32, Float32),
x, y)
($f)(x::Real, y::Real) = ($f)(convert(Float64,x),convert(Float64,y))
end
end
ldexp(x::Float64,e::Int32) = ccall(dlsym(libm, :ldexp), Float64, (Float64,Int32), x, e)
ldexp(x::Float32,e::Int32) = ccall(dlsym(libm, :ldexpf), Float32, (Float32,Int32), x, e)
rand() = ccall(:rand_double, Float64, ())
randf() = ccall(:rand_float, Float32, ())
randui32() = ccall(:genrand_int32, Uint32, ())
randn() = ccall(:randn, Float64, ())