arrow_array/
arithmetic.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use arrow_buffer::{i256, ArrowNativeType, IntervalDayTime, IntervalMonthDayNano};
19use arrow_schema::ArrowError;
20use half::f16;
21use num::complex::ComplexFloat;
22use std::cmp::Ordering;
23
24/// Trait for [`ArrowNativeType`] that adds checked and unchecked arithmetic operations,
25/// and totally ordered comparison operations
26///
27/// The APIs with `_wrapping` suffix do not perform overflow-checking. For integer
28/// types they will wrap around the boundary of the type. For floating point types they
29/// will overflow to INF or -INF preserving the expected sign value
30///
31/// Note `div_wrapping` and `mod_wrapping` will panic for integer types if `rhs` is zero
32/// although this may be subject to change <https://github.com/apache/arrow-rs/issues/2647>
33///
34/// The APIs with `_checked` suffix perform overflow-checking. For integer types
35/// these will return `Err` instead of wrapping. For floating point types they will
36/// overflow to INF or -INF preserving the expected sign value
37///
38/// Comparison of integer types is as per normal integer comparison rules, floating
39/// point values are compared as per IEEE 754's totalOrder predicate see [`f32::total_cmp`]
40///
41pub trait ArrowNativeTypeOp: ArrowNativeType {
42    /// The additive identity
43    const ZERO: Self;
44
45    /// The multiplicative identity
46    const ONE: Self;
47
48    /// The minimum value and identity for the `max` aggregation.
49    /// Note that the aggregation uses the total order predicate for floating point values,
50    /// which means that this value is a negative NaN.
51    const MIN_TOTAL_ORDER: Self;
52
53    /// The maximum value and identity for the `min` aggregation.
54    /// Note that the aggregation uses the total order predicate for floating point values,
55    /// which means that this value is a positive NaN.
56    const MAX_TOTAL_ORDER: Self;
57
58    /// Checked addition operation
59    fn add_checked(self, rhs: Self) -> Result<Self, ArrowError>;
60
61    /// Wrapping addition operation
62    fn add_wrapping(self, rhs: Self) -> Self;
63
64    /// Checked subtraction operation
65    fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError>;
66
67    /// Wrapping subtraction operation
68    fn sub_wrapping(self, rhs: Self) -> Self;
69
70    /// Checked multiplication operation
71    fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError>;
72
73    /// Wrapping multiplication operation
74    fn mul_wrapping(self, rhs: Self) -> Self;
75
76    /// Checked division operation
77    fn div_checked(self, rhs: Self) -> Result<Self, ArrowError>;
78
79    /// Wrapping division operation
80    fn div_wrapping(self, rhs: Self) -> Self;
81
82    /// Checked remainder operation
83    fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError>;
84
85    /// Wrapping remainder operation
86    fn mod_wrapping(self, rhs: Self) -> Self;
87
88    /// Checked negation operation
89    fn neg_checked(self) -> Result<Self, ArrowError>;
90
91    /// Wrapping negation operation
92    fn neg_wrapping(self) -> Self;
93
94    /// Checked exponentiation operation
95    fn pow_checked(self, exp: u32) -> Result<Self, ArrowError>;
96
97    /// Wrapping exponentiation operation
98    fn pow_wrapping(self, exp: u32) -> Self;
99
100    /// Returns true if zero else false
101    fn is_zero(self) -> bool;
102
103    /// Compare operation
104    fn compare(self, rhs: Self) -> Ordering;
105
106    /// Equality operation
107    fn is_eq(self, rhs: Self) -> bool;
108
109    /// Not equal operation
110    #[inline]
111    fn is_ne(self, rhs: Self) -> bool {
112        !self.is_eq(rhs)
113    }
114
115    /// Less than operation
116    #[inline]
117    fn is_lt(self, rhs: Self) -> bool {
118        self.compare(rhs).is_lt()
119    }
120
121    /// Less than equals operation
122    #[inline]
123    fn is_le(self, rhs: Self) -> bool {
124        self.compare(rhs).is_le()
125    }
126
127    /// Greater than operation
128    #[inline]
129    fn is_gt(self, rhs: Self) -> bool {
130        self.compare(rhs).is_gt()
131    }
132
133    /// Greater than equals operation
134    #[inline]
135    fn is_ge(self, rhs: Self) -> bool {
136        self.compare(rhs).is_ge()
137    }
138}
139
140macro_rules! native_type_op {
141    ($t:tt) => {
142        native_type_op!($t, 0, 1);
143    };
144    ($t:tt, $zero:expr, $one: expr) => {
145        native_type_op!($t, $zero, $one, $t::MIN, $t::MAX);
146    };
147    ($t:tt, $zero:expr, $one: expr, $min: expr, $max: expr) => {
148        impl ArrowNativeTypeOp for $t {
149            const ZERO: Self = $zero;
150            const ONE: Self = $one;
151            const MIN_TOTAL_ORDER: Self = $min;
152            const MAX_TOTAL_ORDER: Self = $max;
153
154            #[inline]
155            fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
156                self.checked_add(rhs).ok_or_else(|| {
157                    ArrowError::ArithmeticOverflow(format!(
158                        "Overflow happened on: {:?} + {:?}",
159                        self, rhs
160                    ))
161                })
162            }
163
164            #[inline]
165            fn add_wrapping(self, rhs: Self) -> Self {
166                self.wrapping_add(rhs)
167            }
168
169            #[inline]
170            fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
171                self.checked_sub(rhs).ok_or_else(|| {
172                    ArrowError::ArithmeticOverflow(format!(
173                        "Overflow happened on: {:?} - {:?}",
174                        self, rhs
175                    ))
176                })
177            }
178
179            #[inline]
180            fn sub_wrapping(self, rhs: Self) -> Self {
181                self.wrapping_sub(rhs)
182            }
183
184            #[inline]
185            fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
186                self.checked_mul(rhs).ok_or_else(|| {
187                    ArrowError::ArithmeticOverflow(format!(
188                        "Overflow happened on: {:?} * {:?}",
189                        self, rhs
190                    ))
191                })
192            }
193
194            #[inline]
195            fn mul_wrapping(self, rhs: Self) -> Self {
196                self.wrapping_mul(rhs)
197            }
198
199            #[inline]
200            fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
201                if rhs.is_zero() {
202                    Err(ArrowError::DivideByZero)
203                } else {
204                    self.checked_div(rhs).ok_or_else(|| {
205                        ArrowError::ArithmeticOverflow(format!(
206                            "Overflow happened on: {:?} / {:?}",
207                            self, rhs
208                        ))
209                    })
210                }
211            }
212
213            #[inline]
214            fn div_wrapping(self, rhs: Self) -> Self {
215                self.wrapping_div(rhs)
216            }
217
218            #[inline]
219            fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
220                if rhs.is_zero() {
221                    Err(ArrowError::DivideByZero)
222                } else {
223                    self.checked_rem(rhs).ok_or_else(|| {
224                        ArrowError::ArithmeticOverflow(format!(
225                            "Overflow happened on: {:?} % {:?}",
226                            self, rhs
227                        ))
228                    })
229                }
230            }
231
232            #[inline]
233            fn mod_wrapping(self, rhs: Self) -> Self {
234                self.wrapping_rem(rhs)
235            }
236
237            #[inline]
238            fn neg_checked(self) -> Result<Self, ArrowError> {
239                self.checked_neg().ok_or_else(|| {
240                    ArrowError::ArithmeticOverflow(format!("Overflow happened on: - {:?}", self))
241                })
242            }
243
244            #[inline]
245            fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
246                self.checked_pow(exp).ok_or_else(|| {
247                    ArrowError::ArithmeticOverflow(format!(
248                        "Overflow happened on: {:?} ^ {exp:?}",
249                        self
250                    ))
251                })
252            }
253
254            #[inline]
255            fn pow_wrapping(self, exp: u32) -> Self {
256                self.wrapping_pow(exp)
257            }
258
259            #[inline]
260            fn neg_wrapping(self) -> Self {
261                self.wrapping_neg()
262            }
263
264            #[inline]
265            fn is_zero(self) -> bool {
266                self == Self::ZERO
267            }
268
269            #[inline]
270            fn compare(self, rhs: Self) -> Ordering {
271                self.cmp(&rhs)
272            }
273
274            #[inline]
275            fn is_eq(self, rhs: Self) -> bool {
276                self == rhs
277            }
278        }
279    };
280}
281
282native_type_op!(i8);
283native_type_op!(i16);
284native_type_op!(i32);
285native_type_op!(i64);
286native_type_op!(i128);
287native_type_op!(u8);
288native_type_op!(u16);
289native_type_op!(u32);
290native_type_op!(u64);
291native_type_op!(i256, i256::ZERO, i256::ONE, i256::MIN, i256::MAX);
292
293native_type_op!(IntervalDayTime, IntervalDayTime::ZERO, IntervalDayTime::ONE);
294native_type_op!(
295    IntervalMonthDayNano,
296    IntervalMonthDayNano::ZERO,
297    IntervalMonthDayNano::ONE
298);
299
300macro_rules! native_type_float_op {
301    ($t:tt, $zero:expr, $one:expr, $min:expr, $max:expr) => {
302        impl ArrowNativeTypeOp for $t {
303            const ZERO: Self = $zero;
304            const ONE: Self = $one;
305            const MIN_TOTAL_ORDER: Self = $min;
306            const MAX_TOTAL_ORDER: Self = $max;
307
308            #[inline]
309            fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
310                Ok(self + rhs)
311            }
312
313            #[inline]
314            fn add_wrapping(self, rhs: Self) -> Self {
315                self + rhs
316            }
317
318            #[inline]
319            fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
320                Ok(self - rhs)
321            }
322
323            #[inline]
324            fn sub_wrapping(self, rhs: Self) -> Self {
325                self - rhs
326            }
327
328            #[inline]
329            fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
330                Ok(self * rhs)
331            }
332
333            #[inline]
334            fn mul_wrapping(self, rhs: Self) -> Self {
335                self * rhs
336            }
337
338            #[inline]
339            fn div_checked(self, rhs: Self) -> Result<Self, ArrowError> {
340                if rhs.is_zero() {
341                    Err(ArrowError::DivideByZero)
342                } else {
343                    Ok(self / rhs)
344                }
345            }
346
347            #[inline]
348            fn div_wrapping(self, rhs: Self) -> Self {
349                self / rhs
350            }
351
352            #[inline]
353            fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError> {
354                if rhs.is_zero() {
355                    Err(ArrowError::DivideByZero)
356                } else {
357                    Ok(self % rhs)
358                }
359            }
360
361            #[inline]
362            fn mod_wrapping(self, rhs: Self) -> Self {
363                self % rhs
364            }
365
366            #[inline]
367            fn neg_checked(self) -> Result<Self, ArrowError> {
368                Ok(-self)
369            }
370
371            #[inline]
372            fn neg_wrapping(self) -> Self {
373                -self
374            }
375
376            #[inline]
377            fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
378                Ok(self.powi(exp as i32))
379            }
380
381            #[inline]
382            fn pow_wrapping(self, exp: u32) -> Self {
383                self.powi(exp as i32)
384            }
385
386            #[inline]
387            fn is_zero(self) -> bool {
388                self == $zero
389            }
390
391            #[inline]
392            fn compare(self, rhs: Self) -> Ordering {
393                <$t>::total_cmp(&self, &rhs)
394            }
395
396            #[inline]
397            fn is_eq(self, rhs: Self) -> bool {
398                // Equivalent to `self.total_cmp(&rhs).is_eq()`
399                // but LLVM isn't able to realise this is bitwise equality
400                // https://rust.godbolt.org/z/347nWGxoW
401                self.to_bits() == rhs.to_bits()
402            }
403        }
404    };
405}
406
407// the smallest/largest bit patterns for floating point numbers are NaN, but differ from the canonical NAN constants.
408// See test_float_total_order_min_max for details.
409native_type_float_op!(
410    f16,
411    f16::ZERO,
412    f16::ONE,
413    f16::from_bits(-1 as _),
414    f16::from_bits(i16::MAX as _)
415);
416// from_bits is not yet stable as const fn, see https://github.com/rust-lang/rust/issues/72447
417native_type_float_op!(
418    f32,
419    0.,
420    1.,
421    unsafe { std::mem::transmute(-1_i32) },
422    unsafe { std::mem::transmute(i32::MAX) }
423);
424native_type_float_op!(
425    f64,
426    0.,
427    1.,
428    unsafe { std::mem::transmute(-1_i64) },
429    unsafe { std::mem::transmute(i64::MAX) }
430);
431
432#[cfg(test)]
433mod tests {
434    use super::*;
435
436    #[test]
437    fn test_native_type_is_zero() {
438        assert!(0_i8.is_zero());
439        assert!(0_i16.is_zero());
440        assert!(0_i32.is_zero());
441        assert!(0_i64.is_zero());
442        assert!(0_i128.is_zero());
443        assert!(i256::ZERO.is_zero());
444        assert!(0_u8.is_zero());
445        assert!(0_u16.is_zero());
446        assert!(0_u32.is_zero());
447        assert!(0_u64.is_zero());
448        assert!(f16::ZERO.is_zero());
449        assert!(0.0_f32.is_zero());
450        assert!(0.0_f64.is_zero());
451    }
452
453    #[test]
454    fn test_native_type_comparison() {
455        // is_eq
456        assert!(8_i8.is_eq(8_i8));
457        assert!(8_i16.is_eq(8_i16));
458        assert!(8_i32.is_eq(8_i32));
459        assert!(8_i64.is_eq(8_i64));
460        assert!(8_i128.is_eq(8_i128));
461        assert!(i256::from_parts(8, 0).is_eq(i256::from_parts(8, 0)));
462        assert!(8_u8.is_eq(8_u8));
463        assert!(8_u16.is_eq(8_u16));
464        assert!(8_u32.is_eq(8_u32));
465        assert!(8_u64.is_eq(8_u64));
466        assert!(f16::from_f32(8.0).is_eq(f16::from_f32(8.0)));
467        assert!(8.0_f32.is_eq(8.0_f32));
468        assert!(8.0_f64.is_eq(8.0_f64));
469
470        // is_ne
471        assert!(8_i8.is_ne(1_i8));
472        assert!(8_i16.is_ne(1_i16));
473        assert!(8_i32.is_ne(1_i32));
474        assert!(8_i64.is_ne(1_i64));
475        assert!(8_i128.is_ne(1_i128));
476        assert!(i256::from_parts(8, 0).is_ne(i256::from_parts(1, 0)));
477        assert!(8_u8.is_ne(1_u8));
478        assert!(8_u16.is_ne(1_u16));
479        assert!(8_u32.is_ne(1_u32));
480        assert!(8_u64.is_ne(1_u64));
481        assert!(f16::from_f32(8.0).is_ne(f16::from_f32(1.0)));
482        assert!(8.0_f32.is_ne(1.0_f32));
483        assert!(8.0_f64.is_ne(1.0_f64));
484
485        // is_lt
486        assert!(8_i8.is_lt(10_i8));
487        assert!(8_i16.is_lt(10_i16));
488        assert!(8_i32.is_lt(10_i32));
489        assert!(8_i64.is_lt(10_i64));
490        assert!(8_i128.is_lt(10_i128));
491        assert!(i256::from_parts(8, 0).is_lt(i256::from_parts(10, 0)));
492        assert!(8_u8.is_lt(10_u8));
493        assert!(8_u16.is_lt(10_u16));
494        assert!(8_u32.is_lt(10_u32));
495        assert!(8_u64.is_lt(10_u64));
496        assert!(f16::from_f32(8.0).is_lt(f16::from_f32(10.0)));
497        assert!(8.0_f32.is_lt(10.0_f32));
498        assert!(8.0_f64.is_lt(10.0_f64));
499
500        // is_gt
501        assert!(8_i8.is_gt(1_i8));
502        assert!(8_i16.is_gt(1_i16));
503        assert!(8_i32.is_gt(1_i32));
504        assert!(8_i64.is_gt(1_i64));
505        assert!(8_i128.is_gt(1_i128));
506        assert!(i256::from_parts(8, 0).is_gt(i256::from_parts(1, 0)));
507        assert!(8_u8.is_gt(1_u8));
508        assert!(8_u16.is_gt(1_u16));
509        assert!(8_u32.is_gt(1_u32));
510        assert!(8_u64.is_gt(1_u64));
511        assert!(f16::from_f32(8.0).is_gt(f16::from_f32(1.0)));
512        assert!(8.0_f32.is_gt(1.0_f32));
513        assert!(8.0_f64.is_gt(1.0_f64));
514    }
515
516    #[test]
517    fn test_native_type_add() {
518        // add_wrapping
519        assert_eq!(8_i8.add_wrapping(2_i8), 10_i8);
520        assert_eq!(8_i16.add_wrapping(2_i16), 10_i16);
521        assert_eq!(8_i32.add_wrapping(2_i32), 10_i32);
522        assert_eq!(8_i64.add_wrapping(2_i64), 10_i64);
523        assert_eq!(8_i128.add_wrapping(2_i128), 10_i128);
524        assert_eq!(
525            i256::from_parts(8, 0).add_wrapping(i256::from_parts(2, 0)),
526            i256::from_parts(10, 0)
527        );
528        assert_eq!(8_u8.add_wrapping(2_u8), 10_u8);
529        assert_eq!(8_u16.add_wrapping(2_u16), 10_u16);
530        assert_eq!(8_u32.add_wrapping(2_u32), 10_u32);
531        assert_eq!(8_u64.add_wrapping(2_u64), 10_u64);
532        assert_eq!(
533            f16::from_f32(8.0).add_wrapping(f16::from_f32(2.0)),
534            f16::from_f32(10.0)
535        );
536        assert_eq!(8.0_f32.add_wrapping(2.0_f32), 10_f32);
537        assert_eq!(8.0_f64.add_wrapping(2.0_f64), 10_f64);
538
539        // add_checked
540        assert_eq!(8_i8.add_checked(2_i8).unwrap(), 10_i8);
541        assert_eq!(8_i16.add_checked(2_i16).unwrap(), 10_i16);
542        assert_eq!(8_i32.add_checked(2_i32).unwrap(), 10_i32);
543        assert_eq!(8_i64.add_checked(2_i64).unwrap(), 10_i64);
544        assert_eq!(8_i128.add_checked(2_i128).unwrap(), 10_i128);
545        assert_eq!(
546            i256::from_parts(8, 0)
547                .add_checked(i256::from_parts(2, 0))
548                .unwrap(),
549            i256::from_parts(10, 0)
550        );
551        assert_eq!(8_u8.add_checked(2_u8).unwrap(), 10_u8);
552        assert_eq!(8_u16.add_checked(2_u16).unwrap(), 10_u16);
553        assert_eq!(8_u32.add_checked(2_u32).unwrap(), 10_u32);
554        assert_eq!(8_u64.add_checked(2_u64).unwrap(), 10_u64);
555        assert_eq!(
556            f16::from_f32(8.0).add_checked(f16::from_f32(2.0)).unwrap(),
557            f16::from_f32(10.0)
558        );
559        assert_eq!(8.0_f32.add_checked(2.0_f32).unwrap(), 10_f32);
560        assert_eq!(8.0_f64.add_checked(2.0_f64).unwrap(), 10_f64);
561    }
562
563    #[test]
564    fn test_native_type_sub() {
565        // sub_wrapping
566        assert_eq!(8_i8.sub_wrapping(2_i8), 6_i8);
567        assert_eq!(8_i16.sub_wrapping(2_i16), 6_i16);
568        assert_eq!(8_i32.sub_wrapping(2_i32), 6_i32);
569        assert_eq!(8_i64.sub_wrapping(2_i64), 6_i64);
570        assert_eq!(8_i128.sub_wrapping(2_i128), 6_i128);
571        assert_eq!(
572            i256::from_parts(8, 0).sub_wrapping(i256::from_parts(2, 0)),
573            i256::from_parts(6, 0)
574        );
575        assert_eq!(8_u8.sub_wrapping(2_u8), 6_u8);
576        assert_eq!(8_u16.sub_wrapping(2_u16), 6_u16);
577        assert_eq!(8_u32.sub_wrapping(2_u32), 6_u32);
578        assert_eq!(8_u64.sub_wrapping(2_u64), 6_u64);
579        assert_eq!(
580            f16::from_f32(8.0).sub_wrapping(f16::from_f32(2.0)),
581            f16::from_f32(6.0)
582        );
583        assert_eq!(8.0_f32.sub_wrapping(2.0_f32), 6_f32);
584        assert_eq!(8.0_f64.sub_wrapping(2.0_f64), 6_f64);
585
586        // sub_checked
587        assert_eq!(8_i8.sub_checked(2_i8).unwrap(), 6_i8);
588        assert_eq!(8_i16.sub_checked(2_i16).unwrap(), 6_i16);
589        assert_eq!(8_i32.sub_checked(2_i32).unwrap(), 6_i32);
590        assert_eq!(8_i64.sub_checked(2_i64).unwrap(), 6_i64);
591        assert_eq!(8_i128.sub_checked(2_i128).unwrap(), 6_i128);
592        assert_eq!(
593            i256::from_parts(8, 0)
594                .sub_checked(i256::from_parts(2, 0))
595                .unwrap(),
596            i256::from_parts(6, 0)
597        );
598        assert_eq!(8_u8.sub_checked(2_u8).unwrap(), 6_u8);
599        assert_eq!(8_u16.sub_checked(2_u16).unwrap(), 6_u16);
600        assert_eq!(8_u32.sub_checked(2_u32).unwrap(), 6_u32);
601        assert_eq!(8_u64.sub_checked(2_u64).unwrap(), 6_u64);
602        assert_eq!(
603            f16::from_f32(8.0).sub_checked(f16::from_f32(2.0)).unwrap(),
604            f16::from_f32(6.0)
605        );
606        assert_eq!(8.0_f32.sub_checked(2.0_f32).unwrap(), 6_f32);
607        assert_eq!(8.0_f64.sub_checked(2.0_f64).unwrap(), 6_f64);
608    }
609
610    #[test]
611    fn test_native_type_mul() {
612        // mul_wrapping
613        assert_eq!(8_i8.mul_wrapping(2_i8), 16_i8);
614        assert_eq!(8_i16.mul_wrapping(2_i16), 16_i16);
615        assert_eq!(8_i32.mul_wrapping(2_i32), 16_i32);
616        assert_eq!(8_i64.mul_wrapping(2_i64), 16_i64);
617        assert_eq!(8_i128.mul_wrapping(2_i128), 16_i128);
618        assert_eq!(
619            i256::from_parts(8, 0).mul_wrapping(i256::from_parts(2, 0)),
620            i256::from_parts(16, 0)
621        );
622        assert_eq!(8_u8.mul_wrapping(2_u8), 16_u8);
623        assert_eq!(8_u16.mul_wrapping(2_u16), 16_u16);
624        assert_eq!(8_u32.mul_wrapping(2_u32), 16_u32);
625        assert_eq!(8_u64.mul_wrapping(2_u64), 16_u64);
626        assert_eq!(
627            f16::from_f32(8.0).mul_wrapping(f16::from_f32(2.0)),
628            f16::from_f32(16.0)
629        );
630        assert_eq!(8.0_f32.mul_wrapping(2.0_f32), 16_f32);
631        assert_eq!(8.0_f64.mul_wrapping(2.0_f64), 16_f64);
632
633        // mul_checked
634        assert_eq!(8_i8.mul_checked(2_i8).unwrap(), 16_i8);
635        assert_eq!(8_i16.mul_checked(2_i16).unwrap(), 16_i16);
636        assert_eq!(8_i32.mul_checked(2_i32).unwrap(), 16_i32);
637        assert_eq!(8_i64.mul_checked(2_i64).unwrap(), 16_i64);
638        assert_eq!(8_i128.mul_checked(2_i128).unwrap(), 16_i128);
639        assert_eq!(
640            i256::from_parts(8, 0)
641                .mul_checked(i256::from_parts(2, 0))
642                .unwrap(),
643            i256::from_parts(16, 0)
644        );
645        assert_eq!(8_u8.mul_checked(2_u8).unwrap(), 16_u8);
646        assert_eq!(8_u16.mul_checked(2_u16).unwrap(), 16_u16);
647        assert_eq!(8_u32.mul_checked(2_u32).unwrap(), 16_u32);
648        assert_eq!(8_u64.mul_checked(2_u64).unwrap(), 16_u64);
649        assert_eq!(
650            f16::from_f32(8.0).mul_checked(f16::from_f32(2.0)).unwrap(),
651            f16::from_f32(16.0)
652        );
653        assert_eq!(8.0_f32.mul_checked(2.0_f32).unwrap(), 16_f32);
654        assert_eq!(8.0_f64.mul_checked(2.0_f64).unwrap(), 16_f64);
655    }
656
657    #[test]
658    fn test_native_type_div() {
659        // div_wrapping
660        assert_eq!(8_i8.div_wrapping(2_i8), 4_i8);
661        assert_eq!(8_i16.div_wrapping(2_i16), 4_i16);
662        assert_eq!(8_i32.div_wrapping(2_i32), 4_i32);
663        assert_eq!(8_i64.div_wrapping(2_i64), 4_i64);
664        assert_eq!(8_i128.div_wrapping(2_i128), 4_i128);
665        assert_eq!(
666            i256::from_parts(8, 0).div_wrapping(i256::from_parts(2, 0)),
667            i256::from_parts(4, 0)
668        );
669        assert_eq!(8_u8.div_wrapping(2_u8), 4_u8);
670        assert_eq!(8_u16.div_wrapping(2_u16), 4_u16);
671        assert_eq!(8_u32.div_wrapping(2_u32), 4_u32);
672        assert_eq!(8_u64.div_wrapping(2_u64), 4_u64);
673        assert_eq!(
674            f16::from_f32(8.0).div_wrapping(f16::from_f32(2.0)),
675            f16::from_f32(4.0)
676        );
677        assert_eq!(8.0_f32.div_wrapping(2.0_f32), 4_f32);
678        assert_eq!(8.0_f64.div_wrapping(2.0_f64), 4_f64);
679
680        // div_checked
681        assert_eq!(8_i8.div_checked(2_i8).unwrap(), 4_i8);
682        assert_eq!(8_i16.div_checked(2_i16).unwrap(), 4_i16);
683        assert_eq!(8_i32.div_checked(2_i32).unwrap(), 4_i32);
684        assert_eq!(8_i64.div_checked(2_i64).unwrap(), 4_i64);
685        assert_eq!(8_i128.div_checked(2_i128).unwrap(), 4_i128);
686        assert_eq!(
687            i256::from_parts(8, 0)
688                .div_checked(i256::from_parts(2, 0))
689                .unwrap(),
690            i256::from_parts(4, 0)
691        );
692        assert_eq!(8_u8.div_checked(2_u8).unwrap(), 4_u8);
693        assert_eq!(8_u16.div_checked(2_u16).unwrap(), 4_u16);
694        assert_eq!(8_u32.div_checked(2_u32).unwrap(), 4_u32);
695        assert_eq!(8_u64.div_checked(2_u64).unwrap(), 4_u64);
696        assert_eq!(
697            f16::from_f32(8.0).div_checked(f16::from_f32(2.0)).unwrap(),
698            f16::from_f32(4.0)
699        );
700        assert_eq!(8.0_f32.div_checked(2.0_f32).unwrap(), 4_f32);
701        assert_eq!(8.0_f64.div_checked(2.0_f64).unwrap(), 4_f64);
702    }
703
704    #[test]
705    fn test_native_type_mod() {
706        // mod_wrapping
707        assert_eq!(9_i8.mod_wrapping(2_i8), 1_i8);
708        assert_eq!(9_i16.mod_wrapping(2_i16), 1_i16);
709        assert_eq!(9_i32.mod_wrapping(2_i32), 1_i32);
710        assert_eq!(9_i64.mod_wrapping(2_i64), 1_i64);
711        assert_eq!(9_i128.mod_wrapping(2_i128), 1_i128);
712        assert_eq!(
713            i256::from_parts(9, 0).mod_wrapping(i256::from_parts(2, 0)),
714            i256::from_parts(1, 0)
715        );
716        assert_eq!(9_u8.mod_wrapping(2_u8), 1_u8);
717        assert_eq!(9_u16.mod_wrapping(2_u16), 1_u16);
718        assert_eq!(9_u32.mod_wrapping(2_u32), 1_u32);
719        assert_eq!(9_u64.mod_wrapping(2_u64), 1_u64);
720        assert_eq!(
721            f16::from_f32(9.0).mod_wrapping(f16::from_f32(2.0)),
722            f16::from_f32(1.0)
723        );
724        assert_eq!(9.0_f32.mod_wrapping(2.0_f32), 1_f32);
725        assert_eq!(9.0_f64.mod_wrapping(2.0_f64), 1_f64);
726
727        // mod_checked
728        assert_eq!(9_i8.mod_checked(2_i8).unwrap(), 1_i8);
729        assert_eq!(9_i16.mod_checked(2_i16).unwrap(), 1_i16);
730        assert_eq!(9_i32.mod_checked(2_i32).unwrap(), 1_i32);
731        assert_eq!(9_i64.mod_checked(2_i64).unwrap(), 1_i64);
732        assert_eq!(9_i128.mod_checked(2_i128).unwrap(), 1_i128);
733        assert_eq!(
734            i256::from_parts(9, 0)
735                .mod_checked(i256::from_parts(2, 0))
736                .unwrap(),
737            i256::from_parts(1, 0)
738        );
739        assert_eq!(9_u8.mod_checked(2_u8).unwrap(), 1_u8);
740        assert_eq!(9_u16.mod_checked(2_u16).unwrap(), 1_u16);
741        assert_eq!(9_u32.mod_checked(2_u32).unwrap(), 1_u32);
742        assert_eq!(9_u64.mod_checked(2_u64).unwrap(), 1_u64);
743        assert_eq!(
744            f16::from_f32(9.0).mod_checked(f16::from_f32(2.0)).unwrap(),
745            f16::from_f32(1.0)
746        );
747        assert_eq!(9.0_f32.mod_checked(2.0_f32).unwrap(), 1_f32);
748        assert_eq!(9.0_f64.mod_checked(2.0_f64).unwrap(), 1_f64);
749    }
750
751    #[test]
752    fn test_native_type_neg() {
753        // neg_wrapping
754        assert_eq!(8_i8.neg_wrapping(), -8_i8);
755        assert_eq!(8_i16.neg_wrapping(), -8_i16);
756        assert_eq!(8_i32.neg_wrapping(), -8_i32);
757        assert_eq!(8_i64.neg_wrapping(), -8_i64);
758        assert_eq!(8_i128.neg_wrapping(), -8_i128);
759        assert_eq!(i256::from_parts(8, 0).neg_wrapping(), i256::from_i128(-8));
760        assert_eq!(8_u8.neg_wrapping(), u8::MAX - 7_u8);
761        assert_eq!(8_u16.neg_wrapping(), u16::MAX - 7_u16);
762        assert_eq!(8_u32.neg_wrapping(), u32::MAX - 7_u32);
763        assert_eq!(8_u64.neg_wrapping(), u64::MAX - 7_u64);
764        assert_eq!(f16::from_f32(8.0).neg_wrapping(), f16::from_f32(-8.0));
765        assert_eq!(8.0_f32.neg_wrapping(), -8_f32);
766        assert_eq!(8.0_f64.neg_wrapping(), -8_f64);
767
768        // neg_checked
769        assert_eq!(8_i8.neg_checked().unwrap(), -8_i8);
770        assert_eq!(8_i16.neg_checked().unwrap(), -8_i16);
771        assert_eq!(8_i32.neg_checked().unwrap(), -8_i32);
772        assert_eq!(8_i64.neg_checked().unwrap(), -8_i64);
773        assert_eq!(8_i128.neg_checked().unwrap(), -8_i128);
774        assert_eq!(
775            i256::from_parts(8, 0).neg_checked().unwrap(),
776            i256::from_i128(-8)
777        );
778        assert!(8_u8.neg_checked().is_err());
779        assert!(8_u16.neg_checked().is_err());
780        assert!(8_u32.neg_checked().is_err());
781        assert!(8_u64.neg_checked().is_err());
782        assert_eq!(
783            f16::from_f32(8.0).neg_checked().unwrap(),
784            f16::from_f32(-8.0)
785        );
786        assert_eq!(8.0_f32.neg_checked().unwrap(), -8_f32);
787        assert_eq!(8.0_f64.neg_checked().unwrap(), -8_f64);
788    }
789
790    #[test]
791    fn test_native_type_pow() {
792        // pow_wrapping
793        assert_eq!(8_i8.pow_wrapping(2_u32), 64_i8);
794        assert_eq!(8_i16.pow_wrapping(2_u32), 64_i16);
795        assert_eq!(8_i32.pow_wrapping(2_u32), 64_i32);
796        assert_eq!(8_i64.pow_wrapping(2_u32), 64_i64);
797        assert_eq!(8_i128.pow_wrapping(2_u32), 64_i128);
798        assert_eq!(
799            i256::from_parts(8, 0).pow_wrapping(2_u32),
800            i256::from_parts(64, 0)
801        );
802        assert_eq!(8_u8.pow_wrapping(2_u32), 64_u8);
803        assert_eq!(8_u16.pow_wrapping(2_u32), 64_u16);
804        assert_eq!(8_u32.pow_wrapping(2_u32), 64_u32);
805        assert_eq!(8_u64.pow_wrapping(2_u32), 64_u64);
806        assert_eq!(f16::from_f32(8.0).pow_wrapping(2_u32), f16::from_f32(64.0));
807        assert_eq!(8.0_f32.pow_wrapping(2_u32), 64_f32);
808        assert_eq!(8.0_f64.pow_wrapping(2_u32), 64_f64);
809
810        // pow_checked
811        assert_eq!(8_i8.pow_checked(2_u32).unwrap(), 64_i8);
812        assert_eq!(8_i16.pow_checked(2_u32).unwrap(), 64_i16);
813        assert_eq!(8_i32.pow_checked(2_u32).unwrap(), 64_i32);
814        assert_eq!(8_i64.pow_checked(2_u32).unwrap(), 64_i64);
815        assert_eq!(8_i128.pow_checked(2_u32).unwrap(), 64_i128);
816        assert_eq!(
817            i256::from_parts(8, 0).pow_checked(2_u32).unwrap(),
818            i256::from_parts(64, 0)
819        );
820        assert_eq!(8_u8.pow_checked(2_u32).unwrap(), 64_u8);
821        assert_eq!(8_u16.pow_checked(2_u32).unwrap(), 64_u16);
822        assert_eq!(8_u32.pow_checked(2_u32).unwrap(), 64_u32);
823        assert_eq!(8_u64.pow_checked(2_u32).unwrap(), 64_u64);
824        assert_eq!(
825            f16::from_f32(8.0).pow_checked(2_u32).unwrap(),
826            f16::from_f32(64.0)
827        );
828        assert_eq!(8.0_f32.pow_checked(2_u32).unwrap(), 64_f32);
829        assert_eq!(8.0_f64.pow_checked(2_u32).unwrap(), 64_f64);
830    }
831
832    #[test]
833    fn test_float_total_order_min_max() {
834        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f64::NEG_INFINITY));
835        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::INFINITY));
836
837        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
838        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
839        assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f64::NAN));
840
841        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
842        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
843        assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::NAN));
844
845        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f32::NEG_INFINITY));
846        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::INFINITY));
847
848        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
849        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
850        assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f32::NAN));
851
852        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
853        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
854        assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::NAN));
855
856        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f16::NEG_INFINITY));
857        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::INFINITY));
858
859        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
860        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
861        assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f16::NAN));
862
863        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
864        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
865        assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::NAN));
866    }
867}