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

chore: import gnolang/overflow into gno/tm2/pkg/overflow #3302

Merged
merged 6 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.







129 changes: 129 additions & 0 deletions tm2/pkg/overflow/overflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*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

Check warning on line 50 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L44-L50

Added lines #L44 - L50 were not covered by tests
}

// 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

Check warning on line 60 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L54-L60

Added lines #L54 - L60 were not covered by tests
}

// 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

Check warning on line 70 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L64-L70

Added lines #L64 - L70 were not covered by tests
}

// 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

Check warning on line 80 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L74-L80

Added lines #L74 - L80 were not covered by tests
}

// 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

Check warning on line 90 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L89-L90

Added lines #L89 - L90 were not covered by tests
}

/************* 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")

Check warning on line 99 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L96-L99

Added lines #L96 - L99 were not covered by tests
}
return r

Check warning on line 101 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L101

Added line #L101 was not covered by tests
}

// 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")

Check warning on line 108 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L105-L108

Added lines #L105 - L108 were not covered by tests
}
return r

Check warning on line 110 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L110

Added line #L110 was not covered by tests
}

// 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")

Check warning on line 117 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L114-L117

Added lines #L114 - L117 were not covered by tests
}
return r

Check warning on line 119 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L119

Added line #L119 was not covered by tests
}

// 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")

Check warning on line 126 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L123-L126

Added lines #L123 - L126 were not covered by tests
}
return r

Check warning on line 128 in tm2/pkg/overflow/overflow.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/overflow/overflow.go#L128

Added line #L128 was not covered by tests
}
Loading
Loading