]>
git.proxmox.com Git - rustc.git/blob - src/etc/dec2flt_table.py
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 fractions
import Fraction
29 from collections
import namedtuple
32 N
= 64 # Size of the significand field in bits
33 MIN_SIG
= 2 ** (N
- 1)
34 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.
110 e_range
= range(MIN_E
, MAX_E
+1
)
113 z
= algorithm_m(1, e
)
117 typ
= "([u64; {0}], [i16; {0}])".format(len(e_range
))
118 print(LICENSE
.strip())
119 print("// Table of approximations of powers of ten.")
120 print("// DO NOT MODIFY: Generated by a src/etc/dec2flt_table.py")
121 print("pub const MIN_E: i16 = {};".format(MIN_E
))
122 print("pub const MAX_E: i16 = {};".format(MAX_E
))
124 print("pub const POWERS: ", typ
, " = ([", sep
='')
126 print(" 0x{:x},".format(z
.sig
))
129 print(" {},".format(z
.exp
))
133 if __name__
== '__main__':