Skip to content

Commit

Permalink
Use compact parametric syntax in base/f*.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloferz committed Feb 4, 2017
1 parent 8533c92 commit fe86631
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
42 changes: 21 additions & 21 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ end

FloatTypes = Union{Float32, Float64}

sub_fast{T<:FloatTypes}(x::T) = neg_float_fast(x)
sub_fast(x::FloatTypes) = neg_float_fast(x)

add_fast{T<:FloatTypes}(x::T, y::T) = add_float_fast(x, y)
sub_fast{T<:FloatTypes}(x::T, y::T) = sub_float_fast(x, y)
Expand Down Expand Up @@ -169,11 +169,11 @@ issubnormal_fast(x) = false
ComplexTypes = Union{Complex64, Complex128}

@fastmath begin
abs_fast{T<:ComplexTypes}(x::T) = hypot(real(x), imag(x))
abs2_fast{T<:ComplexTypes}(x::T) = real(x)*real(x) + imag(x)*imag(x)
abs_fast(x::ComplexTypes) = hypot(real(x), imag(x))
abs2_fast(x::ComplexTypes) = real(x)*real(x) + imag(x)*imag(x)
conj_fast{T<:ComplexTypes}(x::T) = T(real(x), -imag(x))
inv_fast{T<:ComplexTypes}(x::T) = conj(x) / abs2(x)
sign_fast{T<:ComplexTypes}(x::T) = x == 0 ? float(zero(x)) : x/abs(x)
inv_fast(x::ComplexTypes) = conj(x) / abs2(x)
sign_fast(x::ComplexTypes) = x == 0 ? float(zero(x)) : x/abs(x)

add_fast{T<:ComplexTypes}(x::T, y::T) =
T(real(x)+real(y), imag(x)+imag(y))
Expand Down Expand Up @@ -243,12 +243,12 @@ end

# builtins

pow_fast{T<:FloatTypes}(x::T, y::Integer) = pow_fast(x, Int32(y))
pow_fast{T<:FloatTypes}(x::T, y::Int32) = Base.powi_llvm(x, y)
pow_fast(x::FloatTypes, y::Integer) = pow_fast(x, Int32(y))
pow_fast(x::FloatTypes, y::Int32) = Base.powi_llvm(x, y)

# TODO: Change sqrt_llvm intrinsic to avoid nan checking; add nan
# checking to sqrt in math.jl; remove sqrt_llvm_fast intrinsic
sqrt_fast{T<:FloatTypes}(x::T) = sqrt_llvm_fast(x)
sqrt_fast(x::FloatTypes) = sqrt_llvm_fast(x)

# libm

Expand Down Expand Up @@ -300,29 +300,29 @@ atan2_fast(x::Float64, y::Float64) =
pow_fast{T<:FloatTypes}(x::Complex{T}, y::T) = exp(y*log(x))
acos_fast{T<:ComplexTypes}(x::T) =
convert(T,π)/2 + im*log(im*x + sqrt(1-x*x))
acosh_fast{T<:ComplexTypes}(x::T) = log(x + sqrt(x+1) * sqrt(x-1))
angle_fast{T<:ComplexTypes}(x::T) = atan2(imag(x), real(x))
asin_fast{T<:ComplexTypes}(x::T) = -im*asinh(im*x)
asinh_fast{T<:ComplexTypes}(x::T) = log(x + sqrt(1+x*x))
atan_fast{T<:ComplexTypes}(x::T) = -im*atanh(im*x)
acosh_fast(x::ComplexTypes) = log(x + sqrt(x+1) * sqrt(x-1))
angle_fast(x::ComplexTypes) = atan2(imag(x), real(x))
asin_fast(x::ComplexTypes) = -im*asinh(im*x)
asinh_fast(x::ComplexTypes) = log(x + sqrt(1+x*x))
atan_fast(x::ComplexTypes) = -im*atanh(im*x)
atanh_fast{T<:ComplexTypes}(x::T) = convert(T,1)/2*(log(1+x) - log(1-x))
cis_fast{T<:ComplexTypes}(x::T) = exp(-imag(x)) * cis(real(x))
cos_fast{T<:ComplexTypes}(x::T) = cosh(im*x)
cis_fast(x::ComplexTypes) = exp(-imag(x)) * cis(real(x))
cos_fast(x::ComplexTypes) = cosh(im*x)
cosh_fast{T<:ComplexTypes}(x::T) = convert(T,1)/2*(exp(x) + exp(-x))
exp10_fast{T<:ComplexTypes}(x::T) =
exp10(real(x)) * cis(imag(x)*log(convert(T,10)))
exp2_fast{T<:ComplexTypes}(x::T) =
exp2(real(x)) * cis(imag(x)*log(convert(T,2)))
exp_fast{T<:ComplexTypes}(x::T) = exp(real(x)) * cis(imag(x))
expm1_fast{T<:ComplexTypes}(x::T) = exp(x)-1
exp_fast(x::ComplexTypes) = exp(real(x)) * cis(imag(x))
expm1_fast(x::ComplexTypes) = exp(x)-1
log10_fast{T<:ComplexTypes}(x::T) = log(x) / log(convert(T,10))
log1p_fast{T<:ComplexTypes}(x::T) = log(1+x)
log1p_fast(x::ComplexTypes) = log(1+x)
log2_fast{T<:ComplexTypes}(x::T) = log(x) / log(convert(T,2))
log_fast{T<:ComplexTypes}(x::T) = T(log(abs2(x))/2, angle(x))
sin_fast{T<:ComplexTypes}(x::T) = -im*sinh(im*x)
sin_fast(x::ComplexTypes) = -im*sinh(im*x)
sinh_fast{T<:ComplexTypes}(x::T) = convert(T,1)/2*(exp(x) - exp(-x))
sqrt_fast{T<:ComplexTypes}(x::T) = sqrt(abs(x)) * cis(angle(x)/2)
tan_fast{T<:ComplexTypes}(x::T) = -im*tanh(im*x)
sqrt_fast(x::ComplexTypes) = sqrt(abs(x)) * cis(angle(x)/2)
tan_fast(x::ComplexTypes) = -im*tanh(im*x)
tanh_fast{T<:ComplexTypes}(x::T) = (a=exp(x); b=exp(-x); (a-b)/(a+b))
end

Expand Down
2 changes: 1 addition & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ truncmask(x, mask) = x

## Array operations on floating point numbers ##

float{T<:AbstractFloat}(A::AbstractArray{T}) = A
float(A::AbstractArray{<:AbstractFloat}) = A

function float{T}(A::AbstractArray{T})
if !isleaftype(T)
Expand Down
2 changes: 1 addition & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const ≈ = isapprox

# default tolerance arguments
rtoldefault{T<:AbstractFloat}(::Type{T}) = sqrt(eps(T))
rtoldefault{T<:Real}(::Type{T}) = 0
rtoldefault(::Type{<:Real}) = 0
rtoldefault{T<:Number,S<:Number}(x::Union{T,Type{T}}, y::Union{S,Type{S}}) = max(rtoldefault(real(T)),rtoldefault(real(S)))

# fused multiply-add
Expand Down

0 comments on commit fe86631

Please sign in to comment.