double-conversion-diy-fp.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. //
  4. // From the double-conversion library. Original license:
  5. //
  6. // Copyright 2010 the V8 project authors. All rights reserved.
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_DIY_FP_H_
  36. #define DOUBLE_CONVERSION_DIY_FP_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-utils.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. // This "Do It Yourself Floating Point" class implements a floating-point number
  43. // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
  44. // have the most significant bit of the significand set.
  45. // Multiplication and Subtraction do not normalize their results.
  46. // DiyFp store only non-negative numbers and are not designed to contain special
  47. // doubles (NaN and Infinity).
  48. class DiyFp {
  49. public:
  50. static const int kSignificandSize = 64;
  51. DiyFp() : f_(0), e_(0) {}
  52. DiyFp(const uint64_t significand, const int32_t exponent) : f_(significand), e_(exponent) {}
  53. // this -= other.
  54. // The exponents of both numbers must be the same and the significand of this
  55. // must be greater or equal than the significand of other.
  56. // The result will not be normalized.
  57. void Subtract(const DiyFp& other) {
  58. DOUBLE_CONVERSION_ASSERT(e_ == other.e_);
  59. DOUBLE_CONVERSION_ASSERT(f_ >= other.f_);
  60. f_ -= other.f_;
  61. }
  62. // Returns a - b.
  63. // The exponents of both numbers must be the same and a must be greater
  64. // or equal than b. The result will not be normalized.
  65. static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
  66. DiyFp result = a;
  67. result.Subtract(b);
  68. return result;
  69. }
  70. // this *= other.
  71. void Multiply(const DiyFp& other) {
  72. // Simply "emulates" a 128 bit multiplication.
  73. // However: the resulting number only contains 64 bits. The least
  74. // significant 64 bits are only used for rounding the most significant 64
  75. // bits.
  76. const uint64_t kM32 = 0xFFFFFFFFU;
  77. const uint64_t a = f_ >> 32;
  78. const uint64_t b = f_ & kM32;
  79. const uint64_t c = other.f_ >> 32;
  80. const uint64_t d = other.f_ & kM32;
  81. const uint64_t ac = a * c;
  82. const uint64_t bc = b * c;
  83. const uint64_t ad = a * d;
  84. const uint64_t bd = b * d;
  85. // By adding 1U << 31 to tmp we round the final result.
  86. // Halfway cases will be rounded up.
  87. const uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32) + (1U << 31);
  88. e_ += other.e_ + 64;
  89. f_ = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
  90. }
  91. // returns a * b;
  92. static DiyFp Times(const DiyFp& a, const DiyFp& b) {
  93. DiyFp result = a;
  94. result.Multiply(b);
  95. return result;
  96. }
  97. void Normalize() {
  98. DOUBLE_CONVERSION_ASSERT(f_ != 0);
  99. uint64_t significand = f_;
  100. int32_t exponent = e_;
  101. // This method is mainly called for normalizing boundaries. In general,
  102. // boundaries need to be shifted by 10 bits, and we optimize for this case.
  103. const uint64_t k10MSBits = DOUBLE_CONVERSION_UINT64_2PART_C(0xFFC00000, 00000000);
  104. while ((significand & k10MSBits) == 0) {
  105. significand <<= 10;
  106. exponent -= 10;
  107. }
  108. while ((significand & kUint64MSB) == 0) {
  109. significand <<= 1;
  110. exponent--;
  111. }
  112. f_ = significand;
  113. e_ = exponent;
  114. }
  115. static DiyFp Normalize(const DiyFp& a) {
  116. DiyFp result = a;
  117. result.Normalize();
  118. return result;
  119. }
  120. uint64_t f() const { return f_; }
  121. int32_t e() const { return e_; }
  122. void set_f(uint64_t new_value) { f_ = new_value; }
  123. void set_e(int32_t new_value) { e_ = new_value; }
  124. private:
  125. static const uint64_t kUint64MSB = DOUBLE_CONVERSION_UINT64_2PART_C(0x80000000, 00000000);
  126. uint64_t f_;
  127. int32_t e_;
  128. };
  129. } // namespace double_conversion
  130. // ICU PATCH: Close ICU namespace
  131. U_NAMESPACE_END
  132. #endif // DOUBLE_CONVERSION_DIY_FP_H_
  133. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING