Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: use mathutil.Max/mathutil.Min instead of myMax/myMin #41192

Merged
merged 10 commits into from
Feb 10, 2023
28 changes: 0 additions & 28 deletions types/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,6 @@ func isPunctuation(c byte) bool {
return (c >= 0x21 && c <= 0x2F) || (c >= 0x3A && c <= 0x40) || (c >= 0x5B && c <= 0x60) || (c >= 0x7B && c <= 0x7E)
}

func myMax(a, b int) int {
if a > b {
return a
}
return b
}

func myMaxInt8(a, b int8) int8 {
if a > b {
return a
}
return b
}

func myMin(a, b int) int {
if a < b {
return a
}
return b
}

func myMinInt8(a, b int8) int8 {
if a < b {
return a
}
return b
}

const (
maxUint = uint64(math.MaxUint64)
uintCutOff = maxUint/uint64(10) + 1
Expand Down
33 changes: 17 additions & 16 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/util/mathutil"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -353,7 +354,7 @@ func (d *MyDecimal) ToString() (str []byte) {
for ; digitsFrac > 0; digitsFrac -= digitsPerWord {
x := d.wordBuf[wordIdx]
wordIdx++
for i := myMin(digitsFrac, digitsPerWord); i > 0; i-- {
for i := mathutil.Min(digitsFrac, digitsPerWord); i > 0; i-- {
y := x / digMask
str[fracIdx] = byte(y) + '0'
fracIdx++
Expand All @@ -380,7 +381,7 @@ func (d *MyDecimal) ToString() (str []byte) {
for ; digitsInt > 0; digitsInt -= digitsPerWord {
wordIdx--
x := d.wordBuf[wordIdx]
for i := myMin(digitsInt, digitsPerWord); i > 0; i-- {
for i := mathutil.Min(digitsInt, digitsPerWord); i > 0; i-- {
y := x / 10
strIdx--
str[strIdx] = '0' + byte(x-y*10)
Expand Down Expand Up @@ -840,7 +841,7 @@ func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err err
if to != d {
copy(to.wordBuf[:], d.wordBuf[:])
to.negative = d.negative
to.digitsInt = int8(myMin(wordsInt, wordBufLen) * digitsPerWord)
to.digitsInt = int8(mathutil.Min(wordsInt, wordBufLen) * digitsPerWord)
}
if wordsFracTo > wordsFrac {
idx := wordsInt + wordsFrac
Expand Down Expand Up @@ -941,7 +942,7 @@ func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err err
frac = wordsFracTo * digitsPerWord
err = ErrTruncated
}
for toIdx = wordsInt + myMax(wordsFracTo, 0); toIdx > 0; toIdx-- {
for toIdx = wordsInt + mathutil.Max(wordsFracTo, 0); toIdx > 0; toIdx-- {
if toIdx < wordBufLen {
to.wordBuf[toIdx] = to.wordBuf[toIdx-1]
} else {
Expand All @@ -965,7 +966,7 @@ func (d *MyDecimal) Round(to *MyDecimal, frac int, roundMode RoundMode) (err err
/* making 'zero' with the proper scale */
idx := wordsFracTo + 1
to.digitsInt = 1
to.digitsFrac = int8(myMax(frac, 0))
to.digitsFrac = int8(mathutil.Max(frac, 0))
to.negative = false
for toIdx < idx {
to.wordBuf[toIdx] = 0
Expand Down Expand Up @@ -1602,7 +1603,7 @@ func DecimalNeg(from *MyDecimal) *MyDecimal {
// of `to` may be changed during evaluating.
func DecimalAdd(from1, from2, to *MyDecimal) error {
from1, from2, to = validateArgs(from1, from2, to)
to.resultFrac = myMaxInt8(from1.resultFrac, from2.resultFrac)
to.resultFrac = mathutil.Max(from1.resultFrac, from2.resultFrac)
if from1.negative == from2.negative {
return doAdd(from1, from2, to)
}
Expand All @@ -1613,7 +1614,7 @@ func DecimalAdd(from1, from2, to *MyDecimal) error {
// DecimalSub subs one decimal from another, sets the result to 'to'.
func DecimalSub(from1, from2, to *MyDecimal) error {
from1, from2, to = validateArgs(from1, from2, to)
to.resultFrac = myMaxInt8(from1.resultFrac, from2.resultFrac)
to.resultFrac = mathutil.Max(from1.resultFrac, from2.resultFrac)
if from1.negative == from2.negative {
_, err := doSub(from1, from2, to)
return err
Expand Down Expand Up @@ -1649,7 +1650,7 @@ func doSub(from1, from2, to *MyDecimal) (cmp int, err error) {
wordsFrac1 = digitsToWords(int(from1.digitsFrac))
wordsInt2 = digitsToWords(int(from2.digitsInt))
wordsFrac2 = digitsToWords(int(from2.digitsFrac))
wordsFracTo = myMax(wordsFrac1, wordsFrac2)
wordsFracTo = mathutil.Max(wordsFrac1, wordsFrac2)

start1 = 0
stop1 = wordsInt1
Expand Down Expand Up @@ -1814,8 +1815,8 @@ func doAdd(from1, from2, to *MyDecimal) error {
wordsFrac1 = digitsToWords(int(from1.digitsFrac))
wordsInt2 = digitsToWords(int(from2.digitsInt))
wordsFrac2 = digitsToWords(int(from2.digitsFrac))
wordsIntTo = myMax(wordsInt1, wordsInt2)
wordsFracTo = myMax(wordsFrac1, wordsFrac2)
wordsIntTo = mathutil.Max(wordsInt1, wordsInt2)
wordsFracTo = mathutil.Max(wordsFrac1, wordsFrac2)
)

var x int32
Expand All @@ -1839,7 +1840,7 @@ func doAdd(from1, from2, to *MyDecimal) error {
idxTo := wordsIntTo + wordsFracTo
to.negative = from1.negative
to.digitsInt = int8(wordsIntTo * digitsPerWord)
to.digitsFrac = myMaxInt8(from1.digitsFrac, from2.digitsFrac)
to.digitsFrac = mathutil.Max(from1.digitsFrac, from2.digitsFrac)

if err != nil {
if to.digitsFrac > int8(wordsFracTo*digitsPerWord) {
Expand Down Expand Up @@ -1977,7 +1978,7 @@ func DecimalMul(from1, from2, to *MyDecimal) error {
tmp1 = wordsIntTo
tmp2 = wordsFracTo
)
to.resultFrac = myMinInt8(from1.resultFrac+from2.resultFrac, mysql.MaxDecimalScale)
to.resultFrac = mathutil.Min(from1.resultFrac+from2.resultFrac, mysql.MaxDecimalScale)
wordsIntTo, wordsFracTo, err = fixWordCntError(wordsIntTo, wordsFracTo)
to.negative = from1.negative != from2.negative
to.digitsFrac = from1.digitsFrac + from2.digitsFrac
Expand Down Expand Up @@ -2092,7 +2093,7 @@ func DecimalMul(from1, from2, to *MyDecimal) error {
// fracIncr - increment of fraction
func DecimalDiv(from1, from2, to *MyDecimal, fracIncr int) error {
from1, from2, to = validateArgs(from1, from2, to)
to.resultFrac = myMinInt8(from1.resultFrac+int8(fracIncr), mysql.MaxDecimalScale)
to.resultFrac = mathutil.Min(from1.resultFrac+int8(fracIncr), mysql.MaxDecimalScale)
return doDivMod(from1, from2, to, nil, fracIncr)
}

Expand Down Expand Up @@ -2122,7 +2123,7 @@ DecimalMod does modulus of two decimals.
*/
func DecimalMod(from1, from2, to *MyDecimal) error {
from1, from2, to = validateArgs(from1, from2, to)
to.resultFrac = myMaxInt8(from1.resultFrac, from2.resultFrac)
to.resultFrac = mathutil.Max(from1.resultFrac, from2.resultFrac)
return doDivMod(from1, from2, nil, to, 0)
}

Expand Down Expand Up @@ -2190,7 +2191,7 @@ func doDivMod(from1, from2, to, mod *MyDecimal, fracIncr int) error {
// digitsFrac=max(frac1, frac2), as for subtraction
// digitsInt=from2.digitsInt
to.negative = from1.negative
to.digitsFrac = myMaxInt8(from1.digitsFrac, from2.digitsFrac)
to.digitsFrac = mathutil.Max(from1.digitsFrac, from2.digitsFrac)
} else {
wordsFracTo = digitsToWords(frac1 + frac2 + fracIncr)
wordsIntTo, wordsFracTo, err = fixWordCntError(wordsIntTo, wordsFracTo)
Expand Down Expand Up @@ -2355,7 +2356,7 @@ func doDivMod(from1, from2, to, mod *MyDecimal, fracIncr int) error {
return ErrOverflow
}
stop1 = start1 + wordsIntTo + wordsFracTo
to.digitsInt = int8(myMin(wordsIntTo*digitsPerWord, int(from2.digitsInt)))
to.digitsInt = int8(mathutil.Min(wordsIntTo*digitsPerWord, int(from2.digitsInt)))
}
if wordsIntTo+wordsFracTo > wordBufLen {
stop1 -= wordsIntTo + wordsFracTo - wordBufLen
Expand Down