Search Apps Documentation Source Content File Folder Download Copy Actions Download

util.gno

0.62 Kb ยท 31 lines
 1package staker
 2
 3import (
 4	"math"
 5)
 6
 7// safeAddInt64 performs safe addition of int64 values, panicking on overflow or underflow
 8func safeAddInt64(a, b int64) int64 {
 9	if a > 0 && b > math.MaxInt64-a {
10		panic("int64 addition overflow")
11	}
12
13	if a < 0 && b < math.MinInt64-a {
14		panic("int64 addition underflow")
15	}
16
17	return a + b
18}
19
20// safeSubInt64 performs safe subtraction of int64 values, panicking on overflow or underflow
21func safeSubInt64(a, b int64) int64 {
22	if b > 0 && a < math.MinInt64+b {
23		panic("int64 subtraction underflow")
24	}
25
26	if b < 0 && a > math.MaxInt64+b {
27		panic("int64 subtraction overflow")
28	}
29
30	return a - b
31}