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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// 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 arrow_buffer::{i256, ArrowNativeType, IntervalDayTime, IntervalMonthDayNano};
use arrow_schema::ArrowError;
use half::f16;
use num::complex::ComplexFloat;
use std::cmp::Ordering;

/// Trait for [`ArrowNativeType`] that adds checked and unchecked arithmetic operations,
/// and totally ordered comparison operations
///
/// The APIs with `_wrapping` suffix do not perform overflow-checking. For integer
/// types they will wrap around the boundary of the type. For floating point types they
/// will overflow to INF or -INF preserving the expected sign value
///
/// Note `div_wrapping` and `mod_wrapping` will panic for integer types if `rhs` is zero
/// although this may be subject to change <https://github.com/apache/arrow-rs/issues/2647>
///
/// The APIs with `_checked` suffix perform overflow-checking. For integer types
/// these will return `Err` instead of wrapping. For floating point types they will
/// overflow to INF or -INF preserving the expected sign value
///
/// Comparison of integer types is as per normal integer comparison rules, floating
/// point values are compared as per IEEE 754's totalOrder predicate see [`f32::total_cmp`]
///
pub trait ArrowNativeTypeOp: ArrowNativeType {
    /// The additive identity
    const ZERO: Self;

    /// The multiplicative identity
    const ONE: Self;

    /// The minimum value and identity for the `max` aggregation.
    /// Note that the aggregation uses the total order predicate for floating point values,
    /// which means that this value is a negative NaN.
    const MIN_TOTAL_ORDER: Self;

    /// The maximum value and identity for the `min` aggregation.
    /// Note that the aggregation uses the total order predicate for floating point values,
    /// which means that this value is a positive NaN.
    const MAX_TOTAL_ORDER: Self;

    /// Checked addition operation
    fn add_checked(self, rhs: Self) -> Result<Self, ArrowError>;

    /// Wrapping addition operation
    fn add_wrapping(self, rhs: Self) -> Self;

    /// Checked subtraction operation
    fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError>;

    /// Wrapping subtraction operation
    fn sub_wrapping(self, rhs: Self) -> Self;

    /// Checked multiplication operation
    fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError>;

    /// Wrapping multiplication operation
    fn mul_wrapping(self, rhs: Self) -> Self;

    /// Checked division operation
    fn div_checked(self, rhs: Self) -> Result<Self, ArrowError>;

    /// Wrapping division operation
    fn div_wrapping(self, rhs: Self) -> Self;

    /// Checked remainder operation
    fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError>;

    /// Wrapping remainder operation
    fn mod_wrapping(self, rhs: Self) -> Self;

    /// Checked negation operation
    fn neg_checked(self) -> Result<Self, ArrowError>;

    /// Wrapping negation operation
    fn neg_wrapping(self) -> Self;

    /// Checked exponentiation operation
    fn pow_checked(self, exp: u32) -> Result<Self, ArrowError>;

    /// Wrapping exponentiation operation
    fn pow_wrapping(self, exp: u32) -> Self;

    /// Returns true if zero else false
    fn is_zero(self) -> bool;

    /// Compare operation
    fn compare(self, rhs: Self) -> Ordering;

    /// Equality operation
    fn is_eq(self, rhs: Self) -> bool;

    /// Not equal operation
    #[inline]
    fn is_ne(self, rhs: Self) -> bool {
        !self.is_eq(rhs)
    }

    /// Less than operation
    #[inline]
    fn is_lt(self, rhs: Self) -> bool {
        self.compare(rhs).is_lt()
    }

    /// Less than equals operation
    #[inline]
    fn is_le(self, rhs: Self) -> bool {
        self.compare(rhs).is_le()
    }

    /// Greater than operation
    #[inline]
    fn is_gt(self, rhs: Self) -> bool {
        self.compare(rhs).is_gt()
    }

    /// Greater than equals operation
    #[inline]
    fn is_ge(self, rhs: Self) -> bool {
        self.compare(rhs).is_ge()
    }
}

macro_rules! native_type_op {
    ($t:tt) => {
        native_type_op!($t, 0, 1);
    };
    ($t:tt, $zero:expr, $one: expr) => {
        native_type_op!($t, $zero, $one, $t::MIN, $t::MAX);
    };
    ($t:tt, $zero:expr, $one: expr, $min: expr, $max: expr) => {
        impl ArrowNativeTypeOp for $t {
            const ZERO: Self = $zero;
            const ONE: Self = $one;
            const MIN_TOTAL_ORDER: Self = $min;
            const MAX_TOTAL_ORDER: Self = $max;

            #[inline]
            fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                self.checked_add(rhs).ok_or_else(|| {
                    ArrowError::ComputeError(format!(
                        "Overflow happened on: {:?} + {:?}",
                        self, rhs
                    ))
                })
            }

            #[inline]
            fn add_wrapping(self, rhs: Self) -> Self {
                self.wrapping_add(rhs)
            }

            #[inline]
            fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                self.checked_sub(rhs).ok_or_else(|| {
                    ArrowError::ComputeError(format!(
                        "Overflow happened on: {:?} - {:?}",
                        self, rhs
                    ))
                })
            }

            #[inline]
            fn sub_wrapping(self, rhs: Self) -> Self {
                self.wrapping_sub(rhs)
            }

            #[inline]
            fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                self.checked_mul(rhs).ok_or_else(|| {
                    ArrowError::ComputeError(format!(
                        "Overflow happened on: {:?} * {:?}",
                        self, rhs
                    ))
                })
            }

            #[inline]
            fn mul_wrapping(self, rhs: Self) -> Self {
                self.wrapping_mul(rhs)
            }

            #[inline]
            fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                if rhs.is_zero() {
                    Err(ArrowError::DivideByZero)
                } else {
                    self.checked_div(rhs).ok_or_else(|| {
                        ArrowError::ComputeError(format!(
                            "Overflow happened on: {:?} / {:?}",
                            self, rhs
                        ))
                    })
                }
            }

            #[inline]
            fn div_wrapping(self, rhs: Self) -> Self {
                self.wrapping_div(rhs)
            }

            #[inline]
            fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                if rhs.is_zero() {
                    Err(ArrowError::DivideByZero)
                } else {
                    self.checked_rem(rhs).ok_or_else(|| {
                        ArrowError::ComputeError(format!(
                            "Overflow happened on: {:?} % {:?}",
                            self, rhs
                        ))
                    })
                }
            }

            #[inline]
            fn mod_wrapping(self, rhs: Self) -> Self {
                self.wrapping_rem(rhs)
            }

            #[inline]
            fn neg_checked(self) -> Result<Self, ArrowError> {
                self.checked_neg().ok_or_else(|| {
                    ArrowError::ComputeError(format!("Overflow happened on: - {:?}", self))
                })
            }

            #[inline]
            fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
                self.checked_pow(exp).ok_or_else(|| {
                    ArrowError::ComputeError(format!("Overflow happened on: {:?} ^ {exp:?}", self))
                })
            }

            #[inline]
            fn pow_wrapping(self, exp: u32) -> Self {
                self.wrapping_pow(exp)
            }

            #[inline]
            fn neg_wrapping(self) -> Self {
                self.wrapping_neg()
            }

            #[inline]
            fn is_zero(self) -> bool {
                self == Self::ZERO
            }

            #[inline]
            fn compare(self, rhs: Self) -> Ordering {
                self.cmp(&rhs)
            }

            #[inline]
            fn is_eq(self, rhs: Self) -> bool {
                self == rhs
            }
        }
    };
}

native_type_op!(i8);
native_type_op!(i16);
native_type_op!(i32);
native_type_op!(i64);
native_type_op!(i128);
native_type_op!(u8);
native_type_op!(u16);
native_type_op!(u32);
native_type_op!(u64);
native_type_op!(i256, i256::ZERO, i256::ONE, i256::MIN, i256::MAX);

native_type_op!(IntervalDayTime, IntervalDayTime::ZERO, IntervalDayTime::ONE);
native_type_op!(
    IntervalMonthDayNano,
    IntervalMonthDayNano::ZERO,
    IntervalMonthDayNano::ONE
);

