Skip to content

Commit

Permalink
chore: import gnolang/overflow into gno/tm2/pkg/overflow (#3302)
Browse files Browse the repository at this point in the history
We remove the dependency on gnolang/overflow, a clone of
https://github.com/JohnCGriffin/overflow which is now unmaintained, and
import it into gno/tm2/pkg/overflow. We can now have full control on it,
fix it and improve it.

This PR just changes the import path, no content change is done yet.

---------

Co-authored-by: Morgan Bazalgette <[email protected]>
  • Loading branch information
mvertes and thehowl authored Dec 8, 2024
1 parent e46f457 commit 666c54a
Show file tree
Hide file tree
Showing 28 changed files with 789 additions and 35 deletions.
1 change: 0 additions & 1 deletion contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnodev/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contribs/gnofaucet/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnofaucet/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contribs/gnogenesis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnogenesis/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contribs/gnohealth/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnohealth/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contribs/gnokeykc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ require (
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnokeykc/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contribs/gnomigrate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
2 changes: 0 additions & 2 deletions contribs/gnomigrate/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
"strings"
"sync"

"github.com/gnolang/overflow"

"github.com/gnolang/gno/gnovm"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/overflow"
"github.com/gnolang/gno/tm2/pkg/store"
)

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/fortytw2/leaktest v1.3.0
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216
github.com/google/gofuzz v1.2.0
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion misc/autocounterd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
Expand Down
2 changes: 0 additions & 2 deletions misc/autocounterd/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion misc/loop/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ require (
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
4 changes: 0 additions & 4 deletions misc/loop/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions tm2/pkg/overflow/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# overflow

Check for int/int8/int16/int64/int32 integer overflow in Golang arithmetic.

Forked from https://github.com/JohnCGriffin/overflow

### Install
```
go get github.com/johncgriffin/overflow
```
Note that because Go has no template types, 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:
```
go generate
```
### Synopsis

```
package main
import "fmt"
import "math"
import "github.com/JohnCGriffin/overflow"
func main() {
addend := math.MaxInt64 - 5
for i := 0; i < 10; i++ {
sum, ok := overflow.Add(addend, i)
fmt.Printf("%v+%v -> (%v,%v)\n",
addend, i, sum, ok)
}
}
```
yields the output
```
9223372036854775802+0 -> (9223372036854775802,true)
9223372036854775802+1 -> (9223372036854775803,true)
9223372036854775802+2 -> (9223372036854775804,true)
9223372036854775802+3 -> (9223372036854775805,true)
9223372036854775802+4 -> (9223372036854775806,true)
9223372036854775802+5 -> (9223372036854775807,true)
9223372036854775802+6 -> (0,false)
9223372036854775802+7 -> (0,false)
9223372036854775802+8 -> (0,false)
9223372036854775802+9 -> (0,false)
```

For int, int64, and int32 types, provide Add, Add32, Add64, Sub, Sub32, Sub64, etc.
Unsigned types not covered at the moment, but such additions are welcome.

### Stay calm and panic

There's a good case to be made that a panic is an unidiomatic but proper response. Iff you
believe that there's no valid way to continue your program after math goes wayward, you can
use the easier Addp, Mulp, Subp, and Divp versions which return the normal result or panic.







131 changes: 131 additions & 0 deletions tm2/pkg/overflow/overflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
Package overflow offers overflow-checked integer arithmetic operations
for int, int32, and int64. Each of the operations returns a
result,bool combination. This was prompted by the need to know when
to flow into higher precision types from the math.big library.
For instance, assuing a 64 bit machine:
10 + 20 -> 30
int(math.MaxInt64) + 1 -> -9223372036854775808
whereas
overflow.Add(10,20) -> (30, true)
overflow.Add(math.MaxInt64,1) -> (0, false)
Add, Sub, Mul, Div are for int. Add64, Add32, etc. are specifically sized.
If anybody wishes an unsigned version, submit a pull request for code
and new tests.
*/
package overflow

//go:generate ./overflow_template.sh

import "math"

func _is64Bit() bool {
maxU32 := uint(math.MaxUint32)
return ((maxU32 << 1) >> 1) == maxU32
}

/********** PARTIAL TEST COVERAGE FROM HERE DOWN *************
The only way that I could see to do this is a combination of
my normal 64 bit system and a GopherJS running on Node. My
understanding is that its ints are 32 bit.
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.
func Add(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Add64(int64(a), int64(b))
return int(r64), ok
}
r32, ok := Add32(int32(a), int32(b))
return int(r32), ok
}

// Sub returns the difference of two ints and a boolean status.
func Sub(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Sub64(int64(a), int64(b))
return int(r64), ok
}
r32, ok := Sub32(int32(a), int32(b))
return int(r32), ok
}

// Mul returns the product of two ints and a boolean status.
func Mul(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Mul64(int64(a), int64(b))
return int(r64), ok
}
r32, ok := Mul32(int32(a), int32(b))
return int(r32), ok
}

// Div returns the quotient of two ints and a boolean status
func Div(a, b int) (int, bool) {
if _is64Bit() {
r64, ok := Div64(int64(a), int64(b))
return int(r64), ok
}
r32, ok := Div32(int32(a), int32(b))
return int(r32), ok
}

// Quotient returns the quotient, remainder and status of two ints
func Quotient(a, b int) (int, int, bool) {
if _is64Bit() {
q64, r64, ok := Quotient64(int64(a), int64(b))
return int(q64), int(r64), ok
}
q32, r32, ok := Quotient32(int32(a), int32(b))
return int(q32), int(r32), ok
}

/************* Panic versions for int ****************/

// Addp returns the sum of two ints, panicking on overflow
func Addp(a, b int) int {
r, ok := Add(a, b)
if !ok {
panic("addition overflow")
}
return r
}

// Subp returns the difference of two ints, panicking on overflow.
func Subp(a, b int) int {
r, ok := Sub(a, b)
if !ok {
panic("subtraction overflow")
}
return r
}

// Mulp returns the product of two ints, panicking on overflow.
func Mulp(a, b int) int {
r, ok := Mul(a, b)
if !ok {
panic("multiplication overflow")
}
return r
}

// Divp returns the quotient of two ints, panicking on overflow.
func Divp(a, b int) int {
r, ok := Div(a, b)
if !ok {
panic("division failure")
}
return r
}
Loading

0 comments on commit 666c54a

Please sign in to comment.