Skip to content

Commit

Permalink
Merge branch 'SupportsUnsignedAndTypeConversionFeaturesdev'
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxe committed Sep 29, 2023
2 parents 98d8fc0 + ba8a0d7 commit d7aa24c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ go get github.com/rwxe/overflow
```
In order to be compatible with old code, and when this project was created, there were no
generics in Go, so the majority of repetitive code is generated by `overflow_template.sh`.

If you have to change an algorithm, change it there and regenerate the Go code via:

```sh
Expand Down Expand Up @@ -113,6 +114,12 @@ Note that using `//go:noinline` in your business function will not affect the in
the library function. Only disabling global inlining through `-gcflags="-l"` will affect the
inlining of this library function.

### Basis and dependencies

This library is based on Go's official compiler implementation and language specification,
which defines the behavior when integer overflow occurs.

### License

[MIT LICENSE](./LICENSE.md)

20 changes: 10 additions & 10 deletions overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ So, FEEL FREE to carefully review the code visually.

// Unspecified size, i.e. normal signed int

// Add sums two ints, returning the result and a boolean status.
// Add sums two ints, returning the result and a ok result indicating whether the operation is safe.
func Add(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Add64(int64(a), int64(b))
Expand All @@ -52,7 +52,7 @@ func Add(a, b int) (int, bool) {
return int(r32), ok
}

// UAdd sums two uints, returning the result and a boolean status.
// UAdd sums two uints, returning the result and a ok result indicating whether the operation is safe.
func UAdd(a, b uint) (uint, bool) {
if _is64Bit() {
r64, ok := UAdd64(uint64(a), uint64(b))
Expand All @@ -62,7 +62,7 @@ func UAdd(a, b uint) (uint, bool) {
return uint(r32), ok
}

// Sub returns the difference of two ints and a boolean status.
// Sub returns the difference of two ints and a ok result indicating whether the operation is safe.
func Sub(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Sub64(int64(a), int64(b))
Expand All @@ -72,7 +72,7 @@ func Sub(a, b int) (int, bool) {
return int(r32), ok
}

// USub returns the difference of two uints and a boolean status.
// USub returns the difference of two uints and a ok result indicating whether the operation is safe.
func USub(a, b uint) (uint, bool) {
if _is64Bit() {
r64, ok := USub64(uint64(a), uint64(b))
Expand All @@ -82,7 +82,7 @@ func USub(a, b uint) (uint, bool) {
return uint(r32), ok
}

// Mul returns the product of two ints and a boolean status.
// Mul returns the product of two ints and a ok result indicating whether the operation is safe.
func Mul(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Mul64(int64(a), int64(b))
Expand All @@ -92,7 +92,7 @@ func Mul(a, b int) (int, bool) {
return int(r32), ok
}

// UMul returns the product of two uints and a boolean status.
// UMul returns the product of two uints and a ok result indicating whether the operation is safe.
func UMul(a, b uint) (uint, bool) {
if _is64Bit() {
r64, ok := UMul64(uint64(a), uint64(b))
Expand All @@ -102,7 +102,7 @@ func UMul(a, b uint) (uint, bool) {
return uint(r32), ok
}

// Div returns the quotient of two ints and a boolean status
// Div returns the quotient of two ints and a ok result indicating whether the operation is safe.
func Div(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Div64(int64(a), int64(b))
Expand All @@ -112,7 +112,7 @@ func Div(a, b int) (int, bool) {
return int(r32), ok
}

// UDiv returns the quotient of two uints and a boolean status
// UDiv returns the quotient of two uints and a ok result indicating whether the operation is safe.
func UDiv(a, b uint) (uint, bool) {
if _is64Bit() {
r64, ok := UDiv64(uint64(a), uint64(b))
Expand All @@ -122,7 +122,7 @@ func UDiv(a, b uint) (uint, bool) {
return uint(r32), ok
}

// Quotient returns the quotient, remainder and status of two ints
// Quotient returns the quotient, remainder and ok result indicating whether the operation is safe.
func Quotient(a, b int) (int, int, bool) {
if _is64Bit() {
q64, r64, ok := Quotient64(int64(a), int64(b))
Expand All @@ -132,7 +132,7 @@ func Quotient(a, b int) (int, int, bool) {
return int(q32), int(r32), ok
}

// UQuotient returns the quotient, remainder and status of two uints
// UQuotient returns the quotient, remainder and ok result indicating whether the operation is safe.
func UQuotient(a, b uint) (uint, uint, bool) {
if _is64Bit() {
uq64, ur64, ok := UQuotient64(uint64(a), uint64(b))
Expand Down
3 changes: 3 additions & 0 deletions overflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func TestQuotient(t *testing.T) {
if _, _, ok = Quotient(1, 0); ok {
t.Error("unexpected lack of failure")
}
if _, _, ok = Quotient(math.MinInt, -1); ok {
t.Error("unexpected lack of failure")
}
uq, ur, ok := UQuotient(100, 3)
if ur != 1 || uq != 33 || !ok {
t.Errorf("expected 100/3 => 33, r=1")
Expand Down

0 comments on commit d7aa24c

Please sign in to comment.