| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 #ifndef DOUBLE_CONVERSION_DIY_FP_H_ | 28 #ifndef DOUBLE_CONVERSION_DIY_FP_H_ |
| 29 #define DOUBLE_CONVERSION_DIY_FP_H_ | 29 #define DOUBLE_CONVERSION_DIY_FP_H_ |
| 30 | 30 |
| 31 #include "utils.h" | 31 #include "utils.h" |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 namespace double_conversion { | 35 namespace double_conversion { |
| 36 | 36 |
| 37 // This "Do It Yourself Floating Point" class implements a floating-point nu
mber | 37 // This "Do It Yourself Floating Point" class implements a floating-point number |
| 38 // with a uint64 significand and an int exponent. Normalized DiyFp numbers w
ill | 38 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will |
| 39 // have the most significant bit of the significand set. | 39 // have the most significant bit of the significand set. |
| 40 // Multiplication and Subtraction do not normalize their results. | 40 // Multiplication and Subtraction do not normalize their results. |
| 41 // DiyFp are not designed to contain special doubles (NaN and Infinity). | 41 // DiyFp are not designed to contain special doubles (NaN and Infinity). |
| 42 class DiyFp { | 42 class DiyFp { |
| 43 public: | 43 public: |
| 44 static const int kSignificandSize = 64; | 44 static const int kSignificandSize = 64; |
| 45 | 45 |
| 46 DiyFp() : f_(0), e_(0) {} | 46 DiyFp() : f_(0), e_(0) {} |
| 47 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} | 47 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} |
| 48 | 48 |
| 49 // this = this - other. | 49 // this = this - other. |
| 50 // The exponents of both numbers must be the same and the significand of
this | 50 // The exponents of both numbers must be the same and the significand of this |
| 51 // must be bigger than the significand of other. | 51 // must be bigger than the significand of other. |
| 52 // The result will not be normalized. | 52 // The result will not be normalized. |
| 53 void Subtract(const DiyFp& other) { | 53 void Subtract(const DiyFp& other) { |
| 54 ASSERT(e_ == other.e_); | 54 ASSERT(e_ == other.e_); |
| 55 ASSERT(f_ >= other.f_); | 55 ASSERT(f_ >= other.f_); |
| 56 f_ -= other.f_; | 56 f_ -= other.f_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 // Returns a - b. | 59 // Returns a - b. |
| 60 // The exponents of both numbers must be the same and this must be bigge
r | 60 // The exponents of both numbers must be the same and this must be bigger |
| 61 // than other. The result will not be normalized. | 61 // than other. The result will not be normalized. |
| 62 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { | 62 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
| 63 DiyFp result = a; | 63 DiyFp result = a; |
| 64 result.Subtract(b); | 64 result.Subtract(b); |
| 65 return result; | 65 return result; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // this = this * other. |
| 69 void Multiply(const DiyFp& other); |
| 68 | 70 |
| 69 // this = this * other. | 71 // returns a * b; |
| 70 void Multiply(const DiyFp& other); | 72 static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
| 73 DiyFp result = a; |
| 74 result.Multiply(b); |
| 75 return result; |
| 76 } |
| 71 | 77 |
| 72 // returns a * b; | 78 void Normalize() { |
| 73 static DiyFp Times(const DiyFp& a, const DiyFp& b) { | 79 ASSERT(f_ != 0); |
| 74 DiyFp result = a; | 80 uint64_t f = f_; |
| 75 result.Multiply(b); | 81 int e = e_; |
| 76 return result; | |
| 77 } | |
| 78 | 82 |
| 79 void Normalize() { | 83 // This method is mainly called for normalizing boundaries. In general |
| 80 ASSERT(f_ != 0); | 84 // boundaries need to be shifted by 10 bits. We thus optimize for this case. |
| 81 uint64_t f = f_; | 85 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); |
| 82 int e = e_; | 86 while ((f & k10MSBits) == 0) { |
| 87 f <<= 10; |
| 88 e -= 10; |
| 89 } |
| 90 while ((f & kUint64MSB) == 0) { |
| 91 f <<= 1; |
| 92 e--; |
| 93 } |
| 94 f_ = f; |
| 95 e_ = e; |
| 96 } |
| 83 | 97 |
| 84 // This method is mainly called for normalizing boundaries. In gener
al | 98 static DiyFp Normalize(const DiyFp& a) { |
| 85 // boundaries need to be shifted by 10 bits. We thus optimize for th
is case. | 99 DiyFp result = a; |
| 86 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); | 100 result.Normalize(); |
| 87 while ((f & k10MSBits) == 0) { | 101 return result; |
| 88 f <<= 10; | 102 } |
| 89 e -= 10; | |
| 90 } | |
| 91 while ((f & kUint64MSB) == 0) { | |
| 92 f <<= 1; | |
| 93 e--; | |
| 94 } | |
| 95 f_ = f; | |
| 96 e_ = e; | |
| 97 } | |
| 98 | 103 |
| 99 static DiyFp Normalize(const DiyFp& a) { | 104 uint64_t f() const { return f_; } |
| 100 DiyFp result = a; | 105 int e() const { return e_; } |
| 101 result.Normalize(); | |
| 102 return result; | |
| 103 } | |
| 104 | 106 |
| 105 uint64_t f() const { return f_; } | 107 void set_f(uint64_t new_value) { f_ = new_value; } |
| 106 int e() const { return e_; } | 108 void set_e(int new_value) { e_ = new_value; } |
| 107 | 109 |
| 108 void set_f(uint64_t new_value) { f_ = new_value; } | 110 private: |
| 109 void set_e(int new_value) { e_ = new_value; } | 111 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); |
| 110 | 112 |
| 111 private: | 113 uint64_t f_; |
| 112 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); | 114 int e_; |
| 113 | 115 }; |
| 114 uint64_t f_; | |
| 115 int e_; | |
| 116 }; | |
| 117 | 116 |
| 118 } // namespace double_conversion | 117 } // namespace double_conversion |
| 119 | 118 |
| 120 } // namespace WTF | 119 } // namespace WTF |
| 121 | 120 |
| 122 #endif // DOUBLE_CONVERSION_DIY_FP_H_ | 121 #endif // DOUBLE_CONVERSION_DIY_FP_H_ |
| OLD | NEW |