• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #ifndef DOUBLE_CONVERSION_DIY_FP_H_
29 #define DOUBLE_CONVERSION_DIY_FP_H_
30 
31 #include "utils.h"
32 
33 namespace WTF {
34 
35 namespace double_conversion {
36 
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 will
39     // have the most significant bit of the significand set.
40     // Multiplication and Subtraction do not normalize their results.
41     // DiyFp are not designed to contain special doubles (NaN and Infinity).
42     class DiyFp {
43     public:
44         static const int kSignificandSize = 64;
45 
DiyFp()46         DiyFp() : f_(0), e_(0) {}
DiyFp(uint64_t f,int e)47         DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
48 
49         // this = this - other.
50         // The exponents of both numbers must be the same and the significand of this
51         // must be bigger than the significand of other.
52         // The result will not be normalized.
Subtract(const DiyFp & other)53         void Subtract(const DiyFp& other) {
54             ASSERT(e_ == other.e_);
55             ASSERT(f_ >= other.f_);
56             f_ -= other.f_;
57         }
58 
59         // Returns a - b.
60         // The exponents of both numbers must be the same and this must be bigger
61         // than other. The result will not be normalized.
Minus(const DiyFp & a,const DiyFp & b)62         static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
63             DiyFp result = a;
64             result.Subtract(b);
65             return result;
66         }
67 
68 
69         // this = this * other.
70         void Multiply(const DiyFp& other);
71 
72         // returns a * b;
Times(const DiyFp & a,const DiyFp & b)73         static DiyFp Times(const DiyFp& a, const DiyFp& b) {
74             DiyFp result = a;
75             result.Multiply(b);
76             return result;
77         }
78 
Normalize()79         void Normalize() {
80             ASSERT(f_ != 0);
81             uint64_t f = f_;
82             int e = e_;
83 
84             // This method is mainly called for normalizing boundaries. In general
85             // boundaries need to be shifted by 10 bits. We thus optimize for this case.
86             const uint64_t k10MSBits = UINT64_2PART_C(0xFFC00000, 00000000);
87             while ((f & k10MSBits) == 0) {
88                 f <<= 10;
89                 e -= 10;
90             }
91             while ((f & kUint64MSB) == 0) {
92                 f <<= 1;
93                 e--;
94             }
95             f_ = f;
96             e_ = e;
97         }
98 
Normalize(const DiyFp & a)99         static DiyFp Normalize(const DiyFp& a) {
100             DiyFp result = a;
101             result.Normalize();
102             return result;
103         }
104 
f()105         uint64_t f() const { return f_; }
e()106         int e() const { return e_; }
107 
set_f(uint64_t new_value)108         void set_f(uint64_t new_value) { f_ = new_value; }
set_e(int new_value)109         void set_e(int new_value) { e_ = new_value; }
110 
111     private:
112         static const uint64_t kUint64MSB = UINT64_2PART_C(0x80000000, 00000000);
113 
114         uint64_t f_;
115         int e_;
116     };
117 
118 }  // namespace double_conversion
119 
120 } // namespace WTF
121 
122 #endif  // DOUBLE_CONVERSION_DIY_FP_H_
123