1use crate::asm;
4use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
5pub use safe::SafeNumeric;
6
7mod safe;
8
9pub trait Numeric:
11 Add
12 + AddAssign
13 + Mul
14 + MulAssign
15 + Sub
16 + SubAssign
17 + Div
18 + DivAssign
19 + Sized
20 + PartialEq
21 + PartialOrd
22{
23 fn addmod(self, other: Self, n: Self) -> Self;
25
26 fn mulmod(self, other: Self, n: Self) -> Self;
28}
29
30macro_rules! impl_numeric {
31 ($($t:ty),+) => {
32 paste::paste!{
33 $(
34 impl Numeric for $t {
35 #[inline(always)]
36 fn addmod(self, other: Self, n: Self) -> Self {
37 unsafe { asm::ext::[<addmod_ $t>](n, other, self) }
38 }
39
40 #[inline(always)]
41 fn mulmod(self, other: Self, n: Self) -> Self {
42 unsafe { asm::ext::[<mulmod_ $t>](n, other, self) }
43 }
44 }
45 )*
46 }
47 };
48}
49
50impl_numeric! {
51 i8,
52 u8,
53 i16,
54 u16,
55 i32,
56 u32,
57 i64,
58 u64
59}