]> git.proxmox.com Git - rustc.git/blobdiff - src/etc/dec2flt_table.py
Imported Upstream version 1.7.0+dfsg1
[rustc.git] / src / etc / dec2flt_table.py
index b0140fb24559dc0eca78542ec0b5c9ee6b8ff652..9fdab1fcfca28a33cd35d67731af7a28b40f0f72 100644 (file)
@@ -25,6 +25,7 @@ even larger, and it's already uncomfortably large (6 KiB).
 """
 from __future__ import print_function
 import sys
+from math import ceil, log
 from fractions import Fraction
 from collections import namedtuple
 
@@ -33,7 +34,6 @@ N = 64  # Size of the significand field in bits
 MIN_SIG = 2 ** (N - 1)
 MAX_SIG = (2 ** N) - 1
 
-
 # Hand-rolled fp representation without arithmetic or any other operations.
 # The significand is normalized and always N bit, but the exponent is
 # unrestricted in range.
@@ -92,7 +92,7 @@ def error(f, e, z):
     ulp_err = abs_err / Fraction(2) ** z.exp
     return float(ulp_err)
 
-LICENSE = """
+HEADER = """
 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
 // file at the top-level directory of this distribution and at
 // http://rust-lang.org/COPYRIGHT.
@@ -102,9 +102,23 @@ LICENSE = """
 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
+
+//! Tables of approximations of powers of ten.
+//! DO NOT MODIFY: Generated by `src/etc/dec2flt_table.py`
 """
 
+
 def main():
+    print(HEADER.strip())
+    print()
+    print_proper_powers()
+    print()
+    print_short_powers(32, 24)
+    print()
+    print_short_powers(64, 53)
+
+
+def print_proper_powers():
     MIN_E = -305
     MAX_E = 305
     e_range = range(MIN_E, MAX_E+1)
@@ -114,13 +128,10 @@ def main():
         err = error(1, e, z)
         assert err < 0.5
         powers.append(z)
-    typ = "([u64; {0}], [i16; {0}])".format(len(e_range))
-    print(LICENSE.strip())
-    print("// Table of approximations of powers of ten.")
-    print("// DO NOT MODIFY: Generated by a src/etc/dec2flt_table.py")
     print("pub const MIN_E: i16 = {};".format(MIN_E))
     print("pub const MAX_E: i16 = {};".format(MAX_E))
     print()
+    typ = "([u64; {0}], [i16; {0}])".format(len(powers))
     print("pub const POWERS: ", typ, " = ([", sep='')
     for z in powers:
         print("    0x{:x},".format(z.sig))
@@ -130,5 +141,17 @@ def main():
     print("]);")
 
 
+def print_short_powers(num_bits, significand_size):
+    max_sig = 2**significand_size - 1
+    # The fast path bails out for exponents >= ceil(log5(max_sig))
+    max_e = int(ceil(log(max_sig, 5)))
+    e_range = range(max_e)
+    typ = "[f{}; {}]".format(num_bits, len(e_range))
+    print("pub const F", num_bits, "_SHORT_POWERS: ", typ, " = [", sep='')
+    for e in e_range:
+        print("    1e{},".format(e))
+    print("];")
+
+
 if __name__ == '__main__':
     main()