1#[cfg(feature = "_int_usize")]
7use crate::isize_up;
8#[allow(unused_imports)]
9use crate::GcdReturn;
10#[cfg(feature = "alloc")]
11use crate::Vec;
12use crate::{Int, NumInt, NumResult as Result, ValueQuant};
13
14macro_rules! impl_int {
24 () => {
25 impl_int![signed
26 i8:"_int_i8"|u8:"_int_u8",
27 i16:"_int_i16"|u16:"_int_u16",
28 i32:"_int_i32"|u32:"_int_u32",
29 i64:"_int_i64"|u64:"_int_u64",
30 i128:"_int_i128"|u128:"_int_u128",
31 isize:"_int_isize"|usize:"_int_usize"
32 ];
33 impl_int![unsigned
34 u8:"_int_u8"|i16:"_int_i16",
35 u16:"_int_u16"|i32:"_int_i32",
36 u32:"_int_u32"|i64:"_int_i64",
37 u64:"_int_u64"|i128:"_int_i128",
38 u128:"_int_u128"|i128:"_int_i128"
39 ];
40 #[cfg(target_pointer_width = "32")]
41 impl_int![unsigned usize:"_int_usize"|isize_up:"_int_i64"];
42 #[cfg(target_pointer_width = "64")]
43 impl_int![unsigned usize:"_int_usize"|isize_up:"_int_i128"];
44 };
45
46 (signed $($t:ident : $cap:literal | $ut:ident : $ucap:literal),+) => {
49 $( impl_int![@signed $t:$cap | $ut:$ucap]; )+
50 };
51 (@signed $t:ident : $cap:literal | $ut:ident:$ucap:literal) => { $crate::paste! {
52 #[cfg(feature = $cap )]
53 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
54 impl NumInt for $t {
55 type OutI = $t;
56
57 impl_int![common_body_iu];
58
59 fn int_gcd_ext(self, other: Self::Rhs)
62 -> Result<GcdReturn<Self::Out, Self::OutI>> {
63 match Int(self).gcd_ext(other) {
64 GcdReturn { gcd, x, y } => Ok(GcdReturn { gcd: gcd.0, x: x.0, y: y.0 }) }}
65 fn int_ref_gcd_ext(&self, other: &Self::Rhs)
66 -> Result<GcdReturn<Self::Out, Self::OutI>> {
67 match Int(*self).gcd_ext(*other) {
68 GcdReturn { gcd, x, y } => Ok(GcdReturn { gcd: gcd.0, x: x.0, y: y.0 }) }}
69
70 #[cfg(feature = $ucap )]
71 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $ucap)))]
72 fn int_midpoint(self, other: Self::Rhs) -> Result<Self::Out> {
73 Ok(Int(self).midpoint(other).0) }
74 #[cfg(feature = $ucap )]
75 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $ucap)))]
76 fn int_ref_midpoint(&self, other: &Self::Rhs) -> Result<Self::Out> {
77 Ok(Int(*self).midpoint(*other).0) }
78
79 fn int_modulo_mul_inv(self, modulus: Self) -> Result<Self> {
82 Int(self).modulo_mul_inv(modulus).map(|n|n.0) }
83 fn int_ref_modulo_mul_inv(&self, modulus: &Self) -> Result<Self> {
84 Int(*self).modulo_mul_inv(*modulus).map(|n|n.0) }
85
86 fn int_modulo_div(self, other: Self, modulus: Self) -> Result<Self> {
87 Int(self).modulo_div(other, modulus).map(|n|n.0) }
88 fn int_ref_modulo_div(&self, other: &Self, modulus: &Self) -> Result<Self> {
89 Int(*self).modulo_div(*other, *modulus).map(|n|n.0) }
90
91 fn int_sqrt_ceil(self) -> Result<Self::Out> {
94 Int(self).sqrt_ceil().map(|n|n.0) }
95 fn int_ref_sqrt_ceil(&self) -> Result<Self::Out> {
96 Int(*self).sqrt_ceil().map(|n|n.0) }
97
98 fn int_sqrt_floor(self) -> Result<Self::Out> {
99 Int(self).sqrt_floor().map(|n|n.0) }
100 fn int_ref_sqrt_floor(&self) -> Result<Self::Out> {
101 Int(*self).sqrt_floor().map(|n|n.0) }
102
103 fn int_sqrt_round(self) -> Result<Self::Out> {
104 Int(self).sqrt_round().map(|n|n.0) }
105 fn int_ref_sqrt_round(&self) -> Result<Self::Out> {
106 Int(*self).sqrt_round().map(|n|n.0) }
107 }
108 }};
109
110 (unsigned $($t:ident : $cap:literal | $io:ident : $iocap:literal),+) => {
113 $( impl_int![@unsigned $t:$cap | $io:$iocap]; )+
114 };
115 (@unsigned $t:ident : $cap:literal | $io:ident : $iocap:literal) => { $crate::paste! {
116 #[cfg(feature = $cap )]
117 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
118 impl NumInt for $t {
119 type OutI = $io;
120
121 impl_int![common_body_iu];
122
123 #[cfg(all(feature = $iocap, feature = "cast"))]
126 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
127 fn int_gcd_ext(self, other: Self::Rhs)
128 -> Result<GcdReturn<Self::Out, Self::OutI>> {
129 Int(self).gcd_ext(other)
130 .map(|res| GcdReturn { gcd: res.gcd.0, x: res.x.0, y: res.y.0 }) }
131 #[cfg(all(feature = $iocap, feature = "cast"))]
132 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
133 fn int_ref_gcd_ext(&self, other: &Self::Rhs)
134 -> Result<GcdReturn<Self::Out, Self::OutI>> {
135 Int(*self).gcd_ext(*other)
136 .map(|res| GcdReturn { gcd: res.gcd.0, x: res.x.0, y: res.y.0 }) }
137
138 fn int_midpoint(self, other: Self::Rhs) -> Result<Self::Out> {
139 Ok(Int(self).midpoint(other).0) }
140 fn int_ref_midpoint(&self, other: &Self::Rhs) -> Result<Self::Out> {
141 Ok(Int(*self).midpoint(*other).0) }
142
143 #[cfg(all(feature = $iocap, feature = "cast"))]
146 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
147 fn int_modulo_mul_inv(self, modulus: Self) -> Result<Self> {
148 Int(self).modulo_mul_inv(modulus).map(|n|n.0) }
149 #[cfg(all(feature = $iocap, feature = "cast"))]
150 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
151 fn int_ref_modulo_mul_inv(&self, modulus: &Self) -> Result<Self> {
152 Int(*self).modulo_mul_inv(*modulus).map(|n|n.0) }
153
154 #[cfg(all(feature = $iocap, feature = "cast"))]
155 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
156 fn int_modulo_div(self, other: Self, modulus: Self) -> Result<Self> {
157 Int(self).modulo_div(other, modulus).map(|n|n.0) }
158 #[cfg(all(feature = $iocap, feature = "cast"))]
159 #[cfg_attr(feature = "nightly_doc", doc(cfg(all(feature = $iocap, feature = "cast"))))]
160 fn int_ref_modulo_div(&self, other: &Self, modulus: &Self) -> Result<Self> {
161 Int(*self).modulo_div(*other, *modulus).map(|n|n.0) }
162
163 fn int_sqrt_ceil(self) -> Result<Self::Out> {
166 Ok(Int(self).sqrt_ceil().0) }
167 fn int_ref_sqrt_ceil(&self) -> Result<Self::Out> {
168 Ok(Int(*self).sqrt_ceil().0) }
169
170 fn int_sqrt_floor(self) -> Result<Self::Out> {
171 Ok(Int(self).sqrt_floor().0) }
172 fn int_ref_sqrt_floor(&self) -> Result<Self::Out> {
173 Ok(Int(*self).sqrt_floor().0) }
174
175 fn int_sqrt_round(self) -> Result<Self::Out> {
176 Int(self).sqrt_round().map(|n|n.0) }
177 fn int_ref_sqrt_round(&self) -> Result<Self::Out> {
178 Int(*self).sqrt_round().map(|n|n.0) }
179 }
180 }};
181
182 (common_body_iu) => {
185 fn int_digital_root(self) -> Result<Self::Out> {
188 Ok(Int(self).digital_root().0) }
189 fn int_ref_digital_root(&self) -> Result<Self::Out> {
190 Ok(Int(*self).digital_root().0) }
191 fn int_digital_root_base(self, base: Self::Rhs) -> Result<Self::Out> {
192 Ok(Int(self).digital_root_base(base).0) }
193 fn int_ref_digital_root_base(&self, base: &Self::Rhs) -> Result<Self::Out> {
194 Ok(Int(*self).digital_root_base(*base).0) }
195
196 fn int_digits(self) -> Result<Self::Out> {
197 Ok(Int(self).digits().0) }
198 fn int_ref_digits(&self) -> Result<Self::Out> {
199 Ok(Int(*self).digits().0) }
200 fn int_digits_sign(self) -> Result<Self::Out> {
201 Ok(Int(self).digits_sign().0) }
202 fn int_ref_digits_sign(&self) -> Result<Self::Out> {
203 Ok(Int(*self).digits_sign().0) }
204 fn int_digits_base(self, base: Self::Rhs) -> Result<Self::Out> {
205 Ok(Int(self).digits_base(base).0) }
206 fn int_ref_digits_base(&self, base: &Self::Rhs) -> Result<Self::Out> {
207 Ok(Int(*self).digits_base(*base).0) }
208 fn int_digits_base_sign(self, base: Self::Rhs) -> Result<Self::Out> {
209 Ok(Int(self).digits_base_sign(base).0) }
210 fn int_ref_digits_base_sign(&self, base: &Self::Rhs) -> Result<Self::Out> {
211 Ok(Int(*self).digits_base_sign(*base).0) }
212
213 fn int_abs(self) -> Result<Self::Out> { Ok(Int(self).abs().0) }
216 fn int_ref_abs(&self) -> Result<Self::Out> { Ok(Int(*self).abs().0) }
217
218 fn int_is_even(self) -> Result<bool> { Ok(Int(self).is_even()) }
219 fn int_ref_is_even(&self) -> Result<bool> { Ok(Int(*self).is_even()) }
220 fn int_is_odd(self) -> Result<bool> { Ok(Int(self).is_odd()) }
221 fn int_ref_is_odd(&self) -> Result<bool> { Ok(Int(*self).is_odd()) }
222
223 fn int_gcd(self, other: Self::Rhs) -> Result<Self::Out> {
224 Ok(Int(self).gcd(other).0) }
225 fn int_ref_gcd(&self, other: &Self::Rhs) -> Result<Self::Out> {
226 Ok(Int(*self).gcd(*other).0) }
227 fn int_lcm(self, other: Self::Rhs) -> Result<Self::Out> {
230 match Int(self).lcm(other) { Ok(res) => Ok(res.0), Err(e) => Err(e) } }
231 fn int_ref_lcm(&self, other: &Self::Rhs) -> Result<Self::Out> {
232 match Int(*self).lcm(*other) { Ok(res) => Ok(res.0), Err(e) => Err(e) } }
233
234 #[cfg(feature = "cast")]
235 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "cast")))]
236 fn int_scale(self, min: Self::Rhs, max: Self::Rhs, a: Self::Rhs, b: Self::Rhs)
237 -> Result<Self::Out> where Self: Sized { Int(self).scale(min, max, a, b).map(|n|n.0) }
238 #[cfg(feature = "cast")]
239 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "cast")))]
240 fn int_ref_scale(&self, min: &Self::Rhs, max: &Self::Rhs, a: &Self::Rhs, b: &Self::Rhs)
241 -> Result<Self::Out> { Int(*self).scale(*min, *max, *a, *b).map(|n|n.0) }
242
243 fn int_scale_wrap(self, min: Self::Rhs, max: Self::Rhs, a: Self::Rhs, b: Self::Rhs)
244 -> Result<Self::Out> where Self: Sized { Ok(Int(self).scale_wrap(min, max, a, b).0) }
245 fn int_ref_scale_wrap(&self, min: &Self::Rhs, max: &Self::Rhs, a: &Self::Rhs, b: &Self::Rhs)
246 -> Result<Self::Out> { Ok(Int(*self).scale_wrap(*min, *max, *a, *b).0) }
247
248 fn int_factorial(self) -> Result<Self::Out> {
251 Int(self).factorial().map(|n|n.0) }
252 fn int_ref_factorial(&self) -> Result<Self::Out> {
253 Int(*self).factorial().map(|n|n.0) }
254 fn int_subfactorial(self) -> Result<Self::Out> {
255 Int(self).subfactorial().map(|n|n.0) }
256 fn int_ref_subfactorial(&self) -> Result<Self::Out> {
257 Int(*self).subfactorial().map(|n|n.0) }
258
259 fn int_permute(self, r: Self) -> Result<Self::Out> {
260 Int(self).permute(r).map(|n|n.0) }
261 fn int_ref_permute(&self, r: &Self) -> Result<Self::Out> {
262 Int(*self).permute(*r).map(|n|n.0) }
263 #[cfg(feature = "cast")]
264 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "cast")))]
265 fn int_permute_rep(self, r: Self) -> Result<Self::Out> {
266 Int(self).permute_rep(r).map(|n|n.0) }
267 #[cfg(feature = "cast")]
268 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "cast")))]
269 fn int_ref_permute_rep(&self, r: &Self) -> Result<Self::Out> {
270 Int(*self).permute_rep(*r).map(|n|n.0) }
271
272 fn int_combine(self, r: Self) -> Result<Self::Out> {
273 Int(self).combine(r).map(|n|n.0) }
274 fn int_ref_combine(&self, r: &Self) -> Result<Self::Out> {
275 Int(*self).combine(*r).map(|n|n.0) }
276 fn int_combine_rep(self, r: Self) -> Result<Self::Out> {
277 Int(self).combine_rep(r).map(|n|n.0) }
278 fn int_ref_combine_rep(&self, r: &Self) -> Result<Self::Out> {
279 Int(*self).combine_rep(*r).map(|n|n.0) }
280
281 fn int_div_rem(self, b: Self::Rhs) -> Result<[Self::Out; 2]> {
284 let [d, r] = Int(self).div_rem(b); Ok([d.0, r.0]) }
285 fn int_div_ceil(self, b: Self) -> Result<Self::Out> {
286 Ok(Int(self).div_ceil(b).0) }
287 fn int_div_floor(self, b: Self) -> Result<Self::Out> {
288 Ok(Int(self).div_floor(b).0) }
289 fn int_div_ties_away(self, b: Self) -> Result<Self::Out> {
290 Ok(Int(self).div_ties_away(b).0) }
291 fn int_div_ties_towards(self, b: Self) -> Result<Self::Out> {
292 Ok(Int(self).div_ties_towards(b).0) }
293 fn int_div_ties_even(self, b: Self) -> Result<Self::Out> {
294 Ok(Int(self).div_ties_even(b).0) }
295 fn int_div_ties_odd(self, b: Self) -> Result<Self::Out> {
296 Ok(Int(self).div_ties_odd(b).0) }
297
298 #[cfg(feature = "alloc")]
301 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
302 fn int_factors(self) -> Result<Vec<Self::Out>> { Ok(Int(self).factors()) }
303 #[cfg(feature = "alloc")]
304 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
305 fn int_ref_factors(&self) -> Result<Vec<Self::Out>> { Ok(Int(*self).factors()) }
306 #[cfg(feature = "alloc")]
307 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
308 fn int_factors_proper(self) -> Result<Vec<Self::Out>> { Ok(Int(self).factors_proper()) }
309 #[cfg(feature = "alloc")]
310 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
311 fn int_ref_factors_proper(&self) -> Result<Vec<Self::Out>> {
312 Ok(Int(*self).factors_proper()) }
313 #[cfg(feature = "alloc")]
314 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
315 fn int_factors_prime(self) -> Result<Vec<Self::Out>> { Ok(Int(self).factors_prime()) }
316 #[cfg(feature = "alloc")]
317 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
318 fn int_ref_factors_prime(&self) -> Result<Vec<Self::Out>> {
319 Ok(Int(*self).factors_prime()) }
320 #[cfg(feature = "alloc")]
321 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
322 fn int_factors_prime_unique(self) -> Result<Vec<Self::Out>> {
323 Ok(Int(self).factors_prime_unique()) }
324 #[cfg(feature = "alloc")]
325 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
326 fn int_ref_factors_prime_unique(&self) -> Result<Vec<Self::Out>> {
327 Ok(Int(*self).factors_prime_unique()) }
328
329 fn int_factors_buf(self, fbuf: &mut [Self::Out], upfbuf: &mut [Self::Out])
332 -> Result<(usize, usize)> { Int(self).factors_buf(fbuf, upfbuf) }
333 fn int_ref_factors_buf(&self, fbuf: &mut [Self::Out], upfbuf: &mut [Self::Out])
334 -> Result<(usize, usize)> { Int(*self).factors_buf(fbuf, upfbuf) }
335
336 fn int_factors_proper_buf(self, fbuf: &mut [Self], upfbuf: &mut [Self])
337 -> Result<(usize, usize)> { Int(self).factors_proper_buf(fbuf, upfbuf) }
338 fn int_ref_factors_proper_buf(&self, fbuf: &mut [Self::Out], upfbuf: &mut [Self::Out])
339 -> Result<(usize, usize)> { Int(*self).factors_proper_buf(fbuf, upfbuf) }
340
341 fn int_factors_prime_buf(self, buffer: &mut [Self])
342 -> Result<usize> { Int(self).factors_prime_buf(buffer) }
343 fn int_ref_factors_prime_buf(&self, buffer: &mut [Self::Out])
344 -> Result<usize> { Int(*self).factors_prime_buf(buffer) }
345
346 fn int_factors_prime_unique_buf(self, buffer: &mut [Self])
347 -> Result<usize> { Int(self).factors_prime_unique_buf(buffer) }
348 fn int_ref_factors_prime_unique_buf(&self, buffer: &mut [Self::Out])
349 -> Result<usize> { Int(*self).factors_prime_unique_buf(buffer) }
350
351 fn int_modulo(self, modulus: Self) -> Result<Self> {
354 Int(self).modulo(modulus).map(|n|n.0) }
355 fn int_ref_modulo(&self, modulus: &Self) -> Result<Self> {
356 Int(*self).modulo(*modulus).map(|n|n.0) }
357
358 fn int_modulo_add(self, other: Self, modulus: Self) -> Result<Self> {
359 Int(self).modulo_add(other, modulus).map(|n|n.0) }
360 fn int_ref_modulo_add(&self, other: &Self, modulus: &Self) -> Result<Self> {
361 Int(*self).modulo_add(*other, *modulus).map(|n|n.0) }
362 fn int_modulo_add_cycles(self, other: Self, modulus: Self)
363 -> Result<ValueQuant<Self, Self>> {
364 Int(self).modulo_add_cycles(other, modulus)
365 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
366 fn int_ref_modulo_add_cycles(&self, other: &Self, modulus: &Self)
367 -> Result<ValueQuant<Self, Self>> {
368 Int(*self).modulo_add_cycles(*other, *modulus)
369 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
370 fn int_modulo_add_inv(self, modulus: Self) -> Result<Self> {
371 Int(self).modulo_add_inv(modulus).map(|n|n.0) }
372 fn int_ref_modulo_add_inv(&self, modulus: &Self) -> Result<Self> {
373 Int(*self).modulo_add_inv(*modulus).map(|n|n.0) }
374
375 fn int_modulo_sub(self, other: Self, modulus: Self) -> Result<Self> {
376 Int(self).modulo_sub(other, modulus).map(|n|n.0) }
377 fn int_ref_modulo_sub(&self, other: &Self, modulus: &Self) -> Result<Self> {
378 Int(*self).modulo_sub(*other, *modulus).map(|n|n.0) }
379 fn int_modulo_sub_cycles(self, other: Self, modulus: Self)
380 -> Result<ValueQuant<Self, Self>> {
381 Int(self).modulo_sub_cycles(other, modulus)
382 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
383 fn int_ref_modulo_sub_cycles(&self, other: &Self, modulus: &Self)
384 -> Result<ValueQuant<Self, Self>> {
385 Int(*self).modulo_sub_cycles(*other, *modulus)
386 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
387
388 fn int_modulo_mul(self, other: Self, modulus: Self) -> Result<Self> {
389 Int(self).modulo_mul(other, modulus).map(|n|n.0) }
390 fn int_ref_modulo_mul(&self, other: &Self, modulus: &Self) -> Result<Self> {
391 Int(*self).modulo_mul(*other, *modulus).map(|n|n.0) }
392 fn int_modulo_mul_cycles(self, other: Self, modulus: Self)
393 -> Result<ValueQuant<Self, Self>> {
394 Int(self).modulo_mul_cycles(other, modulus)
395 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
396 fn int_ref_modulo_mul_cycles(&self, other: &Self, modulus: &Self)
397 -> Result<ValueQuant<Self, Self>> {
398 Int(*self).modulo_mul_cycles(*other, *modulus)
399 .map(|res| ValueQuant { v: res.v.0, q: res.q.0 }) }
400
401 fn int_is_prime(self) -> Result<bool> { Ok(Int(self).is_prime()) }
404 fn int_ref_is_prime(&self) -> Result<bool> { Ok(Int(*self).is_prime()) }
405
406 fn int_prime_nth(self) -> Result<Self::Out> { Int(self).prime_nth().map(|n|n.0) }
407 fn int_ref_prime_nth(&self) -> Result<Self::Out> { Int(*self).prime_nth().map(|n|n.0) }
408
409 fn int_prime_pi(self) -> Result<usize> { Ok(Int(self).prime_pi()) }
410 fn int_ref_prime_pi(&self) -> Result<usize> { Ok(Int(*self).prime_pi()) }
411
412 fn int_totient(self) -> Result<Self::Out> { Ok(Int(self).totient().0) }
413 fn int_ref_totient(&self) -> Result<Self::Out> { Ok(Int(*self).totient().0) }
414
415 fn int_is_square(self) -> Result<bool> { Ok(Int(self).is_square()) }
418 fn int_ref_is_square(&self) -> Result<bool> { Ok(Int(*self).is_square()) }
419 fn int_root_ceil(self, nth: u32) -> Result<Self::Out> {
424 Int(self).root_ceil(nth).map(|n|n.0) }
425 fn int_ref_root_ceil(&self, nth: u32) -> Result<Self::Out> {
426 Int(*self).root_ceil(nth).map(|n|n.0) }
427
428 fn int_root_floor(self, nth: u32) -> Result<Self::Out> {
429 Int(self).root_floor(nth).map(|n|n.0) }
430 fn int_ref_root_floor(&self, nth: u32) -> Result<Self::Out> {
431 Int(*self).root_floor(nth).map(|n|n.0) }
432 };
433}
434impl_int!();