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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Traits and implementations for rounding during operations.

use core::cmp::Ordering;
use core::ops::{Add, AddAssign};
use dashu_base::{Approximation, EstimatedLog2, Sign, UnsignedAbs};
use dashu_int::{IBig, UBig, Word};

/// Built-in rounding modes of the floating numbers.
///
/// # Rounding Error
///
/// For different rounding modes, the [Rounding][crate::round::Rounding] error
/// in the output of operations tells the error range, as described in
/// the table below.
///
/// | Mode     | Rounding | Error (truth - estimation) Range |
/// |----------|----------|----------------------------------|
/// | Zero     | NoOp     | `(-1 ulp 0)` or `(0, 1 ulp)`*    |
/// | Away     | AddOne   | `(-1 ulp, 0)`                    |
/// | Away     | SubOne   | `(0, 1 ulp)`                     |
/// | Down     | SubOne   | `(0, 1 ulp)`                     |
/// | Up       | AddOne   | `(-1 ulp, 0)`                    |
/// | HalfAway | AddOne   | `[-1/2 ulp, 0)`                  |
/// | HalfAway | NoOp     | `(-1/2 ulp, 1/2 ulp)`            |
/// | HalfAway | SubOne   | `(0, 1/2 ulp]`                   |
/// | HalfEven | AddOne   | `[-1/2 ulp, 0)`                  |
/// | HalfEven | NoOp     | `[-1/2 ulp, 1/2 ulp]`            |
/// | HalfEven | SubOne   | `(0, 1/2 ulp]`                   |
///
/// *: Dependends on the sign of the result
///
pub mod mode {
    /// Round toward 0 (default mode for binary float)
    #[derive(Clone, Copy)]
    pub struct Zero;

    /// Round away from 0
    #[derive(Clone, Copy)]
    pub struct Away;

    /// Round toward +∞
    #[derive(Clone, Copy)]
    pub struct Up;

    /// Round toward -∞
    #[derive(Clone, Copy)]
    pub struct Down;

    /// Round to the nearest value, ties are rounded to an even value. (default mode for decimal float)
    #[derive(Clone, Copy)]
    pub struct HalfEven;

    /// Round to the nearest value, ties away from zero
    #[derive(Clone, Copy)]
    pub struct HalfAway;
}

/// The adjustment of a rounding operation
///
/// See [the `mode` module][mode] for the corresponding error bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Rounding {
    /// No adjustment
    NoOp,

    /// Add one
    AddOne,

    /// Subtract one
    SubOne,
}

/// A type representing float operation result
///
/// If the operation result is inexact, the adjustment from the final rounding
/// will be returned along with the result.
pub type Rounded<T> = Approximation<T, Rounding>;

/// A trait describing the rounding strategy
pub trait Round: Copy {
    /// The rounding operation that rounds to an opposite direction
    type Reverse: Round;

    /// Calculate the rounding of the number (integer + rem), assuming rem != 0 and |rem| < 1.
    /// `low_half_test` should tell |rem|.cmp(0.5)
    fn round_low_part<F: FnOnce() -> Ordering>(
        integer: &IBig,
        low_sign: Sign,
        low_half_test: F,
    ) -> Rounding;

    /// Calculate the rounding of the number (integer + fract / X^precision),
    /// assuming |fract| / X^precision < 1. Return the adjustment.
    #[inline]
    fn round_fract<const B: Word>(integer: &IBig, fract: IBig, precision: usize) -> Rounding {
        // this assertion is costly, so only check in debug mode
        debug_assert!(fract.clone().unsigned_abs() < UBig::from_word(B).pow(precision));

        if fract.is_zero() {
            return Rounding::NoOp;
        }
        let (fsign, fmag) = fract.into_parts();

        let test = || {
            // first use the estimated log2 to do coarse comparison, then do the exact comparison
            let (lb, ub) = fmag.log2_bounds();
            let (b_lb, b_ub) = B.log2_bounds();

            // 0.999 and 1.001 are used here to prevent the influence of the precision loss of the multiplcations
            if lb + 0.999 > b_ub * precision as f32 {
                Ordering::Greater
            } else if ub + 1.001 < b_lb * precision as f32 {
                Ordering::Less
            } else {
                (fmag << 1).cmp(&UBig::from_word(B).pow(precision))
            }
        };
        Self::round_low_part::<_>(integer, fsign, test)
    }

