Skip to content

Commit

Permalink
math.unsigned: add missing docstrings for the functions in unint128.v (
Browse files Browse the repository at this point in the history
  • Loading branch information
sudoerp committed Oct 19, 2023
1 parent 79b068b commit 20a1af3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion vlib/math/complex/complex.v
Expand Up @@ -12,7 +12,7 @@ pub mut:
im f64
}

// complex returns a complex struct with the given `re` and `im`
// complex returns a complex struct with the given `re`al and `im`aginary parts
pub fn complex(re f64, im f64) Complex {
return Complex{re, im}
}
Expand Down
17 changes: 16 additions & 1 deletion vlib/math/unsigned/uint128.v
Expand Up @@ -69,7 +69,7 @@ pub fn (u Uint128) and(v Uint128) Uint128 {
return Uint128{u.lo & v.lo, u.hi & v.hi}
}

// and_64 rreturns u & v
// and_64 returns u & v
pub fn (u Uint128) and_64(v u64) Uint128 {
return Uint128{u.lo & v, u.hi & 0}
}
Expand Down Expand Up @@ -101,6 +101,7 @@ pub fn (u Uint128) add(v Uint128) Uint128 {
return Uint128{lo, hi}
}

// add_128 return u + v and the carry
pub fn add_128(x Uint128, y Uint128, carry u64) (Uint128, u64) {
mut sum := Uint128{}
mut carry_out := u64(0)
Expand All @@ -109,6 +110,7 @@ pub fn add_128(x Uint128, y Uint128, carry u64) (Uint128, u64) {
return sum, carry_out
}

// sub_128 returns u - v and the borrow
pub fn sub_128(x Uint128, y Uint128, borrow u64) (Uint128, u64) {
mut diff := Uint128{}
mut borrow_out := u64(0)
Expand All @@ -117,6 +119,7 @@ pub fn sub_128(x Uint128, y Uint128, borrow u64) (Uint128, u64) {
return diff, borrow_out
}

// mul_128 returns u x v
pub fn mul_128(x Uint128, y Uint128) (Uint128, Uint128) {
mut lo := Uint128{}
mut hi := Uint128{}
Expand All @@ -135,6 +138,7 @@ pub fn mul_128(x Uint128, y Uint128) (Uint128, Uint128) {
return hi, lo
}

// div_128 returns u / v
pub fn div_128(hi Uint128, lo Uint128, y_ Uint128) (Uint128, Uint128) {
mut y := y_
if y.is_zero() {
Expand Down Expand Up @@ -211,6 +215,7 @@ pub fn (u Uint128) mul_64(v u64) Uint128 {
return Uint128{lo, hi}
}

// overflowing_mul_64 returns u x v even if result size > 64
pub fn (u Uint128) overflowing_mul_64(v u64) (Uint128, bool) {
hi, lo := bits.mul_64(u.lo, v)
p0, p1 := bits.mul_64(u.hi, v)
Expand All @@ -219,6 +224,7 @@ pub fn (u Uint128) overflowing_mul_64(v u64) (Uint128, bool) {
return Uint128{lo, hi2}, p0 != 0 || c0 != 0
}

// overflowing_add_64 returns u = v even if result size > 64
pub fn (u Uint128) overflowing_add_64(v u64) (Uint128, u64) {
lo, carry := bits.add_64(u.lo, v, 0)
hi, carry2 := bits.add_64(u.hi, 0, carry)
Expand Down Expand Up @@ -366,6 +372,7 @@ pub fn (u Uint128) reverse_bytes() Uint128 {
return Uint128{bits.reverse_bytes_64(u.hi), bits.reverse_bytes_64(u.lo)}
}

// not
pub fn (u Uint128) not() Uint128 {
return Uint128{~u.lo, ~u.hi}
}
Expand Down Expand Up @@ -413,10 +420,12 @@ pub fn uint128_from_64(v u64) Uint128 {
return uint128_new(v, 0)
}

// uint128_new creates new Uint128 with given `lo` and `hi`
pub fn uint128_new(lo u64, hi u64) Uint128 {
return Uint128{lo, hi}
}

// unint_from_dec_str returns an error or new Uint128 from given string
pub fn uint128_from_dec_str(value string) !Uint128 {
mut res := unsigned.uint128_zero
for b_ in value.bytes() {
Expand All @@ -441,26 +450,32 @@ pub fn uint128_from_dec_str(value string) !Uint128 {
return res
}

// / -> returns u / v
pub fn (u Uint128) / (v Uint128) Uint128 {
return u.div(v)
}

// % -> returns u % v
pub fn (u Uint128) % (v Uint128) Uint128 {
return u.mod(v)
}

// + -> returns u + v
pub fn (u Uint128) + (v Uint128) Uint128 {
return u.add(v)
}

// - -> returns u - v
pub fn (u Uint128) - (v Uint128) Uint128 {
return u.sub(v)
}

// * -> returns u * v
pub fn (u Uint128) * (v Uint128) Uint128 {
return u.mul(v)
}

// < -> returns true if u < v
pub fn (u Uint128) < (v Uint128) bool {
return u.cmp(v) == -1
}

0 comments on commit 20a1af3

Please sign in to comment.