macro_rules! native_type_float_op {
    ($t:tt, $zero:expr, $one:expr, $min:expr, $max:expr) => {
        impl ArrowNativeTypeOp for $t {
            const ZERO: Self = $zero;
            const ONE: Self = $one;
            const MIN_TOTAL_ORDER: Self = $min;
            const MAX_TOTAL_ORDER: Self = $max;

            #[inline]
            fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                Ok(self + rhs)
            }

            #[inline]
            fn add_wrapping(self, rhs: Self) -> Self {
                self + rhs
            }

            #[inline]
            fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                Ok(self - rhs)
            }

            #[inline]
            fn sub_wrapping(self, rhs: Self) -> Self {
                self - rhs
            }

            #[inline]
            fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                Ok(self * rhs)
            }

            #[inline]
            fn mul_wrapping(self, rhs: Self) -> Self {
                self * rhs
            }

            #[inline]
            fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                if rhs.is_zero() {
                    Err(ArrowError::DivideByZero)
                } else {
                    Ok(self / rhs)
                }
            }

            #[inline]
            fn div_wrapping(self, rhs: Self) -> Self {
                self / rhs
            }

            #[inline]
            fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
                if rhs.is_zero() {
                    Err(ArrowError::DivideByZero)
                } else {
                    Ok(self % rhs)
                }
            }

            #[inline]
            fn mod_wrapping(self, rhs: Self) -> Self {
                self % rhs
            }

            #[inline]
            fn neg_checked(self) -> Result<Self, ArrowError> {
                Ok(-self)
            }

            #[inline]
            fn neg_wrapping(self) -> Self {
                -self
            }

            #[inline]
            fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
                Ok(self.powi(exp as i32))
            }

            #[inline]
            fn pow_wrapping(self, exp: u32) -> Self {
                self.powi(exp as i32)
            }

            #[inline]
            fn is_zero(self) -> bool {
                self == $zero
            }

            #[inline]
            fn compare(self, rhs: Self) -> Ordering {
                <$t>::total_cmp(&self, &rhs)
            }

            #[inline]
            fn is_eq(self, rhs: Self) -> bool {
                // Equivalent to `self.total_cmp(&rhs).is_eq()`
                // but LLVM isn't able to realise this is bitwise equality
                // https://rust.godbolt.org/z/347nWGxoW
                self.to_bits() == rhs.to_bits()
            }
        }
    };
}

