Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ __inline unsigned __int64 __popcnt64(unsigned __int64 value) {
#endif
#define wabt_popcount_u64 __popcnt64

#if _MSC_VER <= 1800
// Reimplement buggy strtof and strtod
namespace wabt {
float strtof(const char *nptr, char **endptr);
double strtod(const char *nptr, char **endptr);
};
#endif

#else

#error unknown compiler
Expand Down
118 changes: 76 additions & 42 deletions src/literal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ?
Copy link
Member

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.

if (string_starts_with(nptr, end, "0x")) {
Copy link
Member

Choose a reason for hiding this comment

The 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