math.go 7.3 KB

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