github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/cmd/compile/internal/big/arith_test.go (about)

     1  // Copyright 2009 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 big
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  )
    11  
    12  type funWW func(x, y, c Word) (z1, z0 Word)
    13  type argWW struct {
    14  	x, y, c, z1, z0 Word
    15  }
    16  
    17  var sumWW = []argWW{
    18  	{0, 0, 0, 0, 0},
    19  	{0, 1, 0, 0, 1},
    20  	{0, 0, 1, 0, 1},
    21  	{0, 1, 1, 0, 2},
    22  	{12345, 67890, 0, 0, 80235},
    23  	{12345, 67890, 1, 0, 80236},
    24  	{_M, 1, 0, 1, 0},
    25  	{_M, 0, 1, 1, 0},
    26  	{_M, 1, 1, 1, 1},
    27  	{_M, _M, 0, 1, _M - 1},
    28  	{_M, _M, 1, 1, _M},
    29  }
    30  
    31  func testFunWW(t *testing.T, msg string, f funWW, a argWW) {
    32  	z1, z0 := f(a.x, a.y, a.c)
    33  	if z1 != a.z1 || z0 != a.z0 {
    34  		t.Errorf("%s%+v\n\tgot z1:z0 = %#x:%#x; want %#x:%#x", msg, a, z1, z0, a.z1, a.z0)
    35  	}
    36  }
    37  
    38  func TestFunWW(t *testing.T) {
    39  	for _, a := range sumWW {
    40  		arg := a
    41  		testFunWW(t, "addWW_g", addWW_g, arg)
    42  
    43  		arg = argWW{a.y, a.x, a.c, a.z1, a.z0}
    44  		testFunWW(t, "addWW_g symmetric", addWW_g, arg)
    45  
    46  		arg = argWW{a.z0, a.x, a.c, a.z1, a.y}
    47  		testFunWW(t, "subWW_g", subWW_g, arg)
    48  
    49  		arg = argWW{a.z0, a.y, a.c, a.z1, a.x}
    50  		testFunWW(t, "subWW_g symmetric", subWW_g, arg)
    51  	}
    52  }
    53  
    54  type funVV func(z, x, y []Word) (c Word)
    55  type argVV struct {
    56  	z, x, y nat
    57  	c       Word
    58  }
    59  
    60  var sumVV = []argVV{
    61  	{},
    62  	{nat{0}, nat{0}, nat{0}, 0},
    63  	{nat{1}, nat{1}, nat{0}, 0},
    64  	{nat{0}, nat{_M}, nat{1}, 1},
    65  	{nat{80235}, nat{12345}, nat{67890}, 0},
    66  	{nat{_M - 1}, nat{_M}, nat{_M}, 1},
    67  	{nat{0, 0, 0, 0}, nat{_M, _M, _M, _M}, nat{1, 0, 0, 0}, 1},
    68  	{nat{0, 0, 0, _M}, nat{_M, _M, _M, _M - 1}, nat{1, 0, 0, 0}, 0},
    69  	{nat{0, 0, 0, 0}, nat{_M, 0, _M, 0}, nat{1, _M, 0, _M}, 1},
    70  }
    71  
    72  func testFunVV(t *testing.T, msg string, f funVV, a argVV) {
    73  	z := make(nat, len(a.z))
    74  	c := f(z, a.x, a.y)
    75  	for i, zi := range z {
    76  		if zi != a.z[i] {
    77  			t.Errorf("%s%+v\n\tgot z[%d] = %#x; want %#x", msg, a, i, zi, a.z[i])
    78  			break
    79  		}
    80  	}
    81  	if c != a.c {
    82  		t.Errorf("%s%+v\n\tgot c = %#x; want %#x", msg, a, c, a.c)
    83  	}
    84  }
    85  
    86  func TestFunVV(t *testing.T) {
    87  	for _, a := range sumVV {
    88  		arg := a
    89  		testFunVV(t, "addVV_g", addVV_g, arg)
    90  		testFunVV(t, "addVV", addVV, arg)
    91  
    92  		arg = argVV{a.z, a.y, a.x, a.c}
    93  		testFunVV(t, "addVV_g symmetric", addVV_g, arg)
    94  		testFunVV(t, "addVV symmetric", addVV, arg)
    95  
    96  		arg = argVV{a.x, a.z, a.y, a.c}
    97  		testFunVV(t, "subVV_g", subVV_g, arg)
    98  		testFunVV(t, "subVV", subVV, arg)
    99  
   100  		arg = argVV{a.y, a.z, a.x, a.c}
   101  		testFunVV(t, "subVV_g symmetric", subVV_g, arg)
   102  		testFunVV(t, "subVV symmetric", subVV, arg)
   103  	}
   104  }
   105  
   106  // Always the same seed for reproducible results.
   107  var rnd = rand.New(rand.NewSource(0))
   108  
   109  func rndW() Word {
   110  	return Word(rnd.Int63()<<1 | rnd.Int63n(2))
   111  }
   112  
   113  func rndV(n int) []Word {
   114  	v := make([]Word, n)
   115  	for i := range v {
   116  		v[i] = rndW()
   117  	}
   118  	return v
   119  }
   120  
   121  func benchmarkFunVV(b *testing.B, f funVV, n int) {
   122  	x := rndV(n)
   123  	y := rndV(n)
   124  	z := make([]Word, n)
   125  	b.SetBytes(int64(n * _W))
   126  	b.ResetTimer()
   127  	for i := 0; i < b.N; i++ {
   128  		f(z, x, y)
   129  	}
   130  }
   131  
   132  func BenchmarkAddVV_1(b *testing.B)   { benchmarkFunVV(b, addVV, 1) }
   133  func BenchmarkAddVV_2(b *testing.B)   { benchmarkFunVV(b, addVV, 2) }
   134  func BenchmarkAddVV_3(b *testing.B)   { benchmarkFunVV(b, addVV, 3) }
   135  func BenchmarkAddVV_4(b *testing.B)   { benchmarkFunVV(b, addVV, 4) }
   136  func BenchmarkAddVV_5(b *testing.B)   { benchmarkFunVV(b, addVV, 5) }
   137  func BenchmarkAddVV_1e1(b *testing.B) { benchmarkFunVV(b, addVV, 1e1) }
   138  func BenchmarkAddVV_1e2(b *testing.B) { benchmarkFunVV(b, addVV, 1e2) }
   139  func BenchmarkAddVV_1e3(b *testing.B) { benchmarkFunVV(b, addVV, 1e3) }
   140  func BenchmarkAddVV_1e4(b *testing.B) { benchmarkFunVV(b, addVV, 1e4) }
   141  func BenchmarkAddVV_1e5(b *testing.B) { benchmarkFunVV(b, addVV, 1e5) }
   142  
   143  type funVW func(z, x []Word, y Word) (c Word)
   144  type argVW struct {
   145  	z, x nat
   146  	y    Word
   147  	c    Word
   148  }
   149  
   150  var sumVW = []argVW{
   151  	{},
   152  	{nil, nil, 2, 2},
   153  	{nat{0}, nat{0}, 0, 0},
   154  	{nat{1}, nat{0}, 1, 0},
   155  	{nat{1}, nat{1}, 0, 0},
   156  	{nat{0}, nat{_M}, 1, 1},
   157  	{nat{0, 0, 0, 0}, nat{_M, _M, _M, _M}, 1, 1},
   158  	{nat{585}, nat{314}, 271, 0},
   159  }
   160  
   161  var lshVW = []argVW{
   162  	{},
   163  	{nat{0}, nat{0}, 0, 0},
   164  	{nat{0}, nat{0}, 1, 0},
   165  	{nat{0}, nat{0}, 20, 0},
   166  
   167  	{nat{_M}, nat{_M}, 0, 0},
   168  	{nat{_M << 1 & _M}, nat{_M}, 1, 1},
   169  	{nat{_M << 20 & _M}, nat{_M}, 20, _M >> (_W - 20)},
   170  
   171  	{nat{_M, _M, _M}, nat{_M, _M, _M}, 0, 0},
   172  	{nat{_M << 1 & _M, _M, _M}, nat{_M, _M, _M}, 1, 1},
   173  	{nat{_M << 20 & _M, _M, _M}, nat{_M, _M, _M}, 20, _M >> (_W - 20)},
   174  }
   175  
   176  var rshVW = []argVW{
   177  	{},
   178  	{nat{0}, nat{0}, 0, 0},
   179  	{nat{0}, nat{0}, 1, 0},
   180  	{nat{0}, nat{0}, 20, 0},
   181  
   182  	{nat{_M}, nat{_M}, 0, 0},
   183  	{nat{_M >> 1}, nat{_M}, 1, _M << (_W - 1) & _M},
   184  	{nat{_M >> 20}, nat{_M}, 20, _M << (_W - 20) & _M},
   185  
   186  	{nat{_M, _M, _M}, nat{_M, _M, _M}, 0, 0},
   187  	{nat{_M, _M, _M >> 1}, nat{_M, _M, _M}, 1, _M << (_W - 1) & _M},
   188  	{nat{_M, _M, _M >> 20}, nat{_M, _M, _M}, 20, _M << (_W - 20) & _M},
   189  }
   190  
   191  func testFunVW(t *testing.T, msg string, f funVW, a argVW) {
   192  	z := make(nat, len(a.z))
   193  	c := f(z, a.x, a.y)
   194  	for i, zi := range z {
   195  		if zi != a.z[i] {
   196  			t.Errorf("%s%+v\n\tgot z[%d] = %#x; want %#x", msg, a, i, zi, a.z[i])
   197  			break
   198  		}
   199  	}
   200  	if c != a.c {
   201  		t.Errorf("%s%+v\n\tgot c = %#x; want %#x", msg, a, c, a.c)
   202  	}
   203  }
   204  
   205  func makeFunVW(f func(z, x []Word, s uint) (c Word)) funVW {
   206  	return func(z, x []Word, s Word) (c Word) {
   207  		return f(z, x, uint(s))
   208  	}
   209  }
   210  
   211  func TestFunVW(t *testing.T) {
   212  	for _, a := range sumVW {
   213  		arg := a
   214  		testFunVW(t, "addVW_g", addVW_g, arg)
   215  		testFunVW(t, "addVW", addVW, arg)
   216  
   217  		arg = argVW{a.x, a.z, a.y, a.c}
   218  		testFunVW(t, "subVW_g", subVW_g, arg)
   219  		testFunVW(t, "subVW", subVW, arg)
   220  	}
   221  
   222  	shlVW_g := makeFunVW(shlVU_g)
   223  	shlVW := makeFunVW(shlVU)
   224  	for _, a := range lshVW {
   225  		arg := a
   226  		testFunVW(t, "shlVU_g", shlVW_g, arg)
   227  		testFunVW(t, "shlVU", shlVW, arg)
   228  	}
   229  
   230  	shrVW_g := makeFunVW(shrVU_g)
   231  	shrVW := makeFunVW(shrVU)
   232  	for _, a := range rshVW {
   233  		arg := a
   234  		testFunVW(t, "shrVU_g", shrVW_g, arg)
   235  		testFunVW(t, "shrVU", shrVW, arg)
   236  	}
   237  }
   238  
   239  func benchmarkFunVW(b *testing.B, f funVW, n int) {
   240  	x := rndV(n)
   241  	y := rndW()
   242  	z := make([]Word, n)
   243  	b.SetBytes(int64(n * _S))
   244  	b.ResetTimer()
   245  	for i := 0; i < b.N; i++ {
   246  		f(z, x, y)
   247  	}
   248  }
   249  
   250  func BenchmarkAddVW_1(b *testing.B)   { benchmarkFunVW(b, addVW, 1) }
   251  func BenchmarkAddVW_2(b *testing.B)   { benchmarkFunVW(b, addVW, 2) }
   252  func BenchmarkAddVW_3(b *testing.B)   { benchmarkFunVW(b, addVW, 3) }
   253  func BenchmarkAddVW_4(b *testing.B)   { benchmarkFunVW(b, addVW, 4) }
   254  func BenchmarkAddVW_5(b *testing.B)   { benchmarkFunVW(b, addVW, 5) }
   255  func BenchmarkAddVW_1e1(b *testing.B) { benchmarkFunVW(b, addVW, 1e1) }
   256  func BenchmarkAddVW_1e2(b *testing.B) { benchmarkFunVW(b, addVW, 1e2) }
   257  func BenchmarkAddVW_1e3(b *testing.B) { benchmarkFunVW(b, addVW, 1e3) }
   258  func BenchmarkAddVW_1e4(b *testing.B) { benchmarkFunVW(b, addVW, 1e4) }
   259  func BenchmarkAddVW_1e5(b *testing.B) { benchmarkFunVW(b, addVW, 1e5) }
   260  
   261  type funVWW func(z, x []Word, y, r Word) (c Word)
   262  type argVWW struct {
   263  	z, x nat
   264  	y, r Word
   265  	c    Word
   266  }
   267  
   268  var prodVWW = []argVWW{
   269  	{},
   270  	{nat{0}, nat{0}, 0, 0, 0},
   271  	{nat{991}, nat{0}, 0, 991, 0},
   272  	{nat{0}, nat{_M}, 0, 0, 0},
   273  	{nat{991}, nat{_M}, 0, 991, 0},
   274  	{nat{0}, nat{0}, _M, 0, 0},
   275  	{nat{991}, nat{0}, _M, 991, 0},
   276  	{nat{1}, nat{1}, 1, 0, 0},
   277  	{nat{992}, nat{1}, 1, 991, 0},
   278  	{nat{22793}, nat{991}, 23, 0, 0},
   279  	{nat{22800}, nat{991}, 23, 7, 0},
   280  	{nat{0, 0, 0, 22793}, nat{0, 0, 0, 991}, 23, 0, 0},
   281  	{nat{7, 0, 0, 22793}, nat{0, 0, 0, 991}, 23, 7, 0},
   282  	{nat{0, 0, 0, 0}, nat{7893475, 7395495, 798547395, 68943}, 0, 0, 0},
   283  	{nat{991, 0, 0, 0}, nat{7893475, 7395495, 798547395, 68943}, 0, 991, 0},
   284  	{nat{0, 0, 0, 0}, nat{0, 0, 0, 0}, 894375984, 0, 0},
   285  	{nat{991, 0, 0, 0}, nat{0, 0, 0, 0}, 894375984, 991, 0},
   286  	{nat{_M << 1 & _M}, nat{_M}, 1 << 1, 0, _M >> (_W - 1)},
   287  	{nat{_M<<1&_M + 1}, nat{_M}, 1 << 1, 1, _M >> (_W - 1)},
   288  	{nat{_M << 7 & _M}, nat{_M}, 1 << 7, 0, _M >> (_W - 7)},
   289  	{nat{_M<<7&_M + 1<<6}, nat{_M}, 1 << 7, 1 << 6, _M >> (_W - 7)},
   290  	{nat{_M << 7 & _M, _M, _M, _M}, nat{_M, _M, _M, _M}, 1 << 7, 0, _M >> (_W - 7)},
   291  	{nat{_M<<7&_M + 1<<6, _M, _M, _M}, nat{_M, _M, _M, _M}, 1 << 7, 1 << 6, _M >> (_W - 7)},
   292  }
   293  
   294  func testFunVWW(t *testing.T, msg string, f funVWW, a argVWW) {
   295  	z := make(nat, len(a.z))
   296  	c := f(z, a.x, a.y, a.r)
   297  	for i, zi := range z {
   298  		if zi != a.z[i] {
   299  			t.Errorf("%s%+v\n\tgot z[%d] = %#x; want %#x", msg, a, i, zi, a.z[i])
   300  			break
   301  		}
   302  	}
   303  	if c != a.c {
   304  		t.Errorf("%s%+v\n\tgot c = %#x; want %#x", msg, a, c, a.c)
   305  	}
   306  }
   307  
   308  // TODO(gri) mulAddVWW and divWVW are symmetric operations but
   309  //           their signature is not symmetric. Try to unify.
   310  
   311  type funWVW func(z []Word, xn Word, x []Word, y Word) (r Word)
   312  type argWVW struct {
   313  	z  nat
   314  	xn Word
   315  	x  nat
   316  	y  Word
   317  	r  Word
   318  }
   319  
   320  func testFunWVW(t *testing.T, msg string, f funWVW, a argWVW) {
   321  	z := make(nat, len(a.z))
   322  	r := f(z, a.xn, a.x, a.y)
   323  	for i, zi := range z {
   324  		if zi != a.z[i] {
   325  			t.Errorf("%s%+v\n\tgot z[%d] = %#x; want %#x", msg, a, i, zi, a.z[i])
   326  			break
   327  		}
   328  	}
   329  	if r != a.r {
   330  		t.Errorf("%s%+v\n\tgot r = %#x; want %#x", msg, a, r, a.r)
   331  	}
   332  }
   333  
   334  func TestFunVWW(t *testing.T) {
   335  	for _, a := range prodVWW {
   336  		arg := a
   337  		testFunVWW(t, "mulAddVWW_g", mulAddVWW_g, arg)
   338  		testFunVWW(t, "mulAddVWW", mulAddVWW, arg)
   339  
   340  		if a.y != 0 && a.r < a.y {
   341  			arg := argWVW{a.x, a.c, a.z, a.y, a.r}
   342  			testFunWVW(t, "divWVW_g", divWVW_g, arg)
   343  			testFunWVW(t, "divWVW", divWVW, arg)
   344  		}
   345  	}
   346  }
   347  
   348  var mulWWTests = []struct {
   349  	x, y Word
   350  	q, r Word
   351  }{
   352  	{_M, _M, _M - 1, 1},
   353  	// 32 bit only: {0xc47dfa8c, 50911, 0x98a4, 0x998587f4},
   354  }
   355  
   356  func TestMulWW(t *testing.T) {
   357  	for i, test := range mulWWTests {
   358  		q, r := mulWW_g(test.x, test.y)
   359  		if q != test.q || r != test.r {
   360  			t.Errorf("#%d got (%x, %x) want (%x, %x)", i, q, r, test.q, test.r)
   361  		}
   362  	}
   363  }
   364  
   365  var mulAddWWWTests = []struct {
   366  	x, y, c Word
   367  	q, r    Word
   368  }{
   369  	// TODO(agl): These will only work on 64-bit platforms.
   370  	// {15064310297182388543, 0xe7df04d2d35d5d80, 13537600649892366549, 13644450054494335067, 10832252001440893781},
   371  	// {15064310297182388543, 0xdab2f18048baa68d, 13644450054494335067, 12869334219691522700, 14233854684711418382},
   372  	{_M, _M, 0, _M - 1, 1},
   373  	{_M, _M, _M, _M, 0},
   374  }
   375  
   376  func TestMulAddWWW(t *testing.T) {
   377  	for i, test := range mulAddWWWTests {
   378  		q, r := mulAddWWW_g(test.x, test.y, test.c)
   379  		if q != test.q || r != test.r {
   380  			t.Errorf("#%d got (%x, %x) want (%x, %x)", i, q, r, test.q, test.r)
   381  		}
   382  	}
   383  }
   384  
   385  func benchmarkAddMulVVW(b *testing.B, n int) {
   386  	x := rndV(n)
   387  	y := rndW()
   388  	z := make([]Word, n)
   389  	b.SetBytes(int64(n * _W))
   390  	b.ResetTimer()
   391  	for i := 0; i < b.N; i++ {
   392  		addMulVVW(z, x, y)
   393  	}
   394  }
   395  
   396  func BenchmarkAddMulVVW_1(b *testing.B)   { benchmarkAddMulVVW(b, 1) }
   397  func BenchmarkAddMulVVW_2(b *testing.B)   { benchmarkAddMulVVW(b, 2) }
   398  func BenchmarkAddMulVVW_3(b *testing.B)   { benchmarkAddMulVVW(b, 3) }
   399  func BenchmarkAddMulVVW_4(b *testing.B)   { benchmarkAddMulVVW(b, 4) }
   400  func BenchmarkAddMulVVW_5(b *testing.B)   { benchmarkAddMulVVW(b, 5) }
   401  func BenchmarkAddMulVVW_1e1(b *testing.B) { benchmarkAddMulVVW(b, 1e1) }
   402  func BenchmarkAddMulVVW_1e2(b *testing.B) { benchmarkAddMulVVW(b, 1e2) }
   403  func BenchmarkAddMulVVW_1e3(b *testing.B) { benchmarkAddMulVVW(b, 1e3) }
   404  func BenchmarkAddMulVVW_1e4(b *testing.B) { benchmarkAddMulVVW(b, 1e4) }
   405  func BenchmarkAddMulVVW_1e5(b *testing.B) { benchmarkAddMulVVW(b, 1e5) }
   406  
   407  func testWordBitLen(t *testing.T, fname string, f func(Word) int) {
   408  	for i := 0; i <= _W; i++ {
   409  		x := Word(1) << uint(i-1) // i == 0 => x == 0
   410  		n := f(x)
   411  		if n != i {
   412  			t.Errorf("got %d; want %d for %s(%#x)", n, i, fname, x)
   413  		}
   414  	}
   415  }
   416  
   417  func TestWordBitLen(t *testing.T) {
   418  	testWordBitLen(t, "bitLen", bitLen)
   419  	testWordBitLen(t, "bitLen_g", bitLen_g)
   420  }
   421  
   422  // runs b.N iterations of bitLen called on a Word containing (1 << nbits)-1.
   423  func benchmarkBitLenN(b *testing.B, nbits uint) {
   424  	testword := Word((uint64(1) << nbits) - 1)
   425  	for i := 0; i < b.N; i++ {
   426  		bitLen(testword)
   427  	}
   428  }
   429  
   430  // Individual bitLen tests. Numbers chosen to examine both sides
   431  // of powers-of-two boundaries.
   432  func BenchmarkBitLen0(b *testing.B)  { benchmarkBitLenN(b, 0) }
   433  func BenchmarkBitLen1(b *testing.B)  { benchmarkBitLenN(b, 1) }
   434  func BenchmarkBitLen2(b *testing.B)  { benchmarkBitLenN(b, 2) }
   435  func BenchmarkBitLen3(b *testing.B)  { benchmarkBitLenN(b, 3) }
   436  func BenchmarkBitLen4(b *testing.B)  { benchmarkBitLenN(b, 4) }
   437  func BenchmarkBitLen5(b *testing.B)  { benchmarkBitLenN(b, 5) }
   438  func BenchmarkBitLen8(b *testing.B)  { benchmarkBitLenN(b, 8) }
   439  func BenchmarkBitLen9(b *testing.B)  { benchmarkBitLenN(b, 9) }
   440  func BenchmarkBitLen16(b *testing.B) { benchmarkBitLenN(b, 16) }
   441  func BenchmarkBitLen17(b *testing.B) { benchmarkBitLenN(b, 17) }
   442  func BenchmarkBitLen31(b *testing.B) { benchmarkBitLenN(b, 31) }