Skip to content

Commit 21e93b5

Browse files
committed
Add special READMEs before the PR is merged by the upstream
1 parent ba8a0d7 commit 21e93b5

File tree

2 files changed

+123
-27
lines changed

2 files changed

+123
-27
lines changed

README.md

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
[![Build Status](https://travis-ci.org/JohnCGriffin/overflow.png)](https://travis-ci.org/JohnCGriffin/overflow)
21
# overflow
32
Check for integer overflow in Golang arithmetic and type conversion.
3+
4+
Other language README: [中文版](./README.zh_CN.md)
45
### Install
6+
7+
*This is a special README before the PR is merged by the upstream.*
8+
9+
This repository forks from `johncgriffin/overflow` and adds overflow detection for unsigned integer arithmetic and type conversions between integers.
10+
11+
It has been well tested and benchmarked, and passed the code security scan provided by Github Workflow.
12+
13+
[![CodeQL](https://github.com/rwxe/overflow/actions/workflows/codeql.yml/badge.svg)](https://github.com/rwxe/overflow/actions/workflows/codeql.yml)
14+
515
```sh
6-
go get github.com/johncgriffin/overflow
16+
go get github.com/rwxe/overflow
717
```
8-
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:
1122
```sh
1223
go generate
1324
```
@@ -19,18 +30,15 @@ package main
1930

2031
import "fmt"
2132
import "math"
22-
import "github.com/JohnCGriffin/overflow"
33+
import "github.com/rwxe/overflow"
2334

2435
func main() {
25-
2636
addend := math.MaxInt64 - 5
27-
2837
for i := 0; i < 10; i++ {
2938
sum, ok := overflow.Add(addend, i)
3039
fmt.Printf("%v+%v -> (%v,%v)\n",
3140
addend, i, sum, ok)
3241
}
33-
3442
}
3543
```
3644
yields the output
@@ -46,7 +54,6 @@ yields the output
4654
9223372036854775802+8 -> (0,false)
4755
9223372036854775802+9 -> (0,false)
4856
```
49-
5057
For (u)int types, provide (U)Add, (U)Sub, (U)Mul, (U)Div, (U)Quotient, etc.
5158

5259

@@ -79,30 +86,17 @@ Provide UintToInt, IntToUint, Uint64ToInt32, Int32ToUint64, etc.
7986

8087
### Stay calm and panic
8188

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.
8590

8691
### Performance considerations
8792

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.
9794

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.
10196

10297
### Basis and dependencies
10398

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.
106100

107101
### License
108102

README.zh_CN.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# overflow 溢出
2+
检查 Golang 算术和类型转换中的整数溢出。
3+
### 安装
4+
5+
*这是一份PR被上游合并之前的特殊README。*
6+
7+
该库从`johncgriffin/overflow`派生而来并添加了无符号整数的算术和整数之间的溢出检测。
8+
9+
它经过了良好的测试和基准测试,并通过了由GitHub提供的Workflow代码安全扫描。
10+
11+
[![CodeQL](https://github.com/rwxe/overflow/actions/workflows/codeql.yml/badge.svg)](https://github.com/rwxe/overflow/actions/workflows/codeql.yml)
12+
13+
```sh
14+
go get github.com/rwxe/overflow
15+
```
16+
17+
为了兼容旧代码并保持代码简单易读,新的代码仍然不使用泛型,而是使用模板来生成代码。 所以大多数重复代码由`overflow_template.sh`生成。
18+
19+
如果您必须更改算法,请在那里更改并通过以下方式重新生成 Go 代码:
20+
```sh
21+
go generate
22+
```
23+
### 概要
24+
25+
#### 算术溢出检测
26+
```go
27+
package main
28+
29+
import "fmt"
30+
import "math"
31+
import "github.com/rwxe/overflow"
32+
33+
func main() {
34+
addend := math.MaxInt64 - 5
35+
for i := 0; i < 10; i++ {
36+
sum, ok := overflow.Add(addend, i)
37+
fmt.Printf("%v+%v -> (%v,%v)\n",
38+
addend, i, sum, ok)
39+
}
40+
}
41+
```
42+
输出
43+
```go
44+
9223372036854775802+0 -> (9223372036854775802,true)
45+
9223372036854775802+1 -> (9223372036854775803,true)
46+
9223372036854775802+2 -> (9223372036854775804,true)
47+
9223372036854775802+3 -> (9223372036854775805,true)
48+
9223372036854775802+4 -> (9223372036854775806,true)
49+
9223372036854775802+5 -> (9223372036854775807,true)
50+
9223372036854775802+6 -> (0,false)
51+
9223372036854775802+7 -> (0,false)
52+
9223372036854775802+8 -> (0,false)
53+
9223372036854775802+9 -> (0,false)
54+
```
55+
对于 (u)int 类型,提供 (U)Add、(U)Sub、(U)Mul、(U)Div、(U)Quotient 等操作。
56+
57+
58+
#### 类型转换溢出检测
59+
```go
60+
func main() {
61+
var i uint
62+
for i = math.MaxInt - 5; i <= math.MaxInt+5; i++ {
63+
ret, ok := overflow.UintToInt(i)
64+
fmt.Printf("%v -> (%v,%v)\n",
65+
i, ret, ok)
66+
}
67+
}
68+
```
69+
输出
70+
```go
71+
9223372036854775802 -> (9223372036854775802,true)
72+
9223372036854775803 -> (9223372036854775803,true)
73+
9223372036854775804 -> (9223372036854775804,true)
74+
9223372036854775805 -> (9223372036854775805,true)
75+
9223372036854775806 -> (9223372036854775806,true)
76+
9223372036854775807 -> (9223372036854775807,true)
77+
9223372036854775808 -> (-9223372036854775808,false)
78+
9223372036854775809 -> (-9223372036854775807,false)
79+
9223372036854775810 -> (-9223372036854775806,false)
80+
9223372036854775811 -> (-9223372036854775805,false)
81+
9223372036854775812 -> (-9223372036854775804,false)
82+
```
83+
提供UintToInt、IntToUint、Uint64ToInt32、Int32ToUint64等操作。
84+
85+
### 保持冷静并恐慌
86+
87+
有充分的证据表明,恐慌是一种不惯用但正确的反应。 如果你相信在算术和转换出现问题后,没有有效的方法可以继续你的程序,你可以使用更简单的 Addp、Mulp、Subp 、Divp、UintToIntp、IntToUintp 版本,它们返回正常结果或恐慌。
88+
89+
### 性能考虑
90+
91+
与其他语言(例如C++)的整数类型安全库相比,该库使用了一些看似缓慢的操作,例如除法。 但这并不意味着这些方法会很慢,相反,它会比其他语言中的复杂实现更快。 原因是Go不允许强制内联,任何复杂的函数都会被抛弃内联,导致额外的调用开销。 而短函数由于自动内联将快如闪电。 例如,对于无符号64位整数乘法溢出检测,当禁用内联时,除法所需的时间是长乘法的5倍,但允许自动内联后,除法所需的时间是长乘法的1/5。
92+
93+
请注意,在业务函数中使用 `//go:noinline` 不会影响本库函数的内联。 只有通过 `-gcflags="-l"` 禁用全局内联才会影响该本库函数的内联。
94+
95+
### 基于和依赖
96+
97+
该库基于Go的官方编译器实现和语言规范,其定义了整数溢出发生时的行为。
98+
99+
### 许可
100+
101+
[MIT LICENSE](./LICENSE.md)
102+

0 commit comments

Comments
 (0)