Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: src/diy_fp.h

Issue 973001: Version 2.1.4.1... (Closed) Base URL: http://v8.googlecode.com/svn/trunk/
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/conversions.cc ('k') | src/double.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 V8_DIY_FP_H_
29 #define V8_DIY_FP_H_
30
31 namespace v8 {
32 namespace internal {
33
34 // This "Do It Yourself Floating Point" class implements a floating-point number
35 // with a uint64 significand and an int exponent. Normalized DiyFp numbers will
36 // have the most significant bit of the significand set.
37 // Multiplication and Subtraction do not normalize their results.
38 // DiyFp are not designed to contain special doubles (NaN and Infinity).
39 class DiyFp {
40 public:
41 static const int kSignificandSize = 64;
42
43 DiyFp() : f_(0), e_(0) {}
44 DiyFp(uint64_t f, int e) : f_(f), e_(e) {}
45
46 // this = this - other.
47 // The exponents of both numbers must be the same and the significand of this
48 // must be bigger than the significand of other.
49 // The result will not be normalized.
50 void Subtract(const DiyFp& other) {
51 ASSERT(e_ == other.e_);
52 ASSERT(f_ >= other.f_);
53 f_ -= other.f_;
54 }
55
56 // Returns a - b.
57 // The exponents of both numbers must be the same and this must be bigger
58 // than other. The result will not be normalized.
59 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
60 DiyFp result = a;
61 result.Subtract(b);
62 return result;
63 }
64
65
66 // this = this * other.
67 void Multiply(const DiyFp& other) {
68 // Simply "emulates" a 128 bit multiplication.
69 // However: the resulting number only contains 64 bits. The least
70 // significant 64 bits are only used for rounding the most significant 64
71 // bits.
72 const uint64_t kM32 = 0xFFFFFFFFu;
73 uint64_t a = f_ >> 32;
74 uint64_t b = f_ & kM32;
75 uint64_t c = other.f_ >> 32;
76 uint64_t d = other.f_ & kM32;
77 uint64_t ac = a * c;
78 uint64_t bc = b * c;
79 uint64_t ad = a * d;
80 uint64_t bd = b * d;
81 uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
82 tmp += 1U << 31; // round
83 uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
84 e_ += other.e_ + 64;
85 f_ = result_f;
86 }
87
88 // returns a * b;
89 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
90 DiyFp result = a;
91 result.Multiply(b);
92 return result;
93 }
94
95 void Normalize() {
96 ASSERT(f_ != 0);
97 uint64_t f = f_;
98 int e = e_;
99
100 // This method is mainly called for normalizing boundaries. In general
101 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
102 const uint64_t k10MSBits = V8_2PART_UINT64_C(0xFFC00000, 00000000);
103 while ((f & k10MSBits) == 0) {
104 f <<= 10;
105 e -= 10;
106 }
107 while ((f & kUint64MSB) == 0) {
108 f <<= 1;
109 e--;
110 }
111 f_ = f;
112 e_ = e;
113 }
114
115 static DiyFp Normalize(const DiyFp& a) {
116 DiyFp result = a;
117 result.Normalize();
118 return result;
119 }
120
121 uint64_t f() const { return f_; }
122 int e() const { return e_; }
123
124 void set_f(uint64_t new_value) { f_ = new_value; }
125 void set_e(int new_value) { e_ = new_value; }
126
127 private:
128 static const uint64_t kUint64MSB = V8_2PART_UINT64_C(0x80000000, 00000000);
129
130 uint64_t f_;
131 int e_;
132 };
133
134 } } // namespace v8::internal
135
136 #endif // V8_DIY_FP_H_
OLDNEW
« no previous file with comments | « src/conversions.cc ('k') | src/double.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698