1use arrow_buffer::{i256, ArrowNativeType, IntervalDayTime, IntervalMonthDayNano};
19use arrow_schema::ArrowError;
20use half::f16;
21use num::complex::ComplexFloat;
22use std::cmp::Ordering;
23
24pub trait ArrowNativeTypeOp: ArrowNativeType {
42 const ZERO: Self;
44
45 const ONE: Self;
47
48 const MIN_TOTAL_ORDER: Self;
52
53 const MAX_TOTAL_ORDER: Self;
57
58 fn add_checked(self, rhs: Self) -> Result<Self, ArrowError>;
60
61 fn add_wrapping(self, rhs: Self) -> Self;
63
64 fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError>;
66
67 fn sub_wrapping(self, rhs: Self) -> Self;
69
70 fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError>;
72
73 fn mul_wrapping(self, rhs: Self) -> Self;
75
76 fn div_checked(self, rhs: Self) -> Result<Self, ArrowError>;
78
79 fn div_wrapping(self, rhs: Self) -> Self;
81
82 fn mod_checked(self, rhs: Self) -> Result<Self, ArrowError>;
84
85 fn mod_wrapping(self, rhs: Self) -> Self;
87
88 fn neg_checked(self) -> Result<Self, ArrowError>;
90
91 fn neg_wrapping(self) -> Self;
93
94 fn pow_checked(self, exp: u32) -> Result<Self, ArrowError>;
96
97 fn pow_wrapping(self, exp: u32) -> Self;
99
100 fn is_zero(self) -> bool;
102
103 fn compare(self, rhs: Self) -> Ordering;
105
106 fn is_eq(self, rhs: Self) -> bool;
108
109 #[inline]
111 fn is_ne(self, rhs: Self) -> bool {
112 !self.is_eq(rhs)
113 }
114
115 #[inline]
117 fn is_lt(self, rhs: Self) -> bool {
118 self.compare(rhs).is_lt()
119 }
120
121 #[inline]
123 fn is_le(self, rhs: Self) -> bool {
124 self.compare(rhs).is_le()
125 }
126
127 #[inline]
129 fn is_gt(self, rhs: Self) -> bool {
130 self.compare(rhs).is_gt()
131 }
132
133 #[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 self.to_bits() == rhs.to_bits()
402 }
403 }
404 };
405}
406
407native_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);
416native_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 macro_rules! assert_approx_eq {
437 ( $x: expr, $y: expr ) => {{
438 assert_approx_eq!($x, $y, 1.0e-4)
439 }};
440 ( $x: expr, $y: expr, $tol: expr ) => {{
441 let x_val = $x;
442 let y_val = $y;
443 let diff = f64::from((x_val - y_val).abs());
444 assert!(
445 diff <= $tol,
446 "{} != {} (with tolerance = {})",
447 x_val,
448 y_val,
449 $tol
450 );
451 }};
452 }
453
454 #[test]
455 fn test_native_type_is_zero() {
456 assert!(0_i8.is_zero());
457 assert!(0_i16.is_zero());
458 assert!(0_i32.is_zero());
459 assert!(0_i64.is_zero());
460 assert!(0_i128.is_zero());
461 assert!(i256::ZERO.is_zero());
462 assert!(0_u8.is_zero());
463 assert!(0_u16.is_zero());
464 assert!(0_u32.is_zero());
465 assert!(0_u64.is_zero());
466 assert!(f16::ZERO.is_zero());
467 assert!(0.0_f32.is_zero());
468 assert!(0.0_f64.is_zero());
469 }
470
471 #[test]
472 fn test_native_type_comparison() {
473 assert!(8_i8.is_eq(8_i8));
475 assert!(8_i16.is_eq(8_i16));
476 assert!(8_i32.is_eq(8_i32));
477 assert!(8_i64.is_eq(8_i64));
478 assert!(8_i128.is_eq(8_i128));
479 assert!(i256::from_parts(8, 0).is_eq(i256::from_parts(8, 0)));
480 assert!(8_u8.is_eq(8_u8));
481 assert!(8_u16.is_eq(8_u16));
482 assert!(8_u32.is_eq(8_u32));
483 assert!(8_u64.is_eq(8_u64));
484 assert!(f16::from_f32(8.0).is_eq(f16::from_f32(8.0)));
485 assert!(8.0_f32.is_eq(8.0_f32));
486 assert!(8.0_f64.is_eq(8.0_f64));
487
488 assert!(8_i8.is_ne(1_i8));
490 assert!(8_i16.is_ne(1_i16));
491 assert!(8_i32.is_ne(1_i32));
492 assert!(8_i64.is_ne(1_i64));
493 assert!(8_i128.is_ne(1_i128));
494 assert!(i256::from_parts(8, 0).is_ne(i256::from_parts(1, 0)));
495 assert!(8_u8.is_ne(1_u8));
496 assert!(8_u16.is_ne(1_u16));
497 assert!(8_u32.is_ne(1_u32));
498 assert!(8_u64.is_ne(1_u64));
499 assert!(f16::from_f32(8.0).is_ne(f16::from_f32(1.0)));
500 assert!(8.0_f32.is_ne(1.0_f32));
501 assert!(8.0_f64.is_ne(1.0_f64));
502
503 assert!(8_i8.is_lt(10_i8));
505 assert!(8_i16.is_lt(10_i16));
506 assert!(8_i32.is_lt(10_i32));
507 assert!(8_i64.is_lt(10_i64));
508 assert!(8_i128.is_lt(10_i128));
509 assert!(i256::from_parts(8, 0).is_lt(i256::from_parts(10, 0)));
510 assert!(8_u8.is_lt(10_u8));
511 assert!(8_u16.is_lt(10_u16));
512 assert!(8_u32.is_lt(10_u32));
513 assert!(8_u64.is_lt(10_u64));
514 assert!(f16::from_f32(8.0).is_lt(f16::from_f32(10.0)));
515 assert!(8.0_f32.is_lt(10.0_f32));
516 assert!(8.0_f64.is_lt(10.0_f64));
517
518 assert!(8_i8.is_gt(1_i8));
520 assert!(8_i16.is_gt(1_i16));
521 assert!(8_i32.is_gt(1_i32));
522 assert!(8_i64.is_gt(1_i64));
523 assert!(8_i128.is_gt(1_i128));
524 assert!(i256::from_parts(8, 0).is_gt(i256::from_parts(1, 0)));
525 assert!(8_u8.is_gt(1_u8));
526 assert!(8_u16.is_gt(1_u16));
527 assert!(8_u32.is_gt(1_u32));
528 assert!(8_u64.is_gt(1_u64));
529 assert!(f16::from_f32(8.0).is_gt(f16::from_f32(1.0)));
530 assert!(8.0_f32.is_gt(1.0_f32));
531 assert!(8.0_f64.is_gt(1.0_f64));
532 }
533
534 #[test]
535 fn test_native_type_add() {
536 assert_eq!(8_i8.add_wrapping(2_i8), 10_i8);
538 assert_eq!(8_i16.add_wrapping(2_i16), 10_i16);
539 assert_eq!(8_i32.add_wrapping(2_i32), 10_i32);
540 assert_eq!(8_i64.add_wrapping(2_i64), 10_i64);
541 assert_eq!(8_i128.add_wrapping(2_i128), 10_i128);
542 assert_eq!(
543 i256::from_parts(8, 0).add_wrapping(i256::from_parts(2, 0)),
544 i256::from_parts(10, 0)
545 );
546 assert_eq!(8_u8.add_wrapping(2_u8), 10_u8);
547 assert_eq!(8_u16.add_wrapping(2_u16), 10_u16);
548 assert_eq!(8_u32.add_wrapping(2_u32), 10_u32);
549 assert_eq!(8_u64.add_wrapping(2_u64), 10_u64);
550 assert_eq!(
551 f16::from_f32(8.0).add_wrapping(f16::from_f32(2.0)),
552 f16::from_f32(10.0)
553 );
554 assert_eq!(8.0_f32.add_wrapping(2.0_f32), 10_f32);
555 assert_eq!(8.0_f64.add_wrapping(2.0_f64), 10_f64);
556
557 assert_eq!(8_i8.add_checked(2_i8).unwrap(), 10_i8);
559 assert_eq!(8_i16.add_checked(2_i16).unwrap(), 10_i16);
560 assert_eq!(8_i32.add_checked(2_i32).unwrap(), 10_i32);
561 assert_eq!(8_i64.add_checked(2_i64).unwrap(), 10_i64);
562 assert_eq!(8_i128.add_checked(2_i128).unwrap(), 10_i128);
563 assert_eq!(
564 i256::from_parts(8, 0)
565 .add_checked(i256::from_parts(2, 0))
566 .unwrap(),
567 i256::from_parts(10, 0)
568 );
569 assert_eq!(8_u8.add_checked(2_u8).unwrap(), 10_u8);
570 assert_eq!(8_u16.add_checked(2_u16).unwrap(), 10_u16);
571 assert_eq!(8_u32.add_checked(2_u32).unwrap(), 10_u32);
572 assert_eq!(8_u64.add_checked(2_u64).unwrap(), 10_u64);
573 assert_eq!(
574 f16::from_f32(8.0).add_checked(f16::from_f32(2.0)).unwrap(),
575 f16::from_f32(10.0)
576 );
577 assert_eq!(8.0_f32.add_checked(2.0_f32).unwrap(), 10_f32);
578 assert_eq!(8.0_f64.add_checked(2.0_f64).unwrap(), 10_f64);
579 }
580
581 #[test]
582 fn test_native_type_sub() {
583 assert_eq!(8_i8.sub_wrapping(2_i8), 6_i8);
585 assert_eq!(8_i16.sub_wrapping(2_i16), 6_i16);
586 assert_eq!(8_i32.sub_wrapping(2_i32), 6_i32);
587 assert_eq!(8_i64.sub_wrapping(2_i64), 6_i64);
588 assert_eq!(8_i128.sub_wrapping(2_i128), 6_i128);
589 assert_eq!(
590 i256::from_parts(8, 0).sub_wrapping(i256::from_parts(2, 0)),
591 i256::from_parts(6, 0)
592 );
593 assert_eq!(8_u8.sub_wrapping(2_u8), 6_u8);
594 assert_eq!(8_u16.sub_wrapping(2_u16), 6_u16);
595 assert_eq!(8_u32.sub_wrapping(2_u32), 6_u32);
596 assert_eq!(8_u64.sub_wrapping(2_u64), 6_u64);
597 assert_eq!(
598 f16::from_f32(8.0).sub_wrapping(f16::from_f32(2.0)),
599 f16::from_f32(6.0)
600 );
601 assert_eq!(8.0_f32.sub_wrapping(2.0_f32), 6_f32);
602 assert_eq!(8.0_f64.sub_wrapping(2.0_f64), 6_f64);
603
604 assert_eq!(8_i8.sub_checked(2_i8).unwrap(), 6_i8);
606 assert_eq!(8_i16.sub_checked(2_i16).unwrap(), 6_i16);
607 assert_eq!(8_i32.sub_checked(2_i32).unwrap(), 6_i32);
608 assert_eq!(8_i64.sub_checked(2_i64).unwrap(), 6_i64);
609 assert_eq!(8_i128.sub_checked(2_i128).unwrap(), 6_i128);
610 assert_eq!(
611 i256::from_parts(8, 0)
612 .sub_checked(i256::from_parts(2, 0))
613 .unwrap(),
614 i256::from_parts(6, 0)
615 );
616 assert_eq!(8_u8.sub_checked(2_u8).unwrap(), 6_u8);
617 assert_eq!(8_u16.sub_checked(2_u16).unwrap(), 6_u16);
618 assert_eq!(8_u32.sub_checked(2_u32).unwrap(), 6_u32);
619 assert_eq!(8_u64.sub_checked(2_u64).unwrap(), 6_u64);
620 assert_eq!(
621 f16::from_f32(8.0).sub_checked(f16::from_f32(2.0)).unwrap(),
622 f16::from_f32(6.0)
623 );
624 assert_eq!(8.0_f32.sub_checked(2.0_f32).unwrap(), 6_f32);
625 assert_eq!(8.0_f64.sub_checked(2.0_f64).unwrap(), 6_f64);
626 }
627
628 #[test]
629 fn test_native_type_mul() {
630 assert_eq!(8_i8.mul_wrapping(2_i8), 16_i8);
632 assert_eq!(8_i16.mul_wrapping(2_i16), 16_i16);
633 assert_eq!(8_i32.mul_wrapping(2_i32), 16_i32);
634 assert_eq!(8_i64.mul_wrapping(2_i64), 16_i64);
635 assert_eq!(8_i128.mul_wrapping(2_i128), 16_i128);
636 assert_eq!(
637 i256::from_parts(8, 0).mul_wrapping(i256::from_parts(2, 0)),
638 i256::from_parts(16, 0)
639 );
640 assert_eq!(8_u8.mul_wrapping(2_u8), 16_u8);
641 assert_eq!(8_u16.mul_wrapping(2_u16), 16_u16);
642 assert_eq!(8_u32.mul_wrapping(2_u32), 16_u32);
643 assert_eq!(8_u64.mul_wrapping(2_u64), 16_u64);
644 assert_eq!(
645 f16::from_f32(8.0).mul_wrapping(f16::from_f32(2.0)),
646 f16::from_f32(16.0)
647 );
648 assert_eq!(8.0_f32.mul_wrapping(2.0_f32), 16_f32);
649 assert_eq!(8.0_f64.mul_wrapping(2.0_f64), 16_f64);
650
651 assert_eq!(8_i8.mul_checked(2_i8).unwrap(), 16_i8);
653 assert_eq!(8_i16.mul_checked(2_i16).unwrap(), 16_i16);
654 assert_eq!(8_i32.mul_checked(2_i32).unwrap(), 16_i32);
655 assert_eq!(8_i64.mul_checked(2_i64).unwrap(), 16_i64);
656 assert_eq!(8_i128.mul_checked(2_i128).unwrap(), 16_i128);
657 assert_eq!(
658 i256::from_parts(8, 0)
659 .mul_checked(i256::from_parts(2, 0))
660 .unwrap(),
661 i256::from_parts(16, 0)
662 );
663 assert_eq!(8_u8.mul_checked(2_u8).unwrap(), 16_u8);
664 assert_eq!(8_u16.mul_checked(2_u16).unwrap(), 16_u16);
665 assert_eq!(8_u32.mul_checked(2_u32).unwrap(), 16_u32);
666 assert_eq!(8_u64.mul_checked(2_u64).unwrap(), 16_u64);
667 assert_eq!(
668 f16::from_f32(8.0).mul_checked(f16::from_f32(2.0)).unwrap(),
669 f16::from_f32(16.0)
670 );
671 assert_eq!(8.0_f32.mul_checked(2.0_f32).unwrap(), 16_f32);
672 assert_eq!(8.0_f64.mul_checked(2.0_f64).unwrap(), 16_f64);
673 }
674
675 #[test]
676 fn test_native_type_div() {
677 assert_eq!(8_i8.div_wrapping(2_i8), 4_i8);
679 assert_eq!(8_i16.div_wrapping(2_i16), 4_i16);
680 assert_eq!(8_i32.div_wrapping(2_i32), 4_i32);
681 assert_eq!(8_i64.div_wrapping(2_i64), 4_i64);
682 assert_eq!(8_i128.div_wrapping(2_i128), 4_i128);
683 assert_eq!(
684 i256::from_parts(8, 0).div_wrapping(i256::from_parts(2, 0)),
685 i256::from_parts(4, 0)
686 );
687 assert_eq!(8_u8.div_wrapping(2_u8), 4_u8);
688 assert_eq!(8_u16.div_wrapping(2_u16), 4_u16);
689 assert_eq!(8_u32.div_wrapping(2_u32), 4_u32);
690 assert_eq!(8_u64.div_wrapping(2_u64), 4_u64);
691 assert_eq!(
692 f16::from_f32(8.0).div_wrapping(f16::from_f32(2.0)),
693 f16::from_f32(4.0)
694 );
695 assert_eq!(8.0_f32.div_wrapping(2.0_f32), 4_f32);
696 assert_eq!(8.0_f64.div_wrapping(2.0_f64), 4_f64);
697
698 assert_eq!(8_i8.div_checked(2_i8).unwrap(), 4_i8);
700 assert_eq!(8_i16.div_checked(2_i16).unwrap(), 4_i16);
701 assert_eq!(8_i32.div_checked(2_i32).unwrap(), 4_i32);
702 assert_eq!(8_i64.div_checked(2_i64).unwrap(), 4_i64);
703 assert_eq!(8_i128.div_checked(2_i128).unwrap(), 4_i128);
704 assert_eq!(
705 i256::from_parts(8, 0)
706 .div_checked(i256::from_parts(2, 0))
707 .unwrap(),
708 i256::from_parts(4, 0)
709 );
710 assert_eq!(8_u8.div_checked(2_u8).unwrap(), 4_u8);
711 assert_eq!(8_u16.div_checked(2_u16).unwrap(), 4_u16);
712 assert_eq!(8_u32.div_checked(2_u32).unwrap(), 4_u32);
713 assert_eq!(8_u64.div_checked(2_u64).unwrap(), 4_u64);
714 assert_eq!(
715 f16::from_f32(8.0).div_checked(f16::from_f32(2.0)).unwrap(),
716 f16::from_f32(4.0)
717 );
718 assert_eq!(8.0_f32.div_checked(2.0_f32).unwrap(), 4_f32);
719 assert_eq!(8.0_f64.div_checked(2.0_f64).unwrap(), 4_f64);
720 }
721
722 #[test]
723 fn test_native_type_mod() {
724 assert_eq!(9_i8.mod_wrapping(2_i8), 1_i8);
726 assert_eq!(9_i16.mod_wrapping(2_i16), 1_i16);
727 assert_eq!(9_i32.mod_wrapping(2_i32), 1_i32);
728 assert_eq!(9_i64.mod_wrapping(2_i64), 1_i64);
729 assert_eq!(9_i128.mod_wrapping(2_i128), 1_i128);
730 assert_eq!(
731 i256::from_parts(9, 0).mod_wrapping(i256::from_parts(2, 0)),
732 i256::from_parts(1, 0)
733 );
734 assert_eq!(9_u8.mod_wrapping(2_u8), 1_u8);
735 assert_eq!(9_u16.mod_wrapping(2_u16), 1_u16);
736 assert_eq!(9_u32.mod_wrapping(2_u32), 1_u32);
737 assert_eq!(9_u64.mod_wrapping(2_u64), 1_u64);
738 assert_eq!(
739 f16::from_f32(9.0).mod_wrapping(f16::from_f32(2.0)),
740 f16::from_f32(1.0)
741 );
742 assert_eq!(9.0_f32.mod_wrapping(2.0_f32), 1_f32);
743 assert_eq!(9.0_f64.mod_wrapping(2.0_f64), 1_f64);
744
745 assert_eq!(9_i8.mod_checked(2_i8).unwrap(), 1_i8);
747 assert_eq!(9_i16.mod_checked(2_i16).unwrap(), 1_i16);
748 assert_eq!(9_i32.mod_checked(2_i32).unwrap(), 1_i32);
749 assert_eq!(9_i64.mod_checked(2_i64).unwrap(), 1_i64);
750 assert_eq!(9_i128.mod_checked(2_i128).unwrap(), 1_i128);
751 assert_eq!(
752 i256::from_parts(9, 0)
753 .mod_checked(i256::from_parts(2, 0))
754 .unwrap(),
755 i256::from_parts(1, 0)
756 );
757 assert_eq!(9_u8.mod_checked(2_u8).unwrap(), 1_u8);
758 assert_eq!(9_u16.mod_checked(2_u16).unwrap(), 1_u16);
759 assert_eq!(9_u32.mod_checked(2_u32).unwrap(), 1_u32);
760 assert_eq!(9_u64.mod_checked(2_u64).unwrap(), 1_u64);
761 assert_eq!(
762 f16::from_f32(9.0).mod_checked(f16::from_f32(2.0)).unwrap(),
763 f16::from_f32(1.0)
764 );
765 assert_eq!(9.0_f32.mod_checked(2.0_f32).unwrap(), 1_f32);
766 assert_eq!(9.0_f64.mod_checked(2.0_f64).unwrap(), 1_f64);
767 }
768
769 #[test]
770 fn test_native_type_neg() {
771 assert_eq!(8_i8.neg_wrapping(), -8_i8);
773 assert_eq!(8_i16.neg_wrapping(), -8_i16);
774 assert_eq!(8_i32.neg_wrapping(), -8_i32);
775 assert_eq!(8_i64.neg_wrapping(), -8_i64);
776 assert_eq!(8_i128.neg_wrapping(), -8_i128);
777 assert_eq!(i256::from_parts(8, 0).neg_wrapping(), i256::from_i128(-8));
778 assert_eq!(8_u8.neg_wrapping(), u8::MAX - 7_u8);
779 assert_eq!(8_u16.neg_wrapping(), u16::MAX - 7_u16);
780 assert_eq!(8_u32.neg_wrapping(), u32::MAX - 7_u32);
781 assert_eq!(8_u64.neg_wrapping(), u64::MAX - 7_u64);
782 assert_eq!(f16::from_f32(8.0).neg_wrapping(), f16::from_f32(-8.0));
783 assert_eq!(8.0_f32.neg_wrapping(), -8_f32);
784 assert_eq!(8.0_f64.neg_wrapping(), -8_f64);
785
786 assert_eq!(8_i8.neg_checked().unwrap(), -8_i8);
788 assert_eq!(8_i16.neg_checked().unwrap(), -8_i16);
789 assert_eq!(8_i32.neg_checked().unwrap(), -8_i32);
790 assert_eq!(8_i64.neg_checked().unwrap(), -8_i64);
791 assert_eq!(8_i128.neg_checked().unwrap(), -8_i128);
792 assert_eq!(
793 i256::from_parts(8, 0).neg_checked().unwrap(),
794 i256::from_i128(-8)
795 );
796 assert!(8_u8.neg_checked().is_err());
797 assert!(8_u16.neg_checked().is_err());
798 assert!(8_u32.neg_checked().is_err());
799 assert!(8_u64.neg_checked().is_err());
800 assert_eq!(
801 f16::from_f32(8.0).neg_checked().unwrap(),
802 f16::from_f32(-8.0)
803 );
804 assert_eq!(8.0_f32.neg_checked().unwrap(), -8_f32);
805 assert_eq!(8.0_f64.neg_checked().unwrap(), -8_f64);
806 }
807
808 #[test]
809 fn test_native_type_pow() {
810 assert_eq!(8_i8.pow_wrapping(2_u32), 64_i8);
812 assert_eq!(8_i16.pow_wrapping(2_u32), 64_i16);
813 assert_eq!(8_i32.pow_wrapping(2_u32), 64_i32);
814 assert_eq!(8_i64.pow_wrapping(2_u32), 64_i64);
815 assert_eq!(8_i128.pow_wrapping(2_u32), 64_i128);
816 assert_eq!(
817 i256::from_parts(8, 0).pow_wrapping(2_u32),
818 i256::from_parts(64, 0)
819 );
820 assert_eq!(8_u8.pow_wrapping(2_u32), 64_u8);
821 assert_eq!(8_u16.pow_wrapping(2_u32), 64_u16);
822 assert_eq!(8_u32.pow_wrapping(2_u32), 64_u32);
823 assert_eq!(8_u64.pow_wrapping(2_u32), 64_u64);
824 assert_approx_eq!(f16::from_f32(8.0).pow_wrapping(2_u32), f16::from_f32(64.0));
825 assert_approx_eq!(8.0_f32.pow_wrapping(2_u32), 64_f32);
826 assert_approx_eq!(8.0_f64.pow_wrapping(2_u32), 64_f64);
827
828 assert_eq!(8_i8.pow_checked(2_u32).unwrap(), 64_i8);
830 assert_eq!(8_i16.pow_checked(2_u32).unwrap(), 64_i16);
831 assert_eq!(8_i32.pow_checked(2_u32).unwrap(), 64_i32);
832 assert_eq!(8_i64.pow_checked(2_u32).unwrap(), 64_i64);
833 assert_eq!(8_i128.pow_checked(2_u32).unwrap(), 64_i128);
834 assert_eq!(
835 i256::from_parts(8, 0).pow_checked(2_u32).unwrap(),
836 i256::from_parts(64, 0)
837 );
838 assert_eq!(8_u8.pow_checked(2_u32).unwrap(), 64_u8);
839 assert_eq!(8_u16.pow_checked(2_u32).unwrap(), 64_u16);
840 assert_eq!(8_u32.pow_checked(2_u32).unwrap(), 64_u32);
841 assert_eq!(8_u64.pow_checked(2_u32).unwrap(), 64_u64);
842 assert_approx_eq!(
843 f16::from_f32(8.0).pow_checked(2_u32).unwrap(),
844 f16::from_f32(64.0)
845 );
846 assert_approx_eq!(8.0_f32.pow_checked(2_u32).unwrap(), 64_f32);
847 assert_approx_eq!(8.0_f64.pow_checked(2_u32).unwrap(), 64_f64);
848 }
849
850 #[test]
851 fn test_float_total_order_min_max() {
852 assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f64::NEG_INFINITY));
853 assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::INFINITY));
854
855 assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
856 assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
857 assert!(<f64 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f64::NAN));
858
859 assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
860 assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
861 assert!(<f64 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f64::NAN));
862
863 assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f32::NEG_INFINITY));
864 assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::INFINITY));
865
866 assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
867 assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
868 assert!(<f32 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f32::NAN));
869
870 assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
871 assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
872 assert!(<f32 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f32::NAN));
873
874 assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(f16::NEG_INFINITY));
875 assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::INFINITY));
876
877 assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_nan());
878 assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_sign_negative());
879 assert!(<f16 as ArrowNativeTypeOp>::MIN_TOTAL_ORDER.is_lt(-f16::NAN));
880
881 assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_nan());
882 assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_sign_positive());
883 assert!(<f16 as ArrowNativeTypeOp>::MAX_TOTAL_ORDER.is_gt(f16::NAN));
884 }
885}