1 // Regression: hash/crc32's hardware kernels must exist AND be correct.
2 //
3 // crc32_amd64.mx declared castagnoliSSE42, castagnoliSSE42Triple and ieeeCLMUL
4 // as bodiless functions "defined in crc32_amd64.s", and arm64 did the same for
5 // castagnoliUpdate/ieeeUpdate. Those .s files were never ported and there is no
6 // assembler pass for them, so every binary whose closure reached crc32 died at
7 // link with "undefined symbol: hash/crc32.castagnoliSSE42" - which is exactly
8 // where src/net/http stopped once its codegen blockers were gone.
9 //
10 // A CRC is a function of its input, not of the instruction that computed it, so
11 // the same update is now written in Moxie (crcRawUpdate, the reflected register
12 // update with no inversion - the form the hardware kernels have and the form
13 // the callers expect, since they invert themselves).
14 //
15 // This pins both halves: the standard check values, and agreement between the
16 // arch path and the generic table path for lengths that cross every internal
17 // threshold. Update() picks the arch kernels only for the instance's OWN
18 // tables; a table built by another instance falls through to simpleUpdate, so
19 // the two paths for the same polynomial can be compared directly.
20 package main
21 22 import "hash/crc32"
23 24 func fill(n int32) (b []byte) {
25 b = []byte{:n}
26 for i := int32(0); i < n; i++ {
27 b[i] = byte((i*7 + 13) & 0xff)
28 }
29 return b
30 }
31 32 func agree(n int32, c1, c2 *crc32.CRC32) {
33 data := fill(n)
34 ieeeArch := c1.Update(0, c1.IEEETable(), data)
35 ieeeGen := c1.Update(0, c2.IEEETable(), data)
36 castArch := c1.Update(0, c1.MakeTable(crc32.Castagnoli), data)
37 castGen := c1.Update(0, c2.MakeTable(crc32.Castagnoli), data)
38 if ieeeArch != ieeeGen {
39 panic("ieee arch != generic")
40 }
41 if castArch != castGen {
42 panic("castagnoli arch != generic")
43 }
44 if n == 10000 {
45 // Fixed by an independent (non-Moxie) oracle over the same pattern.
46 if ieeeArch != 1494190728 {
47 panic("ieee 10000 value")
48 }
49 if castArch != 369340837 {
50 panic("castagnoli 10000 value")
51 }
52 }
53 }
54 55 func main() {
56 // Termination: a batch program must still be stoppable, so the entry
57 // handles the channel the runtime closes on SIGINT/SIGTERM.
58 select {
59 case <-shutdown:
60 return
61 default:
62 }
63 64 c1 := crc32.NewCRC32()
65 c2 := crc32.NewCRC32()
66 67 // The standard check value for "123456789".
68 if c1.ChecksumIEEE([]byte("123456789")) != 0xCBF43926 {
69 panic("ieee check value")
70 }
71 if c1.Update(0, c1.MakeTable(crc32.Castagnoli), []byte("123456789")) != 0xE3069283 {
72 panic("castagnoli check value")
73 }
74 75 // 504 crosses castagnoliK1*3, 4032 crosses castagnoliK2*3, 64 crosses the
76 // IEEE CLMUL threshold, and the odd sizes leave a tail for the single-stream
77 // update after the triple loop.
78 agree(9, c1, c2)
79 agree(63, c1, c2)
80 agree(64, c1, c2)
81 agree(503, c1, c2)
82 agree(504, c1, c2)
83 agree(505, c1, c2)
84 agree(4031, c1, c2)
85 agree(4032, c1, c2)
86 agree(4033, c1, c2)
87 agree(10000, c1, c2)
88 89 println("ok")
90 }
91