Nigel Tao | 3a7ccfe | 2018-03-25 18:09:51 +1100 | [diff] [blame] | 1 | // Copyright 2018 The Wuffs Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Nigel Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | 3a7ccfe | 2018-03-25 18:09:51 +1100 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | // print-crc32-magic-numbers.go prints the std/crc32 magic number tables. |
| 21 | // |
| 22 | // Usage: go run print-crc32-magic-numbers.go |
| 23 | |
| 24 | import ( |
| 25 | "fmt" |
| 26 | "hash/crc32" |
| 27 | "os" |
| 28 | ) |
| 29 | |
| 30 | func main() { |
| 31 | if err := main1(); err != nil { |
| 32 | os.Stderr.WriteString(err.Error() + "\n") |
| 33 | os.Exit(1) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func main1() error { |
Nigel Tao | 99597e1 | 2018-10-07 21:01:18 +1100 | [diff] [blame] | 38 | tables := [16]crc32.Table{} |
Nigel Tao | a106e2a | 2018-05-03 13:14:12 +1000 | [diff] [blame] | 39 | tables[0] = *crc32.MakeTable(crc32.IEEE) |
| 40 | |
Nigel Tao | 34fe051 | 2018-11-11 10:25:19 +1100 | [diff] [blame] | 41 | // See "Multi-Byte Lookup Tables" in std/crc32/README.md for more detail on |
| 42 | // the slicing-by-M algorithm. We use an M of 16. |
Nigel Tao | a106e2a | 2018-05-03 13:14:12 +1000 | [diff] [blame] | 43 | for i := 0; i < 256; i++ { |
| 44 | crc := tables[0][i] |
Nigel Tao | 99597e1 | 2018-10-07 21:01:18 +1100 | [diff] [blame] | 45 | for j := 1; j < 16; j++ { |
Nigel Tao | a106e2a | 2018-05-03 13:14:12 +1000 | [diff] [blame] | 46 | crc = tables[0][crc&0xFF] ^ (crc >> 8) |
| 47 | tables[j][i] = crc |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | for i, t := range tables { |
| 52 | if i != 0 { |
Nigel Tao | 99597e1 | 2018-10-07 21:01:18 +1100 | [diff] [blame] | 53 | fmt.Println("],[") |
Nigel Tao | 3a7ccfe | 2018-03-25 18:09:51 +1100 | [diff] [blame] | 54 | } |
Nigel Tao | a106e2a | 2018-05-03 13:14:12 +1000 | [diff] [blame] | 55 | for j, x := range t { |
| 56 | fmt.Printf("0x%08X,", x) |
| 57 | if j&7 == 7 { |
| 58 | fmt.Println() |
| 59 | } |
| 60 | } |
Nigel Tao | 3a7ccfe | 2018-03-25 18:09:51 +1100 | [diff] [blame] | 61 | } |
| 62 | return nil |
| 63 | } |