You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that because Go has no template types, the majority of repetitive code is
9
-
generated by overflow_template.sh. If you have to change an
10
-
algorithm, change it there and regenerate the Go code via:
18
+
19
+
In order to be compatible with the old code and keep the code simple and readable, the new code still does not use generics, but uses templates to generate code. So the majority of repetitive code is generated by `overflow_template.sh`.
20
+
21
+
If you have to change an algorithm, change it there and regenerate the Go code via:
11
22
```sh
12
23
go generate
13
24
```
@@ -19,18 +30,15 @@ package main
19
30
20
31
import"fmt"
21
32
import"math"
22
-
import"github.com/JohnCGriffin/overflow"
33
+
import"github.com/rwxe/overflow"
23
34
24
35
funcmain() {
25
-
26
36
addend:= math.MaxInt64 - 5
27
-
28
37
fori:=0; i < 10; i++ {
29
38
sum, ok:= overflow.Add(addend, i)
30
39
fmt.Printf("%v+%v -> (%v,%v)\n",
31
40
addend, i, sum, ok)
32
41
}
33
-
34
42
}
35
43
```
36
44
yields the output
@@ -46,7 +54,6 @@ yields the output
46
54
9223372036854775802+8 -> (0,false)
47
55
9223372036854775802+9 -> (0,false)
48
56
```
49
-
50
57
For (u)int types, provide (U)Add, (U)Sub, (U)Mul, (U)Div, (U)Quotient, etc.
51
58
52
59
@@ -79,30 +86,17 @@ Provide UintToInt, IntToUint, Uint64ToInt32, Int32ToUint64, etc.
79
86
80
87
### Stay calm and panic
81
88
82
-
There's a good case to be made that a panic is an unidiomatic but proper response. Iff you
83
-
believe that there's no valid way to continue your program after math goes wayward, you can
84
-
use the easier Addp, Mulp, Subp, and Divp versions which return the normal result or panic.
89
+
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, Divp, IntToUintp, UintToIntp versions which return the normal result or panic.
85
90
86
91
### Performance considerations
87
92
88
-
Compared with the integer type safety libraries of other languages (such as C++), this
89
-
library uses some seemingly slow operations, such as division. But this does not mean that
90
-
these methods will be slow, on the contrary, it will be faster than complex implementations
91
-
in other languages. The reason is that Go does not allow forced inlining, and any complex
92
-
functions will be abandoned for inlining, resulting in additional calling overhead. Short
93
-
functions are lightning fast due to automatic inlining. For example, for unsigned 64-bit
94
-
integer multiplication overflow detection, when inlining is disabled, division takes five
95
-
times as long as long multiplication, but after automatic inlining is allowed, division
96
-
takes 1/5 of long multiplication.
93
+
Compared with the integer type safety libraries of other languages (such as C++), this library uses some seemingly slow operations, such as division. But this does not mean that these methods will be slow, on the contrary, it will be faster than complex implementations in other languages. The reason is that Go does not allow forced inlining, and any complex functions will be abandoned for inlining, resulting in additional calling overhead. Short functions are lightning fast due to automatic inlining. For example, for unsigned 64-bit integer multiplication overflow detection, when inlining is disabled, division takes five times as long as long multiplication, but after automatic inlining is allowed, division takes 1/5 of long multiplication.
97
94
98
-
Note that using `//go:noinline` in your business function will not affect the inlining of
99
-
the library function. Only disabling global inlining through `-gcflags="-l"` will affect the
100
-
inlining of this library function.
95
+
Note that using `//go:noinline` in your business function will not affect the inlining of the library function. Only disabling global inlining through `-gcflags="-l"` will affect the inlining of this library function.
101
96
102
97
### Basis and dependencies
103
98
104
-
This library is based on Go's official compiler implementation and language specification,
105
-
which defines the behavior when integer overflow occurs.
99
+
This library is based on Go's official compiler implementation and language specification, which defines the behavior when integer overflow occurs.
0 commit comments