math.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package resource
  14. import (
  15. "math/big"
  16. inf "gopkg.in/inf.v0"
  17. )
  18. const (
  19. // maxInt64Factors is the highest value that will be checked when removing factors of 10 from an int64.
  20. // It is also the maximum decimal digits that can be represented with an int64.
  21. maxInt64Factors = 18
  22. )
  23. var (
  24. // Commonly needed big.Int values-- treat as read only!
  25. bigTen = big.NewInt(10)
  26. bigZero = big.NewInt(0)
  27. bigOne = big.NewInt(1)
  28. bigThousand = big.NewInt(1000)
  29. big1024 = big.NewInt(1024)
  30. // Commonly needed inf.Dec values-- treat as read only!
  31. decZero = inf.NewDec(0, 0)
  32. decOne = inf.NewDec(1, 0)
  33. decMinusOne = inf.NewDec(-1, 0)
  34. decThousand = inf.NewDec(1000, 0)
  35. dec1024 = inf.NewDec(1024, 0)
  36. decMinus1024 = inf.NewDec(-1024, 0)
  37. // Largest (in magnitude) number allowed.
  38. maxAllowed = infDecAmount{inf.NewDec((1<<63)-1, 0)} // == max int64
  39. // The maximum value we can represent milli-units for.
  40. // Compare with the return value of Quantity.Value() to
  41. // see if it's safe to use Quantity.MilliValue().
  42. MaxMilliValue = int64(((1 << 63) - 1) / 1000)
  43. )
  44. const mostNegative = -(mostPositive + 1)
  45. const mostPositive = 1<<63 - 1
  46. // int64Add returns a+b, or false if that would overflow int64.
  47. func int64Add(a, b int64) (int64, bool) {
  48. c := a + b
  49. switch {
  50. case a > 0 && b > 0:
  51. if c < 0 {
  52. return 0, false
  53. }
  54. case a < 0 && b < 0:
  55. if c > 0 {
  56. return 0, false
  57. }
  58. if a == mostNegative && b == mostNegative {
  59. return 0, false
  60. }
  61. }
  62. return c, true
  63. }
  64. // int64Multiply returns a*b, or false if that would overflow or underflow int64.
  65. func int64Multiply(a, b int64) (int64, bool) {
  66. if a == 0 || b == 0 || a == 1 || b == 1 {
  67. return a * b, true
  68. }
  69. if a == mostNegative || b == mostNegative {
  70. return 0, false
  71. }
  72. c := a * b
  73. return c, c/b == a
  74. }
  75. // int64MultiplyScale returns a*b, assuming b is greater than one, or false if that would overflow or underflow int64.
  76. // Use when b is known to be greater than one.
  77. func int64MultiplyScale(a int64, b int64) (int64, bool) {
  78. if a == 0 || a == 1 {
  79. return a * b, true
  80. }
  81. if a == mostNegative && b != 1 {
  82. return 0, false
  83. }
  84. c := a * b
  85. return c, c/b == a
  86. }
  87. // int64MultiplyScale10 multiplies a by 10, or returns false if that would overflow. This method is faster than
  88. // int64Multiply(a, 10) because the compiler can optimize constant factor multiplication.
  89. func int64MultiplyScale10(a int64) (int64, bool) {
  90. if a == 0 || a == 1 {
  91. return a * 10, true
  92. }
  93. if a == mostNegative {
  94. return 0, false
  95. }
  96. c := a * 10
  97. return c, c/10 == a
  98. }
  99. // int64MultiplyScale100 multiplies a by 100, or returns false if that would overflow. This method is faster than
  100. // int64Multiply(a, 100) because the compiler can optimize constant factor multiplication.
  101. func int64MultiplyScale100(a int64) (int64, bool) {
  102. if a == 0 || a == 1 {
  103. return a * 100, true
  104. }
  105. if a == mostNegative {
  106. return 0, false
  107. }
  108. c := a * 100
  109. return c, c/100 == a
  110. }
  111. // int64MultiplyScale1000 multiplies a by 1000, or returns false if that would overflow. This method is faster than
  112. // int64Multiply(a, 1000) because the compiler can optimize constant factor multiplication.
  113. func int64MultiplyScale1000(a int64) (int64, bool) {
  114. if a == 0 || a == 1 {
  115. return a * 1000, true
  116. }
  117. if a == mostNegative {
  118. return 0, false
  119. }
  120. c := a * 1000
  121. return c, c/1000 == a
  122. }
  123. // positiveScaleInt64 multiplies base by 10^scale, returning false if the
  124. // value overflows. Passing a negative scale is undefined.
  125. func positiveScaleInt64(base int64, scale Scale) (int64, bool) {
  126. switch scale {
  127. case 0:
  128. return base, true
  129. case 1:
  130. return int64MultiplyScale10(base)
  131. case 2:
  132. return int64MultiplyScale100(base)
  133. case 3:
  134. return int64MultiplyScale1000(base)
  135. case 6:
  136. return int64MultiplyScale(base, 1000000)
  137. case 9:
  138. return int64MultiplyScale(base, 1000000000)
  139. default:
  140. value := base
  141. var ok bool
  142. for i := Scale(0); i < scale; i++ {
  143. if value, ok = int64MultiplyScale(value, 10); !ok {
  144. return 0, false
  145. }
  146. }
  147. return value, true
  148. }
  149. }
  150. // negativeScaleInt64 reduces base by the provided scale, rounding up, until the
  151. // value is zero or the scale is reached. Passing a negative scale is undefined.
  152. // The value returned, if not exact, is rounded away from zero.
  153. func negativeScaleInt64(base int64, scale Scale) (result int64, exact bool) {
  154. if scale == 0 {
  155. return base, true
  156. }
  157. value := base
  158. var fraction bool
  159. for i := Scale(0); i < scale; i++ {
  160. if !fraction && value%10 != 0 {
  161. fraction = true
  162. }
  163. value = value / 10
  164. if value == 0 {
  165. if fraction {
  166. if base > 0 {
  167. return 1, false
  168. }
  169. return -1, false
  170. }
  171. return 0, true
  172. }
  173. }
  174. if fraction {
  175. if base > 0 {
  176. value += 1
  177. } else {
  178. value += -1
  179. }
  180. }
  181. return value, !fraction
  182. }
  183. func pow10Int64(b int64) int64 {
  184. switch b {
  185. case 0:
  186. return 1
  187. case 1:
  188. return 10
  189. case 2:
  190. return 100
  191. case 3:
  192. return 1000
  193. case 4:
  194. return 10000
  195. case 5:
  196. return 100000
  197. case 6:
  198. return 1000000
  199. case 7:
  200. return 10000000
  201. case 8:
  202. return 100000000
  203. case 9:
  204. return 1000000000
  205. case 10:
  206. return 10000000000
  207. case 11:
  208. return 100000000000
  209. case 12:
  210. return 1000000000000
  211. case 13:
  212. return 10000000000000
  213. case 14:
  214. return 100000000000000
  215. case 15:
  216. return 1000000000000000
  217. case 16:
  218. return 10000000000000000
  219. case 17:
  220. return 100000000000000000
  221. case 18:
  222. return 1000000000000000000
  223. default:
  224. return 0
  225. }
  226. }
  227. // negativeScaleInt64 returns the result of dividing base by scale * 10 and the remainder, or
  228. // false if no such division is possible. Dividing by negative scales is undefined.
  229. func divideByScaleInt64(base int64, scale Scale) (result, remainder int64, exact bool) {
  230. if scale == 0 {
  231. return base, 0, true
  232. }
  233. // the max scale representable in base 10 in an int64 is 18 decimal places
  234. if scale >= 18 {
  235. return 0, base, false
  236. }
  237. divisor := pow10Int64(int64(scale))
  238. return base / divisor, base % divisor, true
  239. }
  240. // removeInt64Factors divides in a loop; the return values have the property that
  241. // value == result * base ^ scale
  242. func removeInt64Factors(value int64, base int64) (result int64, times int32) {
  243. times = 0
  244. result = value
  245. negative := result < 0
  246. if negative {
  247. result = -result
  248. }
  249. switch base {
  250. // allow the compiler to optimize the common cases
  251. case 10:
  252. for result >= 10 && result%10 == 0 {
  253. times++
  254. result = result / 10
  255. }
  256. // allow the compiler to optimize the common cases
  257. case 1024:
  258. for result >= 1024 && result%1024 == 0 {
  259. times++
  260. result = result / 1024
  261. }
  262. default:
  263. for result >= base && result%base == 0 {
  264. times++
  265. result = result / base
  266. }
  267. }
  268. if negative {
  269. result = -result
  270. }
  271. return result, times
  272. }
  273. // removeBigIntFactors divides in a loop; the return values have the property that
  274. // d == result * factor ^ times
  275. // d may be modified in place.
  276. // If d == 0, then the return values will be (0, 0)
  277. func removeBigIntFactors(d, factor *big.Int) (result *big.Int, times int32) {
  278. q := big.NewInt(0)
  279. m := big.NewInt(0)
  280. for d.Cmp(bigZero) != 0 {
  281. q.DivMod(d, factor, m)
  282. if m.Cmp(bigZero) != 0 {
  283. break
  284. }
  285. times++
  286. d, q = q, d
  287. }
  288. return d, times
  289. }