Running the Ziran engine and the C engine it replaced on random formulas found NT_RADICAL of NaN returning 1 where the C engine returned 2: the 64-bit unsigned conversion sent NaN down the wrong branch. The same test showed that a value between -2^31-1 and -2^31 aborted the program in the 32-bit conversion, because Ziran checks the float against the type's range before truncating it. Both conversions now match gcc on x86-64. conversion_test checks a table of gcc's results, including NaN, the infinities, and values on either side of each limit, and runs as part of test-ci.
65 lines
3.1 KiB
Ziran
65 lines
3.1 KiB
Ziran
// The engine converts numbers to integers the way the C engine it replaced
|
|
// did on x86-64: truncation toward zero, the minimum signed value for NaN or
|
|
// a value that does not fit, and for the unsigned types the compiler's own
|
|
// instruction sequence. Each row is what gcc produces for the same input at
|
|
// run time. A conversion that differs here changes formula results such as
|
|
// NT_RADICAL of NaN.
|
|
#load "test_support.zi";
|
|
Engine :: #import "engine";
|
|
|
|
Mismatch :: (label: string, target: string) {
|
|
WriteText(2, "converting ")
|
|
WriteText(2, label)
|
|
WriteText(2, " to ")
|
|
WriteText(2, target)
|
|
WriteText(2, " differs from the C compiler\n")
|
|
Fail("float to integer conversion")
|
|
}
|
|
|
|
Expect :: (label: string, x: float64, as_s32: s32, as_s64: s64, as_u32: u32, as_u64: u64) {
|
|
if Engine.TruncateToS32(x) != as_s32 { Mismatch(label, "s32") }
|
|
if Engine.TruncateToS64(x) != as_s64 { Mismatch(label, "s64") }
|
|
if Engine.TruncateToU32(x) != as_u32 { Mismatch(label, "u32") }
|
|
if Engine.TruncateToU64(x) != as_u64 { Mismatch(label, "u64") }
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
int_min: s32 = -2147483647 - 1
|
|
long_min: s64 = -9223372036854775807 - 1
|
|
infinity: float64 = Engine.Infinity()
|
|
|
|
Expect("0", 0.0, 0, 0, 0, 0)
|
|
Expect("1.9", 1.9, 1, 1, 1, 1)
|
|
Expect("-1.9", -1.9, -1, -1, 4294967295, 18446744073709551615)
|
|
Expect("2.5", 2.5, 2, 2, 2, 2)
|
|
Expect("-2.5", -2.5, -2, -2, 4294967294, 18446744073709551614)
|
|
Expect("2147483647.5", 2147483647.5, 2147483647, 2147483647, 2147483647, 2147483647)
|
|
Expect("2147483648", 2147483648.0, int_min, 2147483648, 2147483648, 2147483648)
|
|
Expect("-2147483648.9", -2147483648.9, int_min, -2147483648, 2147483648, 18446744071562067968)
|
|
Expect("-2147483649", -2147483649.0, int_min, -2147483649, 2147483647, 18446744071562067967)
|
|
Expect("4294967295", 4294967295.0, int_min, 4294967295, 4294967295, 4294967295)
|
|
Expect("4294967296", 4294967296.0, int_min, 4294967296, 0, 4294967296)
|
|
Expect("9.2e18", 9.2e18, int_min, 9200000000000000000, 2643984384, 9200000000000000000)
|
|
|
|
// At and beyond 2^63 the signed types saturate to their minimum, and the
|
|
// unsigned 64-bit type shifts the value down by 2^63 before converting.
|
|
Expect("2^63", 9223372036854775808.0, int_min, long_min, 0, 9223372036854775808)
|
|
Expect("-2^63", -9223372036854775808.0, int_min, long_min, 0, 9223372036854775808)
|
|
Expect("-9.3e18", -9.3e18, int_min, long_min, 0, 9223372036854775808)
|
|
Expect("1e19", 1.0e19, int_min, long_min, 0, 10000000000000000000)
|
|
Expect("1.8e19", 1.8e19, int_min, long_min, 0, 18000000000000000000)
|
|
Expect("2^64", 18446744073709551616.0, int_min, long_min, 0, 0)
|
|
Expect("1e20", 1.0e20, int_min, long_min, 0, 0)
|
|
Expect("-1e19", -1.0e19, int_min, long_min, 0, 9223372036854775808)
|
|
|
|
// NaN is not below 2^63, so the unsigned 64-bit conversion takes the
|
|
// signed path and yields 2^63.
|
|
Expect("infinity", infinity, int_min, long_min, 0, 0)
|
|
Expect("-infinity", -infinity, int_min, long_min, 0, 9223372036854775808)
|
|
Expect("NaN", Engine.NotANumber(), int_min, long_min, 0, 9223372036854775808)
|
|
|
|
Say("{\"number_conversion\":\"ok\"}")
|
|
return 0
|
|
}
|