-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmul128_test.go
More file actions
88 lines (70 loc) · 1.52 KB
/
Copy pathmul128_test.go
File metadata and controls
88 lines (70 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Copyright (c) 2026 Markku Rossi
//
// All rights reserved.
//
package ot
import (
"math/rand"
"testing"
)
func TestMul128Basic(t *testing.T) {
zero := Label{0, 0}
one := Label{1, 0}
// 0 * x = 0
lo, hi := mul128(zero, Label{0xdeadbeef, 0x12345678})
if lo != zero || hi != zero {
t.Fatal("0*x != 0")
}
// 1 * x = x
x := Label{0xabcdef, 0x1234}
lo, hi = mul128(one, x)
if lo != x || hi != zero {
t.Fatal("1*x != x")
}
// x * x = x^2
a := Label{2, 0} // polynomial x
lo, hi = mul128(a, a)
if lo.D0 != 4 || lo.D1 != 0 || hi != zero {
t.Fatal("x*x != x^2")
}
}
func TestMul128Cross(t *testing.T) {
// x^63 * x^63 = x^126
a := Label{D0: 1 << 63, D1: 0}
lo, hi := mul128(a, a)
expLo := Label{D1: 1 << 62} // 126 = 64 + 62
expHi := Label{}
if lo != expLo || hi != expHi {
t.Fatalf("got lo=%v hi=%v, expected lo=%v hi=%v", lo, hi, expLo, expHi)
}
}
func TestMul128AllOnes(t *testing.T) {
a := Label{^uint64(0), ^uint64(0)}
b := a
lo1, hi1 := mul128(a, b)
lo2, hi2 := mul128Ref(a, b)
if lo1 != lo2 || hi1 != hi2 {
t.Fatal("all-ones mismatch")
}
}
func TestMul128Random(t *testing.T) {
rng := rand.New(rand.NewSource(1))
for i := 0; i < 1000; i++ {
a := Label{rng.Uint64(), rng.Uint64()}
b := Label{rng.Uint64(), rng.Uint64()}
lo1, hi1 := mul128(a, b)
lo2, hi2 := mul128Ref(a, b)
if lo1 != lo2 || hi1 != hi2 {
t.Fatalf("mismatch on %v * %v", a, b)
}
}
}
func BenchmarkMul128(b *testing.B) {
var b0, b1 Label
for b.Loop() {
lo, hi := mul128Generic(b0, b1)
_ = lo
_ = hi
}
}