@@ -69,7 +69,7 @@ pub fn (u Uint128) and(v Uint128) Uint128 {
69
69
return Uint128 {u.lo & v.lo, u.hi & v.hi}
70
70
}
71
71
72
- // and_64 rreturns u & v
72
+ // and_64 returns u & v
73
73
pub fn (u Uint128) and_64 (v u64 ) Uint128 {
74
74
return Uint128 {u.lo & v, u.hi & 0 }
75
75
}
@@ -101,6 +101,7 @@ pub fn (u Uint128) add(v Uint128) Uint128 {
101
101
return Uint128 {lo, hi}
102
102
}
103
103
104
+ // add_128 return u + v and the carry
104
105
pub fn add_128 (x Uint128 , y Uint128 , carry u64 ) (Uint128 , u64 ) {
105
106
mut sum := Uint128 {}
106
107
mut carry_out := u64 (0 )
@@ -109,6 +110,7 @@ pub fn add_128(x Uint128, y Uint128, carry u64) (Uint128, u64) {
109
110
return sum, carry_out
110
111
}
111
112
113
+ // sub_128 returns u - v and the borrow
112
114
pub fn sub_128 (x Uint128 , y Uint128 , borrow u64 ) (Uint128 , u64 ) {
113
115
mut diff := Uint128 {}
114
116
mut borrow_out := u64 (0 )
@@ -117,6 +119,7 @@ pub fn sub_128(x Uint128, y Uint128, borrow u64) (Uint128, u64) {
117
119
return diff, borrow_out
118
120
}
119
121
122
+ // mul_128 returns u x v
120
123
pub fn mul_128 (x Uint128 , y Uint128 ) (Uint128 , Uint128 ) {
121
124
mut lo := Uint128 {}
122
125
mut hi := Uint128 {}
@@ -135,6 +138,7 @@ pub fn mul_128(x Uint128, y Uint128) (Uint128, Uint128) {
135
138
return hi, lo
136
139
}
137
140
141
+ // div_128 returns u / v
138
142
pub fn div_128 (hi Uint128 , lo Uint128 , y_ Uint128 ) (Uint128 , Uint128 ) {
139
143
mut y := y_
140
144
if y.is_zero () {
@@ -211,6 +215,7 @@ pub fn (u Uint128) mul_64(v u64) Uint128 {
211
215
return Uint128 {lo, hi}
212
216
}
213
217
218
+ // overflowing_mul_64 returns u x v even if result size > 64
214
219
pub fn (u Uint128) overflowing_mul_64 (v u64 ) (Uint128 , bool ) {
215
220
hi , lo := bits.mul_64 (u.lo, v)
216
221
p0 , p1 := bits.mul_64 (u.hi, v)
@@ -219,6 +224,7 @@ pub fn (u Uint128) overflowing_mul_64(v u64) (Uint128, bool) {
219
224
return Uint128 {lo, hi2 }, p0 != 0 || c0 != 0
220
225
}
221
226
227
+ // overflowing_add_64 returns u = v even if result size > 64
222
228
pub fn (u Uint128) overflowing_add_64 (v u64 ) (Uint128 , u64 ) {
223
229
lo , carry := bits.add_64 (u.lo, v, 0 )
224
230
hi , carry2 := bits.add_64 (u.hi, 0 , carry)
@@ -366,6 +372,7 @@ pub fn (u Uint128) reverse_bytes() Uint128 {
366
372
return Uint128 {bits.reverse_bytes_64 (u.hi), bits.reverse_bytes_64 (u.lo)}
367
373
}
368
374
375
+ // not
369
376
pub fn (u Uint128) not () Uint128 {
370
377
return Uint128 {~ u.lo, ~ u.hi}
371
378
}
@@ -413,10 +420,12 @@ pub fn uint128_from_64(v u64) Uint128 {
413
420
return uint128_new (v, 0 )
414
421
}
415
422
423
+ // uint128_new creates new Uint128 with given `lo` and `hi`
416
424
pub fn uint128_new (lo u64 , hi u64 ) Uint128 {
417
425
return Uint128 {lo, hi}
418
426
}
419
427
428
+ // unint_from_dec_str returns an error or new Uint128 from given string
420
429
pub fn uint128_from_dec_str (value string ) ! Uint128 {
421
430
mut res := unsigned.uint128_ zero
422
431
for b_ in value.bytes () {
@@ -441,26 +450,32 @@ pub fn uint128_from_dec_str(value string) !Uint128 {
441
450
return res
442
451
}
443
452
453
+ // / -> returns u / v
444
454
pub fn (u Uint128) / (v Uint128) Uint128 {
445
455
return u.div (v)
446
456
}
447
457
458
+ // % -> returns u % v
448
459
pub fn (u Uint128 ) % (v Uint128 ) Uint128 {
449
460
return u.mod (v)
450
461
}
451
462
463
+ // + -> returns u + v
452
464
pub fn (u Uint128) + (v Uint128) Uint128 {
453
465
return u.add (v)
454
466
}
455
467
468
+ // - -> returns u - v
456
469
pub fn (u Uint128) - (v Uint128) Uint128 {
457
470
return u.sub (v)
458
471
}
459
472
473
+ // * -> returns u * v
460
474
pub fn (u Uint128) * (v Uint128) Uint128 {
461
475
return u.mul (v)
462
476
}
463
477
478
+ // < -> returns true if u < v
464
479
pub fn (u Uint128 ) < (v Uint128 ) bool {
465
480
return u.cmp (v) == - 1
466
481
}
0 commit comments