Skip to content

Commit

Permalink
switching to the type-stable meaning of ^; only give complex result
Browse files Browse the repository at this point in the history
  for complex argument
removing unused sin_float and cos_float
removing pow function, using only ^
  • Loading branch information
JeffBezanson committed Jul 2, 2011
1 parent c163411 commit 8889fe7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 46 deletions.
32 changes: 8 additions & 24 deletions j/complex.j
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ convert(::Type{Complex128}, x::Real) = complex128(x,zero(x))
convert(::Type{Complex128}, z::ComplexNum) = complex128(real(z),imag(z))

promote_rule(::Type{Complex128}, ::Type{Float64}) = Complex128
promote_rule(::Type{Complex128}, ::Type{Float32}) = Complex128
promote_rule{S<:Real}(::Type{Complex128}, ::Type{S}) =
(P = promote_type(Float64,S);
is(P,Float64) ? Complex128 : Complex{P})
Expand All @@ -77,6 +78,7 @@ pi(::Type{Complex64}) = pi(Float32)
convert(::Type{Complex64}, x::Real) = complex64(x,zero(x))
convert(::Type{Complex64}, z::ComplexNum) = complex64(real(z),imag(z))

promote_rule(::Type{Complex64}, ::Type{Float64}) = Complex128
promote_rule(::Type{Complex64}, ::Type{Float32}) = Complex64
promote_rule{S<:Real}(::Type{Complex64}, ::Type{S}) =
(P = promote_type(Float32,S);
Expand Down Expand Up @@ -236,17 +238,13 @@ end

^(x::Int, p::Float) = ^(promote(x,p)...)

function ^(x::Float, p::Float)
if x >= 0
return pow(x, p)
end
if p == 0.5
return sqrt(complex(x))
end
return complex(x)^complex(p)
end
^(z::ComplexNum, p::ComplexNum) = ^(promote(z,p)...)

function ^(z::ComplexNum, p::ComplexNum)
^(z::Real, p::ComplexNum) = ^(promote(z,p)...)

^(z::ComplexNum, p::Float) = ^(promote(z,p)...)

function ^{T<:ComplexNum}(z::T, p::T)
realp = real(p)
if imag(p) == 0
if realp == 0
Expand Down Expand Up @@ -303,20 +301,6 @@ function ^(z::ComplexNum, p::ComplexNum)
complex(rp*cos(ntheta), rp*sin(ntheta))
end

function ^(z::Real, p::ComplexNum)
if imag(p) == 0
return z^real(p)
end
^(promote(z,p)...)
end

function ^(z::ComplexNum, p::Float)
if imag(z) == 0
return real(z)^p
end
^(promote(z,p)...)
end

function tan(z::ComplexNum)
u = exp(imag(z))
v = 1/u
Expand Down
4 changes: 0 additions & 4 deletions j/floatfuncs.j
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ integer_valued(x::Float32) = (trunc(x)==x && abs(x)<=float32(16777216.))

#sqrt(x::Float64) = boxf64(sqrt_float(unbox64(x)))
#sqrt(x::Float32) = boxf32(sqrt_float(unbox32(x)))
#sin(x::Float64) = boxf64(sin_float(unbox64(x)))
#sin(x::Float32) = boxf32(sin_float(unbox32(x)))
#cos(x::Float64) = boxf64(cos_float(unbox64(x)))
#cos(x::Float32) = boxf32(cos_float(unbox32(x)))
^(x::Float64, p::Int32) = boxf64(powi_float(unbox64(x),unbox32(p)))
^(x::Float32, p::Int32) = boxf32(powi_float(unbox32(x),unbox32(p)))

Expand Down
2 changes: 1 addition & 1 deletion j/intfuncs.j
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function ^(x, p::Int)
elseif p == 0
return one(x)
elseif p < 0
return 1/(x^(-p))
return inv(x^(-p))
elseif p == 2
return x*x
end
Expand Down
5 changes: 4 additions & 1 deletion j/math_libm.j
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ end
@libmfunc_1arg_int ilogb

@libfdmfunc_2arg atan2
@libfdmfunc_2arg pow
@libfdmfunc_2arg fmod
@libfdmfunc_2arg copysign
@libfdmfunc_2arg hypot
Expand Down Expand Up @@ -127,3 +126,7 @@ function frexp(x::Float32)
(s, exp[1])
end
@vectorize_1arg frexp

^(x::Float64, y::Float64) = ccall(dlsym(libfdm, :pow), Float64, (Float64,Float64), x, y)
^(x::Float32, y::Float32) = ccall(dlsym(libfdm, :powf), Float32, (Float32,Float32), x, y)
^(x::Float, y::Float) = ^(promote(x,y)...)
17 changes: 1 addition & 16 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace JL_I {
uitofp32, sitofp32, uitofp64, sitofp64,
fptrunc32, fpext64,
// functions
sqrt_float, powi_float, sin_float, cos_float, pow_float,
sqrt_float, powi_float, pow_float,
// c interface
ccall,
};
Expand Down Expand Up @@ -984,20 +984,6 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
Intrinsic::sqrt,
&fxt, 1),
fx);
HANDLE(sin_float,1)
fx = FP(x);
fxt = fx->getType();
return builder.CreateCall(Intrinsic::getDeclaration(jl_Module,
Intrinsic::sin,
&fxt, 1),
fx);
HANDLE(cos_float,1)
fx = FP(x);
fxt = fx->getType();
return builder.CreateCall(Intrinsic::getDeclaration(jl_Module,
Intrinsic::cos,
&fxt, 1),
fx);
HANDLE(pow_float,2)
fx = FP(x);
fy = FP(emit_expr(args[2],ctx,true));
Expand Down Expand Up @@ -1094,7 +1080,6 @@ extern "C" void jl_init_intrinsic_functions()
ADD_I(uitofp32); ADD_I(sitofp32); ADD_I(uitofp64); ADD_I(sitofp64);
ADD_I(fptrunc32); ADD_I(fpext64);
ADD_I(sqrt_float); ADD_I(powi_float); ADD_I(pow_float);
ADD_I(sin_float); ADD_I(cos_float);
ADD_I(ccall);

BOX_F(int8); BOX_F(uint8);
Expand Down

0 comments on commit 8889fe7

Please sign in to comment.