@@ -69,7 +69,7 @@ pub fn (u Uint128) and(v Uint128) Uint128 {
6969 return Uint128 {u.lo & v.lo, u.hi & v.hi}
7070}
7171
72- // and_64 rreturns u & v
72+ // and_64 returns u & v
7373pub fn (u Uint128) and_64 (v u64 ) Uint128 {
7474 return Uint128 {u.lo & v, u.hi & 0 }
7575}
@@ -101,6 +101,7 @@ pub fn (u Uint128) add(v Uint128) Uint128 {
101101 return Uint128 {lo, hi}
102102}
103103
104+ // add_128 return u + v and the carry
104105pub fn add_128 (x Uint128 , y Uint128 , carry u64 ) (Uint128 , u64 ) {
105106 mut sum := Uint128 {}
106107 mut carry_out := u64 (0 )
@@ -109,6 +110,7 @@ pub fn add_128(x Uint128, y Uint128, carry u64) (Uint128, u64) {
109110 return sum, carry_out
110111}
111112
113+ // sub_128 returns u - v and the borrow
112114pub fn sub_128 (x Uint128 , y Uint128 , borrow u64 ) (Uint128 , u64 ) {
113115 mut diff := Uint128 {}
114116 mut borrow_out := u64 (0 )
@@ -117,6 +119,7 @@ pub fn sub_128(x Uint128, y Uint128, borrow u64) (Uint128, u64) {
117119 return diff, borrow_out
118120}
119121
122+ // mul_128 returns u x v
120123pub fn mul_128 (x Uint128 , y Uint128 ) (Uint128 , Uint128 ) {
121124 mut lo := Uint128 {}
122125 mut hi := Uint128 {}
@@ -135,6 +138,7 @@ pub fn mul_128(x Uint128, y Uint128) (Uint128, Uint128) {
135138 return hi, lo
136139}
137140
141+ // div_128 returns u / v
138142pub fn div_128 (hi Uint128 , lo Uint128 , y_ Uint128 ) (Uint128 , Uint128 ) {
139143 mut y := y_
140144 if y.is_zero () {
@@ -211,6 +215,7 @@ pub fn (u Uint128) mul_64(v u64) Uint128 {
211215 return Uint128 {lo, hi}
212216}
213217
218+ // overflowing_mul_64 returns u x v even if result size > 64
214219pub fn (u Uint128) overflowing_mul_64 (v u64 ) (Uint128 , bool ) {
215220 hi , lo := bits.mul_64 (u.lo, v)
216221 p0 , p1 := bits.mul_64 (u.hi, v)
@@ -219,6 +224,7 @@ pub fn (u Uint128) overflowing_mul_64(v u64) (Uint128, bool) {
219224 return Uint128 {lo, hi2 }, p0 != 0 || c0 != 0
220225}
221226
227+ // overflowing_add_64 returns u = v even if result size > 64
222228pub fn (u Uint128) overflowing_add_64 (v u64 ) (Uint128 , u64 ) {
223229 lo , carry := bits.add_64 (u.lo, v, 0 )
224230 hi , carry2 := bits.add_64 (u.hi, 0 , carry)
@@ -366,6 +372,7 @@ pub fn (u Uint128) reverse_bytes() Uint128 {
366372 return Uint128 {bits.reverse_bytes_64 (u.hi), bits.reverse_bytes_64 (u.lo)}
367373}
368374
375+ // not
369376pub fn (u Uint128) not () Uint128 {
370377 return Uint128 {~ u.lo, ~ u.hi}
371378}
@@ -413,10 +420,12 @@ pub fn uint128_from_64(v u64) Uint128 {
413420 return uint128_new (v, 0 )
414421}
415422
423+ // uint128_new creates new Uint128 with given `lo` and `hi`
416424pub fn uint128_new (lo u64 , hi u64 ) Uint128 {
417425 return Uint128 {lo, hi}
418426}
419427
428+ // unint_from_dec_str returns an error or new Uint128 from given string
420429pub fn uint128_from_dec_str (value string ) ! Uint128 {
421430 mut res := unsigned.uint128_ zero
422431 for b_ in value.bytes () {
@@ -441,26 +450,32 @@ pub fn uint128_from_dec_str(value string) !Uint128 {
441450 return res
442451}
443452
453+ // / -> returns u / v
444454pub fn (u Uint128) / (v Uint128) Uint128 {
445455 return u.div (v)
446456}
447457
458+ // % -> returns u % v
448459pub fn (u Uint128 ) % (v Uint128 ) Uint128 {
449460 return u.mod (v)
450461}
451462
463+ // + -> returns u + v
452464pub fn (u Uint128) + (v Uint128) Uint128 {
453465 return u.add (v)
454466}
455467
468+ // - -> returns u - v
456469pub fn (u Uint128) - (v Uint128) Uint128 {
457470 return u.sub (v)
458471}
459472
473+ // * -> returns u * v
460474pub fn (u Uint128) * (v Uint128) Uint128 {
461475 return u.mul (v)
462476}
463477
478+ // < -> returns true if u < v
464479pub fn (u Uint128 ) < (v Uint128 ) bool {
465480 return u.cmp (v) == - 1
466481}
0 commit comments