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

Side by Side Diff: src/double.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_DOUBLE_H_
29 #define V8_DOUBLE_H_
30
31 namespace v8 {
32 namespace internal {
33
34 // We assume that doubles and uint64_t have the same endianness.
35 static uint64_t double_to_uint64(double d) { return bit_cast<uint64_t>(d); }
36 static double uint64_to_double(uint64_t d64) { return bit_cast<double>(d64); }
37
38 // Helper functions for doubles.
39 class Double {
40 public:
41 static const int kSignificandSize = 52; // Excludes the hidden bit.
42
43 Double() : d64_(0.0) {}
44 explicit Double(double d) : d64_(double_to_uint64(d)) {}
45 explicit Double(uint64_t d64) : d64_(d64) {}
46
47 DiyFp AsDiyFp() const {
48 ASSERT(!IsSpecial());
49 return DiyFp(Significand(), Exponent());
50 }
51
52 DiyFp AsNormalizedDiyFp() const {
53 uint64_t f = Significand();
54 int e = Exponent();
55
56 // The current double could be a denormal.
57 while ((f & kHiddenBit) == 0) {
58 f <<= 1;
59 e--;
60 }
61 // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
62 f <<= DiyFp::kSignificandSize - kSignificandSize - 1;
63 e -= DiyFp::kSignificandSize - kSignificandSize - 1;
64 return DiyFp(f, e);
65 }
66
67 int Exponent() const {
68 if (IsDenormal()) return kDenormalExponent;
69
70 uint64_t d64 = AsUint64();
71 int biased_e = (d64 & kExponentMask) >> kSignificandSize;
72 return biased_e - kExponentBias;
73 }
74
75 uint64_t Significand() const {
76 uint64_t d64 = AsUint64();
77 uint64_t significand = d64 & kSignificandMask;
78 if (!IsDenormal()) {
79 return significand + kHiddenBit;
80 } else {
81 return significand;
82 }
83 }
84
85 // Returns true if the double is a denormal.
86 bool IsDenormal() const {
87 uint64_t d64 = AsUint64();
88 return (d64 & kExponentMask) == 0;
89 }
90
91 // We consider denormals not to be special.
92 // Hence only Infinity and NaN are special.
93 bool IsSpecial() const {
94 uint64_t d64 = AsUint64();
95 return (d64 & kExponentMask) == kExponentMask;
96 }
97
98 // Returns the two boundaries of this.
99 // The bigger boundary (m_plus) is normalized. The lower boundary has the same
100 // exponent as m_plus.
101 void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
102 DiyFp v = this->AsDiyFp();
103 bool significand_is_zero = (v.f() == kHiddenBit);
104 DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
105 DiyFp m_minus;
106 if (significand_is_zero && v.e() != kDenormalExponent) {
107 // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
108 // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
109 // at a distance of 1e8.
110 // The only exception is for the smallest normal: the largest denormal is
111 // at the same distance as its successor.
112 // Note: denormals have the same exponent as the smallest normals.
113 m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
114 } else {
115 m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
116 }
117 m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
118 m_minus.set_e(m_plus.e());
119 *out_m_plus = m_plus;
120 *out_m_minus = m_minus;
121 }
122
123 double value() const { return uint64_to_double(d64_); }
124
125 private:
126 static const int kExponentBias = 0x3FF + kSignificandSize;
127 static const int kDenormalExponent = -kExponentBias + 1;
128 static const uint64_t kExponentMask = V8_2PART_UINT64_C(0x7FF00000, 00000000);
129 static const uint64_t kSignificandMask =
130 V8_2PART_UINT64_C(0x000FFFFF,FFFFFFFF);
131 static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000);
132
133 uint64_t d64_;
134
135 // Returns the double's bit as uint64.
136 uint64_t AsUint64() const {
137 return d64_;
138 }
139 };
140
141 } } // namespace v8::internal
142
143 #endif // V8_DOUBLE_H_
OLDNEW
« src/cached_powers.h ('K') | « src/diy_fp.h ('k') | src/globals.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698