github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/utils/math.go (about) 1 // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. 2 3 package utils 4 5 import ( 6 "github.com/pingcap/log" 7 "go.uber.org/zap" 8 ) 9 10 // MinInt choice smallest integer from its arguments. 11 func MinInt(x int, xs ...int) int { 12 min := x 13 for _, n := range xs { 14 if n < min { 15 min = n 16 } 17 } 18 return min 19 } 20 21 // MaxInt choice biggest integer from its arguments. 22 func MaxInt(x int, xs ...int) int { 23 max := x 24 for _, n := range xs { 25 if n > max { 26 max = n 27 } 28 } 29 return max 30 } 31 32 // ClampInt restrict a value to a certain interval. 33 func ClampInt(n, min, max int) int { 34 if min > max { 35 log.Error("clamping integer with min > max", zap.Int("min", min), zap.Int("max", max)) 36 } 37 38 return MinInt(max, MaxInt(min, n)) 39 } 40 41 // MinInt64 choice smallest integer from its arguments. 42 func MinInt64(x int64, xs ...int64) int64 { 43 min := x 44 for _, n := range xs { 45 if n < min { 46 min = n 47 } 48 } 49 return min 50 } 51 52 // NextPowerOfTwo returns the smallest power of two greater than or equal to `i` 53 // Caller should guarantee that i > 0 and the return value is not overflow. 54 func NextPowerOfTwo(i int64) int64 { 55 if i&(i-1) == 0 { 56 return i 57 } 58 i *= 2 59 for i&(i-1) != 0 { 60 i &= i - 1 61 } 62 return i 63 }