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

Side by Side Diff: src/diy_fp.h

Issue 619005: Fast algorithm for double->string conversion. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 10 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
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 64bit significand/int
35 // exponent Floating Point number.
36 // Only the minus and multiply operations are implemented. Both operations have
37 // severe limitations. But we don't need more.
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 this must be bigger
48 // than other. The result will not be normalized.
49 void Subtract(const DiyFp& other) {
50 ASSERT(e_ == other.e_);
51 ASSERT(f_ >= other.f_);
52 f_ -= other.f_;
53 }
54
55 // returns a - b.
56 // The exponents of both numbers must be the same and this must be bigger
57 // than other. The result will not be normalized.
58 static DiyFp Minus(const DiyFp& a, const DiyFp& b) {
59 DiyFp result = a;
60 result.Subtract(b);
61 return result;
62 }
63
64
65 // this = this * other.
66 void Multiply(const DiyFp& other) {
67 // Simply "emulates" a 128 bit multiplication.
68 // However: the resulting number only contains 64 bits. The least
69 // significant 64 bits are only used for rounding the most significant 64
70 // bits.
71 const uint64_t kM32 = 0xFFFFFFFFu;
72 uint64_t a = f_ >> 32;
73 uint64_t b = f_ & kM32;
74 uint64_t c = other.f_ >> 32;
75 uint64_t d = other.f_ & kM32;
76 uint64_t ac = a * c;
77 uint64_t bc = b * c;
78 uint64_t ad = a * d;
79 uint64_t bd = b * d;
80 uint64_t tmp = (bd >> 32) + (ad & kM32) + (bc & kM32);
81 tmp += 1U << 31; // round
82 uint64_t result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32);
83 e_ += other.e_ + 64;
84 f_ = result_f;
85 }
86
87 // returns a * b;
88 static DiyFp Times(const DiyFp& a, const DiyFp& b) {
89 DiyFp result = a;
90 result.Multiply(b);
91 return result;
92 }
93
94 void Normalize() {
95 ASSERT(f_ != 0);
96 uint64_t f = f_;
97 int e = e_;
98
99 // This method is mainly called for normalizing boundaries. In general
100 // boundaries need to be shifted by 10 bits. We thus optimize for this case.
101 const uint64_t k10MSBits = V8_2PART_UINT64_C(0xFFC00000, 00000000);
102 while ((f & k10MSBits) == 0) {
103 f <<= 10;
104 e -= 10;
105 }
106 while ((f & kUint64MSB) == 0) {
107 f <<= 1;
108 e--;
109 }
110 f_ = f;
111 e_ = e;
112 }
113
114 static DiyFp Normalize(const DiyFp& a) {
115 DiyFp result = a;
116 result.Normalize();
117 return result;
118 }
119
120 uint64_t f() const { return f_; }
121 int e() const { return e_; }
122
123 void set_f(uint64_t new_value) { f_ = new_value; }
124 void set_e(int new_value) { e_ = new_value; }
125
126 private:
127 static const uint64_t kUint64MSB = V8_2PART_UINT64_C(0x80000000, 00000000);
128
129 uint64_t f_;
130 int e_;
131 };
132
133 } } // namespace v8::internal
134
135 #endif // V8_DIY_FP_H_
OLDNEW
« src/cached_powers.h ('K') | « src/conversions.cc ('k') | src/double.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698