core/num/imp/flt2dec/strategy/
dragon.rs1use flt2dec::estimator::estimate_scaling_factor;
8use flt2dec::{Decoded, MAX_SIG_DIGITS, round_up};
9
10use crate::cmp::Ordering;
11use crate::mem::MaybeUninit;
12use crate::num::imp::bignum::{Big, Digit};
13use crate::num::imp::flt2dec;
14
15static POW10: [Digit; 10] =
16 [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000];
17#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
20static POW5TO16: [Digit; 2] = [0x86f26fc1, 0x23];
21#[cfg(target_pointer_width = "64")]
22static POW5TO16: [Digit; 1] = [0x2386f26fc1];
23
24#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
25static POW5TO32: [Digit; 3] = [0x85acef81, 0x2d6d415b, 0x4ee];
26#[cfg(target_pointer_width = "64")]
27static POW5TO32: [Digit; 2] = [0x2d6d415b85acef81, 0x4ee];
28
29#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
30static POW5TO64: [Digit; 5] = [0xbf6a1f01, 0x6e38ed64, 0xdaa797ed, 0xe93ff9f4, 0x184f03];
31#[cfg(target_pointer_width = "64")]
32static POW5TO64: [Digit; 3] = [0x6e38ed64bf6a1f01, 0xe93ff9f4daa797ed, 0x184f03];
33
34#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
35static POW5TO128: [Digit; 10] = [
36 0x2e953e01, 0x3df9909, 0xf1538fd, 0x2374e42f, 0xd3cff5ec, 0xc404dc08, 0xbccdb0da, 0xa6337f19,
37 0xe91f2603, 0x24e,
38];
39#[cfg(target_pointer_width = "64")]
40static POW5TO128: [Digit; 5] =
41 [0x3df99092e953e01, 0x2374e42f0f1538fd, 0xc404dc08d3cff5ec, 0xa6337f19bccdb0da, 0x24ee91f2603];
42
43#[cfg(any(target_pointer_width = "16", target_pointer_width = "32"))]
44static POW5TO256: [Digit; 19] = [
45 0x982e7c01, 0xbed3875b, 0xd8d99f72, 0x12152f87, 0x6bde50c6, 0xcf4a6e70, 0xd595d80f, 0x26b2716e,
46 0xadc666b0, 0x1d153624, 0x3c42d35a, 0x63ff540e, 0xcc5573c0, 0x65f9ef17, 0x55bc28f2, 0x80dcc7f7,
47 0xf46eeddc, 0x5fdcefce, 0x553f7,
48];
49#[cfg(target_pointer_width = "64")]
50static POW5TO256: [Digit; 10] = [
51 0xbed3875b982e7c01,
52 0x12152f87d8d99f72,
53 0xcf4a6e706bde50c6,
54 0x26b2716ed595d80f,
55 0x1d153624adc666b0,
56 0x63ff540e3c42d35a,
57 0x65f9ef17cc5573c0,
58 0x80dcc7f755bc28f2,
59 0x5fdcefcef46eeddc,
60 0x553f7,
61];
62
63#[doc(hidden)]
64pub fn mul_pow10(x: &mut Big, n: usize) -> &mut Big {
65 if true {
if !(n < 512) { crate::panicking::panic("assertion failed: n < 512") };
};debug_assert!(n < 512);
66 if n < 8 {
68 return x.mul_small(POW10[n & 7]);
69 }
70 if n & 7 != 0 {
73 x.mul_small(POW10[n & 7] >> (n & 7));
74 }
75 if n & 8 != 0 {
76 x.mul_small(POW10[8] >> 8);
77 }
78 if n & 16 != 0 {
79 x.mul_digits(&POW5TO16);
80 }
81 if n & 32 != 0 {
82 x.mul_digits(&POW5TO32);
83 }
84 if n & 64 != 0 {
85 x.mul_digits(&POW5TO64);
86 }
87 if n & 128 != 0 {
88 x.mul_digits(&POW5TO128);
89 }
90 if n & 256 != 0 {
91 x.mul_digits(&POW5TO256);
92 }
93 x.mul_pow2(n)
94}
95
96fn div_2pow10(x: &mut Big, mut n: usize) -> &mut Big {
97 let largest = POW10.len() - 1;
98 while n > largest {
99 x.div_rem_small(POW10[largest]);
100 n -= largest;
101 }
102 x.div_rem_small(POW10[n] << 1);
103 x
104}
105
106fn div_rem_upto_16<'a>(
108 x: &'a mut Big,
109 scale: &Big,
110 scale2: &Big,
111 scale4: &Big,
112 scale8: &Big,
113) -> (u8, &'a mut Big) {
114 let mut d = 0;
115 if *x >= *scale8 {
116 x.sub(scale8);
117 d += 8;
118 }
119 if *x >= *scale4 {
120 x.sub(scale4);
121 d += 4;
122 }
123 if *x >= *scale2 {
124 x.sub(scale2);
125 d += 2;
126 }
127 if *x >= *scale {
128 x.sub(scale);
129 d += 1;
130 }
131 if true {
if !(*x < *scale) {
crate::panicking::panic("assertion failed: *x < *scale")
};
};debug_assert!(*x < *scale);
132 (d, x)
133}
134
135pub fn format_shortest<'a>(
137 d: &Decoded,
138 buf: &'a mut [MaybeUninit<u8>],
139) -> (&'a [u8], i16) {
140 if !(d.mant > 0) { crate::panicking::panic("assertion failed: d.mant > 0") };assert!(d.mant > 0);
153 if !(d.minus > 0) {
crate::panicking::panic("assertion failed: d.minus > 0")
};assert!(d.minus > 0);
154 if !(d.plus > 0) { crate::panicking::panic("assertion failed: d.plus > 0") };assert!(d.plus > 0);
155 if !d.mant.checked_add(d.plus).is_some() {
crate::panicking::panic("assertion failed: d.mant.checked_add(d.plus).is_some()")
};assert!(d.mant.checked_add(d.plus).is_some());
156 if !d.mant.checked_sub(d.minus).is_some() {
crate::panicking::panic("assertion failed: d.mant.checked_sub(d.minus).is_some()")
};assert!(d.mant.checked_sub(d.minus).is_some());
157 if !(buf.len() >= MAX_SIG_DIGITS) {
crate::panicking::panic("assertion failed: buf.len() >= MAX_SIG_DIGITS")
};assert!(buf.len() >= MAX_SIG_DIGITS);
158
159 let rounding = if d.inclusive { Ordering::Greater } else { Ordering::Equal };
161
162 let mut k = estimate_scaling_factor(d.mant + d.plus, d.exp);
165
166 let mut mant = Big::from_u64(d.mant);
171 let mut minus = Big::from_u64(d.minus);
172 let mut plus = Big::from_u64(d.plus);
173 let mut scale = Big::from_small(1);
174 if d.exp < 0 {
175 scale.mul_pow2(-d.exp as usize);
176 } else {
177 mant.mul_pow2(d.exp as usize);
178 minus.mul_pow2(d.exp as usize);
179 plus.mul_pow2(d.exp as usize);
180 }
181
182 if k >= 0 {
184 mul_pow10(&mut scale, k as usize);
185 } else {
186 mul_pow10(&mut mant, -k as usize);
187 mul_pow10(&mut minus, -k as usize);
188 mul_pow10(&mut plus, -k as usize);
189 }
190
191 if scale.cmp(mant.clone().add(&plus)) < rounding {
198 k += 1;
200 } else {
201 mant.mul_small(10);
202 minus.mul_small(10);
203 plus.mul_small(10);
204 }
205
206 let mut scale2 = scale.clone();
208 scale2.mul_pow2(1);
209 let mut scale4 = scale.clone();
210 scale4.mul_pow2(2);
211 let mut scale8 = scale.clone();
212 scale8.mul_pow2(3);
213
214 let mut down;
215 let mut up;
216 let mut i = 0;
217 loop {
218 let (d, _) = div_rem_upto_16(&mut mant, &scale, &scale2, &scale4, &scale8);
227 if true {
if !(d < 10) { crate::panicking::panic("assertion failed: d < 10") };
};debug_assert!(d < 10);
228 buf[i] = MaybeUninit::new(b'0' + d);
229 i += 1;
230
231 down = mant.cmp(&minus) < rounding;
263 up = scale.cmp(mant.clone().add(&plus)) < rounding;
264 if down || up {
265 break;
266 } mant.mul_small(10);
272 minus.mul_small(10);
273 plus.mul_small(10);
274 }
275
276 if up && (!down || *mant.mul_pow2(1) >= scale) {
280 if let Some(c) = round_up(unsafe { buf[..i].assume_init_mut() }) {
285 buf[i] = MaybeUninit::new(c);
286 i += 1;
287 k += 1;
288 }
289 }
290
291 (unsafe { buf[..i].assume_init_ref() }, k)
293}
294
295pub fn format_exact<'a>(
297 d: &Decoded,
298 buf: &'a mut [MaybeUninit<u8>],
299 limit: i16,
300) -> (&'a [u8], i16) {
301 if !(d.mant > 0) { crate::panicking::panic("assertion failed: d.mant > 0") };assert!(d.mant > 0);
302 if !(d.minus > 0) {
crate::panicking::panic("assertion failed: d.minus > 0")
};assert!(d.minus > 0);
303 if !(d.plus > 0) { crate::panicking::panic("assertion failed: d.plus > 0") };assert!(d.plus > 0);
304 if !d.mant.checked_add(d.plus).is_some() {
crate::panicking::panic("assertion failed: d.mant.checked_add(d.plus).is_some()")
};assert!(d.mant.checked_add(d.plus).is_some());
305 if !d.mant.checked_sub(d.minus).is_some() {
crate::panicking::panic("assertion failed: d.mant.checked_sub(d.minus).is_some()")
};assert!(d.mant.checked_sub(d.minus).is_some());
306
307 let mut k = estimate_scaling_factor(d.mant, d.exp);
309
310 let mut mant = Big::from_u64(d.mant);
312 let mut scale = Big::from_small(1);
313 if d.exp < 0 {
314 scale.mul_pow2(-d.exp as usize);
315 } else {
316 mant.mul_pow2(d.exp as usize);
317 }
318
319 if k >= 0 {
321 mul_pow10(&mut scale, k as usize);
322 } else {
323 mul_pow10(&mut mant, -k as usize);
324 }
325
326 if *div_2pow10(&mut scale.clone(), buf.len()).add(&mant) >= scale {
331 k += 1;
333 } else {
334 mant.mul_small(10);
335 }
336
337 let mut len = if k < limit {
341 0
346 } else if ((k as i32 - limit as i32) as usize) < buf.len() {
347 (k - limit) as usize
348 } else {
349 buf.len()
350 };
351
352 if len > 0 {
353 let mut scale2 = scale.clone();
356 scale2.mul_pow2(1);
357 let mut scale4 = scale.clone();
358 scale4.mul_pow2(2);
359 let mut scale8 = scale.clone();
360 scale8.mul_pow2(3);
361
362 for i in 0..len {
363 if mant.is_zero() {
364 for c in &mut buf[i..len] {
367 *c = MaybeUninit::new(b'0');
368 }
369 return (unsafe { buf[..len].assume_init_ref() }, k);
371 }
372
373 let mut d = 0;
374 if mant >= scale8 {
375 mant.sub(&scale8);
376 d += 8;
377 }
378 if mant >= scale4 {
379 mant.sub(&scale4);
380 d += 4;
381 }
382 if mant >= scale2 {
383 mant.sub(&scale2);
384 d += 2;
385 }
386 if mant >= scale {
387 mant.sub(&scale);
388 d += 1;
389 }
390 if true {
if !(mant < scale) {
crate::panicking::panic("assertion failed: mant < scale")
};
};debug_assert!(mant < scale);
391 if true {
if !(d < 10) { crate::panicking::panic("assertion failed: d < 10") };
};debug_assert!(d < 10);
392 buf[i] = MaybeUninit::new(b'0' + d);
393 mant.mul_small(10);
394 }
395 }
396
397 let order = mant.cmp(scale.mul_small(5));
401 if order == Ordering::Greater
402 || (order == Ordering::Equal
403 && len > 0 && unsafe { buf[len - 1].assume_init() } & 1 == 1)
405 {
406 if let Some(c) = round_up(unsafe { buf[..len].assume_init_mut() }) {
410 k += 1;
414 if k > limit && len < buf.len() {
415 buf[len] = MaybeUninit::new(c);
416 len += 1;
417 }
418 }
419 }
420
421 (unsafe { buf[..len].assume_init_ref() }, k)
423}