fastnum/decimal/dec/parse/from_float/
f2dec.rs

1mod psi_map;
2
3use crate::{
4    decimal::{
5        dec::{
6            math::{div::div, mul::mul},
7            ControlBlock, ExtraPrecision,
8        },
9        signals::Signals,
10        Context, Decimal, Sign,
11    },
12    int::UInt,
13};
14
15type D<const N: usize> = Decimal<N>;
16
17#[inline]
18pub(crate) const fn f2dec<const N: usize>(mant: u64, b_exp: i16, sign: Sign) -> D<N> {
19    if b_exp == 0 {
20        return D::new(
21            UInt::from_digit(mant),
22            ControlBlock::new(
23                0,
24                sign,
25                Signals::empty(),
26                Context::default(),
27                ExtraPrecision::new(),
28            ),
29        );
30    }
31
32    if b_exp < 0 {
33        let (d_exp, psi) = psi_map::lookup(-b_exp);
34
35        let d = D::new(
36            UInt::from_digit(mant),
37            ControlBlock::new(
38                d_exp,
39                sign,
40                Signals::empty(),
41                Context::default(),
42                ExtraPrecision::new(),
43            ),
44        );
45
46        div(d, psi).round_extra_precision().check()
47    } else {
48        let (d_exp, psi) = psi_map::lookup(b_exp);
49
50        let d = D::new(
51            UInt::from_digit(mant),
52            ControlBlock::new(
53                -d_exp,
54                sign,
55                Signals::empty(),
56                Context::default(),
57                ExtraPrecision::new(),
58            ),
59        );
60
61        mul(d, psi).round_extra_precision().check()
62    }
63}