1 #!/usr/bin/env python2.7
3 # Copyright 2015 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
14 Generate powers of ten using William Clinger's ``AlgorithmM`` for use in
15 decimal to floating point conversions.
17 Specifically, computes and outputs (as Rust code) a table of 10^e for some
18 range of exponents e. The output is one array of 64 bit significands and
19 another array of corresponding base two exponents. The approximations are
20 normalized and rounded perfectly, i.e., within 0.5 ULP of the true value.
22 The representation ([u64], [i16]) instead of the more natural [(u64, i16)]
23 is used because (u64, i16) has a ton of padding which would make the table
24 even larger, and it's already uncomfortably large (6 KiB).
26 from __future__ import print_function
28 from math import ceil, log
29 from fractions import Fraction
30 from collections import namedtuple
33 N = 64 # Size of the significand field in bits
34 MIN_SIG = 2 ** (N - 1)
35 MAX_SIG = (2 ** N) - 1
37 # Hand-rolled fp representation without arithmetic or any other operations.
38 # The significand is normalized and always N bit, but the exponent is
39 # unrestricted in range.
40 Fp = namedtuple('Fp', 'sig exp')
43 def algorithm_m(f, e):
63 return ratio_to_float(u, v, k)
66 def ratio_to_float(u, v, k):
82 return Fp(MIN_SIG, z.exp + 1)
84 return Fp(z.sig + 1, z.exp)
88 decimal = f * Fraction(10) ** e
89 binary = z.sig * Fraction(2) ** z.exp
90 abs_err = abs(decimal - binary)
91 # The unit in the last place has value z.exp
92 ulp_err = abs_err / Fraction(2) ** z.exp
96 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
97 // file at the top-level directory of this distribution and at
98 // http://rust-lang.org/COPYRIGHT.
100 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
101 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
102 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
103 // option. This file may not be copied, modified, or distributed
104 // except according to those terms.
106 //! Tables of approximations of powers of ten.
107 //! DO NOT MODIFY: Generated by `src/etc/dec2flt_table.py`
112 print(HEADER.strip())
114 print_proper_powers()
116 print_short_powers(32, 24)
118 print_short_powers(64, 53)
121 def print_proper_powers():
124 e_range = range(MIN_E, MAX_E+1)
127 z = algorithm_m(1, e)
131 print("pub const MIN_E: i16 = {};".format(MIN_E))
132 print("pub const MAX_E: i16 = {};".format(MAX_E))
134 typ = "([u64; {0}], [i16; {0}])".format(len(powers))
135 print("pub const POWERS: ", typ, " = ([", sep='')
137 print(" 0x{:x},".format(z.sig))
140 print(" {},".format(z.exp))
144 def print_short_powers(num_bits, significand_size):
145 max_sig = 2**significand_size - 1
146 # The fast path bails out for exponents >= ceil(log5(max_sig))
147 max_e = int(ceil(log(max_sig, 5)))
148 e_range = range(max_e)
149 typ = "[f{}; {}]".format(num_bits, len(e_range))
150 print("pub const F", num_bits, "_SHORT_POWERS: ", typ, " = [", sep='')
152 print(" 1e{},".format(e))
156 if __name__ == '__main__':