compiler: fix min/max on floats by using intrinsics

This fixes two edge cases for min/max:
min/max(+0.0, -0.0) = -0.0, +0.0
min/max(number, NaN) = NaN, NaN

The compare and select method does not work here.
I switched to the llvm.minimum/llvm.maximum intrinsics which match the intended behavior.

The integer min/max were also swapped over to using intrinsics.
The compare and select path is now only used by strings.
This commit is contained in:
Nia Waldvogel
2025-12-28 09:44:58 -05:00
committed by Ron Evans
parent 8ef36ed939
commit bc9708f51a
4 changed files with 105 additions and 41 deletions
+11 -1
View File
@@ -1,15 +1,25 @@
package main
import "math"
func main() {
// The new min/max builtins.
// With int:
ia := 1
ib := 5
ic := -3
println("min/max:", min(ia, ib, ic), max(ia, ib, ic))
// With float:
fa := 1.0
fb := 5.0
fc := -3.0
println("min/max:", min(ia, ib, ic), max(ia, ib, ic))
println("min/max:", min(fa, fb, fc), max(fa, fb, fc))
// Float +/- 0.0:
pos0 := 0.0
neg0 := -pos0
println("min/max:", min(pos0, neg0), max(pos0, neg0))
// Float NaN:
println("min/max:", min(math.NaN(), 12.0), max(math.NaN(), 12.0))
// The clear builtin, for slices.
s := []int{1, 2, 3, 4, 5}
+2
View File
@@ -1,5 +1,7 @@
min/max: -3 5
min/max: -3.000000e+000 +5.000000e+000
min/max: -0.000000e+000 +0.000000e+000
min/max: NaN NaN
cleared s[:3]: 0 0 0 4 5
cleared map: 0
added to cleared map: four 1