1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT 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.
//
// ---
//
// The C++ implementation preserved here in comments is licensed as follows:
//
// Tencent is pleased to support the open source community by making RapidJSON
// available.
//
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
// rights reserved.
//
// Licensed under the MIT License (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License
// at
//
// http://opensource.org/licenses/MIT
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

use core::ops::{Mul, Sub};
#[cfg(feature = "no-panic")]
use no_panic::no_panic;

#[derive(Copy, Clone, Debug)]
pub struct DiyFp<F, E> {
    pub f: F,
    pub e: E,
}

impl<F, E> DiyFp<F, E> {
    #[cfg_attr(feature = "no-panic", no_panic)]
    pub fn new(f: F, e: E) -> Self {
        DiyFp { f, e }
    }
}

impl<F, E> Sub for DiyFp<F, E>
where
    F: Sub<F, Output = F>,
{
    type Output = Self;

    #[cfg_attr(feature = "no-panic", no_panic)]
    fn sub(self, rhs: Self) -> Self {
        DiyFp {
            f: self.f - rhs.f,
            e: self.e,
        }
    }
}

impl Mul for DiyFp<u32, i32> {
    type Output = Self;

    #[cfg_attr(feature = "no-panic", no_panic)]
    fn mul(self, rhs: Self) -> Self {
        let mut tmp = self.f as u64 * rhs.f as u64;
        tmp += 1u64 << 31; // mult_round
        DiyFp {
            f: (tmp >> 32) as u32,
            e: self.e + rhs.e + 32,
        }
    }
}

impl Mul for DiyFp<u64, isize> {
    type Output = Self;

    #[cfg_attr(feature = "no-panic", no_panic)]
    fn mul(self, rhs: Self) -> Self {
        let m32 = 0xFFFFFFFFu64;
        let a = self.f >> 32;
        let b = self.f & m32;
        let c = rhs.f >> 32;
        let d = rhs.f & m32;
        let ac = a * c;
        let bc = b * c;
        let ad = a * d;
        let bd = b * d;
        let mut tmp = (bd >> 32) + (ad & m32) + (bc & m32);
        tmp += 1u64 << 31; // mult_round
        DiyFp {
            f: ac + (ad >> 32) + (bc >> 32) + (tmp >> 32),
            e: self.e + rhs.e + 64,
        }
    }
}

