| 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() |
| 47 DiyFp(uint64_t f, int e) : f_(f), e_(e) {} | 47 : f_(0), e_(0) {} |
| 48 DiyFp(uint64_t f, int e) |
| 49 : f_(f), e_(e) {} |
| 48 | 50 |
| 49 // this = this - other. | 51 // this = this - other. |
| 50 // The exponents of both numbers must be the same and the significand of
this | 52 // The exponents of both numbers must be the same and the significand of this |
| 51 // must be bigger than the significand of other. | 53 // must be bigger than the significand of other. |
| 52 // The result will not be normalized. | 54 // The result will not be normalized. |
| 53 void Subtract(const DiyFp& other) { | 55 void Subtract(const DiyFp& other) { |
| 54 ASSERT(e_ == other.e_); | 56 ASSERT(e_ == other.e_); |
| 55 ASSERT(f_ >= other.f_); | 57 ASSERT(f_ >= other.f_); |
| 56 f_ -= other.f_; | 58 f_ -= other.f_; |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Returns a - b. | 61 // Returns a - b. |
| 60 // The exponents of both numbers must be the same and this must be bigge
r | 62 // The exponents of both numbers must be the same and this must be bigger |
| 61 // than other. The result will not be normalized. | 63 // than other. The result will not be normalized. |
| 62 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { | 64 static DiyFp Minus(const DiyFp& a, const DiyFp& b) { |
| 63 DiyFp result = a; | 65 DiyFp result = a; |
| 64 result.Subtract(b); | 66 result.Subtract(b); |
| 65 return result; | 67 return result; |
| 66 } | 68 } |
| 67 | 69 |
| 70 // this = this * other. |
| 71 void Multiply(const DiyFp& other); |
| 68 | 72 |
| 69 // this = this * other. | 73 // returns a * b; |
| 70 void Multiply(const DiyFp& other); | 74 static DiyFp Times(const DiyFp& a, const DiyFp& b) { |
| 75 DiyFp result = a; |
| 76 result.Multiply(b); |
| 77 return result; |
| 78 } |
| 71 | 79 |
| 72 // returns a * b; | 80 void Normalize() { |
| 73 static DiyFp Times(const DiyFp& a, const DiyFp& b) { | 81 ASSERT(f_ != 0); |
| 74 DiyFp result = a; | 82 uint64_t f = f_; |
| 75 result.Multiply(b); | 83 int e = e_; |
| 76 return result; | |
| 77 } | |
| 78 | 84 |
| 79 void Normalize() { | 85 // This method is mainly called for normalizing boundaries. In general |
| 80 ASSERT(f_ != 0); | 86 // boundaries need to be shifted by 10 bits. We thus optimize for this case. |
| 81 uint64_t f = f_; | 87 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); |
| 82 int e = e_; | 88 while ((f & k10MSBits) == 0) { |
| 89 f <<= 10; |
| 90 e -= 10; |
| 91 } |
| 92 while ((f & kUint64MSB) == 0) { |
| 93 f <<= 1; |
| 94 e--; |
| 95 } |
| 96 f_ = f; |
| 97 e_ = e; |
| 98 } |
| 83 | 99 |
| 84 // This method is mainly called for normalizing boundaries. In gener
al | 100 static DiyFp Normalize(const DiyFp& a) { |
| 85 // boundaries need to be shifted by 10 bits. We thus optimize for th
is case. | 101 DiyFp result = a; |
| 86 const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000); | 102 result.Normalize(); |
| 87 while ((f & k10MSBits) == 0) { | 103 return result; |
| 88 f <<= 10; | 104 } |
| 89 e -= 10; | |
| 90 } | |
| 91 while ((f & kUint64MSB) == 0) { | |
| 92 f <<= 1; | |
| 93 e--; | |
| 94 } | |
| 95 f_ = f; | |
| 96 e_ = e; | |
| 97 } | |
| 98 | 105 |
| 99 static DiyFp Normalize(const DiyFp& a) { | 106 uint64_t f() const { return f_; } |
| 100 DiyFp result = a; | 107 int e() const { return e_; } |
| 101 result.Normalize(); | |
| 102 return result; | |
| 103 } | |
| 104 | 108 |
| 105 uint64_t f() const { return f_; } | 109 void set_f(uint64_t new_value) { f_ = new_value; } |
| 106 int e() const { return e_; } | 110 void set_e(int new_value) { e_ = new_value; } |
| 107 | 111 |
| 108 void set_f(uint64_t new_value) { f_ = new_value; } | 112 private: |
| 109 void set_e(int new_value) { e_ = new_value; } | 113 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); |
| 110 | 114 |
| 111 private: | 115 uint64_t f_; |
| 112 static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000); | 116 int e_; |
| 113 | 117 }; |
| 114 uint64_t f_; | |
| 115 int e_; | |
| 116 }; | |
| 117 | 118 |
| 118 } // namespace double_conversion | 119 } // namespace double_conversion |
| 119 | 120 |
| 120 } // namespace WTF | 121 } // namespace WTF |
| 121 | 122 |
| 122 #endif // DOUBLE_CONVERSION_DIY_FP_H_ | 123 #endif // DOUBLE_CONVERSION_DIY_FP_H_ |
| OLD | NEW |