crypto_bigint/int/
from.rs1use crate::{Int, Limb, Uint, Word, I128, I64};
4
5impl<const LIMBS: usize> Int<LIMBS> {
6 pub const fn from_i8(n: i8) -> Self {
9 assert!(LIMBS >= 1, "number of limbs must be greater than zero");
10 Uint::new([Limb(n as Word)]).as_int().resize()
11 }
12
13 pub const fn from_i16(n: i16) -> Self {
16 assert!(LIMBS >= 1, "number of limbs must be greater than zero");
17 Uint::new([Limb(n as Word)]).as_int().resize()
18 }
19
20 pub const fn from_i32(n: i32) -> Self {
23 assert!(LIMBS >= 1, "number of limbs must be greater than zero");
24 Uint::new([Limb(n as Word)]).as_int().resize()
25 }
26
27 #[cfg(target_pointer_width = "32")]
30 pub const fn from_i64(n: i64) -> Self {
31 Uint::<{ I64::LIMBS }>::from_u64(n as u64).as_int().resize()
32 }
33
34 #[cfg(target_pointer_width = "64")]
37 pub const fn from_i64(n: i64) -> Self {
38 assert!(LIMBS >= 1, "number of limbs must be greater than zero");
39 Uint::new([Limb(n as Word)]).as_int().resize()
40 }
41
42 pub const fn from_i128(n: i128) -> Self {
45 Uint::<{ I128::LIMBS }>::from_u128(n as u128)
46 .as_int()
47 .resize()
48 }
49}
50
51impl<const LIMBS: usize> From<i8> for Int<LIMBS> {
52 fn from(n: i8) -> Self {
53 debug_assert!(LIMBS > 0, "limbs must be non-zero");
55 Self::from_i8(n)
56 }
57}
58
59impl<const LIMBS: usize> From<i16> for Int<LIMBS> {
60 fn from(n: i16) -> Self {
61 debug_assert!(LIMBS > 0, "limbs must be non-zero");
63 Self::from_i16(n)
64 }
65}
66
67impl<const LIMBS: usize> From<i32> for Int<LIMBS> {
68 fn from(n: i32) -> Self {
69 debug_assert!(LIMBS > 0, "limbs must be non-zero");
71 Self::from_i32(n)
72 }
73}
74
75impl<const LIMBS: usize> From<i64> for Int<LIMBS> {
76 fn from(n: i64) -> Self {
77 debug_assert!(LIMBS >= 8 / Limb::BYTES, "not enough limbs");
79 Self::from_i64(n)
80 }
81}
82
83impl<const LIMBS: usize> From<i128> for Int<LIMBS> {
84 fn from(n: i128) -> Self {
85 debug_assert!(LIMBS >= 16 / Limb::BYTES, "not enough limbs");
87 Self::from_i128(n)
88 }
89}
90
91impl From<I64> for i64 {
92 fn from(n: I64) -> i64 {
93 u64::from(n.0) as i64
94 }
95}
96
97impl From<I128> for i128 {
98 fn from(n: I128) -> i128 {
99 u128::from(n.0) as i128
100 }
101}
102
103impl<const LIMBS: usize, const LIMBS2: usize> From<&Int<LIMBS>> for Int<LIMBS2> {
104 fn from(num: &Int<LIMBS>) -> Int<LIMBS2> {
105 num.resize()
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 #[cfg(target_pointer_width = "64")]
112 use crate::I128 as IntEx;
113 #[cfg(target_pointer_width = "32")]
114 use crate::I64 as IntEx;
115 use crate::{Limb, I128};
116
117 #[test]
118 fn from_i8() {
119 let n = IntEx::from(42i8);
120 assert_eq!(n.as_limbs(), &[Limb(42), Limb(0)]);
121 let n = IntEx::from(-42i8);
122 assert_eq!(n.as_limbs(), &[Limb::MAX - Limb::from(41u32), Limb::MAX]);
123 }
124
125 #[test]
126 fn from_i16() {
127 let n = IntEx::from(42i16);
128 assert_eq!(n.as_limbs(), &[Limb(42), Limb(0)]);
129 let n = IntEx::from(-42i16);
130 assert_eq!(n.as_limbs(), &[Limb::MAX - Limb::from(41u32), Limb::MAX]);
131 }
132
133 #[test]
134 fn from_i32() {
135 let n = IntEx::from(42i32);
136 assert_eq!(n.as_limbs(), &[Limb(42), Limb(0)]);
137 let n = IntEx::from(-42i32);
138 assert_eq!(n.as_limbs(), &[Limb::MAX - Limb::from(41u32), Limb::MAX]);
139 }
140
141 #[test]
142 fn from_i64() {
143 let n = IntEx::from(42i64);
144 assert_eq!(n.as_limbs(), &[Limb(42), Limb(0)]);
145 let n = IntEx::from(-42i64);
146 assert_eq!(n.as_limbs(), &[Limb::MAX - Limb::from(41u32), Limb::MAX]);
147 }
148
149 #[test]
150 fn from_i128() {
151 let n = I128::from(42i128);
152 assert_eq!(&n.as_limbs()[..2], &[Limb(42), Limb(0)]);
153 assert_eq!(i128::from(n), 42i128);
154 let n = I128::from(-42i128);
155 assert_eq!(&n.as_limbs()[..2], &[Limb::MAX - Limb(41), Limb::MAX]);
156 assert_eq!(i128::from(n), -42i128);
157 }
158}