2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
26 // maxInt64Factors is the highest value that will be checked when removing factors of 10 from an int64.
27 // It is also the maximum decimal digits that can be represented with an int64.
32 // Commonly needed big.Int values-- treat as read only!
33 bigTen = big.NewInt(10)
34 bigZero = big.NewInt(0)
35 bigOne = big.NewInt(1)
36 bigThousand = big.NewInt(1000)
37 big1024 = big.NewInt(1024)
39 // Commonly needed inf.Dec values-- treat as read only!
40 decZero = inf.NewDec(0, 0)
41 decOne = inf.NewDec(1, 0)
42 decMinusOne = inf.NewDec(-1, 0)
43 decThousand = inf.NewDec(1000, 0)
44 dec1024 = inf.NewDec(1024, 0)
45 decMinus1024 = inf.NewDec(-1024, 0)
47 // Largest (in magnitude) number allowed.
48 maxAllowed = infDecAmount{inf.NewDec((1<<63)-1, 0)} // == max int64
50 // The maximum value we can represent milli-units for.
51 // Compare with the return value of Quantity.Value() to
52 // see if it's safe to use Quantity.MilliValue().
53 MaxMilliValue = int64(((1 << 63) - 1) / 1000)
56 const mostNegative = -(mostPositive + 1)
57 const mostPositive = 1<<63 - 1
59 // int64Add returns a+b, or false if that would overflow int64.
60 func int64Add(a, b int64) (int64, bool) {
71 if a == mostNegative && b == mostNegative {
78 // int64Multiply returns a*b, or false if that would overflow or underflow int64.
79 func int64Multiply(a, b int64) (int64, bool) {
80 if a == 0 || b == 0 || a == 1 || b == 1 {
83 if a == mostNegative || b == mostNegative {
90 // int64MultiplyScale returns a*b, assuming b is greater than one, or false if that would overflow or underflow int64.
91 // Use when b is known to be greater than one.
92 func int64MultiplyScale(a int64, b int64) (int64, bool) {
96 if a == mostNegative && b != 1 {
103 // int64MultiplyScale10 multiplies a by 10, or returns false if that would overflow. This method is faster than
104 // int64Multiply(a, 10) because the compiler can optimize constant factor multiplication.
105 func int64MultiplyScale10(a int64) (int64, bool) {
106 if a == 0 || a == 1 {
109 if a == mostNegative {
116 // int64MultiplyScale100 multiplies a by 100, or returns false if that would overflow. This method is faster than
117 // int64Multiply(a, 100) because the compiler can optimize constant factor multiplication.
118 func int64MultiplyScale100(a int64) (int64, bool) {
119 if a == 0 || a == 1 {
122 if a == mostNegative {
129 // int64MultiplyScale1000 multiplies a by 1000, or returns false if that would overflow. This method is faster than
130 // int64Multiply(a, 1000) because the compiler can optimize constant factor multiplication.
131 func int64MultiplyScale1000(a int64) (int64, bool) {
132 if a == 0 || a == 1 {
133 return a * 1000, true
135 if a == mostNegative {
139 return c, c/1000 == a
142 // positiveScaleInt64 multiplies base by 10^scale, returning false if the
143 // value overflows. Passing a negative scale is undefined.
144 func positiveScaleInt64(base int64, scale Scale) (int64, bool) {
149 return int64MultiplyScale10(base)
151 return int64MultiplyScale100(base)
153 return int64MultiplyScale1000(base)
155 return int64MultiplyScale(base, 1000000)
157 return int64MultiplyScale(base, 1000000000)
161 for i := Scale(0); i < scale; i++ {
162 if value, ok = int64MultiplyScale(value, 10); !ok {
170 // negativeScaleInt64 reduces base by the provided scale, rounding up, until the
171 // value is zero or the scale is reached. Passing a negative scale is undefined.
172 // The value returned, if not exact, is rounded away from zero.
173 func negativeScaleInt64(base int64, scale Scale) (result int64, exact bool) {
180 for i := Scale(0); i < scale; i++ {
181 if !fraction && value%10 != 0 {
202 return value, !fraction
205 func pow10Int64(b int64) int64 {
234 return 10000000000000
236 return 100000000000000
238 return 1000000000000000
240 return 10000000000000000
242 return 100000000000000000
244 return 1000000000000000000
250 // negativeScaleInt64 returns the result of dividing base by scale * 10 and the remainder, or
251 // false if no such division is possible. Dividing by negative scales is undefined.
252 func divideByScaleInt64(base int64, scale Scale) (result, remainder int64, exact bool) {
256 // the max scale representable in base 10 in an int64 is 18 decimal places
258 return 0, base, false
260 divisor := pow10Int64(int64(scale))
261 return base / divisor, base % divisor, true
264 // removeInt64Factors divides in a loop; the return values have the property that
265 // value == result * base ^ scale
266 func removeInt64Factors(value int64, base int64) (result int64, times int32) {
269 negative := result < 0
274 // allow the compiler to optimize the common cases
276 for result >= 10 && result%10 == 0 {
280 // allow the compiler to optimize the common cases
282 for result >= 1024 && result%1024 == 0 {
284 result = result / 1024
287 for result >= base && result%base == 0 {
289 result = result / base
298 // removeBigIntFactors divides in a loop; the return values have the property that
299 // d == result * factor ^ times
300 // d may be modified in place.
301 // If d == 0, then the return values will be (0, 0)
302 func removeBigIntFactors(d, factor *big.Int) (result *big.Int, times int32) {
305 for d.Cmp(bigZero) != 0 {
306 q.DivMod(d, factor, m)
307 if m.Cmp(bigZero) != 0 {