forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strtod.cpp
257 lines (218 loc) · 6.32 KB
/
strtod.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Taken from
// https://github.com/JuliaLang/julia/blob/v1.1.0/src/support/strtod.c
#include <ATen/core/Macros.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <locale.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <stdlib.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <xlocale.h>
#endif
// The following code is derived from the Python function _PyOS_ascii_strtod
// see http://hg.python.org/cpython/file/default/Python/pystrtod.c
//
// Copyright © 2001-2014 Python Software Foundation; All Rights Reserved
//
// The following modifications have been made:
// - Leading spaces are ignored
// - Parsing of hex floats is supported in the derived version
// - Python functions for tolower, isdigit and malloc have been replaced by the
// respective
// C stdlib functions
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <ctype.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <errno.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <math.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <string.h>
#include <locale>
#define D_PNAN ((double)+NAN)
#define D_PINF ((double)+INFINITY)
namespace {
#ifdef __ANDROID__
int case_insensitive_match(const char* s, const char* t) {
while (*t && tolower(*s) == *t) {
s++;
t++;
}
return *t ? 0 : 1;
}
double parse_inf_or_nan(const char* p, char** endptr) {
double retval;
const char* s;
int negate = 0;
s = p;
if (*s == '-') {
negate = 1;
s++;
} else if (*s == '+') {
s++;
}
if (case_insensitive_match(s, "inf")) {
s += 3;
if (case_insensitive_match(s, "inity"))
s += 5;
retval = negate ? -D_PINF : D_PINF;
} else if (case_insensitive_match(s, "nan")) {
s += 3;
retval = negate ? -D_PNAN : D_PNAN;
} else {
s = p;
retval = -1.0;
}
*endptr = (char*)s;
return retval;
}
#endif
} // namespace
namespace torch {
namespace jit {
#ifdef _MSC_VER
C10_EXPORT double strtod_c(const char* nptr, char** endptr) {
static _locale_t loc = _create_locale(LC_ALL, "C");
return _strtod_l(nptr, endptr, loc);
}
#elif defined(__ANDROID__)
C10_EXPORT double strtod_c(const char* nptr, char** endptr) {
char* fail_pos;
double val;
const char *p, *decimal_point_pos;
const char* end = NULL; /* Silence gcc */
const char* digits_pos = NULL;
int negate = 0;
fail_pos = NULL;
char decimal_point =
std::use_facet<std::numpunct<char>>(std::locale()).decimal_point();
decimal_point_pos = NULL;
/* Parse infinities and nans */
val = parse_inf_or_nan(nptr, endptr);
if (*endptr != nptr)
return val;
/* Set errno to zero, so that we can distinguish zero results
and underflows */
errno = 0;
/* We process the optional sign manually, then pass the remainder to
the system strtod. This ensures that the result of an underflow
has the correct sign. */
p = nptr;
/* parse leading spaces */
while (isspace((unsigned char)*p)) {
p++;
}
/* Process leading sign, if present */
if (*p == '-') {
negate = 1;
p++;
} else if (*p == '+') {
p++;
}
/* This code path is used for hex floats */
if (*p == '0' && (*(p + 1) == 'x' || *(p + 1) == 'X')) {
digits_pos = p;
p += 2;
/* Check that what's left begins with a digit or decimal point */
if (!isxdigit(*p) && *p != '.')
goto invalid_string;
if (decimal_point != '.') {
/* Look for a '.' in the input; if present, it'll need to be
swapped for the current locale's decimal point before we
call strtod. On the other hand, if we find the current
locale's decimal point then the input is invalid. */
while (isxdigit(*p))
p++;
if (*p == '.') {
decimal_point_pos = p++;
/* locate end of number */
while (isxdigit(*p))
p++;
if (*p == 'p' || *p == 'P')
p++;
if (*p == '+' || *p == '-')
p++;
while (isdigit(*p))
p++;
end = p;
} else if (*p == decimal_point)
goto invalid_string;
/* For the other cases, we need not convert the decimal point */
}
} else {
/* Check that what's left begins with a digit or decimal point */
if (!isdigit(*p) && *p != '.')
goto invalid_string;
digits_pos = p;
if (decimal_point != '.') {
/* Look for a '.' in the input; if present, it'll need to be
swapped for the current locale's decimal point before we
call strtod. On the other hand, if we find the current
locale's decimal point then the input is invalid. */
while (isdigit(*p))
p++;
if (*p == '.') {
decimal_point_pos = p++;
/* locate end of number */
while (isdigit(*p))
p++;
if (*p == 'e' || *p == 'E')
p++;
if (*p == '+' || *p == '-')
p++;
while (isdigit(*p))
p++;
end = p;
} else if (*p == decimal_point)
goto invalid_string;
/* For the other cases, we need not convert the decimal point */
}
}
if (decimal_point_pos) {
char *copy, *c;
/* Create a copy of the input, with the '.' converted to the
locale-specific decimal point */
copy = (char*)malloc(end - digits_pos + 2);
if (copy == NULL) {
*endptr = (char*)nptr;
errno = ENOMEM;
return val;
}
c = copy;
memcpy(c, digits_pos, decimal_point_pos - digits_pos);
c += decimal_point_pos - digits_pos;
memcpy(c, &decimal_point, 1);
c += 1;
memcpy(c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
c += end - (decimal_point_pos + 1);
*c = 0;
val = strtod(copy, &fail_pos);
if (fail_pos) {
fail_pos = (char*)digits_pos + (fail_pos - copy);
}
free(copy);
} else {
val = strtod(digits_pos, &fail_pos);
}
if (fail_pos == digits_pos)
goto invalid_string;
if (negate && fail_pos != nptr)
val = -val;
*endptr = fail_pos;
return val;
invalid_string:
*endptr = (char*)nptr;
errno = EINVAL;
return -1.0;
}
#else
C10_EXPORT double strtod_c(const char* nptr, char** endptr) {
/// NOLINTNEXTLINE(hicpp-signed-bitwise)
static locale_t loc = newlocale(LC_ALL_MASK, "C", nullptr);
return strtod_l(nptr, endptr, loc);
}
#endif
C10_EXPORT float strtof_c(const char* nptr, char** endptr) {
return (float)strtod_c(nptr, endptr);
}
} // namespace jit
} // namespace torch