// the smallest/largest bit patterns for floating point numbers are NaN, but differ from the canonical NAN constants.
// See test_float_total_order_min_max for details.
native_type_float_op!(
    f16,
    f16::ZERO,
    f16::ONE,
    f16::from_bits(-1 as _),
    f16::from_bits(i16::MAX as _)
);
// from_bits is not yet stable as const fn, see https://github.com/rust-lang/rust/issues/72447
native_type_float_op!(
    f32,
    0.,
    1.,
    unsafe { std::mem::transmute(-1_i32) },
    unsafe { std::mem::transmute(i32::MAX) }
);
native_type_float_op!(
    f64,
    0.,
    1.,
    unsafe { std::mem::transmute(-1_i64) },
    unsafe { std::mem::transmute(i64::MAX) }
);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_native_type_is_zero() {
        assert!(0_i8.is_zero());
        assert!(0_i16.is_zero());
        assert!(0_i32.is_zero());
        assert!(0_i64.is_zero());
        assert!(0_i128.is_zero());
        assert!(i256::ZERO.is_zero());
        assert!(0_u8.is_zero());
        assert!(0_u16.is_zero());
        assert!(0_u32.is_zero());
        assert!(0_u64.is_zero());
        assert!(f16::ZERO.is_zero());
        assert!(0.0_f32.is_zero());
        assert!(0.0_f64.is_zero());
    }

    #[test]
    fn test_native_type_comparison() {
        // is_eq
        assert!(8_i8.is_eq(8_i8));
        assert!(8_i16.is_eq(8_i16));
        assert!(8_i32.is_eq(8_i32));
        assert!(8_i64.is_eq(8_i64));
        assert!(8_i128.is_eq(8_i128));
        assert!(i256::from_parts(8, 0).is_eq(i256::from_parts(8, 0)));
        assert!(8_u8.is_eq(8_u8));
        assert!(8_u16.is_eq(8_u16));
        assert!(8_u32.is_eq(8_u32));
        assert!(8_u64.is_eq(8_u64));
        assert!(f16::from_f32(8.0).is_eq(f16::from_f32(8.0)));
        assert!(8.0_f32.is_eq(8.0_f32));
        assert!(8.0_f64.is_eq(8.0_f64));

        // is_ne
        assert!(8_i8.is_ne(1_i8));
        assert!(8_i16.is_ne(1_i16));
        assert!(8_i32.is_ne(1_i32));
        assert!(8_i64.is_ne(1_i64));
        assert!(8_i128.is_ne(1_i128));
        assert!(i256::from_parts(8, 0).is_ne(i256::from_parts(1, 0)));
        assert!(8_u8.is_ne(1_u8));
        assert!(8_u16.is_ne(1_u16));
        assert!(8_u32.is_ne(1_u32));
        assert!(8_u64.is_ne(1_u64));
        assert!(f16::from_f32(8.0).is_ne(f16::from_f32(1.0)));
        assert!(8.0_f32.is_ne(1.0_f32));
        assert!(8.0_f64.is_ne(1.0_f64));

        // is_lt
        assert!(8_i8.is_lt(10_i8));
        assert!(8_i16.is_lt(10_i16));
        assert!(8_i32.is_lt(10_i32));
        assert!(8_i64.is_lt(10_i64));
        assert!(8_i128.is_lt(10_i128));
        assert!(i256::from_parts(8, 0).is_lt(i256::from_parts(10, 0)));
        assert!(8_u8.is_lt(10_u8));
        assert!(8_u16.is_lt(10_u16));
        assert!(8_u32.is_lt(10_u32));
        assert!(8_u64.is_lt(10_u64));
        assert!(f16::from_f32(8.0).is_lt(f16::from_f32(10.0)));
        assert!(8.0_f32.is_lt(10.0_f32));
        assert!(8.0_f64.is_lt(10.0_f64));

        // is_gt
        assert!(8_i8.is_gt(1_i8));
        assert!(8_i16.is_gt(1_i16));
        assert!(8_i32.is_gt(1_i32));
        assert!(8_i64.is_gt(1_i64));
        assert!(8_i128.is_gt(1_i128));
        assert!(i256::from_parts(8, 0).is_gt(i256::from_parts(1, 0)));
        assert!(8_u8.is_gt(1_u8));
        assert!(8_u16.is_gt(1_u16));
        assert!(8_u32.is_gt(1_u32));
        assert!(8_u64.is_gt(1_u64));
        assert!(f16::from_f32(8.0).is_gt(f16::from_f32(1.0)));
        assert!(8.0_f32.is_gt(1.0_f32));
        assert!(8.0_f64.is_gt(1.0_f64));
    }

    #[test]
    fn test_native_type_add() {
        // add_wrapping
        assert_eq!(8_i8.add_wrapping(2_i8), 10_i8);
        assert_eq!(8_i16.add_wrapping(2_i16), 10_i16);
        assert_eq!(8_i32.add_wrapping(2_i32), 10_i32);
        assert_eq!(8_i64.add_wrapping(2_i64), 10_i64);
        assert_eq!(8_i128.add_wrapping(2_i128), 10_i128);
        assert_eq!(
            i256::from_parts(8, 0).add_wrapping(i256::from_parts(2, 0)),
            i256::from_parts(10, 0)
        );
        assert_eq!(8_u8.add_wrapping(2_u8), 10_u8);
        assert_eq!(8_u16.add_wrapping(2_u16), 10_u16);
        assert_eq!(8_u32.add_wrapping(2_u32), 10_u32);
        assert_eq!(8_u64.add_wrapping(2_u64), 10_u64);
        assert_eq!(
            f16::from_f32(8.0).add_wrapping(f16::from_f32(2.0)),
            f16::from_f32(10.0)
        );
        assert_eq!(8.0_f32.add_wrapping(2.0_f32), 10_f32);
        assert_eq!(8.0_f64.add_wrapping(2.0_f64), 10_f64);

        // add_checked
        assert_eq!(8_i8.add_checked(2_i8).unwrap(), 10_i8);
        assert_eq!(8_i16.add_checked(2_i16).unwrap(), 10_i16);
        assert_eq!(8_i32.add_checked(2_i32).unwrap(), 10_i32);
        assert_eq!(8_i64.add_checked(2_i64).unwrap(), 10_i64);
        assert_eq!(8_i128.add_checked(2_i128).unwrap(), 10_i128);
        assert_eq!(
            i256::from_parts(8, 0)
                .add_checked(i256::from_parts(2, 0))
                .unwrap(),
            i256::from_parts(10, 0)
        );
        assert_eq!(8_u8.add_checked(2_u8).unwrap(), 10_u8);
        assert_eq!(8_u16.add_checked(2_u16).unwrap(), 10_u16);
        assert_eq!(8_u32.add_checked(2_u32).unwrap(), 10_u32);
        assert_eq!(8_u64.add_checked(2_u64).unwrap(), 10_u64);
        assert_eq!(
            f16::from_f32(8.0).add_checked(f16::from_f32(2.0)).unwrap(),
            f16::from_f32(10.0)
        );
        assert_eq!(8.0_f32.add_checked(2.0_f32).unwrap(), 10_f32);
        assert_eq!(8.0_f64.add_checked(2.0_f64).unwrap(), 10_f64);
    }

    #[test]
    fn test_native_type_sub() {
        // sub_wrapping
        assert_eq!(8_i8.sub_wrapping(2_i8), 6_i8);
        assert_eq!(8_i16.sub_wrapping(2_i16), 6_i16);
        assert_eq!(8_i32.sub_wrapping(2_i32), 6_i32);
        assert_eq!(8_i64.sub_wrapping(2_i64), 6_i64);
        assert_eq!(8_i128.sub_wrapping(2_i128), 6_i128);
        assert_eq!(
            i256::from_parts(8, 0).sub_wrapping(i256::from_parts(2, 0)),
            i256::from_parts(6, 0)
        );
        assert_eq!(8_u8.sub_wrapping(2_u8), 6_u8);
        assert_eq!(8_u16.sub_wrapping(2_u16), 6_u16);
        assert_eq!(8_u32.sub_wrapping(2_u32), 6_u32);
        assert_eq!(8_u64.sub_wrapping(2_u64), 6_u64);
        assert_eq!(
            f16::from_f32(8.0).sub_wrapping(f16::from_f32(2.0)),
            f16::from_f32(6.0)
        );
        assert_eq!(8.0_f32.sub_wrapping(2.0_f32), 6_f32);
        assert_eq!(8.0_f64.sub_wrapping(2.0_f64), 6_f64);

        // sub_checked
        assert_eq!(8_i8.sub_checked(2_i8).unwrap(), 6_i8);
        assert_eq!(8_i16.sub_checked(2_i16).unwrap(), 6_i16);
        assert_eq!(8_i32.sub_checked(2_i32).unwrap(), 6_i32);
        assert_eq!(8_i64.sub_checked(2_i64).unwrap(), 6_i64);
        assert_eq!(8_i128.sub_checked(2_i128).unwrap(), 6_i128);
        assert_eq!(
            i256::from_parts(8, 0)
                .sub_checked(i256::from_parts(2, 0))
                .unwrap(),
            i256::from_parts(6, 0)
        );
        assert_eq!(8_u8.sub_checked(2_u8).unwrap(), 6_u8);
        assert_eq!(8_u16.sub_checked(2_u16).unwrap(), 6_u16);
        assert_eq!(8_u32.sub_checked(2_u32).unwrap(), 6_u32);
        assert_eq!(8_u64.sub_checked(2_u64).unwrap(), 6_u64);
        assert_eq!(
            f16::from_f32(8.0).sub_checked(f16::from_f32(2.0)).unwrap(),
            f16::from_f32(6.0)
        );
        assert_eq!(8.0_f32.sub_checked(2.0_f32).unwrap(), 6_f32);
        assert_eq!(8.0_f64.sub_checked(2.0_f64).unwrap(), 6_f64);
    }

    #[test]
    fn test_native_type_mul() {
        // mul_wrapping
        assert_eq!(8_i8.mul_wrapping(2_i8), 16_i8);
        assert_eq!(8_i16.mul_wrapping(2_i16), 16_i16);
        assert_eq!(8_i32.mul_wrapping(2_i32), 16_i32);
        assert_eq!(8_i64.mul_wrapping(2_i64), 16_i64);
        assert_eq!(8_i128.mul_wrapping(2_i128), 16_i128);
        assert_eq!(
            i256::from_parts(8, 0).mul_wrapping(i256::from_parts(2, 0)),
            i256::from_parts(16, 0)
        );
        assert_eq!(8_u8.mul_wrapping(2_u8), 16_u8);
        assert_eq!(8_u16.mul_wrapping(2_u16), 16_u16);
        assert_eq!(8_u32.mul_wrapping(2_u32), 16_u32);
        assert_eq!(8_u64.mul_wrapping(2_u64), 16_u64);
        assert_eq!(
            f16::from_f32(8.0).mul_wrapping(f16::from_f32(2.0)),
            f16::from_f32(16.0)
        );
        assert_eq!(8.0_f32.mul_wrapping(2.0_f32), 16_f32);
        assert_eq!(8.0_f64.mul_wrapping(2.0_f64), 16_f64);

        // mul_checked
        assert_eq!(8_i8.mul_checked(2_i8).unwrap(), 16_i8);
        assert_eq!(8_i16.mul_checked(2_i16).unwrap(), 16_i16);
        assert_eq!(8_i32.mul_checked(2_i32).unwrap(), 16_i32);
        assert_eq!(8_i64.mul_checked(2_i64).unwrap(), 16_i64);
        assert_eq!(8_i128.mul_checked(2_i128).unwrap(), 16_i128);
        assert_eq!(
            i256::from_parts(8, 0)
                .mul_checked(i256::from_parts(2, 0))
                .unwrap(),
            i256::from_parts(16, 0)
        );
        assert_eq!(8_u8.mul_checked(2_u8).unwrap(), 16_u8);
        assert_eq!(8_u16.mul_checked(2_u16).unwrap(), 16_u16);
        assert_eq!(8_u32.mul_checked(2_u32).unwrap(), 16_u32);
        assert_eq!(8_u64.mul_checked(2_u64).unwrap(), 16_u64);
        assert_eq!(
            f16::from_f32(8.0).mul_checked(f16::from_f32(2.0)).unwrap(),
            f16::from_f32(16.0)
        );
        assert_eq!(8.0_f32.mul_checked(2.0_f32).unwrap(), 16_f32);
        assert_eq!(8.0_f64.mul_checked(2.0_f64).unwrap(), 16_f64);
    }

    #[test]
    fn test_native_type_div() {
        // div_wrapping
        assert_eq!(8_i8.div_wrapping(2_i8), 4_i8);
        assert_eq!(8_i16.div_wrapping(2_i16), 4_i16);
        assert_eq!(8_i32.div_wrapping(2_i32), 4_i32);
        assert_eq!(8_i64.div_wrapping(2_i64), 4_i64);
        assert_eq!(8_i128.div_wrapping(2_i128), 4_i128);
        assert_eq!(
            i256::from_parts(8, 0).div_wrapping(i256::from_parts(2, 0)),
            i256::from_parts(4, 0)
        );
        assert_eq!(8_u8.div_wrapping(2_u8), 4_u8);
        assert_eq!(8_u16.div_wrapping(2_u16), 4_u16);
        assert_eq!(8_u32.div_wrapping(2_u32), 4_u32);
        assert_eq!(8_u64.div_wrapping(2_u64), 4_u64);
        assert_eq!(
            f16::from_f32(8.0).div_wrapping(f16::from_f32(2.0)),
            f16::from_f32(4.0)
        );
        assert_eq!(8.0_f32.div_wrapping(2.0_f32), 4_f32);
        assert_eq!(8.0_f64.div_wrapping(2.0_f64), 4_f64);

        // div_checked
        assert_eq!(8_i8.div_checked(2_i8).unwrap(), 4_i8);
        assert_eq!(8_i16.div_checked(2_i16).unwrap(), 4_i16);
        assert_eq!(8_i32.div_checked(2_i32).unwrap(), 4_i32);
        assert_eq!(8_i64.div_checked(2_i64).unwrap(), 4_i64);
        assert_eq!(8_i128.div_checked(2_i128).unwrap(), 4_i128);
        assert_eq!(
            i256::from_parts(8, 0)
                .div_checked(i256::from_parts(2, 0))
                .unwrap(),
            i256::from_parts(4, 0)
        );
        assert_eq!(8_u8.div_checked(2_u8).unwrap(), 4_u8);
        assert_eq!(8_u16.div_checked(2_u16).unwrap(), 4_u16);
        assert_eq!(8_u32.div_checked(2_u32).unwrap(), 4_u32);
        assert_eq!(8_u64.div_checked(2_u64).unwrap(), 4_u64);
        assert_eq!(
            f16::from_f32(8.0).div_checked(f16::from_f32(2.0)).unwrap(),
            f16::from_f32(4.0)
        );
        assert_eq!(8.0_f32.div_checked(2.0_f32).unwrap(), 4_f32);
        assert_eq!(8.0_f64.div_checked(2.0_f64).unwrap(), 4_f64);
    }

    #[test]
    fn test_native_type_mod() {
        // mod_wrapping
        assert_eq!(9_i8.mod_wrapping(2_i8), 1_i8);
        assert_eq!(9_i16.mod_wrapping(2_i16), 1_i16);
        assert_eq!(9_i32.mod_wrapping(2_i32), 1_i32);
        assert_eq!(9_i64.mod_wrapping(2_i64), 1_i64);
        assert_eq!(9_i128.mod_wrapping(2_i128), 1_i128);
        assert_eq!(
            i256::from_parts(9, 0).mod_wrapping(i256::from_parts(2, 0)),
            i256::from_parts(1, 0)
        );
        assert_eq!(9_u8.mod_wrapping(2_u8), 1_u8);
        assert_eq!(9_u16.mod_wrapping(2_u16), 1_u16);
        assert_eq!(9_u32.mod_wrapping(2_u32), 1_u32);
        assert_eq!(9_u64.mod_wrapping(2_u64), 1_u64);
        assert_eq!(
            f16::from_f32(9.0).mod_wrapping(f16::from_f32(2.0)),
            f16::from_f32(1.0)
        );
        assert_eq!(9.0_f32.mod_wrapping(2.0_f32), 1_f32);
        assert_eq!(9.0_f64.mod_wrapping(2.0_f64), 1_f64);

        // mod_checked
        assert_eq!(9_i8.mod_checked(2_i8).unwrap(), 1_i8);
        assert_eq!(9_i16.mod_checked(2_i16).unwrap(), 1_i16);
        assert_eq!(9_i32.mod_checked(2_i32).unwrap(), 1_i32);
        assert_eq!(9_i64.mod_checked(2_i64).unwrap(), 1_i64);
        assert_eq!(9_i128.mod_checked(2_i128).unwrap(), 1_i128);
        assert_eq!(
            i256::from_parts(9, 0)
                .mod_checked(i256::from_parts(2, 0))
                .unwrap(),
            i256::from_parts(1, 0)
        );
        assert_eq!(9_u8.mod_checked(2_u8).unwrap(), 1_u8);
        assert_eq!(9_u16.mod_checked(2_u16).unwrap(), 1_u16);
        assert_eq!(9_u32.mod_checked(2_u32).unwrap(), 1_u32);
        assert_eq!(9_u64.mod_checked(2_u64).unwrap(), 1_u64);
        assert_eq!(
            f16::from_f32(9.0).mod_checked(f16::from_f32(2.0)).unwrap(),
            f16::from_f32(1.0)
        );
        assert_eq!(9.0_f32.mod_checked(2.0_f32).unwrap(), 1_f32);
        assert_eq!(9.0_f64.mod_checked(2.0_f64).unwrap(), 1_f64);
    }

    #[test]
    fn test_native_type_neg() {
        // neg_wrapping
        assert_eq!(8_i8.neg_wrapping(), -8_i8);
        assert_eq!(8_i16.neg_wrapping(), -8_i16);
        assert_eq!(8_i32.neg_wrapping(), -8_i32);
        assert_eq!(8_i64.neg_wrapping(), -8_i64);
        assert_eq!(8_i128.neg_wrapping(), -8_i128);
        assert_eq!(i256::from_parts(8, 0).neg_wrapping(), i256::from_i128(-8));
        assert_eq!(8_u8.neg_wrapping(), u8::MAX - 7_u8);
        assert_eq!(8_u16.neg_wrapping(), u16::MAX - 7_u16);
        assert_eq!(8_u32.neg_wrapping(), u32::MAX - 7_u32);
        assert_eq!(8_u64.neg_wrapping(), u64::MAX - 7_u64);
        assert_eq!(f16::from_f32(8.0).neg_wrapping(), f16::from_f32(-8.0));
        assert_eq!(8.0_f32.neg_wrapping(), -8_f32);
        assert_eq!(8.0_f64.neg_wrapping(), -8_f64);

        // neg_checked
        assert_eq!(8_i8.neg_checked().unwrap(), -8_i8);
        assert_eq!(8_i16.neg_checked().unwrap(), -8_i16);
        assert_eq!(8_i32.neg_checked().unwrap(), -8_i32);
        assert_eq!(8_i64.neg_checked().unwrap(), -8_i64);
        assert_eq!(8_i128.neg_checked().unwrap(), -8_i128);
        assert_eq!(
            i256::from_parts(8, 0).neg_checked().unwrap(),
            i256::from_i128(-8)
        );
        assert!(8_u8.neg_checked().is_err());
        assert!(8_u16.neg_checked().is_err());
        assert!(8_u32.neg_checked().is_err());
        assert!(8_u64.neg_checked().is_err());
        assert_eq!(
            f16::from_f32(8.0).neg_checked().unwrap(),
            f16::from_f32(-8.0)
        );
        assert_eq!(8.0_f32.neg_checked().unwrap(), -8_f32);
        assert_eq!(8.0_f64.neg_checked().unwrap(), -8_f64);
    }

    #[test]
    fn test_native_type_pow() {
        // pow_wrapping
        assert_eq!(8_i8.pow_wrapping(2_u32), 64_i8);
        assert_eq!(8_i16.pow_wrapping(2_u32), 64_i16);
        assert_eq!(8_i32.pow_wrapping(2_u32), 64_i32);
        assert_eq!(8_i64.pow_wrapping(2_u32), 64_i64);
        assert_eq!(8_i128.pow_wrapping(2_u32), 64_i128);
        assert_eq!(
            i256::from_parts(8, 0).pow_wrapping(2_u32),
            i256::from_parts(64, 0)
        );
        assert_eq!(8_u8.pow_wrapping(2_u32), 64_u8);
        assert_eq!(8_u16.pow_wrapping(2_u32), 64_u16);
        assert_eq!(8_u32.pow_wrapping(2_u32), 64_u32);
        assert_eq!(8_u64.pow_wrapping(2_u32), 64_u64);
        assert_eq!(f16::from_f32(8.0).pow_wrapping(2_u32), f16::from_f32(64.0));
        assert_eq!(8.0_f32.pow_wrapping(2_u32), 64_f32);
        assert_eq!(8.0_f64.pow_wrapping(2_u32), 64_f64);

        // pow_checked
        assert_eq!(8_i8.pow_checked(2_u32).unwrap(), 64_i8);
        assert_eq!(8_i16.pow_checked(2_u32).unwrap(), 64_i16);
        assert_eq!(8_i32.pow_checked(2_u32).unwrap(), 64_i32);
        assert_eq!(8_i64.pow_checked(2_u32).unwrap(), 64_i64);
        assert_eq!(8_i128.pow_checked(2_u32).unwrap(), 64_i128);
        assert_eq!(
            i256::from_parts(8, 0).pow_checked(2_u32).unwrap(),
            i256::from_parts(64, 0)
        );
        assert_eq!(8_u8.pow_checked(2_u32).unwrap(), 64_u8);
        assert_eq!(8_u16.pow_checked(2_u32).unwrap(), 64_u16);
        assert_eq!(8_u32.pow_checked(2_u32).unwrap(), 64_u32);
        assert_eq!(8_u64.pow_checked(2_u32).unwrap(), 64_u64);
        assert_eq!(
            f16::from_f32(8.0).pow_checked(2_u32).unwrap(),
            f16::from_f32(64.0)
        );
        assert_eq!(8.0_f32.pow_checked(2_u32).unwrap(), 64_f32);
        assert_eq!(8.0_f64.pow_checked(2_u32).unwrap(), 64_f64);
    }

    #[test]
    fn test_float_total_order_min_max() {
        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f64::NEG_INFINITY));
        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::INFINITY));

        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f64::NAN));

        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::NAN));

        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f32::NEG_INFINITY));
        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::INFINITY));

        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f32::NAN));

        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::NAN));

        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f16::NEG_INFINITY));
        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::INFINITY));

        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f16::NAN));

        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::NAN));
    }
}