compiler: saturate float-to-int conversions

This works around some UB in LLVM, where an out-of-bounds conversion would produce a poison value.
The selected behavior is saturating, except that NaN is mapped to the minimum value.
This commit is contained in:
Nia Weiss
2021-01-15 10:48:07 -05:00
committed by Ron Evans
parent f159429152
commit a867b56e5f
3 changed files with 70 additions and 4 deletions
+6 -1
View File
@@ -29,7 +29,12 @@ func main() {
var f2 float32 = 5.7
var f3 float32 = -2.3
var f4 float32 = -11.8
println(int32(f1), int32(f2), int32(f3), int32(f4))
var f5 float32 = -1
var f6 float32 = 256
var f7 float32 = -129
var f8 float32 = 0
f8 /= 0
println(int32(f1), int32(f2), int32(f3), int32(f4), uint8(f5), uint8(f6), int8(f7), int8(f6), uint8(f8), int8(f8))
// int -> float
var i1 int32 = 53