    /// Calculate the rounding of the number (integer + numerator / denominator),
    /// assuming |numerator / denominator| < 1. Return the adjustment.
    #[inline]
    fn round_ratio(integer: &IBig, num: IBig, den: &IBig) -> Rounding {
        assert!(!den.is_zero());
        // this assertion can be costly, so only check in debug mode
        debug_assert!(num.clone().unsigned_abs() < den.clone().unsigned_abs());

        if num.is_zero() {
            return Rounding::NoOp;
        }
        let (nsign, nmag) = num.into_parts();
        Self::round_low_part::<_>(integer, nsign * den.sign(), || {
            if den.sign() == Sign::Positive {
                IBig::from(nmag << 1).cmp(den)
            } else {
                den.cmp(&-(nmag << 1))
            }
        })
    }
}

impl Round for mode::Zero {
    type Reverse = mode::Away;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        integer: &IBig,
        low_sign: Sign,
        _low_half_test: F,
    ) -> Rounding {
        if integer.is_zero() {
            return Rounding::NoOp;
        }
        match (integer.sign(), low_sign) {
            (Sign::Positive, Sign::Positive) | (Sign::Negative, Sign::Negative) => Rounding::NoOp,
            (Sign::Positive, Sign::Negative) => Rounding::SubOne,
            (Sign::Negative, Sign::Positive) => Rounding::AddOne,
        }
    }
}

impl Round for mode::Away {
    type Reverse = mode::Zero;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        integer: &IBig,
        low_sign: Sign,
        _low_half_test: F,
    ) -> Rounding {
        if integer.is_zero() {
            match low_sign {
                Sign::Positive => Rounding::AddOne,
                Sign::Negative => Rounding::SubOne,
            }
        } else {
            match (integer.sign(), low_sign) {
                (Sign::Positive, Sign::Positive) => Rounding::AddOne,
                (Sign::Negative, Sign::Negative) => Rounding::SubOne,
                (Sign::Positive, Sign::Negative) | (Sign::Negative, Sign::Positive) => {
                    Rounding::NoOp
                }
            }
        }
    }
}

impl Round for mode::Down {
    type Reverse = mode::Up;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        _integer: &IBig,
        low_sign: Sign,
        _low_half_test: F,
    ) -> Rounding {
        // -1 if fract < 0, otherwise 0
        if low_sign == Sign::Negative {
            Rounding::SubOne
        } else {
            Rounding::NoOp
        }
    }
}

impl Round for mode::Up {
    type Reverse = mode::Down;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        _integer: &IBig,
        low_sign: Sign,
        _low_half_test: F,
    ) -> Rounding {
        // +1 if fract > 0, otherwise 0
        if low_sign == Sign::Positive {
            Rounding::AddOne
        } else {
            Rounding::NoOp
        }
    }
}

impl Round for mode::HalfAway {
    type Reverse = Self;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        integer: &IBig,
        low_sign: Sign,
        low_half_test: F,
    ) -> Rounding {
        match low_half_test() {
            // |rem| < 1/2
            Ordering::Less => Rounding::NoOp,
            // |rem| = 1/2
            Ordering::Equal => {
                // +1 if integer and rem >= 0, -1 if integer and rem <= 0
                if integer >= &IBig::ZERO && low_sign == Sign::Positive {
                    Rounding::AddOne
                } else if integer <= &IBig::ZERO && low_sign == Sign::Negative {
                    Rounding::SubOne
                } else {
                    Rounding::NoOp
                }
            }
            // |rem| > 1/2
            Ordering::Greater => {
                // +1 if rem > 0, -1 if rem < 0
                match low_sign {
                    Sign::Positive => Rounding::AddOne,
                    Sign::Negative => Rounding::SubOne,
                }
            }
        }
    }
}

