github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/runtime/vlop_arm_test.go (about)

     1  // Copyright 2012 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 runtime_test
     6  
     7  import (
     8  	"runtime"
     9  	"testing"
    10  )
    11  
    12  // arm soft division benchmarks adapted from
    13  // http://ridiculousfish.com/files/division_benchmarks.tar.gz
    14  
    15  const numeratorsSize = 1 << 21
    16  
    17  var numerators = randomNumerators()
    18  
    19  type randstate struct {
    20  	hi, lo uint32
    21  }
    22  
    23  func (r *randstate) rand() uint32 {
    24  	r.hi = r.hi<<16 + r.hi>>16
    25  	r.hi += r.lo
    26  	r.lo += r.hi
    27  	return r.hi
    28  }
    29  
    30  func randomNumerators() []uint32 {
    31  	numerators := make([]uint32, numeratorsSize)
    32  	random := &randstate{2147483563, 2147483563 ^ 0x49616E42}
    33  	for i := range numerators {
    34  		numerators[i] = random.rand()
    35  	}
    36  	return numerators
    37  }
    38  
    39  func bmUint32Div(divisor uint32, b *testing.B) {
    40  	var sum uint32
    41  	for i := 0; i < b.N; i++ {
    42  		sum += numerators[i&(numeratorsSize-1)] / divisor
    43  	}
    44  }
    45  
    46  func BenchmarkUint32Div7(b *testing.B)         { bmUint32Div(7, b) }
    47  func BenchmarkUint32Div37(b *testing.B)        { bmUint32Div(37, b) }
    48  func BenchmarkUint32Div123(b *testing.B)       { bmUint32Div(123, b) }
    49  func BenchmarkUint32Div763(b *testing.B)       { bmUint32Div(763, b) }
    50  func BenchmarkUint32Div1247(b *testing.B)      { bmUint32Div(1247, b) }
    51  func BenchmarkUint32Div9305(b *testing.B)      { bmUint32Div(9305, b) }
    52  func BenchmarkUint32Div13307(b *testing.B)     { bmUint32Div(13307, b) }
    53  func BenchmarkUint32Div52513(b *testing.B)     { bmUint32Div(52513, b) }
    54  func BenchmarkUint32Div60978747(b *testing.B)  { bmUint32Div(60978747, b) }
    55  func BenchmarkUint32Div106956295(b *testing.B) { bmUint32Div(106956295, b) }
    56  
    57  func bmUint32Mod(divisor uint32, b *testing.B) {
    58  	var sum uint32
    59  	for i := 0; i < b.N; i++ {
    60  		sum += numerators[i&(numeratorsSize-1)] % divisor
    61  	}
    62  }
    63  
    64  func BenchmarkUint32Mod7(b *testing.B)         { bmUint32Mod(7, b) }
    65  func BenchmarkUint32Mod37(b *testing.B)        { bmUint32Mod(37, b) }
    66  func BenchmarkUint32Mod123(b *testing.B)       { bmUint32Mod(123, b) }
    67  func BenchmarkUint32Mod763(b *testing.B)       { bmUint32Mod(763, b) }
    68  func BenchmarkUint32Mod1247(b *testing.B)      { bmUint32Mod(1247, b) }
    69  func BenchmarkUint32Mod9305(b *testing.B)      { bmUint32Mod(9305, b) }
    70  func BenchmarkUint32Mod13307(b *testing.B)     { bmUint32Mod(13307, b) }
    71  func BenchmarkUint32Mod52513(b *testing.B)     { bmUint32Mod(52513, b) }
    72  func BenchmarkUint32Mod60978747(b *testing.B)  { bmUint32Mod(60978747, b) }
    73  func BenchmarkUint32Mod106956295(b *testing.B) { bmUint32Mod(106956295, b) }
    74  
    75  func TestUsplit(t *testing.T) {
    76  	var den uint32 = 1000000
    77  	for _, x := range []uint32{0, 1, 999999, 1000000, 1010101, 0xFFFFFFFF} {
    78  		q1, r1 := runtime.Usplit(x)
    79  		q2, r2 := x/den, x%den
    80  		if q1 != q2 || r1 != r2 {
    81  			t.Errorf("%d/1e6, %d%%1e6 = %d, %d, want %d, %d", x, x, q1, r1, q2, r2)
    82  		}
    83  	}
    84  }