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