-
Notifications
You must be signed in to change notification settings - Fork 451
/
overflow.go
170 lines (147 loc) · 4.41 KB
/
overflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package basics
import (
"math/bits"
"golang.org/x/exp/constraints"
)
// OverflowTracker is used to track when an operation causes an overflow
type OverflowTracker struct {
Overflowed bool
}
// OAdd adds 2 values with overflow detection
func OAdd[T constraints.Unsigned](a, b T) (res T, overflowed bool) {
res = a + b
overflowed = res < a
return
}
// OSub subtracts b from a with overflow detection
func OSub[T constraints.Unsigned](a, b T) (res T, overflowed bool) {
res = a - b
overflowed = res > a
return
}
// OMul multiplies 2 values with overflow detection
func OMul[T constraints.Unsigned](a, b T) (res T, overflowed bool) {
if b == 0 {
return 0, false
}
c := a * b
if c/b != a {
return 0, true
}
return c, false
}
// MulSaturate multiplies 2 values with saturation on overflow
func MulSaturate[T constraints.Unsigned](a, b T) T {
res, overflowed := OMul(a, b)
if overflowed {
var defaultT T
return ^defaultT
}
return res
}
// AddSaturate adds 2 values with saturation on overflow
func AddSaturate[T constraints.Unsigned](a, b T) T {
res, overflowed := OAdd(a, b)
if overflowed {
var defaultT T
return ^defaultT
}
return res
}
// SubSaturate subtracts 2 values with saturation on underflow
func SubSaturate[T constraints.Unsigned](a, b T) T {
res, overflowed := OSub(a, b)
if overflowed {
return 0
}
return res
}
// Add adds 2 values with overflow detection
func (t *OverflowTracker) Add(a, b uint64) uint64 {
res, overflowed := OAdd(a, b)
if overflowed {
t.Overflowed = true
}
return res
}
// Sub subtracts b from a with overflow detection
func (t *OverflowTracker) Sub(a, b uint64) uint64 {
res, overflowed := OSub(a, b)
if overflowed {
t.Overflowed = true
}
return res
}
// Mul multiplies b by a with overflow detection
func (t *OverflowTracker) Mul(a, b uint64) uint64 {
res, overflowed := OMul(a, b)
if overflowed {
t.Overflowed = true
}
return res
}
// OAddA adds 2 MicroAlgos values with overflow tracking
func OAddA(a, b MicroAlgos) (res MicroAlgos, overflowed bool) {
res.Raw, overflowed = OAdd(a.Raw, b.Raw)
return
}
// OSubA subtracts b from a with overflow tracking
func OSubA(a, b MicroAlgos) (res MicroAlgos, overflowed bool) {
res.Raw, overflowed = OSub(a.Raw, b.Raw)
return
}
// MulAIntSaturate uses MulSaturate to multiply b (int) with a (MicroAlgos)
func MulAIntSaturate(a MicroAlgos, b int) MicroAlgos {
return MicroAlgos{Raw: MulSaturate(a.Raw, uint64(b))}
}
// AddA adds 2 MicroAlgos values with overflow tracking
func (t *OverflowTracker) AddA(a, b MicroAlgos) MicroAlgos {
return MicroAlgos{Raw: t.Add(a.Raw, b.Raw)}
}
// SubA subtracts b from a with overflow tracking
func (t *OverflowTracker) SubA(a, b MicroAlgos) MicroAlgos {
return MicroAlgos{Raw: t.Sub(a.Raw, b.Raw)}
}
// ScalarMulA multiplies an Algo amount by a scalar
func (t *OverflowTracker) ScalarMulA(a MicroAlgos, b uint64) MicroAlgos {
return MicroAlgos{Raw: t.Mul(a.Raw, b)}
}
// MinA returns the smaller of 2 MicroAlgos values
func MinA(a, b MicroAlgos) MicroAlgos {
if a.Raw < b.Raw {
return a
}
return b
}
// Muldiv computes a*b/c. The overflow flag indicates that
// the result was 2^64 or greater.
func Muldiv(a uint64, b uint64, c uint64) (res uint64, overflow bool) {
hi, lo := bits.Mul64(a, b)
if c <= hi {
return 0, true
}
quo, _ := bits.Div64(hi, lo, c)
return quo, false
}
// DivCeil provides `math.Ceil` semantics using integer division. The technique
// avoids slower floating point operations as suggested in https://stackoverflow.com/a/2745086.
//
// The method assumes both numbers are positive and does _not_ check for divide-by-zero.
func DivCeil[T constraints.Integer](numerator, denominator T) T {
return (numerator + denominator - 1) / denominator
}