Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int vs float comparisons in Julia #9030

Merged
merged 2 commits into from
Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,35 +177,35 @@ function cmp(x::FloatingPoint, y::Real)
ifelse(x<y, -1, ifelse(x>y, 1, 0))
end

==(x::Float64, y::Int64 ) = eqfsi64(unbox(Float64,x),unbox(Int64,y))
==(x::Float64, y::UInt64 ) = eqfui64(unbox(Float64,x),unbox(UInt64,y))
==(x::Int64 , y::Float64) = eqfsi64(unbox(Float64,y),unbox(Int64,x))
==(x::UInt64 , y::Float64) = eqfui64(unbox(Float64,y),unbox(UInt64,x))

==(x::Float32, y::Int64 ) = eqfsi64(unbox(Float32,x),unbox(Int64,y))
==(x::Float32, y::UInt64 ) = eqfui64(unbox(Float32,x),unbox(UInt64,y))
==(x::Int64 , y::Float32) = eqfsi64(unbox(Float32,y),unbox(Int64,x))
==(x::UInt64 , y::Float32) = eqfui64(unbox(Float32,y),unbox(UInt64,x))

< (x::Float64, y::Int64 ) = ltfsi64(unbox(Float64,x),unbox(Int64,y))
< (x::Float64, y::UInt64 ) = ltfui64(unbox(Float64,x),unbox(UInt64,y))
< (x::Int64 , y::Float64) = ltsif64(unbox(Int64,x),unbox(Float64,y))
< (x::UInt64 , y::Float64) = ltuif64(unbox(UInt64,x),unbox(Float64,y))

< (x::Float32, y::Int64 ) = ltfsi64(unbox(Float64,float64(x)),unbox(Int64,y))
< (x::Float32, y::UInt64 ) = ltfui64(unbox(Float64,float64(x)),unbox(UInt64,y))
< (x::Int64 , y::Float32) = ltsif64(unbox(Int64,x),unbox(Float64,float64(y)))
< (x::UInt64 , y::Float32) = ltuif64(unbox(UInt64,x),unbox(Float64,float64(y)))

<=(x::Float64, y::Int64 ) = lefsi64(unbox(Float64,x),unbox(Int64,y))
<=(x::Float64, y::UInt64 ) = lefui64(unbox(Float64,x),unbox(UInt64,y))
<=(x::Int64 , y::Float64) = lesif64(unbox(Int64,x),unbox(Float64,y))
<=(x::UInt64 , y::Float64) = leuif64(unbox(UInt64,x),unbox(Float64,y))

<=(x::Float32, y::Int64 ) = lefsi64(unbox(Float64,float64(x)),unbox(Int64,y))
<=(x::Float32, y::UInt64 ) = lefui64(unbox(Float64,float64(x)),unbox(UInt64,y))
<=(x::Int64 , y::Float32) = lesif64(unbox(Int64,x),unbox(Float64,float64(y)))
<=(x::UInt64 , y::Float32) = leuif64(unbox(UInt64,x),unbox(Float64,float64(y)))
for Ti in (Int64,UInt64)
for Tf in (Float32,Float64)
@eval begin
function ==(x::$Tf, y::$Ti)
fy = ($Tf)(y)
(x == fy) & (y == itrunc($Ti,fy))
end
==(y::$Ti, x::$Tf) = x==y

function <(x::$Ti, y::$Tf)
fx = ($Tf)(x)
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x < itrunc($Ti,fx)) ))
end
function <=(x::$Ti, y::$Tf)
fx = ($Tf)(x)
(fx < y) | ((fx == y) & ((fx == $(Tf(typemax(Ti)))) | (x <= itrunc($Ti,fx)) ))
end

function <(x::$Tf, y::$Ti)
fy = ($Tf)(y)
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) < y))
end
function <=(x::$Tf, y::$Ti)
fy = ($Tf)(y)
(x < fy) | ((x == fy) & (fy < $(Tf(typemax(Ti)))) & (itrunc($Ti,fy) <= y))
end
end
end
end

