62
63
{nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
63
64
{nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
64
65
{nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
66
// 3^100 * 3^28 = 3^128
68
natFromString("11790184577738583171520872861412518665678211592275841109096961"),
69
natFromString("515377520732011331036461129765621272702107522001"),
70
natFromString("22876792454961"),
72
// z = 111....1 (70000 digits)
73
// x = 10^(99*700) + ... + 10^1400 + 10^700 + 1
74
// y = 111....1 (700 digits, larger than Karatsuba threshold on 32-bit and 64-bit)
76
natFromString(strings.Repeat("1", 70000)),
77
natFromString("1" + strings.Repeat(strings.Repeat("0", 699)+"1", 99)),
78
natFromString(strings.Repeat("1", 700)),
80
// z = 111....1 (20000 digits)
82
// y = 111....1 (10000 digits)
84
natFromString(strings.Repeat("1", 20000)),
85
natFromString("1" + strings.Repeat("0", 9999) + "1"),
86
natFromString(strings.Repeat("1", 10000)),
90
func natFromString(s string) nat {
91
x, _, err := nat(nil).scan(strings.NewReader(s), 0)
67
98
func TestSet(t *testing.T) {
138
var mulArg, mulTmp nat
142
mulArg = make(nat, n)
143
for i := 0; i < n; i++ {
148
func benchmarkMulLoad() {
149
for j := 1; j <= 10; j++ {
150
x := mulArg[0 : j*100]
169
// allocBytes returns the number of bytes allocated by invoking f.
170
func allocBytes(f func()) uint64 {
171
var stats runtime.MemStats
172
runtime.ReadMemStats(&stats)
173
t := stats.TotalAlloc
175
runtime.ReadMemStats(&stats)
176
return stats.TotalAlloc - t
179
// TestMulUnbalanced tests that multiplying numbers of different lengths
180
// does not cause deep recursion and in turn allocate too much memory.
181
// Test case for issue 3807.
182
func TestMulUnbalanced(t *testing.T) {
183
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
186
allocSize := allocBytes(func() {
189
inputSize := uint64(len(x)+len(y)) * _S
190
if ratio := allocSize / uint64(inputSize); ratio > 10 {
191
t.Errorf("multiplication uses too much memory (%d > %d times the size of inputs)", allocSize, ratio)
195
func rndNat(n int) nat {
196
return nat(rndV(n)).norm()
155
199
func BenchmarkMul(b *testing.B) {
156
203
for i := 0; i < b.N; i++ {
434
func BenchmarkStringPiParallel(b *testing.B) {
436
x, _, _ = x.scan(strings.NewReader(pi), 0)
437
if x.decimalString() != pi {
438
panic("benchmark incorrect: conversion failed")
440
n := runtime.GOMAXPROCS(0)
441
m := b.N / n // n*m <= b.N due to flooring, but the error is neglibible (n is not very large)
442
c := make(chan int, n)
443
for i := 0; i < n; i++ {
445
for j := 0; j < m; j++ {
451
for i := 0; i < n; i++ {
372
456
func BenchmarkScan10Base2(b *testing.B) { ScanHelper(b, 2, 10, 10) }
373
457
func BenchmarkScan100Base2(b *testing.B) { ScanHelper(b, 2, 10, 100) }
374
458
func BenchmarkScan1000Base2(b *testing.B) { ScanHelper(b, 2, 10, 1000) }
463
547
func BenchmarkLeafSize14(b *testing.B) { LeafSizeHelper(b, 10, 14) }
464
548
func BenchmarkLeafSize15(b *testing.B) { LeafSizeHelper(b, 10, 15) }
465
549
func BenchmarkLeafSize16(b *testing.B) { LeafSizeHelper(b, 10, 16) }
466
func BenchmarkLeafSize32(b *testing.B) { LeafSizeHelper(b, 10, 32) } // try some large lengths
550
func BenchmarkLeafSize32(b *testing.B) { LeafSizeHelper(b, 10, 32) } // try some large lengths
467
551
func BenchmarkLeafSize64(b *testing.B) { LeafSizeHelper(b, 10, 64) }
469
553
func LeafSizeHelper(b *testing.B, base Word, size int) {
471
555
originalLeafSize := leafSize
472
resetTable(cacheBase10[:])
556
resetTable(cacheBase10.table[:])
618
702
func TestTrailingZeroBits(t *testing.T) {
621
for i := 0; i < _W; i++ {
622
if trailingZeroBits(x) != i {
623
t.Errorf("Failed at step %d: x: %x got: %d", i, x, trailingZeroBits(x))
704
for i := uint(0); i <= _W; i++ {
705
n := trailingZeroBits(x)
707
t.Errorf("got trailingZeroBits(%#x) = %d; want %d", x, n, i%_W)
712
y := nat(nil).set(natOne)
713
for i := uint(0); i <= 3*_W; i++ {
714
n := y.trailingZeroBits()
716
t.Errorf("got 0x%s.trailingZeroBits() = %d; want %d", y.string(lowercaseDigits[0:16]), n, i)
629
722
var expNNTests = []struct {