@@ -9,10 +9,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
9
9
/// Base div_rem operation on dividing an [`Int`] by a [`Uint`].
10
10
/// Computes the quotient and remainder of `self / rhs`.
11
11
/// Furthermore, returns the sign of `self`.
12
- const fn div_rem_base_uint (
12
+ const fn div_rem_base_uint < const RHS_LIMBS : usize > (
13
13
& self ,
14
- rhs : & NonZero < Uint < LIMBS > > ,
15
- ) -> ( Uint < { LIMBS } > , Uint < { LIMBS } > , ConstChoice ) {
14
+ rhs : & NonZero < Uint < RHS_LIMBS > > ,
15
+ ) -> ( Uint < LIMBS > , Uint < RHS_LIMBS > , ConstChoice ) {
16
16
let ( lhs_mag, lhs_sgn) = self . abs_sign ( ) ;
17
17
let ( quotient, remainder) = lhs_mag. div_rem ( rhs) ;
18
18
( quotient, remainder, lhs_sgn)
@@ -32,23 +32,29 @@ impl<const LIMBS: usize> Int<LIMBS> {
32
32
/// assert_eq!(quotient, I128::from(-2));
33
33
/// assert_eq!(remainder, I128::from(-2));
34
34
/// ```
35
- pub const fn div_rem_uint ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> ( Self , Self ) {
35
+ pub const fn div_rem_uint < const RHS_LIMBS : usize > (
36
+ & self ,
37
+ rhs : & NonZero < Uint < RHS_LIMBS > > ,
38
+ ) -> ( Self , Int < RHS_LIMBS > ) {
36
39
let ( quotient, remainder, lhs_sgn) = self . div_rem_base_uint ( rhs) ;
37
40
(
38
41
Self ( quotient) . wrapping_neg_if ( lhs_sgn) ,
39
- Self ( remainder) . wrapping_neg_if ( lhs_sgn ) ,
42
+ Int :: new_from_abs_sign ( remainder, lhs_sgn ) . expect ( "no overflow; always fits" ) ,
40
43
)
41
44
}
42
45
43
46
/// Perform division.
44
47
/// Note: this operation rounds towards zero, truncating any fractional part of the exact result.
45
- pub const fn div_uint ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> Self {
48
+ pub const fn div_uint < const RHS_LIMBS : usize > ( & self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self {
46
49
self . div_rem_uint ( rhs) . 0
47
50
}
48
51
49
52
/// Compute the remainder.
50
53
/// The remainder will have the same sign as `self` (or be zero).
51
- pub const fn rem_uint ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> Self {
54
+ pub const fn rem_uint < const RHS_LIMBS : usize > (
55
+ & self ,
56
+ rhs : & NonZero < Uint < RHS_LIMBS > > ,
57
+ ) -> Int < RHS_LIMBS > {
52
58
self . div_rem_uint ( rhs) . 1
53
59
}
54
60
}
@@ -135,7 +141,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
135
141
/// (I128::from(-3), U128::ONE)
136
142
/// );
137
143
/// ```
138
- pub fn div_rem_floor_uint ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> ( Self , Uint < LIMBS > ) {
144
+ pub fn div_rem_floor_uint < const RHS_LIMBS : usize > (
145
+ & self ,
146
+ rhs : & NonZero < Uint < RHS_LIMBS > > ,
147
+ ) -> ( Self , Uint < RHS_LIMBS > ) {
139
148
let ( quotient, remainder, lhs_sgn) = self . div_rem_base_uint ( rhs) ;
140
149
141
150
// Increase the quotient by one when self is negative and there is a non-zero remainder.
@@ -166,7 +175,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
166
175
/// I128::from(-3)
167
176
/// );
168
177
/// ```
169
- pub fn div_floor_uint ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> Self {
178
+ pub fn div_floor_uint < const RHS_LIMBS : usize > ( & self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self {
170
179
let ( q, _) = self . div_rem_floor_uint ( rhs) ;
171
180
q
172
181
}
@@ -185,7 +194,10 @@ impl<const LIMBS: usize> Int<LIMBS> {
185
194
/// U128::ONE
186
195
/// );
187
196
/// ```
188
- pub fn normalized_rem ( & self , rhs : & NonZero < Uint < LIMBS > > ) -> Uint < LIMBS > {
197
+ pub fn normalized_rem < const RHS_LIMBS : usize > (
198
+ & self ,
199
+ rhs : & NonZero < Uint < RHS_LIMBS > > ,
200
+ ) -> Uint < RHS_LIMBS > {
189
201
let ( _, r) = self . div_rem_floor_uint ( rhs) ;
190
202
r
191
203
}
@@ -247,34 +259,34 @@ impl<const LIMBS: usize> Int<LIMBS> {
247
259
}
248
260
}
249
261
250
- impl < const LIMBS : usize > Div < & NonZero < Uint < LIMBS > > > for & Int < LIMBS > {
262
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Uint < RHS_LIMBS > > > for & Int < LIMBS > {
251
263
type Output = Int < LIMBS > ;
252
264
253
- fn div ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
265
+ fn div ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
254
266
* self / * rhs
255
267
}
256
268
}
257
269
258
- impl < const LIMBS : usize > Div < & NonZero < Uint < LIMBS > > > for Int < LIMBS > {
270
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Uint < RHS_LIMBS > > > for Int < LIMBS > {
259
271
type Output = Int < LIMBS > ;
260
272
261
- fn div ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
273
+ fn div ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
262
274
self / * rhs
263
275
}
264
276
}
265
277
266
- impl < const LIMBS : usize > Div < NonZero < Uint < LIMBS > > > for & Int < LIMBS > {
278
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Uint < RHS_LIMBS > > > for & Int < LIMBS > {
267
279
type Output = Int < LIMBS > ;
268
280
269
- fn div ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
281
+ fn div ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
270
282
* self / rhs
271
283
}
272
284
}
273
285
274
- impl < const LIMBS : usize > Div < NonZero < Uint < LIMBS > > > for Int < LIMBS > {
286
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Uint < RHS_LIMBS > > > for Int < LIMBS > {
275
287
type Output = Int < LIMBS > ;
276
288
277
- fn div ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
289
+ fn div ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
278
290
self . div_uint ( & rhs)
279
291
}
280
292
}
@@ -291,34 +303,42 @@ impl<const LIMBS: usize> DivAssign<NonZero<Uint<LIMBS>>> for Int<LIMBS> {
291
303
}
292
304
}
293
305
294
- impl < const LIMBS : usize > Div < NonZero < Uint < LIMBS > > > for Wrapping < Int < LIMBS > > {
306
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Uint < RHS_LIMBS > > >
307
+ for Wrapping < Int < LIMBS > >
308
+ {
295
309
type Output = Wrapping < Int < LIMBS > > ;
296
310
297
- fn div ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
311
+ fn div ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
298
312
Wrapping ( self . 0 / rhs)
299
313
}
300
314
}
301
315
302
- impl < const LIMBS : usize > Div < NonZero < Uint < LIMBS > > > for & Wrapping < Int < LIMBS > > {
316
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < NonZero < Uint < RHS_LIMBS > > >
317
+ for & Wrapping < Int < LIMBS > >
318
+ {
303
319
type Output = Wrapping < Int < LIMBS > > ;
304
320
305
- fn div ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
321
+ fn div ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
306
322
* self / rhs
307
323
}
308
324
}
309
325
310
- impl < const LIMBS : usize > Div < & NonZero < Uint < LIMBS > > > for & Wrapping < Int < LIMBS > > {
326
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Uint < RHS_LIMBS > > >
327
+ for & Wrapping < Int < LIMBS > >
328
+ {
311
329
type Output = Wrapping < Int < LIMBS > > ;
312
330
313
- fn div ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
331
+ fn div ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
314
332
* self / * rhs
315
333
}
316
334
}
317
335
318
- impl < const LIMBS : usize > Div < & NonZero < Uint < LIMBS > > > for Wrapping < Int < LIMBS > > {
336
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Div < & NonZero < Uint < RHS_LIMBS > > >
337
+ for Wrapping < Int < LIMBS > >
338
+ {
319
339
type Output = Wrapping < Int < LIMBS > > ;
320
340
321
- fn div ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
341
+ fn div ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
322
342
self / * rhs
323
343
}
324
344
}
@@ -335,34 +355,34 @@ impl<const LIMBS: usize> DivAssign<NonZero<Uint<LIMBS>>> for Wrapping<Int<LIMBS>
335
355
}
336
356
}
337
357
338
- impl < const LIMBS : usize > Rem < & NonZero < Uint < LIMBS > > > for & Int < LIMBS > {
339
- type Output = Int < LIMBS > ;
358
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Uint < RHS_LIMBS > > > for & Int < LIMBS > {
359
+ type Output = Int < RHS_LIMBS > ;
340
360
341
- fn rem ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
361
+ fn rem ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
342
362
* self % * rhs
343
363
}
344
364
}
345
365
346
- impl < const LIMBS : usize > Rem < & NonZero < Uint < LIMBS > > > for Int < LIMBS > {
347
- type Output = Int < LIMBS > ;
366
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Uint < RHS_LIMBS > > > for Int < LIMBS > {
367
+ type Output = Int < RHS_LIMBS > ;
348
368
349
- fn rem ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
369
+ fn rem ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
350
370
self % * rhs
351
371
}
352
372
}
353
373
354
- impl < const LIMBS : usize > Rem < NonZero < Uint < LIMBS > > > for & Int < LIMBS > {
355
- type Output = Int < LIMBS > ;
374
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Uint < RHS_LIMBS > > > for & Int < LIMBS > {
375
+ type Output = Int < RHS_LIMBS > ;
356
376
357
- fn rem ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
377
+ fn rem ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
358
378
* self % rhs
359
379
}
360
380
}
361
381
362
- impl < const LIMBS : usize > Rem < NonZero < Uint < LIMBS > > > for Int < LIMBS > {
363
- type Output = Int < LIMBS > ;
382
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Uint < RHS_LIMBS > > > for Int < LIMBS > {
383
+ type Output = Int < RHS_LIMBS > ;
364
384
365
- fn rem ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
385
+ fn rem ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
366
386
Self :: rem_uint ( & self , & rhs)
367
387
}
368
388
}
@@ -379,34 +399,42 @@ impl<const LIMBS: usize> RemAssign<NonZero<Uint<LIMBS>>> for Int<LIMBS> {
379
399
}
380
400
}
381
401
382
- impl < const LIMBS : usize > Rem < NonZero < Uint < LIMBS > > > for Wrapping < Int < LIMBS > > {
383
- type Output = Wrapping < Int < LIMBS > > ;
402
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Uint < RHS_LIMBS > > >
403
+ for Wrapping < Int < LIMBS > >
404
+ {
405
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
384
406
385
- fn rem ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
407
+ fn rem ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
386
408
Wrapping ( self . 0 % rhs)
387
409
}
388
410
}
389
411
390
- impl < const LIMBS : usize > Rem < NonZero < Uint < LIMBS > > > for & Wrapping < Int < LIMBS > > {
391
- type Output = Wrapping < Int < LIMBS > > ;
412
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < NonZero < Uint < RHS_LIMBS > > >
413
+ for & Wrapping < Int < LIMBS > >
414
+ {
415
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
392
416
393
- fn rem ( self , rhs : NonZero < Uint < LIMBS > > ) -> Self :: Output {
417
+ fn rem ( self , rhs : NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
394
418
* self % rhs
395
419
}
396
420
}
397
421
398
- impl < const LIMBS : usize > Rem < & NonZero < Uint < LIMBS > > > for & Wrapping < Int < LIMBS > > {
399
- type Output = Wrapping < Int < LIMBS > > ;
422
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Uint < RHS_LIMBS > > >
423
+ for & Wrapping < Int < LIMBS > >
424
+ {
425
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
400
426
401
- fn rem ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
427
+ fn rem ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
402
428
* self % * rhs
403
429
}
404
430
}
405
431
406
- impl < const LIMBS : usize > Rem < & NonZero < Uint < LIMBS > > > for Wrapping < Int < LIMBS > > {
407
- type Output = Wrapping < Int < LIMBS > > ;
432
+ impl < const LIMBS : usize , const RHS_LIMBS : usize > Rem < & NonZero < Uint < RHS_LIMBS > > >
433
+ for Wrapping < Int < LIMBS > >
434
+ {
435
+ type Output = Wrapping < Int < RHS_LIMBS > > ;
408
436
409
- fn rem ( self , rhs : & NonZero < Uint < LIMBS > > ) -> Self :: Output {
437
+ fn rem ( self , rhs : & NonZero < Uint < RHS_LIMBS > > ) -> Self :: Output {
410
438
self % * rhs
411
439
}
412
440
}
0 commit comments