macro_rules! diyfp {
    (
        floating_type: $fty:ty,
        significand_type: $sigty:ty,
        exponent_type: $expty:ty,

        diy_significand_size: $diy_significand_size:expr,
        significand_size: $significand_size:expr,
        exponent_bias: $exponent_bias:expr,
        mask_type: $mask_type:ty,
        exponent_mask: $exponent_mask:expr,
        significand_mask: $significand_mask:expr,
        hidden_bit: $hidden_bit:expr,
        cached_powers_f: $cached_powers_f:expr,
        cached_powers_e: $cached_powers_e:expr,
        min_power: $min_power:expr,
    ) => {
        type DiyFp = diyfp::DiyFp<$sigty, $expty>;

        impl DiyFp {
            // Preconditions:
            // `d` must have a positive sign and must not be infinity or NaN.
            /*
            explicit DiyFp(double d) {
                union {
                    double d;
                    uint64_t u64;
                } u = { d };

                int biased_e = static_cast<int>((u.u64 & kDpExponentMask) >> kDpSignificandSize);
                uint64_t significand = (u.u64 & kDpSignificandMask);
                if (biased_e != 0) {
                    f = significand + kDpHiddenBit;
                    e = biased_e - kDpExponentBias;
                }
                else {
                    f = significand;
                    e = kDpMinExponent + 1;
                }
            }
            */
            #[cfg_attr(feature = "no-panic", no_panic)]
            unsafe fn from(d: $fty) -> Self {
                let u: $mask_type = mem::transmute(d);

                let biased_e = ((u & $exponent_mask) >> $significand_size) as $expty;
                let significand = u & $significand_mask;
                if biased_e != 0 {
                    DiyFp {
                        f: significand + $hidden_bit,
                        e: biased_e - $exponent_bias - $significand_size,
                    }
                } else {
                    DiyFp {
                        f: significand,
                        e: 1 - $exponent_bias - $significand_size,
                    }
                }
            }

            // Normalizes so that the highest bit of the diy significand is 1.
            /*
            DiyFp Normalize() const {
                DiyFp res = *this;
                while (!(res.f & (static_cast<uint64_t>(1) << 63))) {
                    res.f <<= 1;
                    res.e--;
                }
                return res;
            }
            */
            #[cfg_attr(feature = "no-panic", no_panic)]
            fn normalize(self) -> DiyFp {
                let mut res = self;
                while (res.f & (1 << ($diy_significand_size - 1))) == 0 {
                    res.f <<= 1;
                    res.e -= 1;
                }
                res
            }

            // Normalizes so that the highest bit of the diy significand is 1.
            //
            // Precondition:
            // `self.f` must be no more than 2 bits longer than the f64 significand.
            /*
            DiyFp NormalizeBoundary() const {
                DiyFp res = *this;
                while (!(res.f & (kDpHiddenBit << 1))) {
                    res.f <<= 1;
                    res.e--;
                }
                res.f <<= (kDiySignificandSize - kDpSignificandSize - 2);
                res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2);
                return res;
            }
            */
            #[cfg_attr(feature = "no-panic", no_panic)]
            fn normalize_boundary(self) -> DiyFp {
                let mut res = self;
                while (res.f & $hidden_bit << 1) == 0 {
                    res.f <<= 1;
                    res.e -= 1;
                }
                res.f <<= $diy_significand_size - $significand_size - 2;
                res.e -= $diy_significand_size - $significand_size - 2;
                res
            }

            // Normalizes `self - e` and `self + e` where `e` is half of the least
            // significant digit of `self`. The plus is normalized so that the highest
            // bit of the diy significand is 1. The minus is normalized so that it has
            // the same exponent as the plus.
            //
            // Preconditions:
            // `self` must have been returned directly from `DiyFp::from_f64`.
            // `self.f` must not be zero.
            /*
            void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const {
                DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary();
                DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1);
                mi.f <<= mi.e - pl.e;
                mi.e = pl.e;
                *plus = pl;
                *minus = mi;
            }
            */
            #[cfg_attr(feature = "no-panic", no_panic)]
            fn normalized_boundaries(self) -> (DiyFp, DiyFp) {
                let pl = DiyFp::new((self.f << 1) + 1, self.e - 1).normalize_boundary();
                let mut mi = if self.f == $hidden_bit {
                    DiyFp::new((self.f << 2) - 1, self.e - 2)
                } else {
                    DiyFp::new((self.f << 1) - 1, self.e - 1)
                };
                mi.f <<= mi.e - pl.e;
                mi.e = pl.e;
                (mi, pl)
            }
        }

        /*
        inline DiyFp GetCachedPower(int e, int* K) {
            //int k = static_cast<int>(ceil((-61 - e) * 0.30102999566398114)) + 374;
            double dk = (-61 - e) * 0.30102999566398114 + 347;  // dk must be positive, so can do ceiling in positive
            int k = static_cast<int>(dk);
            if (dk - k > 0.0)
                k++;

            unsigned index = static_cast<unsigned>((k >> 3) + 1);
            *K = -(-348 + static_cast<int>(index << 3));    // decimal exponent no need lookup table

            return GetCachedPowerByIndex(index);
        }
        */
        #[inline]
        #[cfg_attr(feature = "no-panic", no_panic)]
        fn get_cached_power(e: $expty) -> (DiyFp, isize) {
            let dk = (3 - $diy_significand_size - e) as f64 * 0.30102999566398114f64
                - ($min_power + 1) as f64;
            let mut k = dk as isize;
            if dk - k as f64 > 0.0 {
                k += 1;
            }

            let index = ((k >> 3) + 1) as usize;
            let k = -($min_power + (index << 3) as isize);

            (
                DiyFp::new(*unsafe { $cached_powers_f.get_unchecked(index) }, *unsafe {
                    $cached_powers_e.get_unchecked(index)
                }
                    as $expty),
                k,
            )
        }
    };
}