...

Source file src/cmd/compile/internal/test/mulconst_test.go

Documentation: cmd/compile/internal/test

     1  // Copyright 2020 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 test
     6  
     7  import "testing"
     8  
     9  // Benchmark multiplication of an integer by various constants.
    10  //
    11  // The comment above each sub-benchmark provides an example of how the
    12  // target multiplication operation might be implemented using shift
    13  // (multiplication by a power of 2), addition and subtraction
    14  // operations. It is platform-dependent whether these transformations
    15  // are actually applied.
    16  
    17  var (
    18  	mulSinkI32 int32
    19  	mulSinkI64 int64
    20  	mulSinkU32 uint32
    21  	mulSinkU64 uint64
    22  )
    23  
    24  func BenchmarkMulconstI32(b *testing.B) {
    25  	// 3x = 2x + x
    26  	b.Run("3", func(b *testing.B) {
    27  		x := int32(1)
    28  		for i := 0; i < b.N; i++ {
    29  			x *= 3
    30  		}
    31  		mulSinkI32 = x
    32  	})
    33  	// 5x = 4x + x
    34  	b.Run("5", func(b *testing.B) {
    35  		x := int32(1)
    36  		for i := 0; i < b.N; i++ {
    37  			x *= 5
    38  		}
    39  		mulSinkI32 = x
    40  	})
    41  	// 12x = 8x + 4x
    42  	b.Run("12", func(b *testing.B) {
    43  		x := int32(1)
    44  		for i := 0; i < b.N; i++ {
    45  			x *= 12
    46  		}
    47  		mulSinkI32 = x
    48  	})
    49  	// 120x = 128x - 8x
    50  	b.Run("120", func(b *testing.B) {
    51  		x := int32(1)
    52  		for i := 0; i < b.N; i++ {
    53  			x *= 120
    54  		}
    55  		mulSinkI32 = x
    56  	})
    57  	// -120x = 8x - 120x
    58  	b.Run("-120", func(b *testing.B) {
    59  		x := int32(1)
    60  		for i := 0; i < b.N; i++ {
    61  			x *= -120
    62  		}
    63  		mulSinkI32 = x
    64  	})
    65  	// 65537x = 65536x + x
    66  	b.Run("65537", func(b *testing.B) {
    67  		x := int32(1)
    68  		for i := 0; i < b.N; i++ {
    69  			x *= 65537
    70  		}
    71  		mulSinkI32 = x
    72  	})
    73  	// 65538x = 65536x + 2x
    74  	b.Run("65538", func(b *testing.B) {
    75  		x := int32(1)
    76  		for i := 0; i < b.N; i++ {
    77  			x *= 65538
    78  		}
    79  		mulSinkI32 = x
    80  	})
    81  }
    82  
    83  func BenchmarkMulconstI64(b *testing.B) {
    84  	// 3x = 2x + x
    85  	b.Run("3", func(b *testing.B) {
    86  		x := int64(1)
    87  		for i := 0; i < b.N; i++ {
    88  			x *= 3
    89  		}
    90  		mulSinkI64 = x
    91  	})
    92  	// 5x = 4x + x
    93  	b.Run("5", func(b *testing.B) {
    94  		x := int64(1)
    95  		for i := 0; i < b.N; i++ {
    96  			x *= 5
    97  		}
    98  		mulSinkI64 = x
    99  	})
   100  	// 12x = 8x + 4x
   101  	b.Run("12", func(b *testing.B) {
   102  		x := int64(1)
   103  		for i := 0; i < b.N; i++ {
   104  			x *= 12
   105  		}
   106  		mulSinkI64 = x
   107  	})
   108  	// 120x = 128x - 8x
   109  	b.Run("120", func(b *testing.B) {
   110  		x := int64(1)
   111  		for i := 0; i < b.N; i++ {
   112  			x *= 120
   113  		}
   114  		mulSinkI64 = x
   115  	})
   116  	// -120x = 8x - 120x
   117  	b.Run("-120", func(b *testing.B) {
   118  		x := int64(1)
   119  		for i := 0; i < b.N; i++ {
   120  			x *= -120
   121  		}
   122  		mulSinkI64 = x
   123  	})
   124  	// 65537x = 65536x + x
   125  	b.Run("65537", func(b *testing.B) {
   126  		x := int64(1)
   127  		for i := 0; i < b.N; i++ {
   128  			x *= 65537
   129  		}
   130  		mulSinkI64 = x
   131  	})
   132  	// 65538x = 65536x + 2x
   133  	b.Run("65538", func(b *testing.B) {
   134  		x := int64(1)
   135  		for i := 0; i < b.N; i++ {
   136  			x *= 65538
   137  		}
   138  		mulSinkI64 = x
   139  	})
   140  }
   141  
   142  func BenchmarkMulconstU32(b *testing.B) {
   143  	// 3x = 2x + x
   144  	b.Run("3", func(b *testing.B) {
   145  		x := uint32(1)
   146  		for i := 0; i < b.N; i++ {
   147  			x *= 3
   148  		}
   149  		mulSinkU32 = x
   150  	})
   151  	// 5x = 4x + x
   152  	b.Run("5", func(b *testing.B) {
   153  		x := uint32(1)
   154  		for i := 0; i < b.N; i++ {
   155  			x *= 5
   156  		}
   157  		mulSinkU32 = x
   158  	})
   159  	// 12x = 8x + 4x
   160  	b.Run("12", func(b *testing.B) {
   161  		x := uint32(1)
   162  		for i := 0; i < b.N; i++ {
   163  			x *= 12
   164  		}
   165  		mulSinkU32 = x
   166  	})
   167  	// 120x = 128x - 8x
   168  	b.Run("120", func(b *testing.B) {
   169  		x := uint32(1)
   170  		for i := 0; i < b.N; i++ {
   171  			x *= 120
   172  		}
   173  		mulSinkU32 = x
   174  	})
   175  	// 65537x = 65536x + x
   176  	b.Run("65537", func(b *testing.B) {
   177  		x := uint32(1)
   178  		for i := 0; i < b.N; i++ {
   179  			x *= 65537
   180  		}
   181  		mulSinkU32 = x
   182  	})
   183  	// 65538x = 65536x + 2x
   184  	b.Run("65538", func(b *testing.B) {
   185  		x := uint32(1)
   186  		for i := 0; i < b.N; i++ {
   187  			x *= 65538
   188  		}
   189  		mulSinkU32 = x
   190  	})
   191  }
   192  
   193  func BenchmarkMulconstU64(b *testing.B) {
   194  	// 3x = 2x + x
   195  	b.Run("3", func(b *testing.B) {
   196  		x := uint64(1)
   197  		for i := 0; i < b.N; i++ {
   198  			x *= 3
   199  		}
   200  		mulSinkU64 = x
   201  	})
   202  	// 5x = 4x + x
   203  	b.Run("5", func(b *testing.B) {
   204  		x := uint64(1)
   205  		for i := 0; i < b.N; i++ {
   206  			x *= 5
   207  		}
   208  		mulSinkU64 = x
   209  	})
   210  	// 12x = 8x + 4x
   211  	b.Run("12", func(b *testing.B) {
   212  		x := uint64(1)
   213  		for i := 0; i < b.N; i++ {
   214  			x *= 12
   215  		}
   216  		mulSinkU64 = x
   217  	})
   218  	// 120x = 128x - 8x
   219  	b.Run("120", func(b *testing.B) {
   220  		x := uint64(1)
   221  		for i := 0; i < b.N; i++ {
   222  			x *= 120
   223  		}
   224  		mulSinkU64 = x
   225  	})
   226  	// 65537x = 65536x + x
   227  	b.Run("65537", func(b *testing.B) {
   228  		x := uint64(1)
   229  		for i := 0; i < b.N; i++ {
   230  			x *= 65537
   231  		}
   232  		mulSinkU64 = x
   233  	})
   234  	// 65538x = 65536x + 2x
   235  	b.Run("65538", func(b *testing.B) {
   236  		x := uint64(1)
   237  		for i := 0; i < b.N; i++ {
   238  			x *= 65538
   239  		}
   240  		mulSinkU64 = x
   241  	})
   242  }
   243  

View as plain text