diff options
author | H. Peter Anvin <hpa@zytor.com> | 2007-10-19 13:10:46 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2007-10-19 13:10:46 -0700 |
commit | 2ef4aac272ff0d2ac0bf630f11e6d8d3e19b27d5 (patch) | |
tree | 1fb0b1e1b0116950ba3836ea9a4fda9edba1f939 /float.c | |
parent | a8eace2b79b1068e54c4af93c41b6e58ba879b83 (diff) | |
download | nasm-2ef4aac272ff0d2ac0bf630f11e6d8d3e19b27d5.tar.gz nasm-2ef4aac272ff0d2ac0bf630f11e6d8d3e19b27d5.tar.bz2 nasm-2ef4aac272ff0d2ac0bf630f11e6d8d3e19b27d5.zip |
Allow underscores in numbers; better detection of FP
- Allow underscores as group separators in numbers, for example:
0x1234_5678 is now a legal number. The underscore is just ignored,
it adds no meaning.
- Recognize dotless floating-point numbers, such as "1e30". This
entails distinguishing hexadecimal numbers in the scanner, since
e.g. 0x1e30 is a perfectly legitimate hex constant.
Diffstat (limited to 'float.c')
-rw-r--r-- | float.c | 103 |
1 files changed, 61 insertions, 42 deletions
@@ -113,6 +113,53 @@ static int float_multiply(uint16_t * to, uint16_t * from) /* * --------------------------------------------------------------------------- + * read an exponent; returns INT32_MAX on error + * --------------------------------------------------------------------------- + */ +int32_t read_exponent(const char *string, int32_t max) +{ + int32_t i = 0; + bool neg = false; + + if (*string == '+') { + string++; + } else if (*string == '-') { + neg = true; + string++; + } + while (*string) { + if (*string >= '0' && *string <= '9') { + i = (i * 10) + (*string - '0'); + + /* + * To ensure that underflows and overflows are + * handled properly we must avoid wraparounds of + * the signed integer value that is used to hold + * the exponent. Therefore we cap the exponent at + * +/-5000, which is slightly more/less than + * what's required for normal and denormal numbers + * in single, double, and extended precision, but + * sufficient to avoid signed integer wraparound. + */ + if (i > max) { + break; + } + } else if (*string == '_') { + /* do nothing */ + } else { + error(ERR_NONFATAL, + "invalid character in floating-point constant %s: '%c'", + "exponent", *string); + return INT32_MAX; + } + string++; + } + + return neg ? -i : i; +} + +/* + * --------------------------------------------------------------------------- * convert * --------------------------------------------------------------------------- */ @@ -161,7 +208,6 @@ static bool ieee_flconvert(const char *string, uint16_t * mant, } } } else if (*string == '_') { - /* do nothing */ } else { error(ERR_NONFATAL, @@ -171,48 +217,15 @@ static bool ieee_flconvert(const char *string, uint16_t * mant, } string++; } + if (*string) { - int32_t i = 0; - bool neg = false; - string++; /* eat the E */ - if (*string == '+') { - string++; - } else if (*string == '-') { - neg = true; - string++; - } - while (*string) { - if (*string >= '0' && *string <= '9') { - i = (i * 10) + (*string - '0'); - - /* - * To ensure that underflows and overflows are - * handled properly we must avoid wraparounds of - * the signed integer value that is used to hold - * the exponent. Therefore we cap the exponent at - * +/-5000, which is slightly more/less than - * what's required for normal and denormal numbers - * in single, double, and extended precision, but - * sufficient to avoid signed integer wraparound. - */ - if (i > 5000) { - break; - } - } else if (*string == '_') { + int32_t e; - /* do nothing */ - } else { - error(ERR_NONFATAL, - "invalid character in floating-point constant %s: '%c'", - "exponent", *string); - return false; - } - string++; - } - if (neg) { - i = 0 - i; - } - tenpwr += i; + string++; /* eat the E */ + e = read_exponent(string, 5000); + if (e == INT32_MAX) + return false; + tenpwr += e; } /* @@ -480,8 +493,14 @@ static bool ieee_flconvert_hex(const char *string, uint16_t * mant, twopwr -= 4; } } else if (c == 'p' || c == 'P') { - twopwr += atoi(string); + int32_t e; + e = read_exponent(string, 16384); + if (e == INT32_MAX) + return false; + twopwr += e; break; + } else if (c == '_') { + /* ignore */ } else { error(ERR_NONFATAL, "floating-point constant: `%c' is invalid character", c); |