blob: 61730a4292ea780f91560ac344947d26e8559c14 [file] [log] [blame]
Nigel Tao79a94552017-11-30 16:37:20 +11001// Copyright 2017 The Wuffs Authors.
Nigel Taod4372cb2017-10-12 11:17:41 +11002//
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 Tao89d4f682017-07-27 17:20:31 +100014
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Tao89d4f682017-07-27 17:20:31 +100016// +build ignore
17
18package main
19
Nigel Tao802b9d82017-12-04 10:43:32 +110020// print-deflate-magic-numbers.go prints the std/deflate lcode_magic_numbers
Nigel Tao4675efd2018-03-23 12:40:28 +110021// and dcode_magic_numbers values based on the tables in RFC 1951 secion 3.2.5.
22//
Nigel Tao3e703832018-12-01 10:18:15 +110023// 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 Tao4675efd2018-03-23 12:40:28 +110028// The dcode base numbers are biased by -1 so that (base_number_minus_1 +
Nigel Tao3e703832018-12-01 10:18:15 +110029// extra_bits) fits in the range [0, 32767]. This makes a bitwise-and with
Nigel Tao4675efd2018-03-23 12:40:28 +110030// 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 Tao89d4f682017-07-27 17:20:31 +100034//
Nigel Tao802b9d82017-12-04 10:43:32 +110035// Usage: go run print-deflate-magic-numbers.go
Nigel Tao89d4f682017-07-27 17:20:31 +100036
37import (
38 "fmt"
39 "os"
40)
41
42func main() {
43 if err := main1(); err != nil {
44 os.Stderr.WriteString(err.Error() + "\n")
45 os.Exit(1)
46 }
47}
48
49func main1() error {
Nigel Tao3e703832018-12-01 10:18:15 +110050 // See the "base numbers" comment above.
51 biases := [2]uint32{3, 1}
52
Nigel Tao89d4f682017-07-27 17:20:31 +100053 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 Tao4675efd2018-03-23 12:40:28 +110059 if bn := baseNumbers[i][j]; bn != bad {
Nigel Tao3e703832018-12-01 10:18:15 +110060 x = 0x40000000 | ((bn - biases[i]) << 8) | (extraBits[i][j] << 4)
Nigel Tao89d4f682017-07-27 17:20:31 +100061 }
62 fmt.Printf("0x%08X,", x)
63 if j&7 == 7 {
64 fmt.Println()
65 }
66 }
67 }
68 return nil
69}
70
71const bad = 0xFFFFFFFF
72
73var (
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)