iv-phonic 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,117 @@
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
+
69
+ // returns a * b;
70
+ static DiyFp Times(const DiyFp& a, const DiyFp& b) {
71
+ DiyFp result = a;
72
+ result.Multiply(b);
73
+ return result;
74
+ }
75
+
76
+ void Normalize() {
77
+ ASSERT(f_ != 0);
78
+ uint64_t f = f_;
79
+ int e = e_;
80
+
81
+ // This method is mainly called for normalizing boundaries. In general
82
+ // boundaries need to be shifted by 10 bits. We thus optimize for this case.
83
+ const uint64_t k10MSBits = V8_2PART_UINT64_C(0xFFC00000, 00000000);
84
+ while ((f & k10MSBits) == 0) {
85
+ f <<= 10;
86
+ e -= 10;
87
+ }
88
+ while ((f & kUint64MSB) == 0) {
89
+ f <<= 1;
90
+ e--;
91
+ }
92
+ f_ = f;
93
+ e_ = e;
94
+ }
95
+
96
+ static DiyFp Normalize(const DiyFp& a) {
97
+ DiyFp result = a;
98
+ result.Normalize();
99
+ return result;
100
+ }
101
+
102
+ uint64_t f() const { return f_; }
103
+ int e() const { return e_; }
104
+
105
+ void set_f(uint64_t new_value) { f_ = new_value; }
106
+ void set_e(int new_value) { e_ = new_value; }
107
+
108
+ private:
109
+ static const uint64_t kUint64MSB = V8_2PART_UINT64_C(0x80000000, 00000000);
110
+
111
+ uint64_t f_;
112
+ int e_;
113
+ };
114
+
115
+ } } // namespace v8::internal
116
+
117
+ #endif // V8_DIY_FP_H_
@@ -0,0 +1,169 @@
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
+ #include "diy-fp.h"
32
+
33
+ namespace v8 {
34
+ namespace internal {
35
+
36
+ // We assume that doubles and uint64_t have the same endianness.
37
+ static uint64_t double_to_uint64(double d) { return BitCast<uint64_t>(d); }
38
+ static double uint64_to_double(uint64_t d64) { return BitCast<double>(d64); }
39
+
40
+ // Helper functions for doubles.
41
+ class Double {
42
+ public:
43
+ static const uint64_t kSignMask = V8_2PART_UINT64_C(0x80000000, 00000000);
44
+ static const uint64_t kExponentMask = V8_2PART_UINT64_C(0x7FF00000, 00000000);
45
+ static const uint64_t kSignificandMask =
46
+ V8_2PART_UINT64_C(0x000FFFFF, FFFFFFFF);
47
+ static const uint64_t kHiddenBit = V8_2PART_UINT64_C(0x00100000, 00000000);
48
+
49
+ Double() : d64_(0) {}
50
+ explicit Double(double d) : d64_(double_to_uint64(d)) {}
51
+ explicit Double(uint64_t d64) : d64_(d64) {}
52
+
53
+ DiyFp AsDiyFp() const {
54
+ ASSERT(!IsSpecial());
55
+ return DiyFp(Significand(), Exponent());
56
+ }
57
+
58
+ // this->Significand() must not be 0.
59
+ DiyFp AsNormalizedDiyFp() const {
60
+ uint64_t f = Significand();
61
+ int e = Exponent();
62
+
63
+ ASSERT(f != 0);
64
+
65
+ // The current double could be a denormal.
66
+ while ((f & kHiddenBit) == 0) {
67
+ f <<= 1;
68
+ e--;
69
+ }
70
+ // Do the final shifts in one go. Don't forget the hidden bit (the '-1').
71
+ f <<= DiyFp::kSignificandSize - kSignificandSize - 1;
72
+ e -= DiyFp::kSignificandSize - kSignificandSize - 1;
73
+ return DiyFp(f, e);
74
+ }
75
+
76
+ // Returns the double's bit as uint64.
77
+ uint64_t AsUint64() const {
78
+ return d64_;
79
+ }
80
+
81
+ int Exponent() const {
82
+ if (IsDenormal()) return kDenormalExponent;
83
+
84
+ uint64_t d64 = AsUint64();
85
+ int biased_e = static_cast<int>((d64 & kExponentMask) >> kSignificandSize);
86
+ return biased_e - kExponentBias;
87
+ }
88
+
89
+ uint64_t Significand() const {
90
+ uint64_t d64 = AsUint64();
91
+ uint64_t significand = d64 & kSignificandMask;
92
+ if (!IsDenormal()) {
93
+ return significand + kHiddenBit;
94
+ } else {
95
+ return significand;
96
+ }
97
+ }
98
+
99
+ // Returns true if the double is a denormal.
100
+ bool IsDenormal() const {
101
+ uint64_t d64 = AsUint64();
102
+ return (d64 & kExponentMask) == 0;
103
+ }
104
+
105
+ // We consider denormals not to be special.
106
+ // Hence only Infinity and NaN are special.
107
+ bool IsSpecial() const {
108
+ uint64_t d64 = AsUint64();
109
+ return (d64 & kExponentMask) == kExponentMask;
110
+ }
111
+
112
+ bool IsNan() const {
113
+ uint64_t d64 = AsUint64();
114
+ return ((d64 & kExponentMask) == kExponentMask) &&
115
+ ((d64 & kSignificandMask) != 0);
116
+ }
117
+
118
+
119
+ bool IsInfinite() const {
120
+ uint64_t d64 = AsUint64();
121
+ return ((d64 & kExponentMask) == kExponentMask) &&
122
+ ((d64 & kSignificandMask) == 0);
123
+ }
124
+
125
+
126
+ int Sign() const {
127
+ uint64_t d64 = AsUint64();
128
+ return (d64 & kSignMask) == 0? 1: -1;
129
+ }
130
+
131
+
132
+ // Returns the two boundaries of this.
133
+ // The bigger boundary (m_plus) is normalized. The lower boundary has the same
134
+ // exponent as m_plus.
135
+ void NormalizedBoundaries(DiyFp* out_m_minus, DiyFp* out_m_plus) const {
136
+ DiyFp v = this->AsDiyFp();
137
+ bool significand_is_zero = (v.f() == kHiddenBit);
138
+ DiyFp m_plus = DiyFp::Normalize(DiyFp((v.f() << 1) + 1, v.e() - 1));
139
+ DiyFp m_minus;
140
+ if (significand_is_zero && v.e() != kDenormalExponent) {
141
+ // The boundary is closer. Think of v = 1000e10 and v- = 9999e9.
142
+ // Then the boundary (== (v - v-)/2) is not just at a distance of 1e9 but
143
+ // at a distance of 1e8.
144
+ // The only exception is for the smallest normal: the largest denormal is
145
+ // at the same distance as its successor.
146
+ // Note: denormals have the same exponent as the smallest normals.
147
+ m_minus = DiyFp((v.f() << 2) - 1, v.e() - 2);
148
+ } else {
149
+ m_minus = DiyFp((v.f() << 1) - 1, v.e() - 1);
150
+ }
151
+ m_minus.set_f(m_minus.f() << (m_minus.e() - m_plus.e()));
152
+ m_minus.set_e(m_plus.e());
153
+ *out_m_plus = m_plus;
154
+ *out_m_minus = m_minus;
155
+ }
156
+
157
+ double value() const { return uint64_to_double(d64_); }
158
+
159
+ private:
160
+ static const int kSignificandSize = 52; // Excludes the hidden bit.
161
+ static const int kExponentBias = 0x3FF + kSignificandSize;
162
+ static const int kDenormalExponent = -kExponentBias + 1;
163
+
164
+ uint64_t d64_;
165
+ };
166
+
167
+ } } // namespace v8::internal
168
+
169
+ #endif // V8_DOUBLE_H_
@@ -0,0 +1,81 @@
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_DTOA_H_
29
+ #define V8_DTOA_H_
30
+
31
+ namespace v8 {
32
+ namespace internal {
33
+
34
+ enum DtoaMode {
35
+ // 0.9999999999999999 becomes 0.1
36
+ DTOA_SHORTEST,
37
+ // Fixed number of digits after the decimal point.
38
+ // For instance fixed(0.1, 4) becomes 0.1000
39
+ // If the input number is big, the output will be big.
40
+ DTOA_FIXED,
41
+ // Fixed number of digits (independent of the decimal point).
42
+ DTOA_PRECISION
43
+ };
44
+
45
+ // The maximal length of digits a double can have in base 10.
46
+ // Note that DoubleToAscii null-terminates its input. So the given buffer should
47
+ // be at least kBase10MaximalLength + 1 characters long.
48
+ static const int kBase10MaximalLength = 17;
49
+
50
+ // Converts the given double 'v' to ascii.
51
+ // The result should be interpreted as buffer * 10^(point-length).
52
+ //
53
+ // The output depends on the given mode:
54
+ // - SHORTEST: produce the least amount of digits for which the internal
55
+ // identity requirement is still satisfied. If the digits are printed
56
+ // (together with the correct exponent) then reading this number will give
57
+ // 'v' again. The buffer will choose the representation that is closest to
58
+ // 'v'. If there are two at the same distance, than the one farther away
59
+ // from 0 is chosen (halfway cases - ending with 5 - are rounded up).
60
+ // In this mode the 'requested_digits' parameter is ignored.
61
+ // - FIXED: produces digits necessary to print a given number with
62
+ // 'requested_digits' digits after the decimal point. The produced digits
63
+ // might be too short in which case the caller has to fill the gaps with '0's.
64
+ // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
65
+ // Halfway cases are rounded towards +/-Infinity (away from 0). The call
66
+ // toFixed(0.15, 2) thus returns buffer="2", point=0.
67
+ // The returned buffer may contain digits that would be truncated from the
68
+ // shortest representation of the input.
69
+ // - PRECISION: produces 'requested_digits' where the first digit is not '0'.
70
+ // Even though the length of produced digits usually equals
71
+ // 'requested_digits', the function is allowed to return fewer digits, in
72
+ // which case the caller has to fill the missing digits with '0's.
73
+ // Halfway cases are again rounded away from 0.
74
+ // 'DoubleToAscii' expects the given buffer to be big enough to hold all digits
75
+ // and a terminating null-character.
76
+ bool DoubleToAscii(double v, DtoaMode mode, int requested_digits,
77
+ Vector<char> buffer, int* sign, int* length, int* point);
78
+
79
+ } } // namespace v8::internal
80
+
81
+ #endif // V8_DTOA_H_
@@ -2,4 +2,5 @@ require 'mkmf'
2
2
  $CFLAGS += " -Wall -Werror -Wno-unused-parameter "
