quickfix_ruby_ud 2.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/quickfix/Acceptor.cpp +257 -0
- data/ext/quickfix/Acceptor.h +127 -0
- data/ext/quickfix/Allocator.h +9 -0
- data/ext/quickfix/Application.h +137 -0
- data/ext/quickfix/DOMDocument.h +70 -0
- data/ext/quickfix/DataDictionary.cpp +679 -0
- data/ext/quickfix/DataDictionary.h +607 -0
- data/ext/quickfix/DataDictionaryProvider.cpp +66 -0
- data/ext/quickfix/DataDictionaryProvider.h +67 -0
- data/ext/quickfix/DatabaseConnectionID.h +98 -0
- data/ext/quickfix/DatabaseConnectionPool.h +84 -0
- data/ext/quickfix/Dictionary.cpp +157 -0
- data/ext/quickfix/Dictionary.h +89 -0
- data/ext/quickfix/Event.h +89 -0
- data/ext/quickfix/Except.h +39 -0
- data/ext/quickfix/Exceptions.h +257 -0
- data/ext/quickfix/Field.h +654 -0
- data/ext/quickfix/FieldConvertors.cpp +86 -0
- data/ext/quickfix/FieldConvertors.h +800 -0
- data/ext/quickfix/FieldMap.cpp +254 -0
- data/ext/quickfix/FieldMap.h +327 -0
- data/ext/quickfix/FieldNumbers.h +44 -0
- data/ext/quickfix/FieldTypes.cpp +62 -0
- data/ext/quickfix/FieldTypes.h +817 -0
- data/ext/quickfix/Fields.h +30 -0
- data/ext/quickfix/FileLog.cpp +176 -0
- data/ext/quickfix/FileLog.h +110 -0
- data/ext/quickfix/FileStore.cpp +369 -0
- data/ext/quickfix/FileStore.h +131 -0
- data/ext/quickfix/FixCommonFields.h +13 -0
- data/ext/quickfix/FixFieldNumbers.h +6132 -0
- data/ext/quickfix/FixFields.h +6133 -0
- data/ext/quickfix/FixValues.h +5790 -0
- data/ext/quickfix/Group.cpp +44 -0
- data/ext/quickfix/Group.h +78 -0
- data/ext/quickfix/HostDetailsProvider.cpp +79 -0
- data/ext/quickfix/HostDetailsProvider.h +44 -0
- data/ext/quickfix/HtmlBuilder.h +178 -0
- data/ext/quickfix/HttpConnection.cpp +914 -0
- data/ext/quickfix/HttpConnection.h +74 -0
- data/ext/quickfix/HttpMessage.cpp +229 -0
- data/ext/quickfix/HttpMessage.h +112 -0
- data/ext/quickfix/HttpParser.cpp +49 -0
- data/ext/quickfix/HttpParser.h +49 -0
- data/ext/quickfix/HttpServer.cpp +152 -0
- data/ext/quickfix/HttpServer.h +76 -0
- data/ext/quickfix/Initiator.cpp +310 -0
- data/ext/quickfix/Initiator.h +151 -0
- data/ext/quickfix/Log.cpp +71 -0
- data/ext/quickfix/Log.h +254 -0
- data/ext/quickfix/Message.cpp +617 -0
- data/ext/quickfix/Message.h +419 -0
- data/ext/quickfix/MessageCracker.h +171 -0
- data/ext/quickfix/MessageSorters.cpp +101 -0
- data/ext/quickfix/MessageSorters.h +185 -0
- data/ext/quickfix/MessageStore.cpp +182 -0
- data/ext/quickfix/MessageStore.h +164 -0
- data/ext/quickfix/Mutex.h +120 -0
- data/ext/quickfix/MySQLConnection.h +187 -0
- data/ext/quickfix/MySQLLog.cpp +262 -0
- data/ext/quickfix/MySQLLog.h +158 -0
- data/ext/quickfix/MySQLStore.cpp +323 -0
- data/ext/quickfix/MySQLStore.h +161 -0
- data/ext/quickfix/MySQLStubs.h +203 -0
- data/ext/quickfix/NullStore.cpp +40 -0
- data/ext/quickfix/NullStore.h +89 -0
- data/ext/quickfix/OdbcConnection.h +241 -0
- data/ext/quickfix/OdbcLog.cpp +230 -0
- data/ext/quickfix/OdbcLog.h +109 -0
- data/ext/quickfix/OdbcStore.cpp +313 -0
- data/ext/quickfix/OdbcStore.h +124 -0
- data/ext/quickfix/PUGIXML_DOMDocument.cpp +112 -0
- data/ext/quickfix/PUGIXML_DOMDocument.h +81 -0
- data/ext/quickfix/Parser.cpp +111 -0
- data/ext/quickfix/Parser.h +50 -0
- data/ext/quickfix/PostgreSQLConnection.h +163 -0
- data/ext/quickfix/PostgreSQLLog.cpp +263 -0
- data/ext/quickfix/PostgreSQLLog.h +157 -0
- data/ext/quickfix/PostgreSQLStore.cpp +327 -0
- data/ext/quickfix/PostgreSQLStore.h +160 -0
- data/ext/quickfix/PostgreSQLStubs.h +203 -0
- data/ext/quickfix/Queue.h +66 -0
- data/ext/quickfix/QuickfixRuby.cpp +131900 -0
- data/ext/quickfix/QuickfixRuby.h +56 -0
- data/ext/quickfix/Responder.h +41 -0
- data/ext/quickfix/SSLSocketAcceptor.cpp +409 -0
- data/ext/quickfix/SSLSocketAcceptor.h +186 -0
- data/ext/quickfix/SSLSocketConnection.cpp +434 -0
- data/ext/quickfix/SSLSocketConnection.h +221 -0
- data/ext/quickfix/SSLSocketInitiator.cpp +558 -0
- data/ext/quickfix/SSLSocketInitiator.h +203 -0
- data/ext/quickfix/SSLStubs.h +129 -0
- data/ext/quickfix/Session.cpp +1437 -0
- data/ext/quickfix/Session.h +343 -0
- data/ext/quickfix/SessionFactory.cpp +314 -0
- data/ext/quickfix/SessionFactory.h +84 -0
- data/ext/quickfix/SessionID.h +136 -0
- data/ext/quickfix/SessionSettings.cpp +165 -0
- data/ext/quickfix/SessionSettings.h +283 -0
- data/ext/quickfix/SessionState.h +260 -0
- data/ext/quickfix/Settings.cpp +160 -0
- data/ext/quickfix/Settings.h +56 -0
- data/ext/quickfix/SharedArray.h +274 -0
- data/ext/quickfix/SocketAcceptor.cpp +216 -0
- data/ext/quickfix/SocketAcceptor.h +77 -0
- data/ext/quickfix/SocketConnection.cpp +256 -0
- data/ext/quickfix/SocketConnection.h +102 -0
- data/ext/quickfix/SocketConnector.cpp +112 -0
- data/ext/quickfix/SocketConnector.h +76 -0
- data/ext/quickfix/SocketInitiator.cpp +241 -0
- data/ext/quickfix/SocketInitiator.h +76 -0
- data/ext/quickfix/SocketMonitor.h +26 -0
- data/ext/quickfix/SocketMonitor_UNIX.cpp +238 -0
- data/ext/quickfix/SocketMonitor_UNIX.h +101 -0
- data/ext/quickfix/SocketMonitor_WIN32.cpp +248 -0
- data/ext/quickfix/SocketMonitor_WIN32.h +99 -0
- data/ext/quickfix/SocketServer.cpp +163 -0
- data/ext/quickfix/SocketServer.h +100 -0
- data/ext/quickfix/ThreadedSSLSocketAcceptor.cpp +436 -0
- data/ext/quickfix/ThreadedSSLSocketAcceptor.h +209 -0
- data/ext/quickfix/ThreadedSSLSocketConnection.cpp +364 -0
- data/ext/quickfix/ThreadedSSLSocketConnection.h +191 -0
- data/ext/quickfix/ThreadedSSLSocketInitiator.cpp +434 -0
- data/ext/quickfix/ThreadedSSLSocketInitiator.h +193 -0
- data/ext/quickfix/ThreadedSocketAcceptor.cpp +242 -0
- data/ext/quickfix/ThreadedSocketAcceptor.h +95 -0
- data/ext/quickfix/ThreadedSocketConnection.cpp +227 -0
- data/ext/quickfix/ThreadedSocketConnection.h +89 -0
- data/ext/quickfix/ThreadedSocketInitiator.cpp +238 -0
- data/ext/quickfix/ThreadedSocketInitiator.h +78 -0
- data/ext/quickfix/TimeRange.cpp +227 -0
- data/ext/quickfix/TimeRange.h +215 -0
- data/ext/quickfix/Utility.cpp +639 -0
- data/ext/quickfix/Utility.h +255 -0
- data/ext/quickfix/UtilitySSL.cpp +1612 -0
- data/ext/quickfix/UtilitySSL.h +274 -0
- data/ext/quickfix/Values.h +63 -0
- data/ext/quickfix/config-all.h +10 -0
- data/ext/quickfix/config.h +10 -0
- data/ext/quickfix/config_unix.h +178 -0
- data/ext/quickfix/config_windows.h +0 -0
- data/ext/quickfix/dirent_windows.h +838 -0
- data/ext/quickfix/double-conversion/bignum-dtoa.cc +641 -0
- data/ext/quickfix/double-conversion/bignum-dtoa.h +84 -0
- data/ext/quickfix/double-conversion/bignum.cc +766 -0
- data/ext/quickfix/double-conversion/bignum.h +144 -0
- data/ext/quickfix/double-conversion/cached-powers.cc +176 -0
- data/ext/quickfix/double-conversion/cached-powers.h +64 -0
- data/ext/quickfix/double-conversion/diy-fp.cc +57 -0
- data/ext/quickfix/double-conversion/diy-fp.h +118 -0
- data/ext/quickfix/double-conversion/double-conversion.cc +994 -0
- data/ext/quickfix/double-conversion/double-conversion.h +543 -0
- data/ext/quickfix/double-conversion/fast-dtoa.cc +665 -0
- data/ext/quickfix/double-conversion/fast-dtoa.h +88 -0
- data/ext/quickfix/double-conversion/fixed-dtoa.cc +404 -0
- data/ext/quickfix/double-conversion/fixed-dtoa.h +56 -0
- data/ext/quickfix/double-conversion/ieee.h +402 -0
- data/ext/quickfix/double-conversion/strtod.cc +557 -0
- data/ext/quickfix/double-conversion/strtod.h +45 -0
- data/ext/quickfix/double-conversion/utils.h +374 -0
- data/ext/quickfix/extconf.rb +76 -0
- data/ext/quickfix/index.h +37 -0
- data/ext/quickfix/pugiconfig.hpp +77 -0
- data/ext/quickfix/pugixml.cpp +13237 -0
- data/ext/quickfix/pugixml.hpp +1516 -0
- data/ext/quickfix/scope_guard.hpp +215 -0
- data/ext/quickfix/stdint_msvc.h +254 -0
- data/ext/quickfix/strptime.h +7 -0
- data/lib/quickfix40.rb +274 -0
- data/lib/quickfix41.rb +351 -0
- data/lib/quickfix42.rb +1184 -0
- data/lib/quickfix43.rb +3504 -0
- data/lib/quickfix44.rb +14040 -0
- data/lib/quickfix50.rb +20051 -0
- data/lib/quickfix50sp1.rb +23596 -0
- data/lib/quickfix50sp2.rb +412444 -0
- data/lib/quickfix_fields.rb +79393 -0
- data/lib/quickfix_ruby.rb +82 -0
- data/lib/quickfixt11.rb +65 -0
- data/spec/FIX40.xml +862 -0
- data/spec/FIX41.xml +1282 -0
- data/spec/FIX42.xml +2743 -0
- data/spec/FIX43.xml +4230 -0
- data/spec/FIX44.xml +6600 -0
- data/spec/FIX50.xml +8142 -0
- data/spec/FIX50SP1.xml +9506 -0
- data/spec/FIX50SP2.xml +26069 -0
- data/spec/FIXT11.xml +252 -0
- data/test/DataDictionaryTestCase.rb +268 -0
- data/test/DictionaryTestCase.rb +112 -0
- data/test/FieldBaseTestCase.rb +24 -0
- data/test/MessageStoreTestCase.rb +19 -0
- data/test/MessageTestCase.rb +368 -0
- data/test/SessionSettingsTestCase.rb +41 -0
- metadata +236 -0
@@ -0,0 +1,557 @@
|
|
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 <stdarg.h>
|
29
|
+
#include <limits.h>
|
30
|
+
|
31
|
+
#include "strtod.h"
|
32
|
+
#include "bignum.h"
|
33
|
+
#include "cached-powers.h"
|
34
|
+
#include "ieee.h"
|
35
|
+
|
36
|
+
namespace double_conversion {
|
37
|
+
|
38
|
+
// 2^53 = 9007199254740992.
|
39
|
+
// Any integer with at most 15 decimal digits will hence fit into a double
|
40
|
+
// (which has a 53bit significand) without loss of precision.
|
41
|
+
static const int kMaxExactDoubleIntegerDecimalDigits = 15;
|
42
|
+
// 2^64 = 18446744073709551616 > 10^19
|
43
|
+
static const int kMaxUint64DecimalDigits = 19;
|
44
|
+
|
45
|
+
// Max double: 1.7976931348623157 x 10^308
|
46
|
+
// Min non-zero double: 4.9406564584124654 x 10^-324
|
47
|
+
// Any x >= 10^309 is interpreted as +infinity.
|
48
|
+
// Any x <= 10^-324 is interpreted as 0.
|
49
|
+
// Note that 2.5e-324 (despite being smaller than the min double) will be read
|
50
|
+
// as non-zero (equal to the min non-zero double).
|
51
|
+
static const int kMaxDecimalPower = 309;
|
52
|
+
static const int kMinDecimalPower = -324;
|
53
|
+
|
54
|
+
// 2^64 = 18446744073709551616
|
55
|
+
static const uint64_t kMaxUint64 = UINT64_2PART_C(0xFFFFFFFF, FFFFFFFF);
|
56
|
+
|
57
|
+
|
58
|
+
static const double exact_powers_of_ten[] = {
|
59
|
+
1.0, // 10^0
|
60
|
+
10.0,
|
61
|
+
100.0,
|
62
|
+
1000.0,
|
63
|
+
10000.0,
|
64
|
+
100000.0,
|
65
|
+
1000000.0,
|
66
|
+
10000000.0,
|
67
|
+
100000000.0,
|
68
|
+
1000000000.0,
|
69
|
+
10000000000.0, // 10^10
|
70
|
+
100000000000.0,
|
71
|
+
1000000000000.0,
|
72
|
+
10000000000000.0,
|
73
|
+
100000000000000.0,
|
74
|
+
1000000000000000.0,
|
75
|
+
10000000000000000.0,
|
76
|
+
100000000000000000.0,
|
77
|
+
1000000000000000000.0,
|
78
|
+
10000000000000000000.0,
|
79
|
+
100000000000000000000.0, // 10^20
|
80
|
+
1000000000000000000000.0,
|
81
|
+
// 10^22 = 0x21e19e0c9bab2400000 = 0x878678326eac9 * 2^22
|
82
|
+
10000000000000000000000.0
|
83
|
+
};
|
84
|
+
static const int kExactPowersOfTenSize = ARRAY_SIZE(exact_powers_of_ten);
|
85
|
+
|
86
|
+
// Maximum number of significant digits in the decimal representation.
|
87
|
+
// In fact the value is 772 (see conversions.cc), but to give us some margin
|
88
|
+
// we round up to 780.
|
89
|
+
static const int kMaxSignificantDecimalDigits = 780;
|
90
|
+
|
91
|
+
static Vector<const char> TrimLeadingZeros(Vector<const char> buffer) {
|
92
|
+
for (int i = 0; i < buffer.length(); i++) {
|
93
|
+
if (buffer[i] != '0') {
|
94
|
+
return buffer.SubVector(i, buffer.length());
|
95
|
+
}
|
96
|
+
}
|
97
|
+
return Vector<const char>(buffer.start(), 0);
|
98
|
+
}
|
99
|
+
|
100
|
+
|
101
|
+
static Vector<const char> TrimTrailingZeros(Vector<const char> buffer) {
|
102
|
+
for (int i = buffer.length() - 1; i >= 0; --i) {
|
103
|
+
if (buffer[i] != '0') {
|
104
|
+
return buffer.SubVector(0, i + 1);
|
105
|
+
}
|
106
|
+
}
|
107
|
+
return Vector<const char>(buffer.start(), 0);
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
static void CutToMaxSignificantDigits(Vector<const char> buffer,
|
112
|
+
int exponent,
|
113
|
+
char* significant_buffer,
|
114
|
+
int* significant_exponent) {
|
115
|
+
for (int i = 0; i < kMaxSignificantDecimalDigits - 1; ++i) {
|
116
|
+
significant_buffer[i] = buffer[i];
|
117
|
+
}
|
118
|
+
// The input buffer has been trimmed. Therefore the last digit must be
|
119
|
+
// different from '0'.
|
120
|
+
ASSERT(buffer[buffer.length() - 1] != '0');
|
121
|
+
// Set the last digit to be non-zero. This is sufficient to guarantee
|
122
|
+
// correct rounding.
|
123
|
+
significant_buffer[kMaxSignificantDecimalDigits - 1] = '1';
|
124
|
+
*significant_exponent =
|
125
|
+
exponent + (buffer.length() - kMaxSignificantDecimalDigits);
|
126
|
+
}
|
127
|
+
|
128
|
+
|
129
|
+
// Trims the buffer and cuts it to at most kMaxSignificantDecimalDigits.
|
130
|
+
// If possible the input-buffer is reused, but if the buffer needs to be
|
131
|
+
// modified (due to cutting), then the input needs to be copied into the
|
132
|
+
// buffer_copy_space.
|
133
|
+
static void TrimAndCut(Vector<const char> buffer, int exponent,
|
134
|
+
char* buffer_copy_space, int space_size,
|
135
|
+
Vector<const char>* trimmed, int* updated_exponent) {
|
136
|
+
Vector<const char> left_trimmed = TrimLeadingZeros(buffer);
|
137
|
+
Vector<const char> right_trimmed = TrimTrailingZeros(left_trimmed);
|
138
|
+
exponent += left_trimmed.length() - right_trimmed.length();
|
139
|
+
if (right_trimmed.length() > kMaxSignificantDecimalDigits) {
|
140
|
+
(void) space_size; // Mark variable as used.
|
141
|
+
ASSERT(space_size >= kMaxSignificantDecimalDigits);
|
142
|
+
CutToMaxSignificantDigits(right_trimmed, exponent,
|
143
|
+
buffer_copy_space, updated_exponent);
|
144
|
+
*trimmed = Vector<const char>(buffer_copy_space,
|
145
|
+
kMaxSignificantDecimalDigits);
|
146
|
+
} else {
|
147
|
+
*trimmed = right_trimmed;
|
148
|
+
*updated_exponent = exponent;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
|
153
|
+
// Reads digits from the buffer and converts them to a uint64.
|
154
|
+
// Reads in as many digits as fit into a uint64.
|
155
|
+
// When the string starts with "1844674407370955161" no further digit is read.
|
156
|
+
// Since 2^64 = 18446744073709551616 it would still be possible read another
|
157
|
+
// digit if it was less or equal than 6, but this would complicate the code.
|
158
|
+
static uint64_t ReadUint64(Vector<const char> buffer,
|
159
|
+
int* number_of_read_digits) {
|
160
|
+
uint64_t result = 0;
|
161
|
+
int i = 0;
|
162
|
+
while (i < buffer.length() && result <= (kMaxUint64 / 10 - 1)) {
|
163
|
+
int digit = buffer[i++] - '0';
|
164
|
+
ASSERT(0 <= digit && digit <= 9);
|
165
|
+
result = 10 * result + digit;
|
166
|
+
}
|
167
|
+
*number_of_read_digits = i;
|
168
|
+
return result;
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
// Reads a DiyFp from the buffer.
|
173
|
+
// The returned DiyFp is not necessarily normalized.
|
174
|
+
// If remaining_decimals is zero then the returned DiyFp is accurate.
|
175
|
+
// Otherwise it has been rounded and has error of at most 1/2 ulp.
|
176
|
+
static void ReadDiyFp(Vector<const char> buffer,
|
177
|
+
DiyFp* result,
|
178
|
+
int* remaining_decimals) {
|
179
|
+
int read_digits;
|
180
|
+
uint64_t significand = ReadUint64(buffer, &read_digits);
|
181
|
+
if (buffer.length() == read_digits) {
|
182
|
+
*result = DiyFp(significand, 0);
|
183
|
+
*remaining_decimals = 0;
|
184
|
+
} else {
|
185
|
+
// Round the significand.
|
186
|
+
if (buffer[read_digits] >= '5') {
|
187
|
+
significand++;
|
188
|
+
}
|
189
|
+
// Compute the binary exponent.
|
190
|
+
int exponent = 0;
|
191
|
+
*result = DiyFp(significand, exponent);
|
192
|
+
*remaining_decimals = buffer.length() - read_digits;
|
193
|
+
}
|
194
|
+
}
|
195
|
+
|
196
|
+
|
197
|
+
static bool DoubleStrtod(Vector<const char> trimmed,
|
198
|
+
int exponent,
|
199
|
+
double* result) {
|
200
|
+
#if !defined(DOUBLE_CONVERSION_CORRECT_DOUBLE_OPERATIONS)
|
201
|
+
// On x86 the floating-point stack can be 64 or 80 bits wide. If it is
|
202
|
+
// 80 bits wide (as is the case on Linux) then double-rounding occurs and the
|
203
|
+
// result is not accurate.
|
204
|
+
// We know that Windows32 uses 64 bits and is therefore accurate.
|
205
|
+
// Note that the ARM simulator is compiled for 32bits. It therefore exhibits
|
206
|
+
// the same problem.
|
207
|
+
return false;
|
208
|
+
#endif
|
209
|
+
if (trimmed.length() <= kMaxExactDoubleIntegerDecimalDigits) {
|
210
|
+
int read_digits;
|
211
|
+
// The trimmed input fits into a double.
|
212
|
+
// If the 10^exponent (resp. 10^-exponent) fits into a double too then we
|
213
|
+
// can compute the result-double simply by multiplying (resp. dividing) the
|
214
|
+
// two numbers.
|
215
|
+
// This is possible because IEEE guarantees that floating-point operations
|
216
|
+
// return the best possible approximation.
|
217
|
+
if (exponent < 0 && -exponent < kExactPowersOfTenSize) {
|
218
|
+
// 10^-exponent fits into a double.
|
219
|
+
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
220
|
+
ASSERT(read_digits == trimmed.length());
|
221
|
+
*result /= exact_powers_of_ten[-exponent];
|
222
|
+
return true;
|
223
|
+
}
|
224
|
+
if (0 <= exponent && exponent < kExactPowersOfTenSize) {
|
225
|
+
// 10^exponent fits into a double.
|
226
|
+
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
227
|
+
ASSERT(read_digits == trimmed.length());
|
228
|
+
*result *= exact_powers_of_ten[exponent];
|
229
|
+
return true;
|
230
|
+
}
|
231
|
+
int remaining_digits =
|
232
|
+
kMaxExactDoubleIntegerDecimalDigits - trimmed.length();
|
233
|
+
if ((0 <= exponent) &&
|
234
|
+
(exponent - remaining_digits < kExactPowersOfTenSize)) {
|
235
|
+
// The trimmed string was short and we can multiply it with
|
236
|
+
// 10^remaining_digits. As a result the remaining exponent now fits
|
237
|
+
// into a double too.
|
238
|
+
*result = static_cast<double>(ReadUint64(trimmed, &read_digits));
|
239
|
+
ASSERT(read_digits == trimmed.length());
|
240
|
+
*result *= exact_powers_of_ten[remaining_digits];
|
241
|
+
*result *= exact_powers_of_ten[exponent - remaining_digits];
|
242
|
+
return true;
|
243
|
+
}
|
244
|
+
}
|
245
|
+
return false;
|
246
|
+
}
|
247
|
+
|
248
|
+
|
249
|
+
// Returns 10^exponent as an exact DiyFp.
|
250
|
+
// The given exponent must be in the range [1; kDecimalExponentDistance[.
|
251
|
+
static DiyFp AdjustmentPowerOfTen(int exponent) {
|
252
|
+
ASSERT(0 < exponent);
|
253
|
+
ASSERT(exponent < PowersOfTenCache::kDecimalExponentDistance);
|
254
|
+
// Simply hardcode the remaining powers for the given decimal exponent
|
255
|
+
// distance.
|
256
|
+
ASSERT(PowersOfTenCache::kDecimalExponentDistance == 8);
|
257
|
+
switch (exponent) {
|
258
|
+
case 1: return DiyFp(UINT64_2PART_C(0xa0000000, 00000000), -60);
|
259
|
+
case 2: return DiyFp(UINT64_2PART_C(0xc8000000, 00000000), -57);
|
260
|
+
case 3: return DiyFp(UINT64_2PART_C(0xfa000000, 00000000), -54);
|
261
|
+
case 4: return DiyFp(UINT64_2PART_C(0x9c400000, 00000000), -50);
|
262
|
+
case 5: return DiyFp(UINT64_2PART_C(0xc3500000, 00000000), -47);
|
263
|
+
case 6: return DiyFp(UINT64_2PART_C(0xf4240000, 00000000), -44);
|
264
|
+
case 7: return DiyFp(UINT64_2PART_C(0x98968000, 00000000), -40);
|
265
|
+
default:
|
266
|
+
UNREACHABLE();
|
267
|
+
}
|
268
|
+
|
269
|
+
return DiyFp();
|
270
|
+
}
|
271
|
+
|
272
|
+
|
273
|
+
// If the function returns true then the result is the correct double.
|
274
|
+
// Otherwise it is either the correct double or the double that is just below
|
275
|
+
// the correct double.
|
276
|
+
static bool DiyFpStrtod(Vector<const char> buffer,
|
277
|
+
int exponent,
|
278
|
+
double* result) {
|
279
|
+
DiyFp input;
|
280
|
+
int remaining_decimals;
|
281
|
+
ReadDiyFp(buffer, &input, &remaining_decimals);
|
282
|
+
// Since we may have dropped some digits the input is not accurate.
|
283
|
+
// If remaining_decimals is different than 0 than the error is at most
|
284
|
+
// .5 ulp (unit in the last place).
|
285
|
+
// We don't want to deal with fractions and therefore keep a common
|
286
|
+
// denominator.
|
287
|
+
const int kDenominatorLog = 3;
|
288
|
+
const int kDenominator = 1 << kDenominatorLog;
|
289
|
+
// Move the remaining decimals into the exponent.
|
290
|
+
exponent += remaining_decimals;
|
291
|
+
uint64_t error = (remaining_decimals == 0 ? 0 : kDenominator / 2);
|
292
|
+
|
293
|
+
int old_e = input.e();
|
294
|
+
input.Normalize();
|
295
|
+
error <<= old_e - input.e();
|
296
|
+
|
297
|
+
ASSERT(exponent <= PowersOfTenCache::kMaxDecimalExponent);
|
298
|
+
if (exponent < PowersOfTenCache::kMinDecimalExponent) {
|
299
|
+
*result = 0.0;
|
300
|
+
return true;
|
301
|
+
}
|
302
|
+
DiyFp cached_power;
|
303
|
+
int cached_decimal_exponent;
|
304
|
+
PowersOfTenCache::GetCachedPowerForDecimalExponent(exponent,
|
305
|
+
&cached_power,
|
306
|
+
&cached_decimal_exponent);
|
307
|
+
|
308
|
+
if (cached_decimal_exponent != exponent) {
|
309
|
+
int adjustment_exponent = exponent - cached_decimal_exponent;
|
310
|
+
DiyFp adjustment_power = AdjustmentPowerOfTen(adjustment_exponent);
|
311
|
+
input.Multiply(adjustment_power);
|
312
|
+
if (kMaxUint64DecimalDigits - buffer.length() >= adjustment_exponent) {
|
313
|
+
// The product of input with the adjustment power fits into a 64 bit
|
314
|
+
// integer.
|
315
|
+
ASSERT(DiyFp::kSignificandSize == 64);
|
316
|
+
} else {
|
317
|
+
// The adjustment power is exact. There is hence only an error of 0.5.
|
318
|
+
error += kDenominator / 2;
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
input.Multiply(cached_power);
|
323
|
+
// The error introduced by a multiplication of a*b equals
|
324
|
+
// error_a + error_b + error_a*error_b/2^64 + 0.5
|
325
|
+
// Substituting a with 'input' and b with 'cached_power' we have
|
326
|
+
// error_b = 0.5 (all cached powers have an error of less than 0.5 ulp),
|
327
|
+
// error_ab = 0 or 1 / kDenominator > error_a*error_b/ 2^64
|
328
|
+
int error_b = kDenominator / 2;
|
329
|
+
int error_ab = (error == 0 ? 0 : 1); // We round up to 1.
|
330
|
+
int fixed_error = kDenominator / 2;
|
331
|
+
error += error_b + error_ab + fixed_error;
|
332
|
+
|
333
|
+
old_e = input.e();
|
334
|
+
input.Normalize();
|
335
|
+
error <<= old_e - input.e();
|
336
|
+
|
337
|
+
// See if the double's significand changes if we add/subtract the error.
|
338
|
+
int order_of_magnitude = DiyFp::kSignificandSize + input.e();
|
339
|
+
int effective_significand_size =
|
340
|
+
Double::SignificandSizeForOrderOfMagnitude(order_of_magnitude);
|
341
|
+
int precision_digits_count =
|
342
|
+
DiyFp::kSignificandSize - effective_significand_size;
|
343
|
+
if (precision_digits_count + kDenominatorLog >= DiyFp::kSignificandSize) {
|
344
|
+
// This can only happen for very small denormals. In this case the
|
345
|
+
// half-way multiplied by the denominator exceeds the range of an uint64.
|
346
|
+
// Simply shift everything to the right.
|
347
|
+
int shift_amount = (precision_digits_count + kDenominatorLog) -
|
348
|
+
DiyFp::kSignificandSize + 1;
|
349
|
+
input.set_f(input.f() >> shift_amount);
|
350
|
+
input.set_e(input.e() + shift_amount);
|
351
|
+
// We add 1 for the lost precision of error, and kDenominator for
|
352
|
+
// the lost precision of input.f().
|
353
|
+
error = (error >> shift_amount) + 1 + kDenominator;
|
354
|
+
precision_digits_count -= shift_amount;
|
355
|
+
}
|
356
|
+
// We use uint64_ts now. This only works if the DiyFp uses uint64_ts too.
|
357
|
+
ASSERT(DiyFp::kSignificandSize == 64);
|
358
|
+
ASSERT(precision_digits_count < 64);
|
359
|
+
uint64_t one64 = 1;
|
360
|
+
uint64_t precision_bits_mask = (one64 << precision_digits_count) - 1;
|
361
|
+
uint64_t precision_bits = input.f() & precision_bits_mask;
|
362
|
+
uint64_t half_way = one64 << (precision_digits_count - 1);
|
363
|
+
precision_bits *= kDenominator;
|
364
|
+
half_way *= kDenominator;
|
365
|
+
DiyFp rounded_input(input.f() >> precision_digits_count,
|
366
|
+
input.e() + precision_digits_count);
|
367
|
+
if (precision_bits >= half_way + error) {
|
368
|
+
rounded_input.set_f(rounded_input.f() + 1);
|
369
|
+
}
|
370
|
+
// If the last_bits are too close to the half-way case than we are too
|
371
|
+
// inaccurate and round down. In this case we return false so that we can
|
372
|
+
// fall back to a more precise algorithm.
|
373
|
+
|
374
|
+
*result = Double(rounded_input).value();
|
375
|
+
if (half_way - error < precision_bits && precision_bits < half_way + error) {
|
376
|
+
// Too imprecise. The caller will have to fall back to a slower version.
|
377
|
+
// However the returned number is guaranteed to be either the correct
|
378
|
+
// double, or the next-lower double.
|
379
|
+
return false;
|
380
|
+
} else {
|
381
|
+
return true;
|
382
|
+
}
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
// Returns
|
387
|
+
// - -1 if buffer*10^exponent < diy_fp.
|
388
|
+
// - 0 if buffer*10^exponent == diy_fp.
|
389
|
+
// - +1 if buffer*10^exponent > diy_fp.
|
390
|
+
// Preconditions:
|
391
|
+
// buffer.length() + exponent <= kMaxDecimalPower + 1
|
392
|
+
// buffer.length() + exponent > kMinDecimalPower
|
393
|
+
// buffer.length() <= kMaxDecimalSignificantDigits
|
394
|
+
static int CompareBufferWithDiyFp(Vector<const char> buffer,
|
395
|
+
int exponent,
|
396
|
+
DiyFp diy_fp) {
|
397
|
+
ASSERT(buffer.length() + exponent <= kMaxDecimalPower + 1);
|
398
|
+
ASSERT(buffer.length() + exponent > kMinDecimalPower);
|
399
|
+
ASSERT(buffer.length() <= kMaxSignificantDecimalDigits);
|
400
|
+
// Make sure that the Bignum will be able to hold all our numbers.
|
401
|
+
// Our Bignum implementation has a separate field for exponents. Shifts will
|
402
|
+
// consume at most one bigit (< 64 bits).
|
403
|
+
// ln(10) == 3.3219...
|
404
|
+
ASSERT(((kMaxDecimalPower + 1) * 333 / 100) < Bignum::kMaxSignificantBits);
|
405
|
+
Bignum buffer_bignum;
|
406
|
+
Bignum diy_fp_bignum;
|
407
|
+
buffer_bignum.AssignDecimalString(buffer);
|
408
|
+
diy_fp_bignum.AssignUInt64(diy_fp.f());
|
409
|
+
if (exponent >= 0) {
|
410
|
+
buffer_bignum.MultiplyByPowerOfTen(exponent);
|
411
|
+
} else {
|
412
|
+
diy_fp_bignum.MultiplyByPowerOfTen(-exponent);
|
413
|
+
}
|
414
|
+
if (diy_fp.e() > 0) {
|
415
|
+
diy_fp_bignum.ShiftLeft(diy_fp.e());
|
416
|
+
} else {
|
417
|
+
buffer_bignum.ShiftLeft(-diy_fp.e());
|
418
|
+
}
|
419
|
+
return Bignum::Compare(buffer_bignum, diy_fp_bignum);
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
// Returns true if the guess is the correct double.
|
424
|
+
// Returns false, when guess is either correct or the next-lower double.
|
425
|
+
static bool ComputeGuess(Vector<const char> trimmed, int exponent,
|
426
|
+
double* guess) {
|
427
|
+
if (trimmed.length() == 0) {
|
428
|
+
*guess = 0.0;
|
429
|
+
return true;
|
430
|
+
}
|
431
|
+
if (exponent + trimmed.length() - 1 >= kMaxDecimalPower) {
|
432
|
+
*guess = Double::Infinity();
|
433
|
+
return true;
|
434
|
+
}
|
435
|
+
if (exponent + trimmed.length() <= kMinDecimalPower) {
|
436
|
+
*guess = 0.0;
|
437
|
+
return true;
|
438
|
+
}
|
439
|
+
|
440
|
+
if (DoubleStrtod(trimmed, exponent, guess) ||
|
441
|
+
DiyFpStrtod(trimmed, exponent, guess)) {
|
442
|
+
return true;
|
443
|
+
}
|
444
|
+
if (*guess == Double::Infinity()) {
|
445
|
+
return true;
|
446
|
+
}
|
447
|
+
return false;
|
448
|
+
}
|
449
|
+
|
450
|
+
double Strtod(Vector<const char> buffer, int exponent) {
|
451
|
+
char copy_buffer[kMaxSignificantDecimalDigits];
|
452
|
+
Vector<const char> trimmed;
|
453
|
+
int updated_exponent;
|
454
|
+
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
455
|
+
&trimmed, &updated_exponent);
|
456
|
+
exponent = updated_exponent;
|
457
|
+
|
458
|
+
double guess;
|
459
|
+
bool is_correct = ComputeGuess(trimmed, exponent, &guess);
|
460
|
+
if (is_correct) return guess;
|
461
|
+
|
462
|
+
DiyFp upper_boundary = Double(guess).UpperBoundary();
|
463
|
+
int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
|
464
|
+
if (comparison < 0) {
|
465
|
+
return guess;
|
466
|
+
} else if (comparison > 0) {
|
467
|
+
return Double(guess).NextDouble();
|
468
|
+
} else if ((Double(guess).Significand() & 1) == 0) {
|
469
|
+
// Round towards even.
|
470
|
+
return guess;
|
471
|
+
} else {
|
472
|
+
return Double(guess).NextDouble();
|
473
|
+
}
|
474
|
+
}
|
475
|
+
|
476
|
+
float Strtof(Vector<const char> buffer, int exponent) {
|
477
|
+
char copy_buffer[kMaxSignificantDecimalDigits];
|
478
|
+
Vector<const char> trimmed;
|
479
|
+
int updated_exponent;
|
480
|
+
TrimAndCut(buffer, exponent, copy_buffer, kMaxSignificantDecimalDigits,
|
481
|
+
&trimmed, &updated_exponent);
|
482
|
+
exponent = updated_exponent;
|
483
|
+
|
484
|
+
double double_guess;
|
485
|
+
bool is_correct = ComputeGuess(trimmed, exponent, &double_guess);
|
486
|
+
|
487
|
+
float float_guess = static_cast<float>(double_guess);
|
488
|
+
if (float_guess == double_guess) {
|
489
|
+
// This shortcut triggers for integer values.
|
490
|
+
return float_guess;
|
491
|
+
}
|
492
|
+
|
493
|
+
// We must catch double-rounding. Say the double has been rounded up, and is
|
494
|
+
// now a boundary of a float, and rounds up again. This is why we have to
|
495
|
+
// look at previous too.
|
496
|
+
// Example (in decimal numbers):
|
497
|
+
// input: 12349
|
498
|
+
// high-precision (4 digits): 1235
|
499
|
+
// low-precision (3 digits):
|
500
|
+
// when read from input: 123
|
501
|
+
// when rounded from high precision: 124.
|
502
|
+
// To do this we simply look at the neigbors of the correct result and see
|
503
|
+
// if they would round to the same float. If the guess is not correct we have
|
504
|
+
// to look at four values (since two different doubles could be the correct
|
505
|
+
// double).
|
506
|
+
|
507
|
+
double double_next = Double(double_guess).NextDouble();
|
508
|
+
double double_previous = Double(double_guess).PreviousDouble();
|
509
|
+
|
510
|
+
float f1 = static_cast<float>(double_previous);
|
511
|
+
float f2 = float_guess;
|
512
|
+
float f3 = static_cast<float>(double_next);
|
513
|
+
float f4;
|
514
|
+
if (is_correct) {
|
515
|
+
f4 = f3;
|
516
|
+
} else {
|
517
|
+
double double_next2 = Double(double_next).NextDouble();
|
518
|
+
f4 = static_cast<float>(double_next2);
|
519
|
+
}
|
520
|
+
(void) f2; // Mark variable as used.
|
521
|
+
ASSERT(f1 <= f2 && f2 <= f3 && f3 <= f4);
|
522
|
+
|
523
|
+
// If the guess doesn't lie near a single-precision boundary we can simply
|
524
|
+
// return its float-value.
|
525
|
+
if (f1 == f4) {
|
526
|
+
return float_guess;
|
527
|
+
}
|
528
|
+
|
529
|
+
ASSERT((f1 != f2 && f2 == f3 && f3 == f4) ||
|
530
|
+
(f1 == f2 && f2 != f3 && f3 == f4) ||
|
531
|
+
(f1 == f2 && f2 == f3 && f3 != f4));
|
532
|
+
|
533
|
+
// guess and next are the two possible canditates (in the same way that
|
534
|
+
// double_guess was the lower candidate for a double-precision guess).
|
535
|
+
float guess = f1;
|
536
|
+
float next = f4;
|
537
|
+
DiyFp upper_boundary;
|
538
|
+
if (guess == 0.0f) {
|
539
|
+
float min_float = 1e-45f;
|
540
|
+
upper_boundary = Double(static_cast<double>(min_float) / 2).AsDiyFp();
|
541
|
+
} else {
|
542
|
+
upper_boundary = Single(guess).UpperBoundary();
|
543
|
+
}
|
544
|
+
int comparison = CompareBufferWithDiyFp(trimmed, exponent, upper_boundary);
|
545
|
+
if (comparison < 0) {
|
546
|
+
return guess;
|
547
|
+
} else if (comparison > 0) {
|
548
|
+
return next;
|
549
|
+
} else if ((Single(guess).Significand() & 1) == 0) {
|
550
|
+
// Round towards even.
|
551
|
+
return guess;
|
552
|
+
} else {
|
553
|
+
return next;
|
554
|
+
}
|
555
|
+
}
|
556
|
+
|
557
|
+
} // namespace double_conversion
|
@@ -0,0 +1,45 @@
|
|
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_STRTOD_H_
|
29
|
+
#define DOUBLE_CONVERSION_STRTOD_H_
|
30
|
+
|
31
|
+
#include "double-conversion/utils.h"
|
32
|
+
|
33
|
+
namespace double_conversion {
|
34
|
+
|
35
|
+
// The buffer must only contain digits in the range [0-9]. It must not
|
36
|
+
// contain a dot or a sign. It must not start with '0', and must not be empty.
|
37
|
+
double Strtod(Vector<const char> buffer, int exponent);
|
38
|
+
|
39
|
+
// The buffer must only contain digits in the range [0-9]. It must not
|
40
|
+
// contain a dot or a sign. It must not start with '0', and must not be empty.
|
41
|
+
float Strtof(Vector<const char> buffer, int exponent);
|
42
|
+
|
43
|
+
} // namespace double_conversion
|
44
|
+
|
45
|
+
#endif // DOUBLE_CONVERSION_STRTOD_H_
|