scale_int.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2015 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"
  16. "math/big"
  17. "sync"
  18. )
  19. var (
  20. // A sync pool to reduce allocation.
  21. intPool sync.Pool
  22. maxInt64 = big.NewInt(math.MaxInt64)
  23. )
  24. func init() {
  25. intPool.New = func() interface{} {
  26. return &big.Int{}
  27. }
  28. }
  29. // scaledValue scales given unscaled value from scale to new Scale and returns
  30. // an int64. It ALWAYS rounds up the result when scale down. The final result might
  31. // overflow.
  32. //
  33. // scale, newScale represents the scale of the unscaled decimal.
  34. // The mathematical value of the decimal is unscaled * 10**(-scale).
  35. func scaledValue(unscaled *big.Int, scale, newScale int) int64 {
  36. dif := scale - newScale
  37. if dif == 0 {
  38. return unscaled.Int64()
  39. }
  40. // Handle scale up
  41. // This is an easy case, we do not need to care about rounding and overflow.
  42. // If any intermediate operation causes overflow, the result will overflow.
  43. if dif < 0 {
  44. return unscaled.Int64() * int64(math.Pow10(-dif))
  45. }
  46. // Handle scale down
  47. // We have to be careful about the intermediate operations.
  48. // fast path when unscaled < max.Int64 and exp(10,dif) < max.Int64
  49. const log10MaxInt64 = 19
  50. if unscaled.Cmp(maxInt64) < 0 && dif < log10MaxInt64 {
  51. divide := int64(math.Pow10(dif))
  52. result := unscaled.Int64() / divide
  53. mod := unscaled.Int64() % divide
  54. if mod != 0 {
  55. return result + 1
  56. }
  57. return result
  58. }
  59. // We should only convert back to int64 when getting the result.
  60. divisor := intPool.Get().(*big.Int)
  61. exp := intPool.Get().(*big.Int)
  62. result := intPool.Get().(*big.Int)
  63. defer func() {
  64. intPool.Put(divisor)
  65. intPool.Put(exp)
  66. intPool.Put(result)
  67. }()
  68. // divisor = 10^(dif)
  69. // TODO: create loop up table if exp costs too much.
  70. divisor.Exp(bigTen, exp.SetInt64(int64(dif)), nil)
  71. // reuse exp
  72. remainder := exp
  73. // result = unscaled / divisor
  74. // remainder = unscaled % divisor
  75. result.DivMod(unscaled, divisor, remainder)
  76. if remainder.Sign() != 0 {
  77. return result.Int64() + 1
  78. }
  79. return result.Int64()
  80. }