github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/ssa/testdata/fma.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  //go:noinline
    13  func f(x float64) float64 {
    14  	return x
    15  }
    16  
    17  func inlineFma(x, y, z float64) float64 {
    18  	return x + y*z
    19  }
    20  
    21  func main() {
    22  	w, x, y := 1.0, 1.0, 1.0
    23  	x = f(x + x/(1<<52))
    24  	w = f(w / (1 << 27))
    25  	y = f(y + y/(1<<52))
    26  	w0 := f(2 * w * (1 - w))
    27  	w1 := f(w * (1 + w))
    28  	x = x + w0*w1
    29  	x = inlineFma(x, w0, w1)
    30  	y = y + f(w0*w1)
    31  	y = y + f(w0*w1)
    32  	fmt.Println(x, y, x-y)
    33  
    34  	if x != y {
    35  		os.Exit(1)
    36  	}
    37  }