@@ -13,10 +13,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
13
13
///
14
14
/// Computes the quotient and remainder of `self / rhs`.
15
15
/// Furthermore, returns the signs of `self` and `rhs`.
16
- const fn div_rem_base (
16
+ const fn div_rem_base < const RHS_LIMBS : usize > (
17
17
& self ,
18
- rhs : & NonZero < Self > ,
19
- ) -> ( Uint < { LIMBS } > , Uint < { LIMBS } > , ConstChoice , ConstChoice ) {
18
+ rhs : & NonZero < Int < RHS_LIMBS > > ,
19
+ ) -> ( Uint < LIMBS > , Uint < RHS_LIMBS > , ConstChoice , ConstChoice ) {
20
20
// Step 1: split operands into signs and magnitudes.
21
21
let ( lhs_mag, lhs_sgn) = self . abs_sign ( ) ;
22
22
let ( rhs_mag, rhs_sgn) = rhs. abs_sign ( ) ;
@@ -52,7 +52,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
52
52
/// assert_eq!(quotient.unwrap(), I128::from(2));
53
53
/// assert_eq!(remainder, I128::from(-2));
54
54
/// ```
55
- pub const fn checked_div_rem ( & self , rhs : & NonZero < Self > ) -> ( ConstCtOption < Self > , Self ) {
55
+ pub const fn checked_div_rem < const RHS_LIMBS : usize > (
56
+ & self ,
57
+ rhs : & NonZero < Int < RHS_LIMBS > > ,
58
+ ) -> ( ConstCtOption < Self > , Int < RHS_LIMBS > ) {
56
59
let ( quotient, remainder, lhs_sgn, rhs_sgn) = self . div_rem_base ( rhs) ;
57
60
let opposing_signs = lhs_sgn. ne ( rhs_sgn) ;
58
61
(
@@ -66,12 +69,15 @@ impl<const LIMBS: usize> Int<LIMBS> {
66
69
/// - `self != MIN` or `rhs != MINUS_ONE`.
67
70
///
68
71
/// Note: this operation rounds towards zero, truncating any fractional part of the exact result.
69
- pub fn checked_div ( & self , rhs : & Self ) -> CtOption < Self > {
72
+ pub fn checked_div < const RHS_LIMBS : usize > ( & self , rhs : & Int < RHS_LIMBS > ) -> CtOption < Self > {
70
73
NonZero :: new ( * rhs) . and_then ( |rhs| self . checked_div_rem ( & rhs) . 0 . into ( ) )
71
74
}
72
75
73
76
/// Computes `self` % `rhs`, returns the remainder.
74
- pub const fn rem ( & self , rhs : & NonZero < Self > ) -> Self {
77
+ pub const fn rem < const RHS_LIMBS : usize > (
78
+ & self ,
79
+ rhs : & NonZero < Int < RHS_LIMBS > > ,
80
+ ) -> Int < RHS_LIMBS > {
75
81
self . checked_div_rem ( rhs) . 1
76
82
}
77
83
}
@@ -223,7 +229,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
223
229
/// I128::from(2)
224
230
/// )
225
231
/// ```
226
- pub fn checked_div_floor ( & self , rhs : & Self ) -> CtOption < Self > {
232
+ pub fn checked_div_floor < const RHS_LIMBS : usize > (
233
+ & self ,
234
+ rhs : & Int < RHS_LIMBS > ,
235
+ ) -> CtOption < Self > {
227
236
NonZero :: new ( * rhs) . and_then ( |rhs| self . checked_div_rem_floor ( & rhs) . 0 . into ( ) )
228
237
}
229
238
@@ -257,7 +266,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
257
266
/// assert_eq!(quotient.unwrap(), I128::from(2));
258
267
/// assert_eq!(remainder, I128::from(2));
259
268
/// ```
260
- pub const fn checked_div_rem_floor ( & self , rhs : & NonZero < Self > ) -> ( ConstCtOption < Self > , Self ) {
269
+ pub const fn checked_div_rem_floor < const RHS_LIMBS : usize > (
270
+ & self ,
271
+ rhs : & NonZero < Int < RHS_LIMBS > > ,
272
+ ) -> ( ConstCtOption < Self > , Int < RHS_LIMBS > ) {
261
273
let ( lhs_mag, lhs_sgn) = self . abs_sign ( ) ;
262
274
let ( rhs_mag, rhs_sgn) = rhs. abs_sign ( ) ;
263
275
let ( quotient, remainder) = lhs_mag. div_rem ( & rhs_mag) ;
@@ -283,40 +295,40 @@ impl<const LIMBS: usize> Int<LIMBS> {
283
295
}
284
296
}
285
297
286
- impl < const LIMBS : usize > CheckedDiv for Int < LIMBS > {
287
- fn checked_div ( & self , rhs : & Int < LIMBS > ) -> CtOption < Self > {
298
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > CheckedDiv < Int < RHS_LIMBS > > for Int < LIMBS > {
299
+ fn checked_div ( & self , rhs : & Int < RHS_LIMBS > ) -> CtOption < Self > {
288
300
self . checked_div ( rhs)
289
301
}
290
302
}
291
303
292
- impl < const LIMBS : usize > Div < & NonZero < Int < LIMBS > > > for & Int < LIMBS > {
304
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Int < RHS_LIMBS > > > for & Int < LIMBS > {
293
305
type Output = CtOption < Int < LIMBS > > ;
294
306
295
- fn div ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
307
+ fn div ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
296
308
* self / * rhs
297
309
}
298
310
}
299
311
300
- impl < const LIMBS : usize > Div < & NonZero < Int < LIMBS > > > for Int < LIMBS > {
312
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Int < RHS_LIMBS > > > for Int < LIMBS > {
301
313
type Output = CtOption < Int < LIMBS > > ;
302
314
303
- fn div ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
315
+ fn div ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
304
316
self / * rhs
305
317
}
306
318
}
307
319
308
- impl < const LIMBS : usize > Div < NonZero < Int < LIMBS > > > for & Int < LIMBS > {
320
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Int < RHS_LIMBS > > > for & Int < LIMBS > {
309
321
type Output = CtOption < Int < LIMBS > > ;
310
322
311
- fn div ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
323
+ fn div ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
312
324
* self / rhs
313
325
}
314
326
}
315
327
316
- impl < const LIMBS : usize > Div < NonZero < Int < LIMBS > > > for Int < LIMBS > {
328
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Int < RHS_LIMBS > > > for Int < LIMBS > {
317
329
type Output = CtOption < Int < LIMBS > > ;
318
330
319
- fn div ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
331
+ fn div ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
320
332
self . checked_div ( & rhs)
321
333
}
322
334
}
@@ -333,34 +345,42 @@ impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
333
345
}
334
346
}
335
347
336
- impl < const LIMBS : usize > Div < NonZero < Int < LIMBS > > > for Wrapping < Int < LIMBS > > {
348
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Int < RHS_LIMBS > > >
349
+ for Wrapping < Int < LIMBS > >
350
+ {
337
351
type Output = Wrapping < Int < LIMBS > > ;
338
352
339
- fn div ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
353
+ fn div ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
340
354
Wrapping ( ( self . 0 / rhs) . expect ( "cannot represent positive equivalent of Int::MIN as int" ) )
341
355
}
342
356
}
343
357
344
- impl < const LIMBS : usize > Div < NonZero < Int < LIMBS > > > for & Wrapping < Int < LIMBS > > {
358
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Int < RHS_LIMBS > > >
359
+ for & Wrapping < Int < LIMBS > >
360
+ {
345
361
type Output = Wrapping < Int < LIMBS > > ;
346
362
347
- fn div ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
363
+ fn div ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
348
364
* self / rhs
349
365
}
350
366
}
351
367
352
- impl < const LIMBS : usize > Div < & NonZero < Int < LIMBS > > > for & Wrapping < Int < LIMBS > > {
368
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Int < RHS_LIMBS > > >
369
+ for & Wrapping < Int < LIMBS > >
370
+ {
353
371
type Output = Wrapping < Int < LIMBS > > ;
354
372
355
- fn div ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
373
+ fn div ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
356
374
* self / * rhs
357
375
}
358
376
}
359
377
360
- impl < const LIMBS : usize > Div < & NonZero < Int < LIMBS > > > for Wrapping < Int < LIMBS > > {
378
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Int < RHS_LIMBS > > >
379
+ for Wrapping < Int < LIMBS > >
380
+ {
361
381
type Output = Wrapping < Int < LIMBS > > ;
362
382
363
- fn div ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
383
+ fn div ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
364
384
self / * rhs
365
385
}
366
386
}
@@ -379,34 +399,34 @@ impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>>
379
399
}
380
400
}
381
401
382
- impl < const LIMBS : usize > Rem < & NonZero < Int < LIMBS > > > for & Int < LIMBS > {
383
- type Output = Int < LIMBS > ;
402
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Int < RHS_LIMBS > > > for & Int < LIMBS > {
403
+ type Output = Int < RHS_LIMBS > ;
384
404
385
- fn rem ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
405
+ fn rem ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
386
406
* self % * rhs
387
407
}
388
408
}
389
409
390
- impl < const LIMBS : usize > Rem < & NonZero < Int < LIMBS > > > for Int < LIMBS > {
391
- type Output = Int < LIMBS > ;
410
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Int < RHS_LIMBS > > > for Int < LIMBS > {
411
+ type Output = Int < RHS_LIMBS > ;
392
412
393
- fn rem ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
413
+ fn rem ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
394
414
self % * rhs
395
415
}
396
416
}
397
417
398
- impl < const LIMBS : usize > Rem < NonZero < Int < LIMBS > > > for & Int < LIMBS > {
399
- type Output = Int < LIMBS > ;
418
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Int < RHS_LIMBS > > > for & Int < LIMBS > {
419
+ type Output = Int < RHS_LIMBS > ;
400
420
401
- fn rem ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
421
+ fn rem ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
402
422
* self % rhs
403
423
}
404
424
}
405
425
406
- impl < const LIMBS : usize > Rem < NonZero < Int < LIMBS > > > for Int < LIMBS > {
407
- type Output = Int < LIMBS > ;
426
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Int < RHS_LIMBS > > > for Int < LIMBS > {
427
+ type Output = Int < RHS_LIMBS > ;
408
428
409
- fn rem ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
429
+ fn rem ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
410
430
Self :: rem ( & self , & rhs)
411
431
}
412
432
}
@@ -423,34 +443,42 @@ impl<const LIMBS: usize> RemAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
423
443
}
424
444
}
425
445
426
- impl < const LIMBS : usize > Rem < NonZero < Int < LIMBS > > > for Wrapping < Int < LIMBS > > {
427
- type Output = Wrapping < Int < LIMBS > > ;
446
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Int < RHS_LIMBS > > >
447
+ for Wrapping < Int < LIMBS > >
448
+ {
449
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
428
450
429
- fn rem ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
451
+ fn rem ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
430
452
Wrapping ( self . 0 % rhs)
431
453
}
432
454
}
433
455
434
- impl < const LIMBS : usize > Rem < NonZero < Int < LIMBS > > > for & Wrapping < Int < LIMBS > > {
435
- type Output = Wrapping < Int < LIMBS > > ;
456
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Int < RHS_LIMBS > > >
457
+ for & Wrapping < Int < LIMBS > >
458
+ {
459
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
436
460
437
- fn rem ( self , rhs : NonZero < Int < LIMBS > > ) -> Self :: Output {
461
+ fn rem ( self , rhs : NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
438
462
* self % rhs
439
463
}
440
464
}
441
465
442
- impl < const LIMBS : usize > Rem < & NonZero < Int < LIMBS > > > for & Wrapping < Int < LIMBS > > {
443
- type Output = Wrapping < Int < LIMBS > > ;
466
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Int < RHS_LIMBS > > >
467
+ for & Wrapping < Int < LIMBS > >
468
+ {
469
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
444
470
445
- fn rem ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
471
+ fn rem ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
446
472
* self % * rhs
447
473
}
448
474
}
449
475
450
- impl < const LIMBS : usize > Rem < & NonZero < Int < LIMBS > > > for Wrapping < Int < LIMBS > > {
451
- type Output = Wrapping < Int < LIMBS > > ;
476
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Int < RHS_LIMBS > > >
477
+ for Wrapping < Int < LIMBS > >
478
+ {
479
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
452
480
453
- fn rem ( self , rhs : & NonZero < Int < LIMBS > > ) -> Self :: Output {
481
+ fn rem ( self , rhs : & NonZero < Int < RHS_LIMBS > > ) -> Self :: Output {
454
482
self % * rhs
455
483
}
456
484
}
0 commit comments