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