mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-10 13:01:21 +00:00
18 lines
339 B
Go
18 lines
339 B
Go
|
package math
|
||
|
|
||
|
import (
|
||
|
"math/bits"
|
||
|
)
|
||
|
|
||
|
// SafeMul returns x*y and checks for overflow.
|
||
|
func SafeMul(x, y uint64) (uint64, bool) {
|
||
|
hi, lo := bits.Mul64(x, y)
|
||
|
return lo, hi != 0
|
||
|
}
|
||
|
|
||
|
// SafeAdd returns x+y and checks for overflow.
|
||
|
func SafeAdd(x, y uint64) (uint64, bool) {
|
||
|
sum, carryOut := bits.Add64(x, y, 0)
|
||
|
return sum, carryOut != 0
|
||
|
}
|