diy-fp.h 4.9 KB

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