Nigel Tao | 79a9455 | 2017-11-30 16:37:20 +1100 | [diff] [blame] | 1 | // Copyright 2017 The Wuffs Authors. |
Nigel Tao | d4372cb | 2017-10-12 11:17:41 +1100 | [diff] [blame] | 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. |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 14 | |
Nigel Tao | 788479d | 2021-08-22 10:52:51 +1000 | [diff] [blame] | 15 | //go:build ignore |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 16 | // +build ignore |
| 17 | |
| 18 | package main |
| 19 | |
Nigel Tao | 802b9d8 | 2017-12-04 10:43:32 +1100 | [diff] [blame] | 20 | // print-deflate-magic-numbers.go prints the std/deflate lcode_magic_numbers |
Nigel Tao | 4675efd | 2018-03-23 12:40:28 +1100 | [diff] [blame] | 21 | // and dcode_magic_numbers values based on the tables in RFC 1951 secion 3.2.5. |
| 22 | // |
Nigel Tao | 3e70383 | 2018-12-01 10:18:15 +1100 | [diff] [blame] | 23 | // The lcode base numbers are biased by -3 so that (base_number_minus_3 + |
| 24 | // extra_bits) fits in the range [0, 255]. This makes a bitwise-and with 0xFF a |
| 25 | // no-op, in terms of computed value, but proves to the compiler that the |
| 26 | // result is within a certain range. |
| 27 | // |
Nigel Tao | 4675efd | 2018-03-23 12:40:28 +1100 | [diff] [blame] | 28 | // The dcode base numbers are biased by -1 so that (base_number_minus_1 + |
Nigel Tao | 3e70383 | 2018-12-01 10:18:15 +1100 | [diff] [blame] | 29 | // extra_bits) fits in the range [0, 32767]. This makes a bitwise-and with |
Nigel Tao | 4675efd | 2018-03-23 12:40:28 +1100 | [diff] [blame] | 30 | // 0x7FFF a no-op, in terms of computed value, but proves to the compiler that |
| 31 | // the result is within a certain range. Furthermore, proving that (d + 1) > 0 |
| 32 | // is trivial, for d of type u32[..something], compared to proving that d > 0, |
| 33 | // which usually requires a runtime check (an if branch). |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 34 | // |
Nigel Tao | 802b9d8 | 2017-12-04 10:43:32 +1100 | [diff] [blame] | 35 | // Usage: go run print-deflate-magic-numbers.go |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 36 | |
| 37 | import ( |
| 38 | "fmt" |
| 39 | "os" |
| 40 | ) |
| 41 | |
| 42 | func main() { |
| 43 | if err := main1(); err != nil { |
| 44 | os.Stderr.WriteString(err.Error() + "\n") |
| 45 | os.Exit(1) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func main1() error { |
Nigel Tao | 3e70383 | 2018-12-01 10:18:15 +1100 | [diff] [blame] | 50 | // See the "base numbers" comment above. |
| 51 | biases := [2]uint32{3, 1} |
| 52 | |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 53 | for i := 0; i < 2; i++ { |
| 54 | if i != 0 { |
| 55 | fmt.Println() |
| 56 | } |
| 57 | for j := 0; j < 32; j++ { |
| 58 | x := uint32(0x08000000) |
Nigel Tao | 4675efd | 2018-03-23 12:40:28 +1100 | [diff] [blame] | 59 | if bn := baseNumbers[i][j]; bn != bad { |
Nigel Tao | 3e70383 | 2018-12-01 10:18:15 +1100 | [diff] [blame] | 60 | x = 0x40000000 | ((bn - biases[i]) << 8) | (extraBits[i][j] << 4) |
Nigel Tao | 89d4f68 | 2017-07-27 17:20:31 +1000 | [diff] [blame] | 61 | } |
| 62 | fmt.Printf("0x%08X,", x) |
| 63 | if j&7 == 7 { |
| 64 | fmt.Println() |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | const bad = 0xFFFFFFFF |
| 72 | |
| 73 | var ( |
| 74 | baseNumbers = [2][32]uint32{{ |
| 75 | 3, 4, 5, 6, 7, 8, 9, 10, |
| 76 | 11, 13, 15, 17, 19, 23, 27, 31, |
| 77 | 35, 43, 51, 59, 67, 83, 99, 115, |
| 78 | 131, 163, 195, 227, 258, bad, bad, bad, |
| 79 | }, { |
| 80 | 1, 2, 3, 4, 5, 7, 9, 13, |
| 81 | 17, 25, 33, 49, 65, 97, 129, 193, |
| 82 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, |
| 83 | 4097, 6145, 8193, 12289, 16385, 24577, bad, bad, |
| 84 | }} |
| 85 | |
| 86 | extraBits = [2][32]uint32{{ |
| 87 | 0, 0, 0, 0, 0, 0, 0, 0, |
| 88 | 1, 1, 1, 1, 2, 2, 2, 2, |
| 89 | 3, 3, 3, 3, 4, 4, 4, 4, |
| 90 | 5, 5, 5, 5, 0, bad, bad, bad, |
| 91 | }, { |
| 92 | 0, 0, 0, 0, 1, 1, 2, 2, |
| 93 | 3, 3, 4, 4, 5, 5, 6, 6, |
| 94 | 7, 7, 8, 8, 9, 9, 10, 10, |
| 95 | 11, 11, 12, 12, 13, 13, bad, bad, |
| 96 | }} |
| 97 | ) |