-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patharith.go
104 lines (94 loc) · 2.82 KB
/
arith.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
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package utils
import "math"
// AddWithOverflow returns a+b. If ok is false, a+b overflowed.
func AddWithOverflow(a, b int64) (r int64, ok bool) {
if b > 0 && a > math.MaxInt64-b {
return 0, false
}
if b < 0 && a < math.MinInt64-b {
return 0, false
}
return a + b, true
}
// Add32to64WithOverflow returns a+b. If ok is false, b was outside the
// int32 range or a+b overflowed.
func Add32to64WithOverflow(a int32, b int64) (r int32, ok bool) {
if b > math.MaxInt32 || b < math.MinInt32 {
return 0, false
}
return Add32WithOverflow(a, int32(b))
}
// Add32WithOverflow returns a+b. If ok is false, a+b overflowed.
func Add32WithOverflow(a, b int32) (r int32, ok bool) {
if b > 0 && a > math.MaxInt32-b {
return 0, false
}
if b < 0 && a < math.MinInt32-b {
return 0, false
}
return a + b, true
}
// SubWithOverflow returns a-b. If ok is false, a-b overflowed.
func SubWithOverflow(a, b int64) (r int64, ok bool) {
if b < 0 && a > math.MaxInt64+b {
return 0, false
}
if b > 0 && a < math.MinInt64+b {
return 0, false
}
return a - b, true
}
// Sub32to64WithOverflow returns a-b. If ok is false, b was outside the
// int32 range or a-b overflowed.
func Sub32to64WithOverflow(a int32, b int64) (r int32, ok bool) {
if b > math.MaxInt32 || b < math.MinInt32 {
return 0, false
}
return Sub32WithOverflow(a, int32(b))
}
// Sub32WithOverflow returns a-b. If ok is false, a-b overflowed.
func Sub32WithOverflow(a, b int32) (r int32, ok bool) {
if b < 0 && a > math.MaxInt32+b {
return 0, false
}
if b > 0 && a < math.MinInt32+b {
return 0, false
}
return a - b, true
}
// MulHalfPositiveWithOverflow returns a*b. b must be positive. If ok
// is false, a*b overflowed.
func MulHalfPositiveWithOverflow(a, b int64) (r int64, ok bool) {
if a >= 0 {
if a > math.MaxInt64/b {
return 0, false
}
} else {
if a < math.MinInt64/b {
return 0, false
}
}
return a * b, true
}