impl Round for mode::HalfEven {
    type Reverse = Self;

    #[inline]
    fn round_low_part<F: FnOnce() -> Ordering>(
        integer: &IBig,
        low_sign: Sign,
        low_half_test: F,
    ) -> Rounding {
        match low_half_test() {
            // |rem| < 1/2
            Ordering::Less => Rounding::NoOp,
            // |rem| = 1/2
            Ordering::Equal => {
                // if integer is odd, +1 if rem > 0, -1 if rem < 0
                if integer & 1 == 1 {
                    match low_sign {
                        Sign::Positive => Rounding::AddOne,
                        Sign::Negative => Rounding::SubOne,
                    }
                } else {
                    Rounding::NoOp
                }
            }
            // |rem| > 1/2
            Ordering::Greater => {
                // +1 if rem > 0, -1 if rem < 0
                match low_sign {
                    Sign::Positive => Rounding::AddOne,
                    Sign::Negative => Rounding::SubOne,
                }
            }
        }
    }
}

impl Add<Rounding> for IBig {
    type Output = IBig;

    fn add(self, rhs: Rounding) -> Self::Output {
        match rhs {
            Rounding::NoOp => self,
            Rounding::AddOne => self + IBig::ONE,
            Rounding::SubOne => self - IBig::ONE,
        }
    }
}

impl Add<Rounding> for &IBig {
    type Output = IBig;

    fn add(self, rhs: Rounding) -> Self::Output {
        match rhs {
            Rounding::NoOp => self.clone(),
            Rounding::AddOne => self + IBig::ONE,
            Rounding::SubOne => self - IBig::ONE,
        }
    }
}

