Skip to content

Commit ca02438

Browse files
Merge pull request #67 from JuliaMath/Assume-effects
Teach julia that `NaNMath` functions don't have effects
2 parents 7e44d41 + e51a40a commit ca02438

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/NaNMath.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const libm = OpenLibm_jll.libopenlibm
66
for f in (:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10,
77
:lgamma, :log1p)
88
@eval begin
9-
($f)(x::Float64) = ccall(($(string(f)),libm), Float64, (Float64,), x)
10-
($f)(x::Float32) = ccall(($(string(f,"f")),libm), Float32, (Float32,), x)
9+
Base.@assume_effects :total ($f)(x::Float64) = ccall(($(string(f)),libm), Float64, (Float64,), x)
10+
Base.@assume_effects :total ($f)(x::Float32) = ccall(($(string(f,"f")),libm), Float32, (Float32,), x)
1111
($f)(x::Real) = ($f)(float(x))
1212
if $f !== :lgamma
1313
($f)(x) = (Base.$f)(x)
@@ -25,12 +25,13 @@ end
2525

2626
# Would be more efficient to remove the domain check in Base.sqrt(),
2727
# but this doesn't seem easy to do.
28+
Base.@assume_effects :nothrow sqrt(x::T) where {T<:Union{Float16, Float32, Float64}} = x < 0.0 ? T(NaN) : Base.sqrt(x)
2829
sqrt(x::T) where {T<:AbstractFloat} = x < 0.0 ? T(NaN) : Base.sqrt(x)
2930
sqrt(x::Real) = sqrt(float(x))
3031

3132
# Don't override built-in ^ operator
32-
pow(x::Float64, y::Float64) = ccall((:pow,libm), Float64, (Float64,Float64), x, y)
33-
pow(x::Float32, y::Float32) = ccall((:powf,libm), Float32, (Float32,Float32), x, y)
33+
Base.@assume_effects :total pow(x::Float64, y::Float64) = ccall((:pow,libm), Float64, (Float64,Float64), x, y)
34+
Base.@assume_effects :total pow(x::Float32, y::Float32) = ccall((:powf,libm), Float32, (Float32,Float32), x, y)
3435
# We `promote` first before converting to floating pointing numbers to ensure that
3536
# e.g. `pow(::Float32, ::Int)` ends up calling `pow(::Float32, ::Float32)`
3637
pow(x::Real, y::Real) = pow(promote(x, y)...)

0 commit comments

Comments
 (0)