-
Notifications
You must be signed in to change notification settings - Fork 708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MSVC++ 12.0 float hex string parse workaround #362
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,37 +283,37 @@ static void parse_float_hex(const char* s, | |
if (!seen_dot) | ||
significand_exponent += significand_shift; | ||
|
||
assert(s < end && *s == 'p'); | ||
s++; | ||
|
||
if (significand == 0) { | ||
/* 0 or -0 */ | ||
*out_bits = make_float(is_neg, F32_MIN_EXP, 0); | ||
return; | ||
} | ||
|
||
assert(s < end); | ||
int exponent = 0; | ||
bool exponent_is_neg = false; | ||
/* exponent is always positive, but significand_exponent is signed. | ||
significand_exponent_add is negated if exponent will be negative, so it can | ||
be easily summed to see if the exponent is too large (see below) */ | ||
int significand_exponent_add = 0; | ||
if (*s == '-') { | ||
exponent_is_neg = true; | ||
significand_exponent_add = -significand_exponent; | ||
s++; | ||
} else if (*s == '+') { | ||
if (s < end) { | ||
assert(*s == 'p'); | ||
s++; | ||
significand_exponent_add = significand_exponent; | ||
} | ||
/* exponent is always positive, but significand_exponent is signed. | ||
significand_exponent_add is negated if exponent will be negative, so it can | ||
be easily summed to see if the exponent is too large (see below) */ | ||
int significand_exponent_add = 0; | ||
if (*s == '-') { | ||
exponent_is_neg = true; | ||
significand_exponent_add = -significand_exponent; | ||
s++; | ||
} else if (*s == '+') { | ||
s++; | ||
significand_exponent_add = significand_exponent; | ||
} | ||
|
||
for (; s < end; ++s) { | ||
uint32_t digit = (*s - '0'); | ||
assert(digit <= 9); | ||
exponent = exponent * 10 + digit; | ||
if (exponent + significand_exponent_add >= F32_MAX_EXP) | ||
break; | ||
for (; s < end; ++s) { | ||
uint32_t digit = (*s - '0'); | ||
assert(digit <= 9); | ||
exponent = exponent * 10 + digit; | ||
if (exponent + significand_exponent_add >= F32_MAX_EXP) | ||
break; | ||
} | ||
} | ||
|
||
if (exponent_is_neg) | ||
|
@@ -615,37 +615,38 @@ static void parse_double_hex(const char* s, | |
if (!seen_dot) | ||
significand_exponent += significand_shift; | ||
|
||
assert(s < end && *s == 'p'); | ||
s++; | ||
|
||
if (significand == 0) { | ||
/* 0 or -0 */ | ||
*out_bits = make_double(is_neg, F64_MIN_EXP, 0); | ||
return; | ||
} | ||
|
||
assert(s < end); | ||
int exponent = 0; | ||
bool exponent_is_neg = false; | ||
/* exponent is always positive, but significand_exponent is signed. | ||
significand_exponent_add is negated if exponent will be negative, so it can | ||
be easily summed to see if the exponent is too large (see below) */ | ||
int significand_exponent_add = 0; | ||
if (*s == '-') { | ||
exponent_is_neg = true; | ||
significand_exponent_add = -significand_exponent; | ||
s++; | ||
} else if (*s == '+') { | ||
if (s < end) { | ||
assert(*s == 'p'); | ||
s++; | ||
significand_exponent_add = significand_exponent; | ||
} | ||
|
||
for (; s < end; ++s) { | ||
uint32_t digit = (*s - '0'); | ||
assert(digit <= 9); | ||
exponent = exponent * 10 + digit; | ||
if (exponent + significand_exponent_add >= F64_MAX_EXP) | ||
break; | ||
/* exponent is always positive, but significand_exponent is signed. | ||
significand_exponent_add is negated if exponent will be negative, so it can | ||
be easily summed to see if the exponent is too large (see below) */ | ||
int significand_exponent_add = 0; | ||
if (*s == '-') { | ||
exponent_is_neg = true; | ||
significand_exponent_add = -significand_exponent; | ||
s++; | ||
} else if (*s == '+') { | ||
s++; | ||
significand_exponent_add = significand_exponent; | ||
} | ||
|
||
for (; s < end; ++s) { | ||
uint32_t digit = (*s - '0'); | ||
assert(digit <= 9); | ||
exponent = exponent * 10 + digit; | ||
if (exponent + significand_exponent_add >= F64_MAX_EXP) | ||
break; | ||
} | ||
} | ||
|
||
if (exponent_is_neg) | ||
|
@@ -842,4 +843,37 @@ void write_double_hex(char* out, size_t size, uint64_t bits) { | |
out[len] = '\0'; | ||
} | ||
|
||
#if COMPILER_IS_MSVC | ||
#if _MSC_VER <= 1800 | ||
float strtof(const char *nptr, char **endptr) { | ||
const char* end = nptr + strlen(nptr); | ||
// review:: should we check for leading whitespaces ? | ||
if (string_starts_with(nptr, end, "0x")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can start with a leading '+' or '-' too. |
||
uint32_t out_bits = 0; | ||
parse_float_hex(nptr, end, &out_bits); | ||
float value; | ||
memcpy((void*)&value, &out_bits, sizeof(value)); | ||
|
||
*endptr = (char*)end; | ||
return value; | ||
} | ||
return ::strtof(nptr, endptr); | ||
} | ||
double strtod(const char *nptr, char **endptr) { | ||
const char* end = nptr + strlen(nptr); | ||
// review:: should we check for leading whitespaces ? | ||
if (string_starts_with(nptr, end, "0x")) { | ||
uint64_t out_bits = 0; | ||
parse_double_hex(nptr, end, &out_bits); | ||
double value; | ||
memcpy((void*)&value, &out_bits, sizeof(value)); | ||
|
||
*endptr = (char*)end; | ||
return value; | ||
} | ||
return ::strtod(nptr, endptr); | ||
} | ||
#endif | ||
#endif | ||
} // namespace wabt | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably better to match the behavior of
strtof
and slip whitespace.