Source file test/strength.go

     1  // runoutput
     2  
     3  // Copyright 2016 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Generate test of strength reduction for multiplications
     8  // with constants. Especially useful for amd64/386.
     9  
    10  package main
    11  
    12  import "fmt"
    13  
    14  func testMul(fact, bits int) string {
    15  	n := fmt.Sprintf("testMul_%d_%d", fact, bits)
    16  	fmt.Printf("func %s(s int%d) {\n", n, bits)
    17  
    18  	want := 0
    19  	for i := 0; i < 200; i++ {
    20  		fmt.Printf(`	if want, got := int%d(%d), s*%d; want != got {
    21  		failed = true
    22  		fmt.Printf("got %d * %%d == %%d, wanted %d\n",  s, got)
    23  	}
    24  `, bits, want, i, i, want)
    25  		want += fact
    26  	}
    27  
    28  	fmt.Printf("}\n")
    29  	return fmt.Sprintf("%s(%d)", n, fact)
    30  }
    31  
    32  func main() {
    33  	fmt.Printf("package main\n")
    34  	fmt.Printf("import \"fmt\"\n")
    35  	fmt.Printf("var failed = false\n")
    36  
    37  	f1 := testMul(17, 32)
    38  	f2 := testMul(131, 64)
    39  
    40  	fmt.Printf("func main() {\n")
    41  	fmt.Println(f1)
    42  	fmt.Println(f2)
    43  	fmt.Printf("if failed {\n	panic(\"multiplication failed\")\n}\n")
    44  	fmt.Printf("}\n")
    45  }
    46  

View as plain text