3
3
  dir_config('iv', 'ext')
4
4
  $CFLAGS += " -I../../include "
5
+
5
6
  create_makefile('iv/phonic')
@@ -25,10 +25,12 @@ class AstFactory : public core::Space<2> {
25
25
  }
26
26
 
27
27
  template<typename Range>
28
- Identifier* NewIdentifier(const Range& range,
28
+ Identifier* NewIdentifier(core::Token::Type type,
29
+ const Range& range,
29
30
  std::size_t begin,
30
31
  std::size_t end) {
31
32
  Identifier* ident = new(this)Identifier(range, this);
33
+ ident->set_type(type);
32
34
  ident->Location(begin, end);
33
35
  return ident;
34
36
  }
@@ -0,0 +1,505 @@
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
+ #include "v8.h"
29
+
30
+ #include "fast-dtoa.h"
31
+
32
+ #include "cached-powers.h"
33
+ #include "diy-fp.h"
34
+ #include "double.h"
35
+
36
+ namespace v8 {
37
+ namespace internal {
38
+
39
+ // The minimal and maximal target exponent define the range of w's binary
40
+ // exponent, where 'w' is the result of multiplying the input by a cached power
41
+ // of ten.
42
+ //
43
+ // A different range might be chosen on a different platform, to optimize digit
44
+ // generation, but a smaller range requires more powers of ten to be cached.
45
+ static const int minimal_target_exponent = -60;
46
+ static const int maximal_target_exponent = -32;
47
+
48
+
49
+ // Adjusts the last digit of the generated number, and screens out generated
50
+ // solutions that may be inaccurate. A solution may be inaccurate if it is
51
+ // outside the safe interval, or if we ctannot prove that it is closer to the
52
+ // input than a neighboring representation of the same length.
53
+ //
54
+ // Input: * buffer containing the digits of too_high / 10^kappa
55
+ // * the buffer's length
56
+ // * distance_too_high_w == (too_high - w).f() * unit
57
+ // * unsafe_interval == (too_high - too_low).f() * unit
58
+ // * rest = (too_high - buffer * 10^kappa).f() * unit
59
+ // * ten_kappa = 10^kappa * unit
60
+ // * unit = the common multiplier
61
+ // Output: returns true if the buffer is guaranteed to contain the closest
62
+ // representable number to the input.
63
+ // Modifies the generated digits in the buffer to approach (round towards) w.
64
+ bool RoundWeed(Vector<char> buffer,
65
+ int length,
66
+ uint64_t distance_too_high_w,
67
+ uint64_t unsafe_interval,
68
+ uint64_t rest,
69
+ uint64_t ten_kappa,
70
+ uint64_t unit) {
71
+ uint64_t small_distance = distance_too_high_w - unit;
72
+ uint64_t big_distance = distance_too_high_w + unit;
73
+ // Let w_low = too_high - big_distance, and
74
+ // w_high = too_high - small_distance.
75
+ // Note: w_low < w < w_high
76
+ //
77
+ // The real w (* unit) must lie somewhere inside the interval
78
+ // ]w_low; w_low[ (often written as "(w_low; w_low)")
79
+
80
+ // Basically the buffer currently contains a number in the unsafe interval
81
+ // ]too_low; too_high[ with too_low < w < too_high
82
+ //
83
+ // too_high - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
84
+ // ^v 1 unit ^ ^ ^ ^
85
+ // boundary_high --------------------- . . . .
86
+ // ^v 1 unit . . . .
87
+ // - - - - - - - - - - - - - - - - - - - + - - + - - - - - - . .
88
+ // . . ^ . .
89
+ // . big_distance . . .
90
+ // . . . . rest
91
+ // small_distance . . . .
92
+ // v . . . .
93
+ // w_high - - - - - - - - - - - - - - - - - - . . . .
94
+ // ^v 1 unit . . . .
95
+ // w ---------------------------------------- . . . .
96
+ // ^v 1 unit v . . .
97
+ // w_low - - - - - - - - - - - - - - - - - - - - - . . .
98
+ // . . v
99
+ // buffer --------------------------------------------------+-------+--------
100
+ // . .
101
+ // safe_interval .
102
+ // v .
103
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - .
104
+ // ^v 1 unit .
105
+ // boundary_low ------------------------- unsafe_interval
106
+ // ^v 1 unit v
107
+ // too_low - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108
+ //
109
+ //
110
+ // Note that the value of buffer could lie anywhere inside the range too_low
111
+ // to too_high.
112
+ //
113
+ // boundary_low, boundary_high and w are approximations of the real boundaries
114
+ // and v (the input number). They are guaranteed to be precise up to one unit.
115
+ // In fact the error is guaranteed to be strictly less than one unit.
116
+ //
117
+ // Anything that lies outside the unsafe interval is guaranteed not to round
118
+ // to v when read again.
119
+ // Anything that lies inside the safe interval is guaranteed to round to v
120
+ // when read again.
121
+ // If the number inside the buffer lies inside the unsafe interval but not
122
+ // inside the safe interval then we simply do not know and bail out (returning
123
+ // false).
124
+ //
125
+ // Similarly we have to take into account the imprecision of 'w' when rounding
126
+ // the buffer. If we have two potential representations we need to make sure
127
+ // that the chosen one is closer to w_low and w_high since v can be anywhere
128
+ // between them.
129
+ //
130
+ // By generating the digits of too_high we got the largest (closest to
131
+ // too_high) buffer that is still in the unsafe interval. In the case where
132
+ // w_high < buffer < too_high we try to decrement the buffer.
133
+ // This way the buffer approaches (rounds towards) w.
134
+ // There are 3 conditions that stop the decrementation process:
135
+ // 1) the buffer is already below w_high
136
+ // 2) decrementing the buffer would make it leave the unsafe interval
137
+ // 3) decrementing the buffer would yield a number below w_high and farther
138
+ // away than the current number. In other words:
139
+ // (buffer{-1} < w_high) && w_high - buffer{-1} > buffer - w_high
140
+ // Instead of using the buffer directly we use its distance to too_high.
141
+ // Conceptually rest ~= too_high - buffer
142
+ while (rest < small_distance && // Negated condition 1
143
+ unsafe_interval - rest >= ten_kappa && // Negated condition 2
144
+ (rest + ten_kappa < small_distance || // buffer{-1} > w_high
145
+ small_distance - rest >= rest + ten_kappa - small_distance)) {
146
+ buffer[length - 1]--;
147
+ rest += ten_kappa;
148
+ }
149
+
150
+ // We have approached w+ as much as possible. We now test if approaching w-
151
+ // would require changing the buffer. If yes, then we have two possible
152
+ // representations close to w, but we cannot decide which one is closer.
153
+ if (rest < big_distance &&
154
+ unsafe_interval - rest >= ten_kappa &&
155
+ (rest + ten_kappa < big_distance ||
156
+ big_distance - rest > rest + ten_kappa - big_distance)) {
157
+ return false;
158
+ }
159
+
160
+ // Weeding test.
161
+ // The safe interval is [too_low + 2 ulp; too_high - 2 ulp]
162
+ // Since too_low = too_high - unsafe_interval this is equivalent to
163
+ // [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
164
+ // Conceptually we have: rest ~= too_high - buffer
165
+ return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit);
166
+ }
167
+
168
+
169
+
170
+ static const uint32_t kTen4 = 10000;
171
+ static const uint32_t kTen5 = 100000;
172
+ static const uint32_t kTen6 = 1000000;
173
+ static const uint32_t kTen7 = 10000000;
174
+ static const uint32_t kTen8 = 100000000;
175
+ static const uint32_t kTen9 = 1000000000;
176
+
177
+ // Returns the biggest power of ten that is less than or equal than the given
178
+ // number. We furthermore receive the maximum number of bits 'number' has.
179
+ // If number_bits == 0 then 0^-1 is returned
180
+ // The number of bits must be <= 32.
181
+ // Precondition: (1 << number_bits) <= number < (1 << (number_bits + 1)).
182
+ static void BiggestPowerTen(uint32_t number,
183
+ int number_bits,
184
+ uint32_t* power,
185
+ int* exponent) {
186
+ switch (number_bits) {
187
+ case 32:
188
+ case 31:
189
+ case 30:
190
+ if (kTen9 <= number) {
191
+ *power = kTen9;
192
+ *exponent = 9;
193
+ break;
194
+ } // else fallthrough
195
+ case 29:
196
+ case 28:
197
+ case 27:
198
+ if (kTen8 <= number) {
199
+ *power = kTen8;
200
+ *exponent = 8;
201
+ break;
202
+ } // else fallthrough
203
+ case 26:
204
+ case 25:
205
+ case 24:
206
+ if (kTen7 <= number) {
207
+ *power = kTen7;
208
+ *exponent = 7;
209
+ break;
210
+ } // else fallthrough
211
+ case 23:
212
+ case 22:
213
+ case 21:
214
+ case 20:
215
+ if (kTen6 <= number) {
216
+ *power = kTen6;
217
+ *exponent = 6;
218
+ break;
219
+ } // else fallthrough
220
+ case 19:
221
+ case 18:
222
+ case 17:
223
+ if (kTen5 <= number) {
224
+ *power = kTen5;
225
+ *exponent = 5;
226
+ break;
227
+ } // else fallthrough
228
+ case 16:
229
+ case 15:
230
+ case 14:
231
+ if (kTen4 <= number) {
232
+ *power = kTen4;
233
+ *exponent = 4;
234
+ break;
235
+ } // else fallthrough
236
+ case 13:
237
+ case 12:
238
+ case 11:
239
+ case 10:
240
+ if (1000 <= number) {
241
+ *power = 1000;
242
+ *exponent = 3;
243
+ break;
244
+ } // else fallthrough
245
+ case 9:
246
+ case 8:
247
+ case 7:
248
+ if (100 <= number) {
249
+ *power = 100;
250
+ *exponent = 2;
251
+ break;
252
+ } // else fallthrough
253
+ case 6:
254
+ case 5:
255
+ case 4:
256
+ if (10 <= number) {
257
+ *power = 10;
258
+ *exponent = 1;
259
+ break;
260
+ } // else fallthrough
261
+ case 3:
262
+ case 2:
263
+ case 1:
264
+ if (1 <= number) {
265
+ *power = 1;
266
+ *exponent = 0;
267
+ break;
268
+ } // else fallthrough
269
+ case 0:
270
+ *power = 0;
271
+ *exponent = -1;
272
+ break;
273
+ default:
274
+ // Following assignments are here to silence compiler warnings.
275
+ *power = 0;
276
+ *exponent = 0;
277
+ UNREACHABLE();
278
+ }
279
+ }
280
+
281
+
282
+ // Generates the digits of input number w.
283
+ // w is a floating-point number (DiyFp), consisting of a significand and an
284
+ // exponent. Its exponent is bounded by minimal_target_exponent and
285
+ // maximal_target_exponent.
286
+ // Hence -60 <= w.e() <= -32.
287
+ //
288
+ // Returns false if it fails, in which case the generated digits in the buffer
289
+ // should not be used.
290
+ // Preconditions:
291
+ // * low, w and high are correct up to 1 ulp (unit in the last place). That
292
+ // is, their error must be less that a unit of their last digits.
293
+ // * low.e() == w.e() == high.e()
294
+ // * low < w < high, and taking into account their error: low~ <= high~
295
+ // * minimal_target_exponent <= w.e() <= maximal_target_exponent
296
+ // Postconditions: returns false if procedure fails.
297
+ // otherwise:
298
+ // * buffer is not null-terminated, but len contains the number of digits.
299
+ // * buffer contains the shortest possible decimal digit-sequence
300
+ // such that LOW < buffer * 10^kappa < HIGH, where LOW and HIGH are the
301
+ // correct values of low and high (without their error).
302
+ // * if more than one decimal representation gives the minimal number of
303
+ // decimal digits then the one closest to W (where W is the correct value
304
+ // of w) is chosen.
305
+ // Remark: this procedure takes into account the imprecision of its input
306
+ // numbers. If the precision is not enough to guarantee all the postconditions
307
+ // then false is returned. This usually happens rarely (~0.5%).
308
+ //
309
+ // Say, for the sake of example, that
310
+ // w.e() == -48, and w.f() == 0x1234567890abcdef
311
+ // w's value can be computed by w.f() * 2^w.e()
312
+ // We can obtain w's integral digits by simply shifting w.f() by -w.e().
313
+ // -> w's integral part is 0x1234
314
+ // w's fractional part is therefore 0x567890abcdef.
315
+ // Printing w's integral part is easy (simply print 0x1234 in decimal).
316
+ // In order to print its fraction we repeatedly multiply the fraction by 10 and
317
+ // get each digit. Example the first digit after the point would be computed by
318
+ // (0x567890abcdef * 10) >> 48. -> 3
319
+ // The whole thing becomes slightly more complicated because we want to stop
320
+ // once we have enough digits. That is, once the digits inside the buffer
321
+ // represent 'w' we can stop. Everything inside the interval low - high
322
+ // represents w. However we have to pay attention to low, high and w's
323
+ // imprecision.
324
+ bool DigitGen(DiyFp low,
325
+ DiyFp w,
326
+ DiyFp high,
327
+ Vector<char> buffer,
328
+ int* length,
329
+ int* kappa) {
330
+ ASSERT(low.e() == w.e() && w.e() == high.e());
331
+ ASSERT(low.f() + 1 <= high.f() - 1);
332
+ ASSERT(minimal_target_exponent <= w.e() && w.e() <= maximal_target_exponent);
333
+ // low, w and high are imprecise, but by less than one ulp (unit in the last
334
+ // place).
335
+ // If we remove (resp. add) 1 ulp from low (resp. high) we are certain that
336
+ // the new numbers are outside of the interval we want the final
337
+ // representation to lie in.
338
+ // Inversely adding (resp. removing) 1 ulp from low (resp. high) would yield
339
+ // numbers that are certain to lie in the interval. We will use this fact
340
+ // later on.
341
+ // We will now start by generating the digits within the uncertain
342
+ // interval. Later we will weed out representations that lie outside the safe
343
+ // interval and thus _might_ lie outside the correct interval.
344
+ uint64_t unit = 1;
345
+ DiyFp too_low = DiyFp(low.f() - unit, low.e());
346
+ DiyFp too_high = DiyFp(high.f() + unit, high.e());
347
+ // too_low and too_high are guaranteed to lie outside the interval we want the
348
+ // generated number in.
349
+ DiyFp unsafe_interval = DiyFp::Minus(too_high, too_low);
350
+ // We now cut the input number into two parts: the integral digits and the
351
+ // fractionals. We will not write any decimal separator though, but adapt
352
+ // kappa instead.
353
+ // Reminder: we are currently computing the digits (stored inside the buffer)
354
+ // such that: too_low < buffer * 10^kappa < too_high
355
+ // We use too_high for the digit_generation and stop as soon as possible.
356
+ // If we stop early we effectively round down.
357
+ DiyFp one = DiyFp(static_cast<uint64_t>(1) << -w.e(), w.e());
358
+ // Division by one is a shift.
359
+ uint32_t integrals = static_cast<uint32_t>(too_high.f() >> -one.e());
360
+ // Modulo by one is an and.
361
+ uint64_t fractionals = too_high.f() & (one.f() - 1);
362
+ uint32_t divider;
363
+ int divider_exponent;
364
+ BiggestPowerTen(integrals, DiyFp::kSignificandSize - (-one.e()),
365
+ &divider, &divider_exponent);
366
+ *kappa = divider_exponent + 1;
367
+ *length = 0;
368
+ // Loop invariant: buffer = too_high / 10^kappa (integer division)
369
+ // The invariant holds for the first iteration: kappa has been initialized
370
+ // with the divider exponent + 1. And the divider is the biggest power of ten
371
+ // that is smaller than integrals.
372
+ while (*kappa > 0) {
373
+ int digit = integrals / divider;
374
+ buffer[*length] = '0' + digit;
375
+ (*length)++;
376
+ integrals %= divider;
377
+ (*kappa)--;
378
+ // Note that kappa now equals the exponent of the divider and that the
379
+ // invariant thus holds again.
380
+ uint64_t rest =
381
+ (static_cast<uint64_t>(integrals) << -one.e()) + fractionals;
382
+ // Invariant: too_high = buffer * 10^kappa + DiyFp(rest, one.e())
383
+ // Reminder: unsafe_interval.e() == one.e()
384
+ if (rest < unsafe_interval.f()) {
385
+ // Rounding down (by not emitting the remaining digits) yields a number
386
+ // that lies within the unsafe interval.
387
+ return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f(),
388
+ unsafe_interval.f(), rest,
389
+ static_cast<uint64_t>(divider) << -one.e(), unit);
390
+ }
391
+ divider /= 10;
392
+ }
393
+
394
+ // The integrals have been generated. We are at the point of the decimal
395
+ // separator. In the following loop we simply multiply the remaining digits by
396
+ // 10 and divide by one. We just need to pay attention to multiply associated
397
+ // data (like the interval or 'unit'), too.
398
+ // Instead of multiplying by 10 we multiply by 5 (cheaper operation) and
399
+ // increase its (imaginary) exponent. At the same time we decrease the
400
+ // divider's (one's) exponent and shift its significand.
401
+ // Basically, if fractionals was a DiyFp (with fractionals.e == one.e):
402
+ // fractionals.f *= 10;
403
+ // fractionals.f >>= 1; fractionals.e++; // value remains unchanged.
404
+ // one.f >>= 1; one.e++; // value remains unchanged.
405
+ // and we have again fractionals.e == one.e which allows us to divide
406
+ // fractionals.f() by one.f()
407
+ // We simply combine the *= 10 and the >>= 1.
408
+ while (true) {
409
+ fractionals *= 5;
410
+ unit *= 5;
411
+ unsafe_interval.set_f(unsafe_interval.f() * 5);
412
+ unsafe_interval.set_e(unsafe_interval.e() + 1); // Will be optimized out.
413
+ one.set_f(one.f() >> 1);
414
+ one.set_e(one.e() + 1);
415
+ // Integer division by one.
416
+ int digit = static_cast<int>(fractionals >> -one.e());
417
+ buffer[*length] = '0' + digit;
418
+ (*length)++;
419
+ fractionals &= one.f() - 1; // Modulo by one.
420
+ (*kappa)--;
421
+ if (fractionals < unsafe_interval.f()) {
422
+ return RoundWeed(buffer, *length, DiyFp::Minus(too_high, w).f() * unit,
423
+ unsafe_interval.f(), fractionals, one.f(), unit);
424
+ }
425
+ }
426
+ }
427
+
428
+
429
+ // Provides a decimal representation of v.
430
+ // Returns true if it succeeds, otherwise the result cannot be trusted.
431
+ // There will be *length digits inside the buffer (not null-terminated).
432
+ // If the function returns true then
433
+ // v == (double) (buffer * 10^decimal_exponent).
434
+ // The digits in the buffer are the shortest representation possible: no
435
+ // 0.09999999999999999 instead of 0.1. The shorter representation will even be
436
+ // chosen even if the longer one would be closer to v.
437
+ // The last digit will be closest to the actual v. That is, even if several
438
+ // digits might correctly yield 'v' when read again, the closest will be
439
+ // computed.
440
+ bool grisu3(double v, Vector<char> buffer, int* length, int* decimal_exponent) {
441
+ DiyFp w = Double(v).AsNormalizedDiyFp();
442
+ // boundary_minus and boundary_plus are the boundaries between v and its
443
+ // closest floating-point neighbors. Any number strictly between
444
+ // boundary_minus and boundary_plus will round to v when convert to a double.
445
+ // Grisu3 will never output representations that lie exactly on a boundary.
446
+ DiyFp boundary_minus, boundary_plus;
447
+ Double(v).NormalizedBoundaries(&boundary_minus, &boundary_plus);
448
+ ASSERT(boundary_plus.e() == w.e());
449
+ DiyFp ten_mk; // Cached power of ten: 10^-k
450
+ int mk; // -k
451
+ GetCachedPower(w.e() + DiyFp::kSignificandSize, minimal_target_exponent,
452
+ maximal_target_exponent, &mk, &ten_mk);
453
+ ASSERT(minimal_target_exponent <= w.e() + ten_mk.e() +
454
+ DiyFp::kSignificandSize &&
455
+ maximal_target_exponent >= w.e() + ten_mk.e() +
456
+ DiyFp::kSignificandSize);
457
+ // Note that ten_mk is only an approximation of 10^-k. A DiyFp only contains a
458
+ // 64 bit significand and ten_mk is thus only precise up to 64 bits.
459
+
460
+ // The DiyFp::Times procedure rounds its result, and ten_mk is approximated
461
+ // too. The variable scaled_w (as well as scaled_boundary_minus/plus) are now
462
+ // off by a small amount.
463
+ // In fact: scaled_w - w*10^k < 1ulp (unit in the last place) of scaled_w.
464
+ // In other words: let f = scaled_w.f() and e = scaled_w.e(), then
465
+ // (f-1) * 2^e < w*10^k < (f+1) * 2^e
466
+ DiyFp scaled_w = DiyFp::Times(w, ten_mk);
467
+ ASSERT(scaled_w.e() ==
468
+ boundary_plus.e() + ten_mk.e() + DiyFp::kSignificandSize);
469
+ // In theory it would be possible to avoid some recomputations by computing
470
+ // the difference between w and boundary_minus/plus (a power of 2) and to
471
+ // compute scaled_boundary_minus/plus by subtracting/adding from
472
+ // scaled_w. However the code becomes much less readable and the speed
473
+ // enhancements are not terriffic.
474
+ DiyFp scaled_boundary_minus = DiyFp::Times(boundary_minus, ten_mk);
475
+ DiyFp scaled_boundary_plus = DiyFp::Times(boundary_plus, ten_mk);
476
+
477
+ // DigitGen will generate the digits of scaled_w. Therefore we have
478
+ // v == (double) (scaled_w * 10^-mk).
479
+ // Set decimal_exponent == -mk and pass it to DigitGen. If scaled_w is not an
480
+ // integer than it will be updated. For instance if scaled_w == 1.23 then
481
+ // the buffer will be filled with "123" und the decimal_exponent will be
482
+ // decreased by 2.
483
+ int kappa;
484
+ bool result = DigitGen(scaled_boundary_minus, scaled_w, scaled_boundary_plus,
485
+ buffer, length, &kappa);
486
+ *decimal_exponent = -mk + kappa;
487
+ return result;
488
+ }
489
+
490
+
491
+ bool FastDtoa(double v,
492
+ Vector<char> buffer,
493
+ int* length,
494
+ int* point) {
495
+ ASSERT(v > 0);
496
+ ASSERT(!Double(v).IsSpecial());
497
+
498
+ int decimal_exponent;
499
+ bool result = grisu3(v, buffer, length, &decimal_exponent);
500
+ *point = *length + decimal_exponent;
501
+ buffer[*length] = '\0';
502
+ return result;
503
+ }
504
+
505
+ } } // namespace v8::internal