fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
3.3 KiB109 linesraw
1#!/usr/bin/env python3
2
3"""
4Generate powers of five using Daniel Lemire's ``Eisel-Lemire algorithm`` for use in
5decimal to floating point conversions.
6
7Specifically, computes and outputs (as Rust code) a table of 10^e for some
8range of exponents e. The output is one array of 128 bit significands.
9The base two exponents can be inferred using a logarithmic slope
10of the decimal exponent. The approximations are normalized and rounded perfectly,
11i.e., within 0.5 ULP of the true value.
12
13Adapted from Daniel Lemire's fast_float ``table_generation.py``,
14available here: <https://github.com/fastfloat/fast_float/blob/main/script/table_generation.py>.
15"""
16from __future__ import print_function
17from math import ceil, floor, log
18from collections import deque
19
20HEADER = """
21//! Pre-computed tables powers-of-5 for extended-precision representations.
22//!
23//! These tables enable fast scaling of the significant digits
24//! of a float to the decimal exponent, with minimal rounding
25//! errors, in a 128 or 192-bit representation.
26//!
27//! DO NOT MODIFY: Generated by `src/etc/dec2flt_table.py`
28"""
29
30STATIC_WARNING = """
31// Use static to avoid long compile times: Rust compiler errors
32// can have the entire table compiled multiple times, and then
33// emit code multiple times, even if it's stripped out in
34// the final binary.
35"""
36
37def main():
38    min_exp = minimum_exponent(10)
39    max_exp = maximum_exponent(10)
40    bias = -minimum_exponent(5)
41
42    print(HEADER.strip())
43    print()
44    print('pub const SMALLEST_POWER_OF_FIVE: i32 = {};'.format(min_exp))
45    print('pub const LARGEST_POWER_OF_FIVE: i32 = {};'.format(max_exp))
46    print('pub const N_POWERS_OF_FIVE: usize = ', end='')
47    print('(LARGEST_POWER_OF_FIVE - SMALLEST_POWER_OF_FIVE + 1) as usize;')
48    print()
49    print_proper_powers(min_exp, max_exp, bias)
50
51
52def minimum_exponent(base):
53    return ceil(log(5e-324, base) - log(0xFFFFFFFFFFFFFFFF, base))
54
55
56def maximum_exponent(base):
57    return floor(log(1.7976931348623157e+308, base))
58
59
60def print_proper_powers(min_exp, max_exp, bias):
61    powers = deque()
62
63    # Add negative exponents.
64    # 2^(2b)/(5^−q) with b=64 + int(math.ceil(log2(5^−q)))
65    powers = []
66    for q in range(min_exp, 0):
67        power5 = 5 ** -q
68        z = 0
69        while (1 << z) < power5:
70            z += 1
71        if q >= -27:
72            b = z + 127
73            c = 2 ** b // power5 + 1
74            powers.append((c, q))
75        else:
76            b = 2 * z + 2 * 64
77            c = 2 ** b // power5 + 1
78            # truncate
79            while c >= (1<<128):
80                c //= 2
81            powers.append((c, q))
82
83    # Add positive exponents
84    for q in range(0, max_exp + 1):
85        power5 = 5 ** q
86        # move the most significant bit in position
87        while power5 < (1<<127):
88            power5 *= 2
89        # *truncate*
90        while power5 >= (1<<128):
91            power5 //= 2
92        powers.append((power5, q))
93
94    # Print the powers.
95    print(STATIC_WARNING.strip())
96    print('#[rustfmt::skip]')
97    typ = '[(u64, u64); N_POWERS_OF_FIVE]'
98    print('pub static POWER_OF_FIVE_128: {} = ['.format(typ))
99    for c, exp in powers:
100        hi = '0x{:x}'.format(c // (1 << 64))
101        lo = '0x{:x}'.format(c % (1 << 64))
102        value = '    ({}, {}), '.format(hi, lo)
103        comment = '// {}^{}'.format(5, exp)
104        print(value.ljust(46, ' ') + comment)
105    print('];')
106
107
108if __name__ == '__main__':
109    main()