mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
implement bool conversion for float
This commit is contained in:
1 parent
80d168cc74
commit
abf5ab467b
2 files changed
+22
-1
No files matched your search
@@ -82,7 +82,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
|
||||
|
||||
fn boolean_val(&mut self, val: JitValue) -> Result<Value, JitCompileError> {
|
||||
match val.ty {
|
||||
JitType::Float => Err(JitCompileError::NotSupported),
|
||||
JitType::Float => {
|
||||
let zero = self.builder.ins().f64const(0);
|
||||
let val = self.builder.ins().fcmp(FloatCC::NotEqual, val.val, zero);
|
||||
Ok(self.builder.ins().bint(types::I64, val))
|
||||
},
|
||||
JitType::Int => Ok(val.val),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,3 +90,20 @@ fn test_div() {
|
||||
assert_bits_eq!(div(2.0, f64::NEG_INFINITY), Ok(-0.0f64));
|
||||
assert_bits_eq!(div(-1.0, f64::INFINITY), Ok(-0.0f64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_bool() {
|
||||
let if_bool = jit_function! { if_bool(a:f64) -> i64 => r##"
|
||||
def if_bool(a: float):
|
||||
if a:
|
||||
return 1
|
||||
return 0
|
||||
"## };
|
||||
|
||||
assert_eq!(if_bool(5.2), Ok(1));
|
||||
assert_eq!(if_bool(-3.4), Ok(1));
|
||||
assert_eq!(if_bool(f64::NAN), Ok(1));
|
||||
assert_eq!(if_bool(f64::INFINITY), Ok(1));
|
||||
|
||||
assert_eq!(if_bool(0.0), Ok(0));
|
||||
}
|
||||
Reference in new issue
Block a user