...

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

Documentation: cmd/compile/internal/test

     1  // Copyright 2016 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 (
     8  	"testing"
     9  )
    10  
    11  var boolres bool
    12  
    13  var i64res int64
    14  
    15  func BenchmarkDivconstI64(b *testing.B) {
    16  	for i := 0; i < b.N; i++ {
    17  		i64res = int64(i) / 7
    18  	}
    19  }
    20  
    21  func BenchmarkModconstI64(b *testing.B) {
    22  	for i := 0; i < b.N; i++ {
    23  		i64res = int64(i) % 7
    24  	}
    25  }
    26  
    27  func BenchmarkDivisiblePow2constI64(b *testing.B) {
    28  	for i := 0; i < b.N; i++ {
    29  		boolres = int64(i)%16 == 0
    30  	}
    31  }
    32  func BenchmarkDivisibleconstI64(b *testing.B) {
    33  	for i := 0; i < b.N; i++ {
    34  		boolres = int64(i)%7 == 0
    35  	}
    36  }
    37  
    38  func BenchmarkDivisibleWDivconstI64(b *testing.B) {
    39  	for i := 0; i < b.N; i++ {
    40  		i64res = int64(i) / 7
    41  		boolres = int64(i)%7 == 0
    42  	}
    43  }
    44  
    45  var u64res uint64
    46  
    47  func TestDivmodConstU64(t *testing.T) {
    48  	// Test division by c. Function f must be func(n) { return n/c, n%c }
    49  	testdiv := func(c uint64, f func(uint64) (uint64, uint64)) func(*testing.T) {
    50  		return func(t *testing.T) {
    51  			x := uint64(12345)
    52  			for i := 0; i < 10000; i++ {
    53  				x += x << 2
    54  				q, r := f(x)
    55  				if r < 0 || r >= c || q*c+r != x {
    56  					t.Errorf("divmod(%d, %d) returned incorrect (%d, %d)", x, c, q, r)
    57  				}
    58  			}
    59  			max := uint64(1<<64-1) / c * c
    60  			xs := []uint64{0, 1, c - 1, c, c + 1, 2*c - 1, 2 * c, 2*c + 1,
    61  				c*c - 1, c * c, c*c + 1, max - 1, max, max + 1, 1<<64 - 1}
    62  			for _, x := range xs {
    63  				q, r := f(x)
    64  				if r < 0 || r >= c || q*c+r != x {
    65  					t.Errorf("divmod(%d, %d) returned incorrect (%d, %d)", x, c, q, r)
    66  				}
    67  			}
    68  		}
    69  	}
    70  	t.Run("2", testdiv(2, func(n uint64) (uint64, uint64) { return n / 2, n % 2 }))
    71  	t.Run("3", testdiv(3, func(n uint64) (uint64, uint64) { return n / 3, n % 3 }))
    72  	t.Run("4", testdiv(4, func(n uint64) (uint64, uint64) { return n / 4, n % 4 }))
    73  	t.Run("5", testdiv(5, func(n uint64) (uint64, uint64) { return n / 5, n % 5 }))
    74  	t.Run("6", testdiv(6, func(n uint64) (uint64, uint64) { return n / 6, n % 6 }))
    75  	t.Run("7", testdiv(7, func(n uint64) (uint64, uint64) { return n / 7, n % 7 }))
    76  	t.Run("8", testdiv(8, func(n uint64) (uint64, uint64) { return n / 8, n % 8 }))
    77  	t.Run("9", testdiv(9, func(n uint64) (uint64, uint64) { return n / 9, n % 9 }))
    78  	t.Run("10", testdiv(10, func(n uint64) (uint64, uint64) { return n / 10, n % 10 }))
    79  	t.Run("11", testdiv(11, func(n uint64) (uint64, uint64) { return n / 11, n % 11 }))
    80  	t.Run("12", testdiv(12, func(n uint64) (uint64, uint64) { return n / 12, n % 12 }))
    81  	t.Run("13", testdiv(13, func(n uint64) (uint64, uint64) { return n / 13, n % 13 }))
    82  	t.Run("14", testdiv(14, func(n uint64) (uint64, uint64) { return n / 14, n % 14 }))
    83  	t.Run("15", testdiv(15, func(n uint64) (uint64, uint64) { return n / 15, n % 15 }))
    84  	t.Run("16", testdiv(16, func(n uint64) (uint64, uint64) { return n / 16, n % 16 }))
    85  	t.Run("17", testdiv(17, func(n uint64) (uint64, uint64) { return n / 17, n % 17 }))
    86  	t.Run("255", testdiv(255, func(n uint64) (uint64, uint64) { return n / 255, n % 255 }))
    87  	t.Run("256", testdiv(256, func(n uint64) (uint64, uint64) { return n / 256, n % 256 }))
    88  	t.Run("257", testdiv(257, func(n uint64) (uint64, uint64) { return n / 257, n % 257 }))
    89  	t.Run("65535", testdiv(65535, func(n uint64) (uint64, uint64) { return n / 65535, n % 65535 }))
    90  	t.Run("65536", testdiv(65536, func(n uint64) (uint64, uint64) { return n / 65536, n % 65536 }))
    91  	t.Run("65537", testdiv(65537, func(n uint64) (uint64, uint64) { return n / 65537, n % 65537 }))
    92  	t.Run("1<<32-1", testdiv(1<<32-1, func(n uint64) (uint64, uint64) { return n / (1<<32 - 1), n % (1<<32 - 1) }))
    93  	t.Run("1<<32+1", testdiv(1<<32+1, func(n uint64) (uint64, uint64) { return n / (1<<32 + 1), n % (1<<32 + 1) }))
    94  	t.Run("1<<64-1", testdiv(1<<64-1, func(n uint64) (uint64, uint64) { return n / (1<<64 - 1), n % (1<<64 - 1) }))
    95  }
    96  
    97  func BenchmarkDivconstU64(b *testing.B) {
    98  	b.Run("3", func(b *testing.B) {
    99  		x := uint64(123456789123456789)
   100  		for i := 0; i < b.N; i++ {
   101  			x += x << 4
   102  			u64res = uint64(x) / 3
   103  		}
   104  	})
   105  	b.Run("5", func(b *testing.B) {
   106  		x := uint64(123456789123456789)
   107  		for i := 0; i < b.N; i++ {
   108  			x += x << 4
   109  			u64res = uint64(x) / 5
   110  		}
   111  	})
   112  	b.Run("37", func(b *testing.B) {
   113  		x := uint64(123456789123456789)
   114  		for i := 0; i < b.N; i++ {
   115  			x += x << 4
   116  			u64res = uint64(x) / 37
   117  		}
   118  	})
   119  	b.Run("1234567", func(b *testing.B) {
   120  		x := uint64(123456789123456789)
   121  		for i := 0; i < b.N; i++ {
   122  			x += x << 4
   123  			u64res = uint64(x) / 1234567
   124  		}
   125  	})
   126  }
   127  
   128  func BenchmarkModconstU64(b *testing.B) {
   129  	for i := 0; i < b.N; i++ {
   130  		u64res = uint64(i) % 7
   131  	}
   132  }
   133  
   134  func BenchmarkDivisibleconstU64(b *testing.B) {
   135  	for i := 0; i < b.N; i++ {
   136  		boolres = uint64(i)%7 == 0
   137  	}
   138  }
   139  
   140  func BenchmarkDivisibleWDivconstU64(b *testing.B) {
   141  	for i := 0; i < b.N; i++ {
   142  		u64res = uint64(i) / 7
   143  		boolres = uint64(i)%7 == 0
   144  	}
   145  }
   146  
   147  var i32res int32
   148  
   149  func BenchmarkDivconstI32(b *testing.B) {
   150  	for i := 0; i < b.N; i++ {
   151  		i32res = int32(i) / 7
   152  	}
   153  }
   154  
   155  func BenchmarkModconstI32(b *testing.B) {
   156  	for i := 0; i < b.N; i++ {
   157  		i32res = int32(i) % 7
   158  	}
   159  }
   160  
   161  func BenchmarkDivisiblePow2constI32(b *testing.B) {
   162  	for i := 0; i < b.N; i++ {
   163  		boolres = int32(i)%16 == 0
   164  	}
   165  }
   166  
   167  func BenchmarkDivisibleconstI32(b *testing.B) {
   168  	for i := 0; i < b.N; i++ {
   169  		boolres = int32(i)%7 == 0
   170  	}
   171  }
   172  
   173  func BenchmarkDivisibleWDivconstI32(b *testing.B) {
   174  	for i := 0; i < b.N; i++ {
   175  		i32res = int32(i) / 7
   176  		boolres = int32(i)%7 == 0
   177  	}
   178  }
   179  
   180  var u32res uint32
   181  
   182  func BenchmarkDivconstU32(b *testing.B) {
   183  	for i := 0; i < b.N; i++ {
   184  		u32res = uint32(i) / 7
   185  	}
   186  }
   187  
   188  func BenchmarkModconstU32(b *testing.B) {
   189  	for i := 0; i < b.N; i++ {
   190  		u32res = uint32(i) % 7
   191  	}
   192  }
   193  
   194  func BenchmarkDivisibleconstU32(b *testing.B) {
   195  	for i := 0; i < b.N; i++ {
   196  		boolres = uint32(i)%7 == 0
   197  	}
   198  }
   199  
   200  func BenchmarkDivisibleWDivconstU32(b *testing.B) {
   201  	for i := 0; i < b.N; i++ {
   202  		u32res = uint32(i) / 7
   203  		boolres = uint32(i)%7 == 0
   204  	}
   205  }
   206  
   207  var i16res int16
   208  
   209  func BenchmarkDivconstI16(b *testing.B) {
   210  	for i := 0; i < b.N; i++ {
   211  		i16res = int16(i) / 7
   212  	}
   213  }
   214  
   215  func BenchmarkModconstI16(b *testing.B) {
   216  	for i := 0; i < b.N; i++ {
   217  		i16res = int16(i) % 7
   218  	}
   219  }
   220  
   221  func BenchmarkDivisiblePow2constI16(b *testing.B) {
   222  	for i := 0; i < b.N; i++ {
   223  		boolres = int16(i)%16 == 0
   224  	}
   225  }
   226  
   227  func BenchmarkDivisibleconstI16(b *testing.B) {
   228  	for i := 0; i < b.N; i++ {
   229  		boolres = int16(i)%7 == 0
   230  	}
   231  }
   232  
   233  func BenchmarkDivisibleWDivconstI16(b *testing.B) {
   234  	for i := 0; i < b.N; i++ {
   235  		i16res = int16(i) / 7
   236  		boolres = int16(i)%7 == 0
   237  	}
   238  }
   239  
   240  var u16res uint16
   241  
   242  func BenchmarkDivconstU16(b *testing.B) {
   243  	for i := 0; i < b.N; i++ {
   244  		u16res = uint16(i) / 7
   245  	}
   246  }
   247  
   248  func BenchmarkModconstU16(b *testing.B) {
   249  	for i := 0; i < b.N; i++ {
   250  		u16res = uint16(i) % 7
   251  	}
   252  }
   253  
   254  func BenchmarkDivisibleconstU16(b *testing.B) {
   255  	for i := 0; i < b.N; i++ {
   256  		boolres = uint16(i)%7 == 0
   257  	}
   258  }
   259  
   260  func BenchmarkDivisibleWDivconstU16(b *testing.B) {
   261  	for i := 0; i < b.N; i++ {
   262  		u16res = uint16(i) / 7
   263  		boolres = uint16(i)%7 == 0
   264  	}
   265  }
   266  
   267  var i8res int8
   268  
   269  func BenchmarkDivconstI8(b *testing.B) {
   270  	for i := 0; i < b.N; i++ {
   271  		i8res = int8(i) / 7
   272  	}
   273  }
   274  
   275  func BenchmarkModconstI8(b *testing.B) {
   276  	for i := 0; i < b.N; i++ {
   277  		i8res = int8(i) % 7
   278  	}
   279  }
   280  
   281  func BenchmarkDivisiblePow2constI8(b *testing.B) {
   282  	for i := 0; i < b.N; i++ {
   283  		boolres = int8(i)%16 == 0
   284  	}
   285  }
   286  
   287  func BenchmarkDivisibleconstI8(b *testing.B) {
   288  	for i := 0; i < b.N; i++ {
   289  		boolres = int8(i)%7 == 0
   290  	}
   291  }
   292  
   293  func BenchmarkDivisibleWDivconstI8(b *testing.B) {
   294  	for i := 0; i < b.N; i++ {
   295  		i8res = int8(i) / 7
   296  		boolres = int8(i)%7 == 0
   297  	}
   298  }
   299  
   300  var u8res uint8
   301  
   302  func BenchmarkDivconstU8(b *testing.B) {
   303  	for i := 0; i < b.N; i++ {
   304  		u8res = uint8(i) / 7
   305  	}
   306  }
   307  
   308  func BenchmarkModconstU8(b *testing.B) {
   309  	for i := 0; i < b.N; i++ {
   310  		u8res = uint8(i) % 7
   311  	}
   312  }
   313  
   314  func BenchmarkDivisibleconstU8(b *testing.B) {
   315  	for i := 0; i < b.N; i++ {
   316  		boolres = uint8(i)%7 == 0
   317  	}
   318  }
   319  
   320  func BenchmarkDivisibleWDivconstU8(b *testing.B) {
   321  	for i := 0; i < b.N; i++ {
   322  		u8res = uint8(i) / 7
   323  		boolres = uint8(i)%7 == 0
   324  	}
   325  }
   326  

View as plain text