]> git.proxmox.com Git - rustc.git/blob - src/vendor/dtoa/src/lib.rs
New upstream version 1.17.0+dfsg1
[rustc.git] / src / vendor / dtoa / src / lib.rs
1 // Copyright 2016 Dtoa Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 #[macro_use] mod diyfp;
10 #[macro_use] mod dtoa;
11
12 use std::{io, mem, ops, ptr, slice};
13
14 #[inline]
15 pub fn write<W: io::Write, V: Floating>(wr: W, value: V) -> io::Result<usize> {
16 value.write(wr)
17 }
18
19 pub trait Floating {
20 fn write<W: io::Write>(self, W) -> io::Result<usize>;
21 }
22
23 impl Floating for f32 {
24 fn write<W: io::Write>(self, wr: W) -> io::Result<usize> {
25 dtoa! {
26 floating_type: f32,
27 significand_type: u32,
28 exponent_type: i32,
29
30 diy_significand_size: 32,
31 significand_size: 23,
32 exponent_bias: 0x7F,
33 mask_type: u32,
34 exponent_mask: 0x7F800000,
35 significand_mask: 0x007FFFFF,
36 hidden_bit: 0x00800000,
37 cached_powers_f: CACHED_POWERS_F_32,
38 cached_powers_e: CACHED_POWERS_E_32,
39 min_power: (-36),
40 };
41 unsafe { dtoa(wr, self) }
42 }
43 }
44
45 impl Floating for f64 {
46 fn write<W: io::Write>(self, wr: W) -> io::Result<usize> {
47 dtoa! {
48 floating_type: f64,
49 significand_type: u64,
50 exponent_type: isize,
51
52 diy_significand_size: 64,
53 significand_size: 52,
54 exponent_bias: 0x3FF,
55 mask_type: u64,
56 exponent_mask: 0x7FF0000000000000,
57 significand_mask: 0x000FFFFFFFFFFFFF,
58 hidden_bit: 0x0010000000000000,
59 cached_powers_f: CACHED_POWERS_F_64,
60 cached_powers_e: CACHED_POWERS_E_64,
61 min_power: (-348),
62 };
63 unsafe { dtoa(wr, self) }
64 }
65 }
66
67 ////////////////////////////////////////////////////////////////////////////////
68
69 const MAX_DECIMAL_PLACES: isize = 324;
70
71 static DEC_DIGITS_LUT: &'static [u8] =
72 b"0001020304050607080910111213141516171819\
73 2021222324252627282930313233343536373839\
74 4041424344454647484950515253545556575859\
75 6061626364656667686970717273747576777879\
76 8081828384858687888990919293949596979899";
77
78 // 10^-36, 10^-28, ..., 10^52
79 static CACHED_POWERS_F_32: [u32; 12] = [
80 0xaa242499, 0xfd87b5f3, 0xbce50865, 0x8cbccc09,
81 0xd1b71759, 0x9c400000, 0xe8d4a510, 0xad78ebc6,
82 0x813f3979, 0xc097ce7c, 0x8f7e32ce, 0xd5d238a5,
83 ];
84
85 static CACHED_POWERS_E_32: [i16; 12] = [
86 -151, -125, -98, -71, -45, -18, 8, 35, 62, 88, 115, 141,
87 ];
88
89 // 10^-348, 10^-340, ..., 10^340
90 static CACHED_POWERS_F_64: [u64; 87] = [
91 0xfa8fd5a0081c0288, 0xbaaee17fa23ebf76,
92 0x8b16fb203055ac76, 0xcf42894a5dce35ea,
93 0x9a6bb0aa55653b2d, 0xe61acf033d1a45df,
94 0xab70fe17c79ac6ca, 0xff77b1fcbebcdc4f,
95 0xbe5691ef416bd60c, 0x8dd01fad907ffc3c,
96 0xd3515c2831559a83, 0x9d71ac8fada6c9b5,
97 0xea9c227723ee8bcb, 0xaecc49914078536d,
98 0x823c12795db6ce57, 0xc21094364dfb5637,
99 0x9096ea6f3848984f, 0xd77485cb25823ac7,
100 0xa086cfcd97bf97f4, 0xef340a98172aace5,
101 0xb23867fb2a35b28e, 0x84c8d4dfd2c63f3b,
102 0xc5dd44271ad3cdba, 0x936b9fcebb25c996,
103 0xdbac6c247d62a584, 0xa3ab66580d5fdaf6,
104 0xf3e2f893dec3f126, 0xb5b5ada8aaff80b8,
105 0x87625f056c7c4a8b, 0xc9bcff6034c13053,
106 0x964e858c91ba2655, 0xdff9772470297ebd,
107 0xa6dfbd9fb8e5b88f, 0xf8a95fcf88747d94,
108 0xb94470938fa89bcf, 0x8a08f0f8bf0f156b,
109 0xcdb02555653131b6, 0x993fe2c6d07b7fac,
110 0xe45c10c42a2b3b06, 0xaa242499697392d3,
111 0xfd87b5f28300ca0e, 0xbce5086492111aeb,
112 0x8cbccc096f5088cc, 0xd1b71758e219652c,
113 0x9c40000000000000, 0xe8d4a51000000000,
114 0xad78ebc5ac620000, 0x813f3978f8940984,
115 0xc097ce7bc90715b3, 0x8f7e32ce7bea5c70,
116 0xd5d238a4abe98068, 0x9f4f2726179a2245,
117 0xed63a231d4c4fb27, 0xb0de65388cc8ada8,
118 0x83c7088e1aab65db, 0xc45d1df942711d9a,
119 0x924d692ca61be758, 0xda01ee641a708dea,
120 0xa26da3999aef774a, 0xf209787bb47d6b85,
121 0xb454e4a179dd1877, 0x865b86925b9bc5c2,
122 0xc83553c5c8965d3d, 0x952ab45cfa97a0b3,
123 0xde469fbd99a05fe3, 0xa59bc234db398c25,
124 0xf6c69a72a3989f5c, 0xb7dcbf5354e9bece,
125 0x88fcf317f22241e2, 0xcc20ce9bd35c78a5,
126 0x98165af37b2153df, 0xe2a0b5dc971f303a,
127 0xa8d9d1535ce3b396, 0xfb9b7cd9a4a7443c,
128 0xbb764c4ca7a44410, 0x8bab8eefb6409c1a,
129 0xd01fef10a657842c, 0x9b10a4e5e9913129,
130 0xe7109bfba19c0c9d, 0xac2820d9623bf429,
131 0x80444b5e7aa7cf85, 0xbf21e44003acdd2d,
132 0x8e679c2f5e44ff8f, 0xd433179d9c8cb841,
133 0x9e19db92b4e31ba9, 0xeb96bf6ebadf77d9,
134 0xaf87023b9bf0ee6b,
135 ];
136 static CACHED_POWERS_E_64: [i16; 87] = [
137 -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980,
138 -954, -927, -901, -874, -847, -821, -794, -768, -741, -715,
139 -688, -661, -635, -608, -582, -555, -529, -502, -475, -449,
140 -422, -396, -369, -343, -316, -289, -263, -236, -210, -183,
141 -157, -130, -103, -77, -50, -24, 3, 30, 56, 83,
142 109, 136, 162, 189, 216, 242, 269, 295, 322, 348,
143 375, 402, 428, 455, 481, 508, 534, 561, 588, 614,
144 641, 667, 694, 720, 747, 774, 800, 827, 853, 880,
145 907, 933, 960, 986, 1013, 1039, 1066,
146 ];