Skip to content

Commit

Permalink
check for out-of-range values in iround(). closes #1142
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Apr 28, 2013
1 parent 8b028f5 commit 2356fb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,20 +444,37 @@ static Value *emit_iround(Value *x, bool issigned, jl_codectx_t *ctx)
int64_t topbit;
Type *intt, *floatt;
Value *bits = JL_INT(x);
Value *max, *min;

if (bits->getType()->getPrimitiveSizeInBits() == 32) {
nmantissa = 23;
expoffs = 127;
expbits = 0xff;
topbit = BIT31;
intt = T_int32; floatt = T_float32;
if (issigned) {
max = ConstantFP::get(floatt, 2.1474835e9);
min = ConstantFP::get(floatt, -2.1474836e9);
}
else {
max = ConstantFP::get(floatt, 4.294967e9);
min = ConstantFP::get(floatt, 0.0);
}
}
else {
nmantissa = 52;
expoffs = 1023;
expbits = 0x7ff;
topbit = BIT63;
intt = T_int64; floatt = T_float64;
if (issigned) {
max = ConstantFP::get(floatt, 9.223372036854775e18);
min = ConstantFP::get(floatt, -9.223372036854776e18);
}
else {
max = ConstantFP::get(floatt, 1.844674407370955e19);
min = ConstantFP::get(floatt, 0.0);
}
}

// itrunc(x + copysign(0.5,x))
Expand All @@ -476,6 +493,9 @@ static Value *emit_iround(Value *x, bool issigned, jl_codectx_t *ctx)
builder.CreateBitCast(signedhalf, floatt));

Value *src = builder.CreateSelect(isint, FP(x), sum);
raise_exception_unless(builder.CreateAnd(builder.CreateFCmpOLE(src, max),
builder.CreateFCmpOGE(src, min)),
jlinexacterr_var, ctx);
if (issigned)
return builder.CreateFPToSI(src, intt);
else
Expand Down
17 changes: 17 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,23 @@ for x = 2^24-10:2^24+10
@test iceil(y) == i
end

@test_fails iround(Inf)
@test_fails iround(NaN)
@test iround(2.5) == 3
@test iround(-1.9) == -2
@test_fails iround(Int64, 9.223372036854776e18)
@test iround(Int64, 9.223372036854775e18) == 9223372036854774784
@test_fails iround(Int64, -9.223372036854778e18)
@test iround(Int64, -9.223372036854776e18) == typemin(Int64)
@test_fails iround(Uint64, 1.8446744073709552e19)
@test iround(Uint64, 1.844674407370955e19) == 0xfffffffffffff800
@test_fails iround(Int32, 2.1474836f9)
@test iround(Int32, 2.1474835f9) == 2147483520
@test_fails iround(Int32, -2.147484f9)
@test iround(Int32, -2.1474836f9) == typemin(Int32)
@test_fails iround(Uint32, 4.2949673f9)
@test iround(Uint32, 4.294967f9) == 0xffffff00

for n = 1:100
m = 1
for (p,k) in factor(n)
Expand Down

0 comments on commit 2356fb8

Please sign in to comment.