==(x::Float32, y::Union(Int32,UInt32)) = float64(x)==float64(y)
==(x::Union(Int32,UInt32), y::Float32) = float64(x)==float64(y)
Expand Down
10 changes: 0 additions & 10 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ t_func[eq_float] = (2, 2, cmp_tfunc)
t_func[ne_float] = (2, 2, cmp_tfunc)
t_func[lt_float] = (2, 2, cmp_tfunc)
t_func[le_float] = (2, 2, cmp_tfunc)
t_func[eqfsi64] = (2, 2, cmp_tfunc)
t_func[eqfui64] = (2, 2, cmp_tfunc)
t_func[ltfsi64] = (2, 2, cmp_tfunc)
t_func[ltfui64] = (2, 2, cmp_tfunc)
t_func[lefsi64] = (2, 2, cmp_tfunc)
t_func[lefui64] = (2, 2, cmp_tfunc)
t_func[ltsif64] = (2, 2, cmp_tfunc)
t_func[ltuif64] = (2, 2, cmp_tfunc)
t_func[lesif64] = (2, 2, cmp_tfunc)
t_func[leuif64] = (2, 2, cmp_tfunc)
t_func[fpiseq] = (2, 2, cmp_tfunc)
t_func[fpislt] = (2, 2, cmp_tfunc)
t_func[nan_dom_err] = (2, 2, (a, b)->a)
Expand Down
138 changes: 0 additions & 138 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ namespace JL_I {
eq_float, ne_float,
lt_float, le_float,
fpiseq, fpislt,
// mixed-type comparisons
eqfsi64, eqfui64,
ltfsi64, ltfui64,
lefsi64, lefui64,
ltsif64, ltuif64,
lesif64, leuif64,
// bitwise operators
and_int, or_int, xor_int, not_int, shl_int, lshr_int, ashr_int,
bswap_int, ctpop_int, ctlz_int, cttz_int,
Expand Down Expand Up @@ -1114,133 +1108,6 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
HANDLE(lt_float,2) return builder.CreateFCmpOLT(FP(x), FP(y));
HANDLE(le_float,2) return builder.CreateFCmpOLE(FP(x), FP(y));

HANDLE(eqfsi64,2) return emit_eqfsi(x, y);
HANDLE(eqfui64,2) return emit_eqfui(x, y);
HANDLE(ltfsi64,2) {
x = FP(x);
fy = JL_INT(y);
Value *ffy = builder.CreateSIToFP(fy, T_float64);
Value *siffy = builder.CreateFPToSI(ffy, T_int64);
return builder.CreateOr(
builder.CreateFCmpOLT(x, ffy),
builder.CreateAnd(
// ensure x < 2.0^63
builder.CreateFCmpOLT(x, ConstantFP::get(T_float64,
9.223372036854776e18)),
builder.CreateAnd(builder.CreateFCmpOEQ(x, ffy),
builder.CreateICmpSGT(fy, siffy))
)
);
}
HANDLE(ltfui64,2) {
x = FP(x);
fy = JL_INT(y);
Value *ffy = builder.CreateUIToFP(fy, T_float64);
Value *uiffy = builder.CreateFPToUI(ffy, T_int64);
return builder.CreateOr(
builder.CreateFCmpOLT(x, ffy),
builder.CreateAnd(
// ensure x < 2.0^64
builder.CreateFCmpOLT(x, ConstantFP::get(T_float64,
1.8446744073709552e19)),
builder.CreateAnd(builder.CreateFCmpOEQ(x, ffy),
builder.CreateICmpUGT(fy, uiffy))
)
);
}
HANDLE(lefsi64,2) {
x = FP(x);
fy = JL_INT(y);
Value *ffy = builder.CreateSIToFP(fy, T_float64);
return builder.CreateOr(
builder.CreateFCmpOLT(x, ffy),
builder.CreateAnd(
builder.CreateFCmpOLT(x, ConstantFP::get(T_float64,
9.223372036854776e18)),
builder.CreateAnd(
builder.CreateFCmpOEQ(x, ffy),
builder.CreateICmpSGE(fy, builder.CreateFPToSI(ffy,T_int64))
)
)
);
}
HANDLE(lefui64,2) {
x = FP(x);
fy = JL_INT(y);
Value *ffy = builder.CreateUIToFP(fy, T_float64);
return builder.CreateOr(
builder.CreateFCmpOLT(x, ffy),
builder.CreateAnd(
builder.CreateFCmpOLT(x, ConstantFP::get(T_float64,
1.8446744073709552e19)),
builder.CreateAnd(
builder.CreateFCmpOEQ(x, ffy),
builder.CreateICmpUGE(fy, builder.CreateFPToUI(ffy, T_int64))
)
)
);
}
HANDLE(ltsif64,2) {
x = JL_INT(x);
fy = FP(y);
Value *fx = builder.CreateSIToFP(x, T_float64);
return builder.CreateOr(
builder.CreateOr(
builder.CreateFCmpOGE(fy, ConstantFP::get(T_float64,
9.223372036854776e18)),
builder.CreateFCmpOLT(fx, fy)),
builder.CreateAnd(
builder.CreateFCmpOEQ(fx, fy),
builder.CreateICmpSLT(x, builder.CreateFPToSI(fx, T_int64))
)
);
}
HANDLE(ltuif64,2) {
x = JL_INT(x);
fy = FP(y);
Value *fx = builder.CreateUIToFP(x, T_float64);
return builder.CreateOr(
builder.CreateOr(
builder.CreateFCmpOGE(fy, ConstantFP::get(T_float64,
1.8446744073709552e19)),
builder.CreateFCmpOLT(fx, fy)),
builder.CreateAnd(
builder.CreateFCmpOEQ(fx, fy),
builder.CreateICmpULT(x, builder.CreateFPToUI(fx, T_int64))
)
);
}
HANDLE(lesif64,2) {
x = JL_INT(x);
fy = FP(y);
Value *fx = builder.CreateSIToFP(x, T_float64);
return builder.CreateOr(
builder.CreateOr(
builder.CreateFCmpOGE(fy, ConstantFP::get(T_float64,
9.223372036854776e18)),
builder.CreateFCmpOLT(fx, fy)),
builder.CreateAnd(
builder.CreateFCmpOEQ(fx, fy),
builder.CreateICmpSLE(x, builder.CreateFPToSI(fx, T_int64))
)
);
}
HANDLE(leuif64,2) {
x = JL_INT(x);
fy = FP(y);
Value *fx = builder.CreateUIToFP(x, T_float64);
return builder.CreateOr(
builder.CreateOr(
builder.CreateFCmpOGE(fy, ConstantFP::get(T_float64,
1.8446744073709552e19)),
builder.CreateFCmpOLT(fx, fy)),
builder.CreateAnd(
builder.CreateFCmpOEQ(fx, fy),
builder.CreateICmpULE(x, builder.CreateFPToUI(fx, T_int64))
)
);
}

HANDLE(fpiseq,2) {
Value *xi = JL_INT(x);
Value *yi = JL_INT(y);
Expand Down Expand Up @@ -1492,11 +1359,6 @@ extern "C" void jl_init_intrinsic_functions(void)
ADD_I(sle_int); ADD_I(ule_int);
ADD_I(eq_float); ADD_I(ne_float);
ADD_I(lt_float); ADD_I(le_float);
ADD_I(eqfsi64); ADD_I(eqfui64);
ADD_I(ltfsi64); ADD_I(ltfui64);
ADD_I(lefsi64); ADD_I(lefui64);
ADD_I(ltsif64); ADD_I(ltuif64);
ADD_I(lesif64); ADD_I(leuif64);
ADD_I(fpiseq); ADD_I(fpislt);
ADD_I(and_int); ADD_I(or_int); ADD_I(xor_int); ADD_I(not_int);
ADD_I(shl_int); ADD_I(lshr_int); ADD_I(ashr_int); ADD_I(bswap_int);
Expand Down