Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(u128): saturating, checked - add and sub #84

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ func SaturatingAddU64(a, b U64) U64 {
return U64(sum)
}

func SaturatingAddU128(a, b U128) U128 {
sumLow, carry := bits.Add64(uint64(a[0]), uint64(b[0]), 0)
sumHigh, overflow := bits.Add64(uint64(a[1]), uint64(b[1]), carry)
// check for overflow
if overflow == 1 || (carry == 1 && sumHigh <= uint64(a[1]) && sumHigh <= uint64(b[1])) {
return MaxU128()
}
return U128{U64(sumLow), U64(sumHigh)}
}

func SaturatingSubU64(a, b U64) U64 {
diff, borrow := bits.Sub64(uint64(a), uint64(b), 0)
if borrow != 0 {
Expand All @@ -110,6 +120,16 @@ func SaturatingSubU64(a, b U64) U64 {
return U64(diff)
}

func SaturatingSubU128(a, b U128) U128 {
low, borrow := bits.Sub64(uint64(a[0]), uint64(b[0]), 0)
high, _ := bits.Sub64(uint64(a[1]), uint64(b[1]), borrow)
// check for underflow
if borrow == 1 || high > uint64(a[1]) {
return U128{}
}
return U128{U64(low), U64(high)}
}

func SaturatingMulU64(a, b U64) U64 {
if a == 0 || b == 0 {
return U64(0)
Expand Down Expand Up @@ -137,3 +157,23 @@ func CheckedAddU64(a, b U64) (U64, error) {
}
return U64(sum), nil
}

func CheckedAddU128(a, b U128) (U128, error) {
sumLow, carry := bits.Add64(uint64(a[0]), uint64(b[0]), 0)
sumHigh, overflow := bits.Add64(uint64(a[1]), uint64(b[1]), carry)
// check for overflow
if overflow == 1 || (carry == 1 && sumHigh <= uint64(a[1]) && sumHigh <= uint64(b[1])) {
return U128{}, errOverflow
}
return U128{U64(sumLow), U64(sumHigh)}, nil
}

func CheckedSubU128(a, b U128) (U128, error) {
low, borrow := bits.Sub64(uint64(a[0]), uint64(b[0]), 0)
high, _ := bits.Sub64(uint64(a[1]), uint64(b[1]), borrow)
// check for underflow
if borrow == 1 || high > uint64(a[1]) {
return U128{0, 0}, errUnderflow
}
return U128{U64(low), U64(high)}, nil
}
94 changes: 94 additions & 0 deletions math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,26 @@ func Test_SaturatingAddU64(t *testing.T) {
}
}

func Test_SaturatingAddU128(t *testing.T) {
testExamples := []struct {
label string
a U128
b U128
expect U128
}{
{"2 + 1", NewU128(2), NewU128(1), NewU128(3)},
{"MaxU128+1", MaxU128(), NewU128(1), MaxU128()},
{"MaxU128+MaxU128", MaxU128(), MaxU128(), MaxU128()},
}

for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
result := SaturatingAddU128(testExample.a, testExample.b)
assert.Equal(t, testExample.expect, result)
})
}
}

func Test_SaturatingSubU64(t *testing.T) {
testExamples := []struct {
label string
Expand All @@ -192,6 +212,26 @@ func Test_SaturatingSubU64(t *testing.T) {
}
}

func Test_SaturatingSubU128(t *testing.T) {
testExamples := []struct {
label string
a U128
b U128
expect U128
}{
{"2-1", NewU128(2), NewU128(1), NewU128(1)},
{"0-1", NewU128(0), NewU128(1), NewU128(0)},
{"0-MaxU128", NewU128(0), MaxU128(), NewU128(0)},
}

for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
result := SaturatingSubU128(testExample.a, testExample.b)
assert.Equal(t, testExample.expect, result)
})
}
}

func Test_SaturatingMul(t *testing.T) {
testExamples := []struct {
label string
Expand Down Expand Up @@ -268,3 +308,57 @@ func Test_CheckedAddU64(t *testing.T) {
})
}
}

func Test_CheckedAddU128(t *testing.T) {
testExamples := []struct {
label string
a U128
b U128
expect U128
hasExpectErr bool
}{
{"2 + 1", NewU128(2), NewU128(1), NewU128(3), false},
{"MaxU128+1", MaxU128(), NewU128(1), NewU128(0), true},
{"MaxU128+MaxU128", MaxU128(), MaxU128(), NewU128(0), true},
}

for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
result, err := CheckedAddU128(testExample.a, testExample.b)
assert.Equal(t, testExample.expect, result)

if testExample.hasExpectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

func Test_CheckedSubU128(t *testing.T) {
testExamples := []struct {
label string
a U128
b U128
expect U128
hasExpectErr bool
}{
{"2-1", NewU128(2), NewU128(1), NewU128(1), false},
{"0-1", NewU128(0), NewU128(1), NewU128(0), true},
{"0-MaxU128", NewU128(0), MaxU128(), NewU128(0), true},
}

for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
result, err := CheckedSubU128(testExample.a, testExample.b)
assert.Equal(t, testExample.expect, result)

if testExample.hasExpectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
20 changes: 0 additions & 20 deletions u128.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,6 @@ func (n U128) Gte(other U128) bool {
return n.ToBigInt().Cmp(other.ToBigInt()) >= 0
}

//func (n U128) SaturatingAdd(other U128) U128 {
// sumLow, carry := bits.Add64(uint64(n[0]), uint64(other[0]), 0)
// sumHigh, overflow := bits.Add64(uint64(n[1]), uint64(other[1]), carry)
// // check for overflow
// if overflow == 1 || (carry == 1 && sumHigh <= uint64(n[1]) && sumHigh <= uint64(other[1])) {
// return MaxU128()
// }
// return U128{U64(sumLow), U64(sumHigh)}
//}
//
//func (n U128) SaturatingSub(other U128) U128 {
// low, borrow := bits.Sub64(uint64(n[0]), uint64(other[0]), 0)
// high, _ := bits.Sub64(uint64(n[1]), uint64(other[1]), borrow)
// // check for underflow
// if borrow == 1 || high > uint64(n[1]) {
// return U128{0, 0}
// }
// return U128{U64(low), U64(high)}
//}
//
//func (n U128) SaturatingMul(other U128) U128 {
// result := new(big.Int).Mul(n.ToBigInt(), other.ToBigInt())
// // check for overflow
Expand Down