1515// Add, Sub, Mul, Div are for int. Add64, Add32, etc. are specifically sized.
1616//
1717// If anybody wishes an unsigned version, submit a pull request for code and new tests.
18-
1918package overflow
2019
21- //go:generate ./overflow_template.sh
20+ //go:generate ./overflow_template.sh
2221
2322import "math"
2423
@@ -27,7 +26,6 @@ func _is64Bit() bool {
2726 return ((maxU32 << 1 ) >> 1 ) == maxU32
2827}
2928
30-
3129/********** PARTIAL TEST COVERAGE FROM HERE DOWN *************
3230
3331The only way that I could see to do this is a combination of
@@ -40,6 +38,7 @@ So, FEEL FREE to carefully review the code visually.
4038
4139// Unspecified size, i.e. normal signed int
4240
41+ // Add sums two ints, returning the result and a boolean status.
4342func Add (a , b int ) (int , bool ) {
4443 if _is64Bit () {
4544 r64 , ok := Add64 (int64 (a ), int64 (b ))
@@ -49,6 +48,7 @@ func Add(a, b int) (int, bool) {
4948 return int (r32 ), ok
5049}
5150
51+ // Sub returns the difference of two ints and a boolean status.
5252func Sub (a , b int ) (int , bool ) {
5353 if _is64Bit () {
5454 r64 , ok := Sub64 (int64 (a ), int64 (b ))
@@ -58,6 +58,7 @@ func Sub(a, b int) (int, bool) {
5858 return int (r32 ), ok
5959}
6060
61+ // Mul returns the product of two ints and a boolean status.
6162func Mul (a , b int ) (int , bool ) {
6263 if _is64Bit () {
6364 r64 , ok := Mul64 (int64 (a ), int64 (b ))
@@ -67,6 +68,7 @@ func Mul(a, b int) (int, bool) {
6768 return int (r32 ), ok
6869}
6970
71+ // Div returns the quotient of two ints and a boolean status
7072func Div (a , b int ) (int , bool ) {
7173 if _is64Bit () {
7274 r64 , ok := Div64 (int64 (a ), int64 (b ))
@@ -76,6 +78,7 @@ func Div(a, b int) (int, bool) {
7678 return int (r32 ), ok
7779}
7880
81+ // Quotient returns the quotient, remainder and status of two ints
7982func Quotient (a , b int ) (int , int , bool ) {
8083 if _is64Bit () {
8184 q64 , r64 , ok := Quotient64 (int64 (a ), int64 (b ))
@@ -87,6 +90,7 @@ func Quotient(a, b int) (int, int, bool) {
8790
8891/************* Panic versions for int ****************/
8992
93+ // Addp returns the sum of two ints, panicking on overflow
9094func Addp (a , b int ) int {
9195 r , ok := Add (a , b )
9296 if ! ok {
@@ -95,6 +99,7 @@ func Addp(a, b int) int {
9599 return r
96100}
97101
102+ // Subp returns the difference of two ints, panicking on overflow.
98103func Subp (a , b int ) int {
99104 r , ok := Sub (a , b )
100105 if ! ok {
@@ -103,6 +108,7 @@ func Subp(a, b int) int {
103108 return r
104109}
105110
111+ // Mulp returns the product of two ints, panicking on overflow.
106112func Mulp (a , b int ) int {
107113 r , ok := Mul (a , b )
108114 if ! ok {
@@ -111,6 +117,7 @@ func Mulp(a, b int) int {
111117 return r
112118}
113119
120+ // Divp returns the quotient of two ints, panicking on overflow.
114121func Divp (a , b int ) int {
115122 r , ok := Div (a , b )
116123 if ! ok {
0 commit comments