2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2009-10-11 17:17:52 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
|
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2009-10-11 17:17:52 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* float.c floating-point constant support for the Netwide Assembler
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2007-09-19 09:31:26 +08:00
|
|
|
#include <ctype.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "nasm.h"
|
2007-10-16 10:46:32 +08:00
|
|
|
#include "float.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* -----------------
|
|
|
|
* local variables
|
|
|
|
* -----------------
|
|
|
|
*/
|
|
|
|
static bool daz = false; /* denormals as zero */
|
|
|
|
static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
2007-10-16 10:46:32 +08:00
|
|
|
* -----------
|
|
|
|
* constants
|
|
|
|
* -----------
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
/* "A limb is like a digit but bigger */
|
|
|
|
typedef uint32_t fp_limb;
|
|
|
|
typedef uint64_t fp_2limb;
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define LIMB_BITS 32
|
2007-10-30 11:20:12 +08:00
|
|
|
#define LIMB_BYTES (LIMB_BITS/8)
|
2009-10-11 17:17:52 +08:00
|
|
|
#define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
|
|
|
|
#define LIMB_MASK ((fp_limb)(~0))
|
|
|
|
#define LIMB_ALL_BYTES ((fp_limb)0x01010101)
|
|
|
|
#define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
|
2007-10-30 11:20:12 +08:00
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
|
2007-10-30 11:20:12 +08:00
|
|
|
#define MANT_LIMBS 6
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
|
|
|
|
#define MANT_DIGITS 52
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
/* the format and the argument list depend on MANT_LIMBS */
|
|
|
|
#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
|
2007-10-16 10:46:32 +08:00
|
|
|
#define MANT_ARG SOME_ARG(mant, 0)
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
|
|
|
|
(a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef DEBUG_FLOAT
|
|
|
|
#define dprintf(x) printf x
|
2009-10-11 17:17:52 +08:00
|
|
|
#else
|
2007-10-16 10:46:32 +08:00
|
|
|
#define dprintf(x) do { } while (0)
|
2009-10-11 17:17:52 +08:00
|
|
|
#endif
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* multiply
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
2007-10-30 11:20:12 +08:00
|
|
|
static int float_multiply(fp_limb *to, fp_limb *from)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_2limb temp[MANT_LIMBS * 2];
|
|
|
|
int i, j;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2007-10-20 05:42:29 +08:00
|
|
|
/*
|
2007-10-16 10:46:32 +08:00
|
|
|
* guaranteed that top bit of 'from' is set -- so we only have
|
|
|
|
* to worry about _one_ bit shift to the left
|
|
|
|
*/
|
|
|
|
dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
|
|
|
|
dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
memset(temp, 0, sizeof temp);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
for (i = 0; i < MANT_LIMBS; i++) {
|
|
|
|
for (j = 0; j < MANT_LIMBS; j++) {
|
|
|
|
fp_2limb n;
|
|
|
|
n = (fp_2limb) to[i] * (fp_2limb) from[j];
|
|
|
|
temp[i + j] += n >> LIMB_BITS;
|
|
|
|
temp[i + j + 1] += (fp_limb)n;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
for (i = MANT_LIMBS * 2; --i;) {
|
|
|
|
temp[i - 1] += temp[i] >> LIMB_BITS;
|
|
|
|
temp[i] &= LIMB_MASK;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
|
2007-10-30 11:20:12 +08:00
|
|
|
SOME_ARG(temp, MANT_LIMBS)));
|
2007-09-19 09:31:26 +08:00
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
if (temp[0] & LIMB_TOP_BIT) {
|
|
|
|
for (i = 0; i < MANT_LIMBS; i++) {
|
|
|
|
to[i] = temp[i] & LIMB_MASK;
|
2007-09-19 09:31:26 +08:00
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
|
|
|
|
return 0;
|
2007-09-19 09:31:26 +08:00
|
|
|
} else {
|
2007-10-30 11:20:12 +08:00
|
|
|
for (i = 0; i < MANT_LIMBS; i++) {
|
|
|
|
to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
|
|
|
|
return -1;
|
2007-09-19 09:31:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-20 04:10:46 +08:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* read an exponent; returns INT32_MAX on error
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
2007-10-20 05:17:51 +08:00
|
|
|
static int32_t read_exponent(const char *string, int32_t max)
|
2007-10-20 04:10:46 +08:00
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
bool neg = false;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-20 04:10:46 +08:00
|
|
|
if (*string == '+') {
|
2009-10-11 17:17:52 +08:00
|
|
|
string++;
|
2007-10-20 04:10:46 +08:00
|
|
|
} else if (*string == '-') {
|
2009-10-11 17:17:52 +08:00
|
|
|
neg = true;
|
|
|
|
string++;
|
2007-10-20 04:10:46 +08:00
|
|
|
}
|
|
|
|
while (*string) {
|
2009-10-11 17:17:52 +08:00
|
|
|
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)
|
|
|
|
i = max;
|
|
|
|
} else if (*string == '_') {
|
|
|
|
/* do nothing */
|
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"invalid character in floating-point constant %s: '%c'",
|
|
|
|
"exponent", *string);
|
|
|
|
return INT32_MAX;
|
|
|
|
}
|
|
|
|
string++;
|
2007-10-20 04:10:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return neg ? -i : i;
|
|
|
|
}
|
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* convert
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
2007-10-30 11:20:12 +08:00
|
|
|
static bool ieee_flconvert(const char *string, fp_limb *mant,
|
2007-10-16 10:46:32 +08:00
|
|
|
int32_t * exponent)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-14 00:47:53 +08:00
|
|
|
char digits[MANT_DIGITS];
|
|
|
|
char *p, *q, *r;
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb mult[MANT_LIMBS], bit;
|
|
|
|
fp_limb *m;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t tenpwr, twopwr;
|
2007-10-16 10:46:32 +08:00
|
|
|
int32_t extratwos;
|
|
|
|
bool started, seendot, warned;
|
2007-11-13 10:25:24 +08:00
|
|
|
|
|
|
|
warned = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
p = digits;
|
|
|
|
tenpwr = 0;
|
2007-10-17 02:48:07 +08:00
|
|
|
started = seendot = false;
|
2007-11-13 10:25:24 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
while (*string && *string != 'E' && *string != 'e') {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*string == '.') {
|
2007-10-16 10:46:32 +08:00
|
|
|
if (!seendot) {
|
2007-10-11 05:58:45 +08:00
|
|
|
seendot = true;
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2005-01-16 06:15:51 +08:00
|
|
|
"too many periods in floating-point constant");
|
2007-10-16 10:46:32 +08:00
|
|
|
return false;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
} else if (*string >= '0' && *string <= '9') {
|
|
|
|
if (*string == '0' && !started) {
|
2007-10-16 10:46:32 +08:00
|
|
|
if (seendot) {
|
2005-01-16 06:15:51 +08:00
|
|
|
tenpwr--;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
2007-10-11 05:58:45 +08:00
|
|
|
started = true;
|
2007-10-16 10:46:32 +08:00
|
|
|
if (p < digits + sizeof(digits)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*p++ = *string - '0';
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
|
|
|
if (!warned) {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_WARN_FL_TOOLONG|ERR_PASS1,
|
2007-10-16 10:46:32 +08:00
|
|
|
"floating-point constant significand contains "
|
|
|
|
"more than %i digits", MANT_DIGITS);
|
|
|
|
warned = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!seendot) {
|
2005-01-16 06:15:51 +08:00
|
|
|
tenpwr++;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
} else if (*string == '_') {
|
|
|
|
/* do nothing */
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2007-10-16 10:46:32 +08:00
|
|
|
"invalid character in floating-point constant %s: '%c'",
|
|
|
|
"significand", *string);
|
|
|
|
return false;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
string++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
if (*string) {
|
2009-10-11 17:17:52 +08:00
|
|
|
int32_t e;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2007-10-20 04:10:46 +08:00
|
|
|
string++; /* eat the E */
|
2009-10-11 17:17:52 +08:00
|
|
|
e = read_exponent(string, 5000);
|
|
|
|
if (e == INT32_MAX)
|
|
|
|
return false;
|
|
|
|
tenpwr += e;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, the memory interval [digits,p) contains a
|
2007-10-16 10:46:32 +08:00
|
|
|
* series of decimal digits zzzzzzz, such that our number X
|
|
|
|
* satisfies X = 0.zzzzzzz * 10^tenpwr.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-10-16 10:46:32 +08:00
|
|
|
q = digits;
|
|
|
|
dprintf(("X = 0."));
|
|
|
|
while (q < p) {
|
|
|
|
dprintf(("%c", *q + '0'));
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
dprintf((" * 10^%i\n", tenpwr));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* Now convert [digits,p) to our internal representation.
|
|
|
|
*/
|
2007-10-30 11:20:12 +08:00
|
|
|
bit = LIMB_TOP_BIT;
|
|
|
|
for (m = mant; m < mant + MANT_LIMBS; m++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*m = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
m = mant;
|
|
|
|
q = digits;
|
2007-10-11 05:58:45 +08:00
|
|
|
started = false;
|
2002-05-01 04:51:32 +08:00
|
|
|
twopwr = 0;
|
2007-10-30 11:20:12 +08:00
|
|
|
while (m < mant + MANT_LIMBS) {
|
|
|
|
fp_limb carry = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
while (p > q && !p[-1]) {
|
2005-01-16 06:15:51 +08:00
|
|
|
p--;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
if (p <= q) {
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
for (r = p; r-- > q;) {
|
2007-10-16 10:46:32 +08:00
|
|
|
int32_t i;
|
2005-01-16 06:15:51 +08:00
|
|
|
i = 2 * *r + carry;
|
2007-10-16 10:46:32 +08:00
|
|
|
if (i >= 10) {
|
|
|
|
carry = 1;
|
|
|
|
i -= 10;
|
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
carry = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
*r = i;
|
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
if (carry) {
|
|
|
|
*m |= bit;
|
|
|
|
started = true;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
if (started) {
|
2007-10-16 10:46:32 +08:00
|
|
|
if (bit == 1) {
|
2007-10-30 11:20:12 +08:00
|
|
|
bit = LIMB_TOP_BIT;
|
2007-10-16 10:46:32 +08:00
|
|
|
m++;
|
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
bit >>= 1;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
twopwr--;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
twopwr += tenpwr;
|
|
|
|
|
|
|
|
/*
|
2007-10-16 10:46:32 +08:00
|
|
|
* At this point, the 'mant' array contains the first frac-
|
|
|
|
* tional places of a base-2^16 real number which when mul-
|
|
|
|
* tiplied by 2^twopwr and 5^tenpwr gives X.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-10-16 10:46:32 +08:00
|
|
|
dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
|
|
|
|
tenpwr));
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* Now multiply 'mant' by 5^tenpwr.
|
|
|
|
*/
|
|
|
|
if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
|
2007-10-30 11:20:12 +08:00
|
|
|
for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
|
|
|
|
*m = LIMB_BYTE(0xcc);
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2007-10-30 11:20:12 +08:00
|
|
|
mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
|
2005-01-16 06:15:51 +08:00
|
|
|
extratwos = -2;
|
|
|
|
tenpwr = -tenpwr;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If tenpwr was 1000...000b, then it becomes 1000...000b. See
|
|
|
|
* the "ANSI C" comment below for more details on that case.
|
|
|
|
*
|
|
|
|
* Because we already truncated tenpwr to +5000...-5000 inside
|
|
|
|
* the exponent parsing code, this shouldn't happen though.
|
|
|
|
*/
|
|
|
|
} else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
|
2007-10-30 11:20:12 +08:00
|
|
|
mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
|
|
|
|
for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
|
2005-01-16 06:15:51 +08:00
|
|
|
*m = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
extratwos = 3;
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
extratwos = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
while (tenpwr) {
|
2007-10-16 10:46:32 +08:00
|
|
|
dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
|
|
|
|
twopwr, tenpwr, extratwos));
|
|
|
|
if (tenpwr & 1) {
|
|
|
|
dprintf(("mant*mult\n"));
|
|
|
|
twopwr += extratwos + float_multiply(mant, mult);
|
|
|
|
}
|
|
|
|
dprintf(("mult*mult\n"));
|
|
|
|
extratwos = extratwos * 2 + float_multiply(mult, mult);
|
2005-01-16 06:15:51 +08:00
|
|
|
tenpwr >>= 1;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In ANSI C, the result of right-shifting a signed integer is
|
|
|
|
* considered implementation-specific. To ensure that the loop
|
|
|
|
* terminates even if tenpwr was 1000...000b to begin with, we
|
|
|
|
* manually clear the MSB, in case a 1 was shifted in.
|
|
|
|
*
|
|
|
|
* Because we already truncated tenpwr to +5000...-5000 inside
|
|
|
|
* the exponent parsing code, this shouldn't matter; neverthe-
|
|
|
|
* less it is the right thing to do here.
|
|
|
|
*/
|
|
|
|
tenpwr &= (uint32_t) - 1 >> 1;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-16 10:46:32 +08:00
|
|
|
* At this point, the 'mant' array contains the first frac-
|
|
|
|
* tional places of a base-2^16 real number in [0.5,1) that
|
|
|
|
* when multiplied by 2^twopwr gives X. Or it contains zero
|
|
|
|
* of course. We are done.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
*exponent = twopwr;
|
2007-10-16 10:46:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* operations of specific bits
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
|
|
|
|
static void set_bit(fp_limb *mant, int bit)
|
|
|
|
{
|
|
|
|
mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test a single bit */
|
|
|
|
static int test_bit(const fp_limb *mant, int bit)
|
|
|
|
{
|
|
|
|
return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Report if the mantissa value is all zero */
|
|
|
|
static bool is_zero(const fp_limb *mant)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MANT_LIMBS; i++)
|
2009-10-11 17:17:52 +08:00
|
|
|
if (mant[i])
|
|
|
|
return false;
|
2007-10-30 11:20:12 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
* round a mantissa off after i words
|
|
|
|
* ---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ROUND_COLLECT_BITS \
|
|
|
|
do { \
|
|
|
|
m = mant[i] & (2*bit-1); \
|
|
|
|
for (j = i+1; j < MANT_LIMBS; j++) \
|
|
|
|
m = m | mant[j]; \
|
2007-10-30 11:20:12 +08:00
|
|
|
} while (0)
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ROUND_ABS_DOWN \
|
|
|
|
do { \
|
|
|
|
mant[i] &= ~(bit-1); \
|
|
|
|
for (j = i+1; j < MANT_LIMBS; j++) \
|
|
|
|
mant[j] = 0; \
|
|
|
|
return false; \
|
2007-10-30 11:20:12 +08:00
|
|
|
} while (0)
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
#define ROUND_ABS_UP \
|
|
|
|
do { \
|
|
|
|
mant[i] = (mant[i] & ~(bit-1)) + bit; \
|
|
|
|
for (j = i+1; j < MANT_LIMBS; j++) \
|
|
|
|
mant[j] = 0; \
|
|
|
|
while (i > 0 && !mant[i]) \
|
|
|
|
++mant[--i]; \
|
|
|
|
return !mant[0]; \
|
2007-10-30 11:20:12 +08:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static bool ieee_round(bool minus, fp_limb *mant, int bits)
|
2007-10-16 10:46:32 +08:00
|
|
|
{
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb m = 0;
|
2007-10-16 10:46:32 +08:00
|
|
|
int32_t j;
|
2007-10-30 11:20:12 +08:00
|
|
|
int i = bits / LIMB_BITS;
|
|
|
|
int p = bits % LIMB_BITS;
|
|
|
|
fp_limb bit = LIMB_TOP_BIT >> p;
|
|
|
|
|
|
|
|
if (rc == FLOAT_RC_NEAR) {
|
2009-10-11 17:17:52 +08:00
|
|
|
if (mant[i] & bit) {
|
|
|
|
mant[i] &= ~bit;
|
|
|
|
ROUND_COLLECT_BITS;
|
|
|
|
mant[i] |= bit;
|
|
|
|
if (m) {
|
|
|
|
ROUND_ABS_UP;
|
|
|
|
} else {
|
|
|
|
if (test_bit(mant, bits-1)) {
|
|
|
|
ROUND_ABS_UP;
|
|
|
|
} else {
|
|
|
|
ROUND_ABS_DOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ROUND_ABS_DOWN;
|
|
|
|
}
|
2007-10-30 11:20:12 +08:00
|
|
|
} else if (rc == FLOAT_RC_ZERO ||
|
2009-10-11 17:17:52 +08:00
|
|
|
rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
|
|
|
|
ROUND_ABS_DOWN;
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
2009-10-11 17:17:52 +08:00
|
|
|
/* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
|
|
|
|
/* Round toward +/- infinity */
|
|
|
|
ROUND_COLLECT_BITS;
|
|
|
|
if (m) {
|
|
|
|
ROUND_ABS_UP;
|
|
|
|
} else {
|
|
|
|
ROUND_ABS_DOWN;
|
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2007-10-17 01:32:57 +08:00
|
|
|
return false;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-23 08:34:10 +08:00
|
|
|
/* Returns a value >= 16 if not a valid hex digit */
|
|
|
|
static unsigned int hexval(char c)
|
2007-10-16 10:46:32 +08:00
|
|
|
{
|
2007-10-23 08:34:10 +08:00
|
|
|
unsigned int v = (unsigned char) c;
|
|
|
|
|
|
|
|
if (v >= '0' && v <= '9')
|
|
|
|
return v - '0';
|
2007-10-16 10:46:32 +08:00
|
|
|
else
|
2007-10-23 08:34:10 +08:00
|
|
|
return (v|0x20) - 'a' + 10;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-23 08:34:10 +08:00
|
|
|
/* Handle floating-point numbers with radix 2^bits and binary exponent */
|
|
|
|
static bool ieee_flconvert_bin(const char *string, int bits,
|
2009-10-11 17:17:52 +08:00
|
|
|
fp_limb *mant, int32_t *exponent)
|
2007-10-16 10:46:32 +08:00
|
|
|
{
|
|
|
|
static const int log2tbl[16] =
|
|
|
|
{ -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb mult[MANT_LIMBS + 1], *mp;
|
2007-10-16 10:46:32 +08:00
|
|
|
int ms;
|
|
|
|
int32_t twopwr;
|
2007-10-23 08:34:10 +08:00
|
|
|
bool seendot, seendigit;
|
2007-10-16 10:46:32 +08:00
|
|
|
unsigned char c;
|
2008-07-04 11:12:37 +08:00
|
|
|
const int radix = 1 << bits;
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb v;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
twopwr = 0;
|
2007-10-23 08:34:10 +08:00
|
|
|
seendot = seendigit = false;
|
2007-10-16 10:46:32 +08:00
|
|
|
ms = 0;
|
|
|
|
mp = NULL;
|
|
|
|
|
|
|
|
memset(mult, 0, sizeof mult);
|
|
|
|
|
|
|
|
while ((c = *string++) != '\0') {
|
|
|
|
if (c == '.') {
|
|
|
|
if (!seendot)
|
|
|
|
seendot = true;
|
|
|
|
else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2007-10-16 10:46:32 +08:00
|
|
|
"too many periods in floating-point constant");
|
2007-10-17 01:32:57 +08:00
|
|
|
return false;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
2007-10-23 08:34:10 +08:00
|
|
|
} else if ((v = hexval(c)) < (unsigned int)radix) {
|
2007-10-16 10:46:32 +08:00
|
|
|
if (!seendigit && v) {
|
|
|
|
int l = log2tbl[v];
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
seendigit = true;
|
2007-10-16 10:46:32 +08:00
|
|
|
mp = mult;
|
2007-10-30 11:20:12 +08:00
|
|
|
ms = (LIMB_BITS-1)-l;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2007-10-23 08:34:10 +08:00
|
|
|
twopwr = seendot ? twopwr-bits+l : l+1-bits;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (seendigit) {
|
|
|
|
if (ms <= 0) {
|
|
|
|
*mp |= v >> -ms;
|
|
|
|
mp++;
|
2007-10-30 11:20:12 +08:00
|
|
|
if (mp > &mult[MANT_LIMBS])
|
|
|
|
mp = &mult[MANT_LIMBS]; /* Guard slot */
|
|
|
|
ms += LIMB_BITS;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
*mp |= v << ms;
|
2007-10-23 08:34:10 +08:00
|
|
|
ms -= bits;
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
if (!seendot)
|
2007-10-23 08:34:10 +08:00
|
|
|
twopwr += bits;
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
|
|
|
if (seendot)
|
2007-10-23 08:34:10 +08:00
|
|
|
twopwr -= bits;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
} else if (c == 'p' || c == 'P') {
|
2009-10-11 17:17:52 +08:00
|
|
|
int32_t e;
|
|
|
|
e = read_exponent(string, 20000);
|
|
|
|
if (e == INT32_MAX)
|
|
|
|
return false;
|
|
|
|
twopwr += e;
|
2007-10-16 10:46:32 +08:00
|
|
|
break;
|
2009-10-11 17:17:52 +08:00
|
|
|
} else if (c == '_') {
|
|
|
|
/* ignore */
|
2007-10-16 10:46:32 +08:00
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2007-10-16 10:46:32 +08:00
|
|
|
"floating-point constant: `%c' is invalid character", c);
|
2007-10-17 01:32:57 +08:00
|
|
|
return false;
|
2007-10-16 10:46:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!seendigit) {
|
2008-07-04 11:11:30 +08:00
|
|
|
memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
|
2007-10-16 10:46:32 +08:00
|
|
|
*exponent = 0;
|
|
|
|
} else {
|
2008-07-04 11:11:30 +08:00
|
|
|
memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
|
2007-10-16 10:46:32 +08:00
|
|
|
*exponent = twopwr;
|
|
|
|
}
|
2007-10-17 01:32:57 +08:00
|
|
|
|
|
|
|
return true;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-10-17 01:32:57 +08:00
|
|
|
* Shift a mantissa to the right by i bits.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-10-30 11:20:12 +08:00
|
|
|
static void ieee_shr(fp_limb *mant, int i)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb n, m;
|
2007-10-17 01:32:57 +08:00
|
|
|
int j = 0;
|
|
|
|
int sr, sl, offs;
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
|
|
|
|
offs = i/LIMB_BITS;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-17 01:32:57 +08:00
|
|
|
if (sr == 0) {
|
2009-10-11 17:17:52 +08:00
|
|
|
if (offs)
|
|
|
|
for (j = MANT_LIMBS-1; j >= offs; j--)
|
|
|
|
mant[j] = mant[j-offs];
|
2007-10-17 01:32:57 +08:00
|
|
|
} else {
|
2009-10-11 17:17:52 +08:00
|
|
|
n = mant[MANT_LIMBS-1-offs] >> sr;
|
|
|
|
for (j = MANT_LIMBS-1; j > offs; j--) {
|
|
|
|
m = mant[j-offs-1];
|
|
|
|
mant[j] = (m << sl) | n;
|
|
|
|
n = m >> sr;
|
|
|
|
}
|
|
|
|
mant[j--] = n;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
2007-10-17 01:32:57 +08:00
|
|
|
while (j >= 0)
|
2009-10-11 17:17:52 +08:00
|
|
|
mant[j--] = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-17 01:32:57 +08:00
|
|
|
/* Produce standard IEEE formats, with implicit or explicit integer
|
|
|
|
bit; this makes the following assumptions:
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-10-17 01:32:57 +08:00
|
|
|
- the sign bit is the MSB, followed by the exponent,
|
|
|
|
followed by the integer bit if present.
|
2007-09-19 08:50:34 +08:00
|
|
|
- the sign bit plus exponent fit in 16 bits.
|
|
|
|
- the exponent bias is 2^(n-1)-1 for an n-bit exponent */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
struct ieee_format {
|
2007-10-30 11:20:12 +08:00
|
|
|
int bytes;
|
2007-10-17 01:32:57 +08:00
|
|
|
int mantissa; /* Fractional bits in the mantissa */
|
2009-10-11 17:17:52 +08:00
|
|
|
int explicit; /* Explicit integer */
|
2007-10-16 10:46:32 +08:00
|
|
|
int exponent; /* Bits in the exponent */
|
2007-09-19 08:50:34 +08:00
|
|
|
};
|
|
|
|
|
2007-10-17 01:32:57 +08:00
|
|
|
/*
|
|
|
|
* The 16- and 128-bit formats are expected to be in IEEE 754r.
|
|
|
|
* AMD SSE5 uses the 16-bit format.
|
|
|
|
*
|
|
|
|
* The 32- and 64-bit formats are the original IEEE 754 formats.
|
|
|
|
*
|
|
|
|
* The 80-bit format is x87-specific, but widely used.
|
2007-10-30 11:20:12 +08:00
|
|
|
*
|
|
|
|
* The 8-bit format appears to be the consensus 8-bit floating-point
|
|
|
|
* format. It is apparently used in graphics applications.
|
2007-10-17 01:32:57 +08:00
|
|
|
*/
|
2007-10-30 11:20:12 +08:00
|
|
|
static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
|
|
|
|
static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
|
|
|
|
static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
|
|
|
|
static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
|
|
|
|
static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
|
|
|
|
static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
|
2007-10-17 01:32:57 +08:00
|
|
|
|
|
|
|
/* Types of values we can generate */
|
|
|
|
enum floats {
|
|
|
|
FL_ZERO,
|
|
|
|
FL_DENORMAL,
|
|
|
|
FL_NORMAL,
|
|
|
|
FL_INFINITY,
|
|
|
|
FL_QNAN,
|
|
|
|
FL_SNAN
|
|
|
|
};
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-07-04 11:16:07 +08:00
|
|
|
static int to_packed_bcd(const char *str, const char *p,
|
2009-10-11 17:17:52 +08:00
|
|
|
int s, uint8_t *result,
|
|
|
|
const struct ieee_format *fmt)
|
2008-07-04 11:12:37 +08:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
char c;
|
|
|
|
int tv = -1;
|
|
|
|
|
|
|
|
if (fmt != &ieee_80) {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"packed BCD requires an 80-bit format");
|
|
|
|
return 0;
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (p >= str) {
|
2009-10-11 17:17:52 +08:00
|
|
|
c = *p--;
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
if (tv < 0) {
|
|
|
|
if (n == 9) {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"packed BCD truncated to 18 digits");
|
|
|
|
}
|
|
|
|
tv = c-'0';
|
|
|
|
} else {
|
|
|
|
if (n < 9)
|
|
|
|
*result++ = tv + ((c-'0') << 4);
|
|
|
|
n++;
|
|
|
|
tv = -1;
|
|
|
|
}
|
|
|
|
} else if (c == '_') {
|
|
|
|
/* do nothing */
|
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"invalid character `%c' in packed BCD constant", c);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
if (tv >= 0) {
|
2009-10-11 17:17:52 +08:00
|
|
|
if (n < 9)
|
|
|
|
*result++ = tv;
|
|
|
|
n++;
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
while (n < 9) {
|
2009-10-11 17:17:52 +08:00
|
|
|
*result++ = 0;
|
|
|
|
n++;
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
*result = (s < 0) ? 0x80 : 0;
|
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
return 1; /* success */
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int to_float(const char *str, int s, uint8_t *result,
|
2007-10-16 10:46:32 +08:00
|
|
|
const struct ieee_format *fmt)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2008-01-22 08:19:52 +08:00
|
|
|
fp_limb mant[MANT_LIMBS];
|
2007-10-17 01:32:57 +08:00
|
|
|
int32_t exponent = 0;
|
2008-07-04 11:12:37 +08:00
|
|
|
const int32_t expmax = 1 << (fmt->exponent - 1);
|
2007-10-30 11:20:12 +08:00
|
|
|
fp_limb one_mask = LIMB_TOP_BIT >>
|
2009-10-11 17:17:52 +08:00
|
|
|
((fmt->exponent+fmt->explicit) % LIMB_BITS);
|
2008-07-04 11:12:37 +08:00
|
|
|
const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
|
2007-09-19 08:50:34 +08:00
|
|
|
int i;
|
2007-10-17 01:32:57 +08:00
|
|
|
int shift;
|
|
|
|
enum floats type;
|
|
|
|
bool ok;
|
2008-07-04 11:12:37 +08:00
|
|
|
const bool minus = s < 0;
|
|
|
|
const int bits = fmt->bytes * 8;
|
|
|
|
const char *strend;
|
|
|
|
|
|
|
|
if (!str[0]) {
|
2016-03-04 06:36:01 +08:00
|
|
|
nasm_panic(0,
|
2009-10-11 17:17:52 +08:00
|
|
|
"internal errror: empty string passed to float_const");
|
|
|
|
return 0;
|
2008-07-04 11:12:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
strend = strchr(str, '\0');
|
|
|
|
if (strend[-1] == 'P' || strend[-1] == 'p')
|
2009-10-11 17:17:52 +08:00
|
|
|
return to_packed_bcd(str, strend-2, s, result, fmt);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 12:55:56 +08:00
|
|
|
if (str[0] == '_') {
|
2009-10-11 17:17:52 +08:00
|
|
|
/* Special tokens */
|
2007-10-16 10:46:32 +08:00
|
|
|
|
|
|
|
switch (str[2]) {
|
|
|
|
case 'n': /* __nan__ */
|
|
|
|
case 'N':
|
|
|
|
case 'q': /* __qnan__ */
|
|
|
|
case 'Q':
|
2009-10-11 17:17:52 +08:00
|
|
|
type = FL_QNAN;
|
2007-10-16 10:46:32 +08:00
|
|
|
break;
|
|
|
|
case 's': /* __snan__ */
|
|
|
|
case 'S':
|
2009-10-11 17:17:52 +08:00
|
|
|
type = FL_SNAN;
|
2007-10-16 10:46:32 +08:00
|
|
|
break;
|
|
|
|
case 'i': /* __infinity__ */
|
|
|
|
case 'I':
|
2009-10-11 17:17:52 +08:00
|
|
|
type = FL_INFINITY;
|
|
|
|
break;
|
|
|
|
default:
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"internal error: unknown FP constant token `%s'\n", str);
|
|
|
|
type = FL_QNAN;
|
2007-10-16 10:46:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-09-19 12:55:56 +08:00
|
|
|
} else {
|
2007-10-23 08:34:10 +08:00
|
|
|
if (str[0] == '0') {
|
2009-10-11 17:17:52 +08:00
|
|
|
switch (str[1]) {
|
|
|
|
case 'x': case 'X':
|
|
|
|
case 'h': case 'H':
|
|
|
|
ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
|
|
|
|
break;
|
|
|
|
case 'o': case 'O':
|
|
|
|
case 'q': case 'Q':
|
|
|
|
ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
|
|
|
|
break;
|
|
|
|
case 'b': case 'B':
|
|
|
|
case 'y': case 'Y':
|
|
|
|
ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
|
|
|
|
break;
|
|
|
|
case 'd': case 'D':
|
|
|
|
case 't': case 'T':
|
|
|
|
ok = ieee_flconvert(str+2, mant, &exponent);
|
|
|
|
break;
|
|
|
|
case 'p': case 'P':
|
|
|
|
return to_packed_bcd(str+2, strend-1, s, result, fmt);
|
|
|
|
default:
|
|
|
|
/* Leading zero was just a zero? */
|
|
|
|
ok = ieee_flconvert(str, mant, &exponent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (str[0] == '$') {
|
|
|
|
ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
|
|
|
|
} else {
|
2007-10-17 01:32:57 +08:00
|
|
|
ok = ieee_flconvert(str, mant, &exponent);
|
2009-10-11 17:17:52 +08:00
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2009-10-11 17:17:52 +08:00
|
|
|
if (!ok) {
|
|
|
|
type = FL_QNAN;
|
|
|
|
} else if (mant[0] & LIMB_TOP_BIT) {
|
2007-10-16 10:46:32 +08:00
|
|
|
/*
|
|
|
|
* Non-zero.
|
|
|
|
*/
|
|
|
|
exponent--;
|
|
|
|
if (exponent >= 2 - expmax && exponent <= expmax) {
|
2009-10-11 17:17:52 +08:00
|
|
|
type = FL_NORMAL;
|
2007-10-17 01:32:57 +08:00
|
|
|
} else if (exponent > 0) {
|
2009-10-11 17:17:52 +08:00
|
|
|
if (pass0 == 1)
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"overflow in floating-point constant");
|
|
|
|
type = FL_INFINITY;
|
|
|
|
} else {
|
|
|
|
/* underflow or denormal; the denormal code handles
|
|
|
|
actual underflow. */
|
|
|
|
type = FL_DENORMAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Zero */
|
|
|
|
type = FL_ZERO;
|
|
|
|
}
|
2007-10-17 01:32:57 +08:00
|
|
|
}
|
2007-10-16 10:46:32 +08:00
|
|
|
|
2007-10-17 01:32:57 +08:00
|
|
|
switch (type) {
|
|
|
|
case FL_ZERO:
|
2007-10-17 02:32:58 +08:00
|
|
|
zero:
|
2009-10-11 17:17:52 +08:00
|
|
|
memset(mant, 0, sizeof mant);
|
|
|
|
break;
|
2007-10-17 01:32:57 +08:00
|
|
|
|
|
|
|
case FL_DENORMAL:
|
|
|
|
{
|
2009-10-11 17:17:52 +08:00
|
|
|
shift = -(exponent + expmax - 2 - fmt->exponent)
|
|
|
|
+ fmt->explicit;
|
|
|
|
ieee_shr(mant, shift);
|
|
|
|
ieee_round(minus, mant, bits);
|
|
|
|
if (mant[one_pos] & one_mask) {
|
|
|
|
/* One's position is set, we rounded up into normal range */
|
|
|
|
exponent = 1;
|
|
|
|
if (!fmt->explicit)
|
|
|
|
mant[one_pos] &= ~one_mask; /* remove explicit one */
|
|
|
|
mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
|
|
|
|
} else {
|
|
|
|
if (daz || is_zero(mant)) {
|
|
|
|
/* Flush denormals to zero */
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"underflow in floating-point constant");
|
|
|
|
goto zero;
|
|
|
|
} else {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_WARN_FL_DENORM|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"denormal floating-point constant");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-10-17 01:32:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case FL_NORMAL:
|
2009-10-11 17:17:52 +08:00
|
|
|
exponent += expmax - 1;
|
|
|
|
ieee_shr(mant, fmt->exponent+fmt->explicit);
|
|
|
|
ieee_round(minus, mant, bits);
|
|
|
|
/* did we scale up by one? */
|
|
|
|
if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
|
|
|
|
ieee_shr(mant, 1);
|
|
|
|
exponent++;
|
|
|
|
if (exponent >= (expmax << 1)-1) {
|
2016-02-18 12:27:41 +08:00
|
|
|
nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
|
2009-10-11 17:17:52 +08:00
|
|
|
"overflow in floating-point constant");
|
|
|
|
type = FL_INFINITY;
|
|
|
|
goto overflow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fmt->explicit)
|
|
|
|
mant[one_pos] &= ~one_mask; /* remove explicit one */
|
|
|
|
mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
|
|
|
|
break;
|
2007-10-17 01:32:57 +08:00
|
|
|
|
|
|
|
case FL_INFINITY:
|
|
|
|
case FL_QNAN:
|
|
|
|
case FL_SNAN:
|
2007-10-17 02:32:58 +08:00
|
|
|
overflow:
|
2009-10-11 17:17:52 +08:00
|
|
|
memset(mant, 0, sizeof mant);
|
|
|
|
mant[0] = (((fp_limb)1 << fmt->exponent)-1)
|
|
|
|
<< (LIMB_BITS-1 - fmt->exponent);
|
|
|
|
if (fmt->explicit)
|
|
|
|
mant[one_pos] |= one_mask;
|
|
|
|
if (type == FL_QNAN)
|
|
|
|
set_bit(mant, fmt->exponent+fmt->explicit+1);
|
|
|
|
else if (type == FL_SNAN)
|
|
|
|
set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
|
|
|
|
break;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-30 11:20:12 +08:00
|
|
|
mant[0] |= minus ? LIMB_TOP_BIT : 0;
|
|
|
|
|
2008-01-22 08:19:52 +08:00
|
|
|
for (i = fmt->bytes - 1; i >= 0; i--)
|
2009-10-11 17:17:52 +08:00
|
|
|
*result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
|
2007-09-19 08:50:34 +08:00
|
|
|
|
|
|
|
return 1; /* success */
|
2007-09-19 07:39:03 +08:00
|
|
|
}
|
|
|
|
|
2016-02-18 12:27:41 +08:00
|
|
|
int float_const(const char *number, int sign, uint8_t *result, int bytes)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-19 07:39:03 +08:00
|
|
|
switch (bytes) {
|
2007-10-30 11:20:12 +08:00
|
|
|
case 1:
|
|
|
|
return to_float(number, sign, result, &ieee_8);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 2:
|
2007-10-16 10:46:32 +08:00
|
|
|
return to_float(number, sign, result, &ieee_16);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 4:
|
2007-10-16 10:46:32 +08:00
|
|
|
return to_float(number, sign, result, &ieee_32);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 8:
|
2007-10-16 10:46:32 +08:00
|
|
|
return to_float(number, sign, result, &ieee_64);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 10:
|
2007-10-17 01:32:57 +08:00
|
|
|
return to_float(number, sign, result, &ieee_80);
|
2007-09-19 08:50:34 +08:00
|
|
|
case 16:
|
2007-10-16 10:46:32 +08:00
|
|
|
return to_float(number, sign, result, &ieee_128);
|
2007-09-19 07:39:03 +08:00
|
|
|
default:
|
2016-03-04 06:36:01 +08:00
|
|
|
nasm_panic(0, "strange value %d passed to float_const", bytes);
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
2007-10-17 05:40:27 +08:00
|
|
|
|
|
|
|
/* Set floating-point options */
|
|
|
|
int float_option(const char *option)
|
|
|
|
{
|
|
|
|
if (!nasm_stricmp(option, "daz")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
daz = true;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "nodaz")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
daz = false;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "near")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
rc = FLOAT_RC_NEAR;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "down")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
rc = FLOAT_RC_DOWN;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "up")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
rc = FLOAT_RC_UP;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "zero")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
rc = FLOAT_RC_ZERO;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else if (!nasm_stricmp(option, "default")) {
|
2009-10-11 17:17:52 +08:00
|
|
|
rc = FLOAT_RC_NEAR;
|
|
|
|
daz = false;
|
|
|
|
return 0;
|
2007-10-17 05:40:27 +08:00
|
|
|
} else {
|
2009-10-11 17:17:52 +08:00
|
|
|
return -1; /* Unknown option */
|
2007-10-17 05:40:27 +08:00
|
|
|
}
|
|
|
|
}
|