Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix edge cases in interpreter's float-to-int128 conversion (#15199)

Ryan Buckley802482cac3c1
diff --git a/cranelift/codegen/meta/src/shared/instructions.rs b/cranelift/codegen/meta/src/shared/instructions.rs
index d7122622a2..db50c52b55 100644
--- a/cranelift/codegen/meta/src/shared/instructions.rs
+++ b/cranelift/codegen/meta/src/shared/instructions.rs
@@ -3193,9 +3193,9 @@ pub(crate) fn define(
r#"
Converts floating point scalars to unsigned integer.
- Only operates on `x` if it is a scalar. If `x` is NaN or if
- the unsigned integral value cannot be represented in the result
- type, this instruction traps.
+ Only operates on `x` if it is a scalar. This instruction traps if
+ `x` is NaN, or if it cannot be represented in the result type
+ after being truncated toward 0.
"#,
&formats.unary,
@@ -3212,9 +3212,9 @@ pub(crate) fn define(
r#"
Converts floating point scalars to signed integer.
- Only operates on `x` if it is a scalar. If `x` is NaN or if
- the unsigned integral value cannot be represented in the result
- type, this instruction traps.
+ Only operates on `x` if it is a scalar. This instruction traps if
+ `x` is NaN, or if it cannot be represented in the result type
+ after being truncated toward 0.
"#,
&formats.unary,
diff --git a/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif b/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif
index a511ea6f8f..975fae2d19 100644
--- a/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif
+++ b/cranelift/filetests/filetests/runtests/conversion-i8-i16.clif
@@ -124,6 +124,9 @@ block0(v0: f32):
; run: %f32_to_i8_sat(-NaN) == 0
; run: %f32_to_i8_sat(+Inf) == 0x7f
; run: %f32_to_i8_sat(-Inf) == 0x80
+; run: %f32_to_i8_sat(-0x4.c) == -4
+; run: %f32_to_i8_sat(-0x0.c) == 0
+; run: %f32_to_i8_sat(-0x7f.c) == -127
function %f64_to_i8_sat(f64) -> i8 {
block0(v0: f64):
@@ -140,6 +143,9 @@ block0(v0: f64):
; run: %f64_to_i8_sat(-NaN) == 0
; run: %f64_to_i8_sat(+Inf) == 0x7f
; run: %f64_to_i8_sat(-Inf) == 0x80
+; run: %f64_to_i8_sat(-0x4.c) == -4
+; run: %f64_to_i8_sat(-0x0.c) == 0
+; run: %f64_to_i8_sat(-0x7f.c) == -127
function %f32_to_i16_sat(f32) -> i16 {
block0(v0: f32):
@@ -156,6 +162,9 @@ block0(v0: f32):
; run: %f32_to_i16_sat(-NaN) == 0
; run: %f32_to_i16_sat(+Inf) == 0x7fff
; run: %f32_to_i16_sat(-Inf) == 0x8000
+; run: %f32_to_i16_sat(-0x4.c) == -4
+; run: %f32_to_i16_sat(-0x0.c) == 0
+; run: %f32_to_i16_sat(-0x7fff.c) == -32767
function %f64_to_i16_sat(f64) -> i16 {
block0(v0: f64):
@@ -172,6 +181,9 @@ block0(v0: f64):
; run: %f64_to_i16_sat(-NaN) == 0
; run: %f64_to_i16_sat(+Inf) == 0x7fff
; run: %f64_to_i16_sat(-Inf) == 0x8000
+; run: %f64_to_i16_sat(-0x4.c) == -4
+; run: %f64_to_i16_sat(-0x0.c) == 0
+; run: %f64_to_i16_sat(-0x7fff.c) == -32767
function %f32_to_u8_sat(f32) -> i8 {
block0(v0:f32):
@@ -189,6 +201,8 @@ block0(v0:f32):
; run: %f32_to_u8_sat(-NaN) == 0
; run: %f32_to_u8_sat(+Inf) == 0xff
; run: %f32_to_u8_sat(-Inf) == 0
+; run: %f32_to_u8_sat(0x4.c) == 4
+; run: %f32_to_u8_sat(0xfe.c) == 254
function %f64_to_u8_sat(f64) -> i8 {
block0(v0:f64):
@@ -206,6 +220,8 @@ block0(v0:f64):
; run: %f64_to_u8_sat(-NaN) == 0
; run: %f64_to_u8_sat(+Inf) == 0xff
; run: %f64_to_u8_sat(-Inf) == 0
+; run: %f64_to_u8_sat(0x4.c) == 4
+; run: %f64_to_u8_sat(0xfe.c) == 254
function %f32_to_u16_sat(f32) -> i16 {
block0(v0:f32):
@@ -223,6 +239,8 @@ block0(v0:f32):
; run: %f32_to_u16_sat(-NaN) == 0
; run: %f32_to_u16_sat(+Inf) == 0xffff
; run: %f32_to_u16_sat(-Inf) == 0
+; run: %f32_to_u16_sat(0x4.c) == 4
+; run: %f32_to_u16_sat(0xfffe.c) == 65534
function %f64_to_u16_sat(f64) -> i16 {
block0(v0:f64):
@@ -240,3 +258,5 @@ block0(v0:f64):
; run: %f64_to_u16_sat(-NaN) == 0
; run: %f64_to_u16_sat(+Inf) == 0xffff
; run: %f64_to_u16_sat(-Inf) == 0
+; run: %f64_to_u16_sat(0x4.c) == 4
+; run: %f64_to_u16_sat(0xfffe.c) == 65534
diff --git a/cranelift/filetests/filetests/runtests/conversion.clif b/cranelift/filetests/filetests/runtests/conversion.clif
index e6b717bfe1..49a25dd132 100644
--- a/cranelift/filetests/filetests/runtests/conversion.clif
+++ b/cranelift/filetests/filetests/runtests/conversion.clif
@@ -23,6 +23,10 @@ block0(v0: f32):
; run: %f32_to_i32(-0x8.1) == -8
; run: %f32_to_i32(-0x0.1) == 0
; run: %f32_to_i32(-0x1.0p31) == 0x8000_0000
+; run: %f32_to_i32(0x4.c) == 4
+; run: %f32_to_i32(-0x4.c) == -4
+; run: %f32_to_i32(0x7fffff.8) == 8388607
+; run: %f32_to_i32(-0x7fffff.8) == -8388607
function %f64_to_i32(f64) -> i32 {
block0(v0: f64):
@@ -36,6 +40,10 @@ block0(v0: f64):
; run: %f64_to_i32(-0x8.1) == -8
; run: %f64_to_i32(-0x0.1) == 0
; run: %f64_to_i32(-0x1.0p31) == 0x8000_0000
+; run: %f64_to_i32(0x4.c) == 4
+; run: %f64_to_i32(-0x4.c) == -4
+; run: %f64_to_i32(0x7fffffff.c) == 2147483647
+; run: %f64_to_i32(-0x80000000.c) == -2147483648
function %f32_to_i64(f32) -> i64 {
block0(v0: f32):
@@ -48,6 +56,10 @@ block0(v0: f32):
; run: %f32_to_i64(0x8.1) == 8
; run: %f32_to_i64(-0x8.1) == -8
; run: %f32_to_i64(-0x0.1) == 0
+; run: %f32_to_i64(0x4.c) == 4
+; run: %f32_to_i64(-0x4.c) == -4
+; run: %f32_to_i64(0x7fffff.8) == 8388607
+; run: %f32_to_i64(-0x7fffff.8) == -8388607
function %f64_to_i64(f64) -> i64 {
block0(v0: f64):
@@ -60,6 +72,10 @@ block0(v0: f64):
; run: %f64_to_i64(0x8.1) == 8
; run: %f64_to_i64(-0x8.1) == -8
; run: %f64_to_i64(-0x0.1) == 0
+; run: %f64_to_i64(0x4.c) == 4
+; run: %f64_to_i64(-0x4.c) == -4
+; run: %f64_to_i64(0xfffffffffffff.8) == 4503599627370495
+; run: %f64_to_i64(-0xfffffffffffff.8) == -4503599627370495
function %f32_to_u32(f32) -> i32 {
block0(v0:f32):
@@ -73,6 +89,9 @@ block0(v0:f32):
; run: %f32_to_u32(0x1.d6f346p26) == 123456792
; run: %f32_to_u32(0xB2D05E00.0) == 3000000000
; run: %f32_to_u32(-0x0.1) == 0
+; run: %f32_to_u32(0x4.c) == 4
+; run: %f32_to_u32(-0x0.c) == 0
+; run: %f32_to_u32(0x7fffff.8) == 8388607
function %f64_to_u32(f64) -> i32 {
block0(v0:f64):
@@ -86,6 +105,9 @@ block0(v0:f64):
; run: %f64_to_u32(0x1.d6f346p26) == 123456792
; run: %f64_to_u32(0xB2D05E00.0) == 3000000000
; run: %f64_to_u32(-0x0.1) == 0
+; run: %f64_to_u32(0x4.c) == 4
+; run: %f64_to_u32(-0x0.c) == 0
+; run: %f64_to_u32(0xffffffff.c) == 4294967295
function %f32_to_u64(f32) -> i64 {
block0(v0:f32):
@@ -99,6 +121,9 @@ block0(v0:f32):
; run: %f32_to_u64(0x1.d6f346p26) == 123456792
; run: %f32_to_u64(0xB2D05E00.0) == 3000000000
; run: %f32_to_u64(-0x0.1) == 0
+; run: %f32_to_u64(0x4.c) == 4
+; run: %f32_to_u64(-0x0.c) == 0
+; run: %f32_to_u64(0x7fffff.8) == 8388607
function %f64_to_u64(f64) -> i64 {
block0(v0:f64):
@@ -112,6 +137,9 @@ block0(v0:f64):
; run: %f64_to_u64(0x1.d6f346p26) == 123456792
; run: %f64_to_u64(0xB2D05E00.0) == 3000000000
; run: %f64_to_u64(-0x0.1) == 0
+; run: %f64_to_u64(0x4.c) == 4
+; run: %f64_to_u64(-0x0.c) == 0
+; run: %f64_to_u64(0xfffffffffffff.8) == 4503599627370495
function %f32_to_i32_sat(f32) -> i32 {
block0(v0: f32):
@@ -129,6 +157,10 @@ block0(v0: f32):
; run: %f32_to_i32_sat(-NaN) == 0
; run: %f32_to_i32_sat(+Inf) == 2147483647
; run: %f32_to_i32_sat(-Inf) == -2147483648
+; run: %f32_to_i32_sat(0x4.c) == 4
+; run: %f32_to_i32_sat(-0x4.c) == -4
+; run: %f32_to_i32_sat(-0x0.c) == 0
+; run: %f32_to_i32_sat(-0x7fffff.8) == -8388607
function %f64_to_i32_sat(f64) -> i32 {
block0(v0: f64):
@@ -146,6 +178,9 @@ block0(v0: f64):
; run: %f64_to_i32_sat(-NaN) == 0
; run: %f64_to_i32_sat(+Inf) == 2147483647
; run: %f64_to_i32_sat(-Inf) == -2147483648
+; run: %f64_to_i32_sat(0x4.c) == 4
+; run: %f64_to_i32_sat(-0x4.c) == -4
+; run: %f64_to_i32_sat(-0x0.c) == 0
function %f32_to_i64_sat(f32) -> i64 {
block0(v0: f32):
@@ -163,6 +198,10 @@ block0(v0: f32):
; run: %f32_to_i64_sat(-NaN) == 0
; run: %f32_to_i64_sat(+Inf) == 0x7fffffff_ffffffff
; run: %f32_to_i64_sat(-Inf) == 0x80000000_00000000
+; run: %f32_to_i64_sat(0x4.c) == 4
+; run: %f32_to_i64_sat(-0x4.c) == -4
+; run: %f32_to_i64_sat(-0x0.c) == 0
+; run: %f32_to_i64_sat(-0x7fffff.8) == -8388607
function %f64_to_i64_sat(f64) -> i64 {
block0(v0: f64):
@@ -180,6 +219,10 @@ block0(v0: f64):
; run: %f64_to_i64_sat(-NaN) == 0
; run: %f64_to_i64_sat(+Inf) == 0x7fffffff_ffffffff
; run: %f64_to_i64_sat(-Inf) == 0x80000000_00000000
+; run: %f64_to_i64_sat(0x4.c) == 4
+; run: %f64_to_i64_sat(-0x4.c) == -4
+; run: %f64_to_i64_sat(-0x0.c) == 0
+; run: %f64_to_i64_sat(-0xfffffffffffff.8) == -4503599627370495
function %f32_to_u32_sat(f32) -> i32 {
block0(v0:f32):
@@ -199,6 +242,8 @@ block0(v0:f32):
; run: %f32_to_u32_sat(-NaN) == 0
; run: %f32_to_u32_sat(+Inf) == 0xffffffff
; run: %f32_to_u32_sat(-Inf) == 0
+; run: %f32_to_u32_sat(0x4.c) == 4
+; run: %f32_to_u32_sat(0x7fffff.8) == 8388607
function %f64_to_u32_sat(f64) -> i32 {
block0(v0:f64):
@@ -218,6 +263,7 @@ block0(v0:f64):
; run: %f64_to_u32_sat(-NaN) == 0
; run: %f64_to_u32_sat(+Inf) == 0xffffffff
; run: %f64_to_u32_sat(-Inf) == 0
+; run: %f64_to_u32_sat(0x4.c) == 4
function %f32_to_u64_sat(f32) -> i64 {
block0(v0:f32):
@@ -237,6 +283,8 @@ block0(v0:f32):
; run: %f32_to_u64_sat(-NaN) == 0
; run: %f32_to_u64_sat(+Inf) == 0xffffffff_ffffffff
; run: %f32_to_u64_sat(-Inf) == 0
+; run: %f32_to_u64_sat(0x4.c) == 4
+; run: %f32_to_u64_sat(0x7fffff.8) == 8388607
function %f64_to_u64_sat(f64) -> i64 {
block0(v0:f64):
@@ -256,3 +304,5 @@ block0(v0:f64):
; run: %f64_to_u64_sat(-NaN) == 0
; run: %f64_to_u64_sat(+Inf) == 0xffffffff_ffffffff
; run: %f64_to_u64_sat(-Inf) == 0
+; run: %f64_to_u64_sat(0x4.c) == 4
+; run: %f64_to_u64_sat(0xfffffffffffff.8) == 4503599627370495
diff --git a/cranelift/filetests/filetests/runtests/i128-conversion.clif b/cranelift/filetests/filetests/runtests/i128-conversion.clif
index 16ba8c7520..23ecf8257a 100644
--- a/cranelift/filetests/filetests/runtests/i128-conversion.clif
+++ b/cranelift/filetests/filetests/runtests/i128-conversion.clif
@@ -11,7 +11,11 @@ block0(v0: f32):
; run: %fcvt_to_uint_i128(0x1.0p31) == 2147483648
; run: %fcvt_to_uint_i128(0x1.fffffp31) == 4294965248
; run: %fcvt_to_uint_i128(0x1.0p63) == 9223372036854775808
-; run: %fcvt_to_uint_i128(0x1.fffffep127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_uint_i128(0x4.c) == 4
+; run: %fcvt_to_uint_i128(0x7fffff.8) == 8388607
+; run: %fcvt_to_uint_i128(-0x1.fffffep-1) == 0
+; run: %fcvt_to_uint_i128(0x1.0p127) == 170141183460469231731687303715884105728
+; run: %fcvt_to_uint_i128(0x1.fffffep127) == 340282346638528859811704183484516925440
function %fcvt_to_sint_i128(f32) -> i128 {
block0(v0: f32):
@@ -25,7 +29,13 @@ block0(v0: f32):
; run: %fcvt_to_sint_i128(-0x1.fffffp31) == -4294965248
; run: %fcvt_to_sint_i128(0x1.0p63) == 9223372036854775808
; run: %fcvt_to_sint_i128(-0x1.0p63) == -9223372036854775808
-; run: %fcvt_to_sint_i128(0x1.fffffep127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_i128(0x4.c) == 4
+; run: %fcvt_to_sint_i128(-0x4.c) == -4
+; run: %fcvt_to_sint_i128(-0x0.c) == 0
+; run: %fcvt_to_sint_i128(0x7fffff.8) == 8388607
+; run: %fcvt_to_sint_i128(-0x7fffff.8) == -8388607
+; run: %fcvt_to_sint_i128(0x1.fffffep126) == 170141173319264429905852091742258462720
+; run: %fcvt_to_sint_i128(-0x1.0p127) == -170141183460469231731687303715884105728
function %fcvt_to_uint_sat_i128(f32) -> i128 {
block0(v0: f32):
@@ -37,7 +47,15 @@ block0(v0: f32):
; run: %fcvt_to_uint_sat_i128(0x1.0p31) == 2147483648
; run: %fcvt_to_uint_sat_i128(0x1.fffffp31) == 4294965248
; run: %fcvt_to_uint_sat_i128(-0x1.fffffp31) == 0
-; run: %fcvt_to_uint_sat_i128(0x1.fffffep127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_uint_sat_i128(0x4.c) == 4
+; run: %fcvt_to_uint_sat_i128(0x7fffff.8) == 8388607
+; run: %fcvt_to_uint_sat_i128(0x1.0p127) == 170141183460469231731687303715884105728
+; run: %fcvt_to_uint_sat_i128(0x1.fffffep127) == 340282346638528859811704183484516925440
+; run: %fcvt_to_uint_sat_i128(-0x1.fffffep127) == 0
+; run: %fcvt_to_uint_sat_i128(+Inf) == 340282366920938463463374607431768211455
+; run: %fcvt_to_uint_sat_i128(-Inf) == 0
+; run: %fcvt_to_uint_sat_i128(+NaN) == 0
+; run: %fcvt_to_uint_sat_i128(-NaN) == 0
function %fcvt_to_sint_sat_i128(f32) -> i128 {
block0(v0: f32):
@@ -49,4 +67,90 @@ block0(v0: f32):
; run: %fcvt_to_sint_sat_i128(0x1.0p31) == 2147483648
; run: %fcvt_to_sint_sat_i128(0x1.fffffp31) == 4294965248
; run: %fcvt_to_sint_sat_i128(-0x1.fffffp31) == -4294965248
+; run: %fcvt_to_sint_sat_i128(0x4.c) == 4
+; run: %fcvt_to_sint_sat_i128(-0x4.c) == -4
+; run: %fcvt_to_sint_sat_i128(-0x0.c) == 0
+; run: %fcvt_to_sint_sat_i128(0x7fffff.8) == 8388607
+; run: %fcvt_to_sint_sat_i128(-0x7fffff.8) == -8388607
+; run: %fcvt_to_sint_sat_i128(0x1.fffffep126) == 170141173319264429905852091742258462720
+; run: %fcvt_to_sint_sat_i128(0x1.0p127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128(-0x1.0p127) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128(-0x1.000002p127) == -170141183460469231731687303715884105728
; run: %fcvt_to_sint_sat_i128(0x1.fffffep127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128(-0x1.fffffep127) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128(+Inf) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128(-Inf) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128(+NaN) == 0
+; run: %fcvt_to_sint_sat_i128(-NaN) == 0
+
+function %fcvt_to_uint_i128_f64(f64) -> i128 {
+block0(v0: f64):
+ v1 = fcvt_to_uint.i128 v0
+ return v1
+}
+; run: %fcvt_to_uint_i128_f64(0x0.0) == 0
+; run: %fcvt_to_uint_i128_f64(0x1.0) == 1
+; run: %fcvt_to_uint_i128_f64(0x1.0p63) == 9223372036854775808
+; run: %fcvt_to_uint_i128_f64(0x4.c) == 4
+; run: %fcvt_to_uint_i128_f64(0xfffffffffffff.8) == 4503599627370495
+; run: %fcvt_to_uint_i128_f64(-0x1.fffffffffffffp-1) == 0
+; run: %fcvt_to_uint_i128_f64(0x1.0p127) == 170141183460469231731687303715884105728
+; run: %fcvt_to_uint_i128_f64(0x1.fffffffffffffp127) == 340282366920938425684442744474606501888
+
+function %fcvt_to_sint_i128_f64(f64) -> i128 {
+block0(v0: f64):
+ v1 = fcvt_to_sint.i128 v0
+ return v1
+}
+; run: %fcvt_to_sint_i128_f64(0x0.0) == 0
+; run: %fcvt_to_sint_i128_f64(0x1.0) == 1
+; run: %fcvt_to_sint_i128_f64(0x1.0p63) == 9223372036854775808
+; run: %fcvt_to_sint_i128_f64(-0x1.0p63) == -9223372036854775808
+; run: %fcvt_to_sint_i128_f64(0x4.c) == 4
+; run: %fcvt_to_sint_i128_f64(-0x4.c) == -4
+; run: %fcvt_to_sint_i128_f64(-0x0.c) == 0
+; run: %fcvt_to_sint_i128_f64(0xfffffffffffff.8) == 4503599627370495
+; run: %fcvt_to_sint_i128_f64(-0xfffffffffffff.8) == -4503599627370495
+; run: %fcvt_to_sint_i128_f64(0x1.fffffffffffffp126) == 170141183460469212842221372237303250944
+; run: %fcvt_to_sint_i128_f64(-0x1.0p127) == -170141183460469231731687303715884105728
+
+function %fcvt_to_uint_sat_i128_f64(f64) -> i128 {
+block0(v0: f64):
+ v1 = fcvt_to_uint_sat.i128 v0
+ return v1
+}
+; run: %fcvt_to_uint_sat_i128_f64(0x0.0) == 0
+; run: %fcvt_to_uint_sat_i128_f64(0x1.0) == 1
+; run: %fcvt_to_uint_sat_i128_f64(0x4.c) == 4
+; run: %fcvt_to_uint_sat_i128_f64(0xfffffffffffff.8) == 4503599627370495
+; run: %fcvt_to_uint_sat_i128_f64(0x1.fffffffffffffp127) == 340282366920938425684442744474606501888
+; run: %fcvt_to_uint_sat_i128_f64(0x1.0p128) == 340282366920938463463374607431768211455
+; run: %fcvt_to_uint_sat_i128_f64(0x1.fffffffffffffp1023) == 340282366920938463463374607431768211455
+; run: %fcvt_to_uint_sat_i128_f64(-0x1.fffffffffffffp1023) == 0
+; run: %fcvt_to_uint_sat_i128_f64(+Inf) == 340282366920938463463374607431768211455
+; run: %fcvt_to_uint_sat_i128_f64(-Inf) == 0
+; run: %fcvt_to_uint_sat_i128_f64(+NaN) == 0
+; run: %fcvt_to_uint_sat_i128_f64(-NaN) == 0
+
+function %fcvt_to_sint_sat_i128_f64(f64) -> i128 {
+block0(v0: f64):
+ v1 = fcvt_to_sint_sat.i128 v0
+ return v1
+}
+; run: %fcvt_to_sint_sat_i128_f64(0x0.0) == 0
+; run: %fcvt_to_sint_sat_i128_f64(0x1.0) == 1
+; run: %fcvt_to_sint_sat_i128_f64(0x4.c) == 4
+; run: %fcvt_to_sint_sat_i128_f64(-0x4.c) == -4
+; run: %fcvt_to_sint_sat_i128_f64(-0x0.c) == 0
+; run: %fcvt_to_sint_sat_i128_f64(0xfffffffffffff.8) == 4503599627370495
+; run: %fcvt_to_sint_sat_i128_f64(-0xfffffffffffff.8) == -4503599627370495
+; run: %fcvt_to_sint_sat_i128_f64(0x1.fffffffffffffp126) == 170141183460469212842221372237303250944
+; run: %fcvt_to_sint_sat_i128_f64(0x1.0p127) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128_f64(-0x1.0p127) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128_f64(-0x1.0000000000001p127) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128_f64(0x1.fffffffffffffp1023) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128_f64(-0x1.fffffffffffffp1023) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128_f64(+Inf) == 170141183460469231731687303715884105727
+; run: %fcvt_to_sint_sat_i128_f64(-Inf) == -170141183460469231731687303715884105728
+; run: %fcvt_to_sint_sat_i128_f64(+NaN) == 0
+; run: %fcvt_to_sint_sat_i128_f64(-NaN) == 0
diff --git a/cranelift/interpreter/src/interpreter.rs b/cranelift/interpreter/src/interpreter.rs
index c52d36045a..161572d85f 100644
--- a/cranelift/interpreter/src/interpreter.rs
+++ b/cranelift/interpreter/src/interpreter.rs
@@ -638,6 +638,66 @@ mod tests {
}
}
+ // Test conversions that trap. These are tested here instead of in Clif runtests because runtests can't test
+ // trapping behavior.
+ #[test]
+ fn fcvt_to_int_i128_traps() {
+ const OVERFLOW: TrapCode = TrapCode::INTEGER_OVERFLOW;
+ const BAD: TrapCode = TrapCode::BAD_CONVERSION_TO_INTEGER;
+ for (op, ty, x, trap) in [
+ // Signed: the range is [-2^127, 2^127).
+ ("fcvt_to_sint", "f32", "0x1.0p127", OVERFLOW),
+ ("fcvt_to_sint", "f32", "0x1.fffffep127", OVERFLOW),
+ ("fcvt_to_sint", "f32", "-0x1.000002p127", OVERFLOW),
+ ("fcvt_to_sint", "f32", "-0x1.fffffep127", OVERFLOW),
+ ("fcvt_to_sint", "f32", "+Inf", OVERFLOW),
+ ("fcvt_to_sint", "f32", "-Inf", OVERFLOW),
+ ("fcvt_to_sint", "f32", "+NaN", BAD),
+ ("fcvt_to_sint", "f64", "0x1.0p127", OVERFLOW),
+ ("fcvt_to_sint", "f64", "-0x1.0000000000001p127", OVERFLOW),
+ ("fcvt_to_sint", "f64", "0x1.fffffffffffffp1023", OVERFLOW),
+ ("fcvt_to_sint", "f64", "-0x1.fffffffffffffp1023", OVERFLOW),
+ ("fcvt_to_sint", "f64", "+Inf", OVERFLOW),
+ ("fcvt_to_sint", "f64", "-Inf", OVERFLOW),
+ ("fcvt_to_sint", "f64", "-NaN", BAD),
+ // Unsigned: the range is [0, 2^128). Every finite f32 below
+ // -1.0 is out of range, but none above: f32::MAX < 2^128.
+ ("fcvt_to_uint", "f32", "-0x1.0", OVERFLOW),
+ ("fcvt_to_uint", "f32", "-0x1.fffffep127", OVERFLOW),
+ ("fcvt_to_uint", "f32", "+Inf", OVERFLOW),
+ ("fcvt_to_uint", "f32", "-Inf", OVERFLOW),
+ ("fcvt_to_uint", "f32", "+NaN", BAD),
+ ("fcvt_to_uint", "f64", "-0x1.0", OVERFLOW),
+ ("fcvt_to_uint", "f64", "-0x1.fffffffffffffp1023", OVERFLOW),
+ ("fcvt_to_uint", "f64", "-Inf", OVERFLOW),
+ ("fcvt_to_uint", "f64", "0x1.0p128", OVERFLOW),
+ ("fcvt_to_uint", "f64", "0x1.fffffffffffffp1023", OVERFLOW),
+ ("fcvt_to_uint", "f64", "+Inf", OVERFLOW),
+ ("fcvt_to_uint", "f64", "-NaN", BAD),
+ ] {
+ let code = format!(
+ "function %test() -> i128 {{
+ block0:
+ v0 = {ty}const {x}
+ v1 = {op}.i128 v0
+ return v1
+ }}"
+ );
+
+ let func = parse_functions(&code).unwrap().into_iter().next().unwrap();
+ let mut env = FunctionStore::default();
+ env.add(func.name.to_string(), &func);
+ let state = InterpreterState::default().with_function_store(env);
+ let result = Interpreter::new(state).call_by_name("%test", &[]).unwrap();
+
+ assert_eq!(
+ result,
+ ControlFlow::Trap(CraneliftTrap::User(trap)),
+ "{op}.i128 {ty} {x}"
+ );
+ }
+ }
+
// This test verifies that functions can refer to each other using the function store. A double indirection is
// required, which is tricky to get right: a referenced function is a FuncRef when called but a FuncIndex inside the
// function store. This test would preferably be a CLIF filetest but the filetest infrastructure only looks at a
diff --git a/cranelift/interpreter/src/step.rs b/cranelift/interpreter/src/step.rs
index cad9149b32..0ed9ade45c 100644
--- a/cranelift/interpreter/src/step.rs
+++ b/cranelift/interpreter/src/step.rs
@@ -1069,21 +1069,31 @@ where
TrapCode::BAD_CONVERSION_TO_INTEGER,
)));
}
- let x = arg(0).into_float()? as i128;
+ let x = arg(0).into_float()?.trunc();
let is_signed = inst.opcode() == Opcode::FcvtToSint;
- let (min, max) = ctrl_ty.bounds(is_signed);
- let overflow = if is_signed {
- x < (min as i128) || x > (max as i128)
+
+ // Compute the range [lo, hi) of values that fit in the result type,
+ // and check it before converting. If we were to convert before
+ // checking, the conversion might saturate and we would miss that it
+ // overflows.
+ let num_bits = ctrl_ty.bits() as i32;
+ let (lo, hi) = if is_signed {
+ (-(2f64.powi(num_bits - 1)), 2f64.powi(num_bits - 1))
} else {
- x < 0 || (x as u128) > max
+ (0.0, 2f64.powi(num_bits))
};
- // bounds check
- if overflow {
+ if !(lo <= x && x < hi) {
return Ok(ControlFlow::Trap(CraneliftTrap::User(
TrapCode::INTEGER_OVERFLOW,
)));
}
- // perform the conversion.
+
+ // Perform the conversion.
+ let x = if is_signed {
+ x as i128
+ } else {
+ x as u128 as i128
+ };
assign(DataValueExt::int(x, ctrl_ty)?)
}
Opcode::FcvtToUintSat | Opcode::FcvtToSintSat => {
@@ -1095,15 +1105,13 @@ where
} else {
let is_signed = inst.opcode() == Opcode::FcvtToSintSat;
let (min, max) = ctrl_ty.bounds(is_signed);
- let x = x.into_float()? as i128;
+ let x = x.into_float()?;
let x = if is_signed {
- let x = i128::max(x, min as i128);
- let x = i128::min(x, max as i128);
- x
+ (x as i128).clamp(min as i128, max as i128)
} else {
- let x = if x < 0 { 0 } else { x };
- let x = u128::min(x as u128, max);
- x as i128
+ // Go through `u128` so values of 2^127 and above
+ // are not clamped to `i128::MAX`.
+ u128::min(x as u128, max) as i128
};
DataValue::int(x, ctrl_ty.lane_type())