...

Source file src/cmd/compile/internal/test/testdata/pgo/devirtualize/mult.pkg/mult.go

Documentation: cmd/compile/internal/test/testdata/pgo/devirtualize/mult.pkg

     1  // Copyright 2023 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  // WARNING: Please avoid updating this file.
     6  // See the warning in ../devirt.go for more details.
     7  
     8  package mult
     9  
    10  var sink int
    11  
    12  type Multiplier interface {
    13  	Multiply(a, b int) int
    14  }
    15  
    16  type Mult struct{}
    17  
    18  func (Mult) Multiply(a, b int) int {
    19  	for i := 0; i < 1000; i++ {
    20  		sink++
    21  	}
    22  	return a * b
    23  }
    24  
    25  type NegMult struct{}
    26  
    27  func (NegMult) Multiply(a, b int) int {
    28  	for i := 0; i < 1000; i++ {
    29  		sink++
    30  	}
    31  	return -1 * a * b
    32  }
    33  
    34  // N.B. Different types than AddFunc to test intra-line disambiguation.
    35  type MultFunc func(int64, int64) int64
    36  
    37  func MultFn(a, b int64) int64 {
    38  	for i := 0; i < 1000; i++ {
    39  		sink++
    40  	}
    41  	return a * b
    42  }
    43  
    44  func NegMultFn(a, b int64) int64 {
    45  	for i := 0; i < 1000; i++ {
    46  		sink++
    47  	}
    48  	return -1 * a * b
    49  }
    50  
    51  //go:noinline
    52  func MultClosure() MultFunc {
    53  	// Explicit closure to differentiate from AddClosure.
    54  	c := 1
    55  	return func(a, b int64) int64 {
    56  		for i := 0; i < 1000; i++ {
    57  			sink++
    58  		}
    59  		return a * b * int64(c)
    60  	}
    61  }
    62  
    63  //go:noinline
    64  func NegMultClosure() MultFunc {
    65  	c := 1
    66  	return func(a, b int64) int64 {
    67  		for i := 0; i < 1000; i++ {
    68  			sink++
    69  		}
    70  		return -1 * a * b * int64(c)
    71  	}
    72  }
    73  

View as plain text