Skip to content

Commit

Permalink
adding intrinsic to compute iround efficiently
Browse files Browse the repository at this point in the history
adding itrunc for explicit truncating conversion to integer
preparation for fixing issue #170
  • Loading branch information
JeffBezanson committed Aug 12, 2011
1 parent 07b1273 commit 2e826e4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sys.ji: VERSION j/sysimg.j j/start_image.j src/boot.j src/dump.c j/*.j
clean:
rm -f julia
rm -f *~ *#
rm -f sys.ji
$(MAKE) -C j clean
$(MAKE) -C src clean
$(MAKE) -C ui clean
Expand Down
6 changes: 6 additions & 0 deletions j/float.j
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## floating point conversions ##

iround(x::Float32) = boxsi32(fpiround32(unbox32(x)))
iround(x::Float64) = boxsi64(fpiround64(unbox64(x)))

itrunc(x::Float32) = boxsi32(fptosi32(unbox32(x)))
itrunc(x::Float64) = boxsi64(fptosi64(unbox64(x)))

convert(::Type{Float32}, x::Bool) = boxf32(sitofp32(unbox8(x)))
convert(::Type{Float32}, x::Int8) = boxf32(sitofp32(unbox8(x)))
convert(::Type{Float32}, x::Int16) = boxf32(sitofp32(unbox16(x)))
Expand Down
3 changes: 3 additions & 0 deletions j/floatfuncs.j
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ function hex2num(s)
end
return boxf64(unbox64(parse_int(Int64, s, 16)))
end

@vectorize_1arg Real iround
@vectorize_1arg Real itrunc
2 changes: 1 addition & 1 deletion j/math_libm.j
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end
@libmfunc_1arg_float Real round

#@libmfunc_1arg_int Real lrint
@libmfunc_1arg_int Real lround iround
#@libmfunc_1arg_int Real lround iround
@libmfunc_1arg_int Real ilogb

@libfdmfunc_2arg Number atan2
Expand Down
34 changes: 34 additions & 0 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace JL_I {
trunc8, trunc16, trunc32, trunc64, trunc_int,
fptoui8, fptosi8, fptoui16, fptosi16, fptoui32, fptosi32,
fptoui64, fptosi64,
fpiround32, fpiround64,
uitofp32, sitofp32, uitofp64, sitofp64,
fptrunc32, fpext64,
// functions
Expand Down Expand Up @@ -957,6 +958,38 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
return builder.CreateFPToUI(FP(x), T_int64);
HANDLE(fptosi64,1)
return builder.CreateFPToSI(FP(x), T_int64);
HANDLE(fpiround32,1)
{
Value *bits = builder.CreateBitCast(x, T_int32);
Value *half = builder.CreateBitCast(ConstantFP::get(T_float32, 0.5),
T_int32);
Value *signedhalf =
builder.CreateOr(half,
builder.CreateAnd(bits,
ConstantInt::get(T_int32,
BIT31)));
return builder.
CreateFPToSI(builder.CreateFAdd(x,
builder.CreateBitCast(signedhalf,
T_float32)),
T_int32);
}
HANDLE(fpiround64,1)
{
Value *bits = builder.CreateBitCast(x, T_int64);
Value *half = builder.CreateBitCast(ConstantFP::get(T_float64, 0.5),
T_int64);
Value *signedhalf =
builder.CreateOr(half,
builder.CreateAnd(bits,
ConstantInt::get(T_int64,
BIT63)));
return builder.
CreateFPToSI(builder.CreateFAdd(x,
builder.CreateBitCast(signedhalf,
T_float64)),
T_int64);
}
HANDLE(uitofp32,1)
return builder.CreateUIToFP(x, T_float32);
HANDLE(sitofp32,1)
Expand Down Expand Up @@ -1081,6 +1114,7 @@ extern "C" void jl_init_intrinsic_functions()
ADD_I(fptoui8); ADD_I(fptosi8);
ADD_I(fptoui16); ADD_I(fptosi16); ADD_I(fptoui32); ADD_I(fptosi32);
ADD_I(fptoui64); ADD_I(fptosi64);
ADD_I(fpiround32); ADD_I(fpiround64);
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);
Expand Down

0 comments on commit 2e826e4

Please sign in to comment.