Skip to content

Commit abf5ab4

Browse files
committed
implement bool conversion for float
1 parent 80d168c commit abf5ab4

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

jit/src/instructions.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
8282

8383
fn boolean_val(&mut self, val: JitValue) -> Result<Value, JitCompileError> {
8484
match val.ty {
85-
JitType::Float => Err(JitCompileError::NotSupported),
85+
JitType::Float => {
86+
let zero = self.builder.ins().f64const(0);
87+
let val = self.builder.ins().fcmp(FloatCC::NotEqual, val.val, zero);
88+
Ok(self.builder.ins().bint(types::I64, val))
89+
},
8690
JitType::Int => Ok(val.val),
8791
}
8892
}

jit/tests/float_tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,20 @@ fn test_div() {
9090
assert_bits_eq!(div(2.0, f64::NEG_INFINITY), Ok(-0.0f64));
9191
assert_bits_eq!(div(-1.0, f64::INFINITY), Ok(-0.0f64));
9292
}
93+
94+
#[test]
95+
fn test_if_bool() {
96+
let if_bool = jit_function! { if_bool(a:f64) -> i64 => r##"
97+
def if_bool(a: float):
98+
if a:
99+
return 1
100+
return 0
101+
"## };
102+
103+
assert_eq!(if_bool(5.2), Ok(1));
104+
assert_eq!(if_bool(-3.4), Ok(1));
105+
assert_eq!(if_bool(f64::NAN), Ok(1));
106+
assert_eq!(if_bool(f64::INFINITY), Ok(1));
107+
108+
assert_eq!(if_bool(0.0), Ok(0));
109+
}

0 commit comments

Comments
 (0)