impl AddAssign<Rounding> for IBig {
    fn add_assign(&mut self, rhs: Rounding) {
        match rhs {
            Rounding::NoOp => {}
            Rounding::AddOne => *self += IBig::ONE,
            Rounding::SubOne => *self -= IBig::ONE,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use super::{mode::*, Rounding::*};

    #[test]
    fn test_from_fract() {
        #[rustfmt::skip]
        fn test_all_rounding<const B: Word, const D: usize>(
            input: &(i32, i32, Rounding, Rounding, Rounding, Rounding, Rounding, Rounding),
        ) {
            let (value, fract, rnd_zero, rnd_away, rnd_up, rnd_down, rnd_halfeven, rnd_halfaway) = *input;
            let (value, fract) = (IBig::from(value), IBig::from(fract));
            assert_eq!(Zero::round_fract::<B>(&value, fract.clone(), D), rnd_zero);
            assert_eq!(Away::round_fract::<B>(&value, fract.clone(), D), rnd_away);
            assert_eq!(Up::round_fract::<B>(&value, fract.clone(), D), rnd_up);
            assert_eq!(Down::round_fract::<B>(&value, fract.clone(), D), rnd_down);
            assert_eq!(HalfEven::round_fract::<B>(&value, fract.clone(), D), rnd_halfeven);
            assert_eq!(HalfAway::round_fract::<B>(&value, fract.clone(), D), rnd_halfaway);
        }

        // cases for radix = 2, 2 digit fraction
        #[rustfmt::skip]
        let binary_cases = [
            // (integer value, fraction part, roundings...)
            // Mode: Zero  , Away  , Up    , Down  , HEven,  HAway
            ( 0,  3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 0,  2, NoOp  , AddOne, AddOne, NoOp  , NoOp  , AddOne),
            ( 0,  1, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 0,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0, -1, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            ( 0, -2, NoOp  , SubOne, NoOp  , SubOne, NoOp  , SubOne),
            ( 0, -3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            ( 1,  3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1,  2, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1,  1, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 1,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1, -1, SubOne, NoOp  , NoOp  , SubOne, NoOp  , NoOp  ),
            ( 1, -2, SubOne, NoOp  , NoOp  , SubOne, SubOne, NoOp  ),
            ( 1, -3, SubOne, NoOp  , NoOp  , SubOne, SubOne, SubOne),
            (-1,  3, AddOne, NoOp  , AddOne, NoOp  , AddOne, AddOne),
            (-1,  2, AddOne, NoOp  , AddOne, NoOp  , AddOne, NoOp  ),
            (-1,  1, AddOne, NoOp  , AddOne, NoOp  , NoOp  , NoOp  ),
            (-1,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1, -1, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            (-1, -2, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            (-1, -3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
        ];
        binary_cases.iter().for_each(test_all_rounding::<2, 2>);

        // cases for radix = 3, 1 digit fraction
        #[rustfmt::skip]
        let tenary_cases = [
            // (integer value, fraction part, roundings...)
            // Mode: Zero,   Away  , Up    , Down  , HEven , HAway
            ( 0,  2, NoOp,   AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 0,  1, NoOp,   AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 0,  0, NoOp,   NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0, -1, NoOp,   SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            ( 0, -2, NoOp,   SubOne, NoOp  , SubOne, SubOne, SubOne),
            ( 1,  2, NoOp,   AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1,  1, NoOp,   AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 1,  0, NoOp,   NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1, -1, SubOne, NoOp  , NoOp  , SubOne, NoOp  , NoOp  ),
            ( 1, -2, SubOne, NoOp  , NoOp  , SubOne, SubOne, SubOne),
            (-1,  2, AddOne, NoOp  , AddOne, NoOp  , AddOne, AddOne),
            (-1,  1, AddOne, NoOp  , AddOne, NoOp  , NoOp  , NoOp  ),
            (-1,  0, NoOp,   NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1, -1, NoOp,   SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            (-1, -2, NoOp,   SubOne, NoOp  , SubOne, SubOne, SubOne),
        ];
        tenary_cases.iter().for_each(test_all_rounding::<3, 1>);

        // cases for radix = 10, 1 digit fraction
        #[rustfmt::skip]
        let decimal_cases = [
            // (integer value, fraction part, roundings...)
            // Mode: Zero  , Away  , Up    , Down  , HEven , HAway
            ( 0,  7, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 0,  5, NoOp  , AddOne, AddOne, NoOp  , NoOp  , AddOne),
            ( 0,  2, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 0,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0, -2, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            ( 0, -5, NoOp  , SubOne, NoOp  , SubOne, NoOp  , SubOne),
            ( 0, -7, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            ( 1,  7, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1,  5, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1,  2, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 1,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1, -2, SubOne, NoOp  , NoOp  , SubOne, NoOp  , NoOp  ),
            ( 1, -5, SubOne, NoOp  , NoOp  , SubOne, SubOne, NoOp  ),
            ( 1, -7, SubOne, NoOp  , NoOp  , SubOne, SubOne, SubOne),
            (-1,  7, AddOne, NoOp  , AddOne, NoOp  , AddOne, AddOne),
            (-1,  5, AddOne, NoOp  , AddOne, NoOp  , AddOne, NoOp  ),
            (-1,  2, AddOne, NoOp  , AddOne, NoOp  , NoOp  , NoOp  ),
            (-1,  0, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1, -2, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            (-1, -5, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            (-1, -7, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
        ];
        decimal_cases.iter().for_each(test_all_rounding::<10, 1>);
    }

    #[test]
    fn test_from_ratio() {
        #[rustfmt::skip]
        fn test_all_rounding(
            input: &(i32, i32, i32, Rounding, Rounding, Rounding, Rounding, Rounding, Rounding),
        ) {
            let (value, num, den, rnd_zero, rnd_away, rnd_up, rnd_down, rnd_halfeven, rnd_halfaway) = *input;
            let (value, num, den) = (IBig::from(value), IBig::from(num), IBig::from(den));
            assert_eq!(Zero::round_ratio(&value, num.clone(), &den), rnd_zero);
            assert_eq!(Away::round_ratio(&value, num.clone(), &den), rnd_away);
            assert_eq!(Up::round_ratio(&value, num.clone(), &den), rnd_up);
            assert_eq!(Down::round_ratio(&value, num.clone(), &den), rnd_down);
            assert_eq!(HalfEven::round_ratio(&value, num.clone(), &den), rnd_halfeven);
            assert_eq!(HalfAway::round_ratio(&value, num.clone(), &den), rnd_halfaway);
        }

        // cases for radix = 2, 2 digit fraction
        #[rustfmt::skip]
        let test_cases = [
            // (integer value, mumerator, denominator, roundings...)
            // Mode:     Zero  , Away  , Up    , Down  , HEven , HAway
            ( 0,  0,  2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0,  1,  2, NoOp  , AddOne, AddOne, NoOp  , NoOp  , AddOne),
            ( 0, -1,  2, NoOp  , SubOne, NoOp  , SubOne, NoOp  , SubOne),
            ( 0,  0, -2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0,  1, -2, NoOp  , SubOne, NoOp  , SubOne, NoOp  , SubOne),
            ( 0, -1, -2, NoOp  , AddOne, AddOne, NoOp  , NoOp  , AddOne),
            ( 1,  0,  2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1,  1,  2, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1, -1,  2, SubOne, NoOp  , NoOp  , SubOne, SubOne, NoOp  ),
            ( 1,  0, -2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1,  1, -2, SubOne, NoOp  , NoOp  , SubOne, SubOne, NoOp  ),
            ( 1, -1, -2, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            (-1,  0,  2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1,  1,  2, AddOne, NoOp  , AddOne, NoOp  , AddOne, NoOp  ),
            (-1, -1,  2, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            (-1,  0, -2, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1,  1, -2, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            (-1, -1, -2, AddOne, NoOp  , AddOne, NoOp  , AddOne, NoOp  ),

            ( 0, -2,  3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            ( 0, -1,  3, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            ( 0,  0,  3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0,  1,  3, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 0,  2,  3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 0, -2, -3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 0, -1, -3, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 0,  0, -3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 0,  1, -3, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            ( 0,  2, -3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            ( 1, -2,  3, SubOne, NoOp  , NoOp  , SubOne, SubOne, SubOne),
            ( 1, -1,  3, SubOne, NoOp  , NoOp  , SubOne, NoOp  , NoOp  ),
            ( 1,  0,  3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1,  1,  3, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 1,  2,  3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1, -2, -3, NoOp  , AddOne, AddOne, NoOp  , AddOne, AddOne),
            ( 1, -1, -3, NoOp  , AddOne, AddOne, NoOp  , NoOp  , NoOp  ),
            ( 1,  0, -3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            ( 1,  1, -3, SubOne, NoOp  , NoOp  , SubOne, NoOp  , NoOp  ),
            ( 1,  2, -3, SubOne, NoOp  , NoOp  , SubOne, SubOne, SubOne),
            (-1, -2,  3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
            (-1, -1,  3, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            (-1,  0,  3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1,  1,  3, AddOne, NoOp  , AddOne, NoOp  , NoOp  , NoOp  ),
            (-1,  2,  3, AddOne, NoOp  , AddOne, NoOp  , AddOne, AddOne),
            (-1, -2, -3, AddOne, NoOp  , AddOne, NoOp  , AddOne, AddOne),
            (-1, -1, -3, AddOne, NoOp  , AddOne, NoOp  , NoOp  , NoOp  ),
            (-1,  0, -3, NoOp  , NoOp  , NoOp  , NoOp  , NoOp  , NoOp  ),
            (-1,  1, -3, NoOp  , SubOne, NoOp  , SubOne, NoOp  , NoOp  ),
            (-1,  2, -3, NoOp  , SubOne, NoOp  , SubOne, SubOne, SubOne),
        ];
        test_cases.iter().for_each(test_all_rounding);
    }
}