2002-05-01 04:51:32 +08:00
|
|
|
/* float.c floating-point constant support for the Netwide Assembler
|
|
|
|
*
|
|
|
|
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
|
|
|
* Julian Hall. All rights reserved. The software is
|
|
|
|
* redistributable under the licence given in the file "Licence"
|
|
|
|
* distributed in the NASM archive.
|
|
|
|
*
|
|
|
|
* initial version 13/ix/96 by Simon Tatham
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-04-12 10:40:54 +08:00
|
|
|
#include <inttypes.h>
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
|
|
|
|
#define TRUE 1
|
|
|
|
#define FALSE 0
|
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
#define MANT_WORDS 10 /* 112 bits + 48 for accuracy == 160 */
|
|
|
|
#define MANT_DIGITS 49 /* 50 digits don't fit in 160 bits */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* guaranteed top bit of from is set
|
|
|
|
* => we only have to worry about _one_ bit shift to the left
|
|
|
|
*/
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static int ieee_multiply(uint16_t *to, uint16_t *from)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t temp[MANT_WORDS * 2];
|
2005-01-16 06:15:51 +08:00
|
|
|
int i, j;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < MANT_WORDS * 2; i++)
|
|
|
|
temp[i] = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < MANT_WORDS; i++)
|
|
|
|
for (j = 0; j < MANT_WORDS; j++) {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t n;
|
|
|
|
n = (uint32_t)to[i] * (uint32_t)from[j];
|
2005-01-16 06:15:51 +08:00
|
|
|
temp[i + j] += n >> 16;
|
|
|
|
temp[i + j + 1] += n & 0xFFFF;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = MANT_WORDS * 2; --i;) {
|
|
|
|
temp[i - 1] += temp[i] >> 16;
|
|
|
|
temp[i] &= 0xFFFF;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
if (temp[0] & 0x8000) {
|
2007-09-19 08:50:34 +08:00
|
|
|
memcpy(to, temp, 2*MANT_WORDS);
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
for (i = 0; i < MANT_WORDS; i++)
|
|
|
|
to[i] = (temp[i] << 1) + !!(temp[i + 1] & 0x8000);
|
|
|
|
return -1;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
static void ieee_flconvert(char *string, uint16_t *mant,
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t *exponent, efunc error)
|
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-04-12 10:40:54 +08:00
|
|
|
uint16_t mult[MANT_WORDS], bit;
|
|
|
|
uint16_t *m;
|
|
|
|
int32_t tenpwr, twopwr;
|
2005-01-16 06:15:51 +08:00
|
|
|
int extratwos, started, seendot;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
p = digits;
|
|
|
|
tenpwr = 0;
|
|
|
|
started = seendot = FALSE;
|
|
|
|
while (*string && *string != 'E' && *string != 'e') {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (*string == '.') {
|
|
|
|
if (!seendot)
|
|
|
|
seendot = TRUE;
|
|
|
|
else {
|
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"too many periods in floating-point constant");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (*string >= '0' && *string <= '9') {
|
|
|
|
if (*string == '0' && !started) {
|
|
|
|
if (seendot)
|
|
|
|
tenpwr--;
|
|
|
|
} else {
|
|
|
|
started = TRUE;
|
|
|
|
if (p < digits + sizeof(digits))
|
|
|
|
*p++ = *string - '0';
|
|
|
|
if (!seendot)
|
|
|
|
tenpwr++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error(ERR_NONFATAL,
|
|
|
|
"floating-point constant: `%c' is invalid character",
|
|
|
|
*string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
string++;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
if (*string) {
|
2005-01-16 06:15:51 +08:00
|
|
|
string++; /* eat the E */
|
|
|
|
tenpwr += atoi(string);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point, the memory interval [digits,p) contains a
|
|
|
|
* series of decimal digits zzzzzzz such that our number X
|
|
|
|
* satisfies
|
|
|
|
*
|
|
|
|
* X = 0.zzzzzzz * 10^tenpwr
|
|
|
|
*/
|
|
|
|
|
|
|
|
bit = 0x8000;
|
2005-01-16 06:15:51 +08:00
|
|
|
for (m = mant; m < mant + MANT_WORDS; m++)
|
|
|
|
*m = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
m = mant;
|
|
|
|
q = digits;
|
|
|
|
started = FALSE;
|
|
|
|
twopwr = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
while (m < mant + MANT_WORDS) {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint16_t carry = 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
while (p > q && !p[-1])
|
|
|
|
p--;
|
|
|
|
if (p <= q)
|
|
|
|
break;
|
|
|
|
for (r = p; r-- > q;) {
|
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
i = 2 * *r + carry;
|
|
|
|
if (i >= 10)
|
|
|
|
carry = 1, i -= 10;
|
|
|
|
else
|
|
|
|
carry = 0;
|
|
|
|
*r = i;
|
|
|
|
}
|
|
|
|
if (carry)
|
|
|
|
*m |= bit, started = TRUE;
|
|
|
|
if (started) {
|
|
|
|
if (bit == 1)
|
|
|
|
bit = 0x8000, m++;
|
|
|
|
else
|
|
|
|
bit >>= 1;
|
|
|
|
} else
|
|
|
|
twopwr--;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
twopwr += tenpwr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point the `mant' array contains the first six
|
|
|
|
* fractional places of a base-2^16 real number, which when
|
|
|
|
* multiplied by 2^twopwr and 5^tenpwr gives X. So now we
|
|
|
|
* really do multiply by 5^tenpwr.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tenpwr < 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
for (m = mult; m < mult + MANT_WORDS; m++)
|
|
|
|
*m = 0xCCCC;
|
|
|
|
extratwos = -2;
|
|
|
|
tenpwr = -tenpwr;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else if (tenpwr > 0) {
|
2005-01-16 06:15:51 +08:00
|
|
|
mult[0] = 0xA000;
|
|
|
|
for (m = mult + 1; m < mult + MANT_WORDS; m++)
|
|
|
|
*m = 0;
|
|
|
|
extratwos = 3;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else
|
2005-01-16 06:15:51 +08:00
|
|
|
extratwos = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
while (tenpwr) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (tenpwr & 1)
|
|
|
|
twopwr += extratwos + ieee_multiply(mant, mult);
|
|
|
|
extratwos = extratwos * 2 + ieee_multiply(mult, mult);
|
|
|
|
tenpwr >>= 1;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Conversion is done. The elements of `mant' contain the first
|
|
|
|
* fractional places of a base-2^16 real number in [0.5,1)
|
|
|
|
* which we can multiply by 2^twopwr to get X. Or, of course,
|
|
|
|
* it contains zero.
|
|
|
|
*/
|
|
|
|
*exponent = twopwr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shift a mantissa to the right by i (i < 16) bits.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static void ieee_shr(uint16_t *mant, int i)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
uint16_t n = 0, m;
|
2005-01-16 06:15:51 +08:00
|
|
|
int j;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
for (j = 0; j < MANT_WORDS; j++) {
|
|
|
|
m = (mant[j] << (16 - i)) & 0xFFFF;
|
|
|
|
mant[j] = (mant[j] >> i) | n;
|
|
|
|
n = m;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Round a mantissa off after i words.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static int ieee_round(uint16_t *mant, int i)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
if (mant[i] & 0x8000) {
|
2005-01-16 06:15:51 +08:00
|
|
|
do {
|
|
|
|
++mant[--i];
|
|
|
|
mant[i] &= 0xFFFF;
|
|
|
|
} while (i > 0 && !mant[i]);
|
|
|
|
return !i && !mant[i];
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
|
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
/* Produce standard IEEE formats, with implicit "1" bit; this makes
|
|
|
|
the following assumptions:
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
- the sign bit is the MSB, followed by the exponent.
|
|
|
|
- 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 {
|
|
|
|
int words;
|
|
|
|
int mantissa; /* Bits in the mantissa */
|
|
|
|
int exponent; /* Bits in the exponent */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ieee_format ieee_16 = { 1, 10, 5 };
|
|
|
|
static const struct ieee_format ieee_32 = { 2, 23, 8 };
|
|
|
|
static const struct ieee_format ieee_64 = { 4, 52, 11 };
|
|
|
|
static const struct ieee_format ieee_128 = { 8, 112, 15 };
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
/* Produce all the standard IEEE formats: 16, 32, 64, and 128 bits */
|
2007-04-14 00:47:53 +08:00
|
|
|
static int to_float(char *str, int32_t sign, uint8_t *result,
|
2007-09-19 08:50:34 +08:00
|
|
|
const struct ieee_format *fmt, efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-19 08:50:34 +08:00
|
|
|
uint16_t mant[MANT_WORDS], *mp;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t exponent;
|
2007-09-19 08:50:34 +08:00
|
|
|
int32_t expmax = 1 << (fmt->exponent-1);
|
|
|
|
uint16_t implicit_one = 0x8000 >> fmt->exponent;
|
|
|
|
int i;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sign = (sign < 0 ? 0x8000L : 0L);
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
ieee_flconvert(str, mant, &exponent, error);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (mant[0] & 0x8000) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Non-zero.
|
|
|
|
*/
|
|
|
|
exponent--;
|
2007-09-19 08:50:34 +08:00
|
|
|
if (exponent >= 2-expmax && exponent <= expmax) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Normalised.
|
|
|
|
*/
|
2007-09-19 08:50:34 +08:00
|
|
|
exponent += expmax;
|
|
|
|
ieee_shr(mant, fmt->exponent);
|
|
|
|
ieee_round(mant, fmt->words);
|
|
|
|
/* did we scale up by one? */
|
|
|
|
if (mant[0] & (implicit_one << 1)) {
|
|
|
|
ieee_shr(mant, 1);
|
|
|
|
exponent++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mant[0] &= (implicit_one-1); /* remove leading one */
|
|
|
|
mant[0] |= exponent << (15 - fmt->exponent);
|
|
|
|
} else if (exponent < 2-expmax && exponent >= 2-expmax-fmt->mantissa) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Denormal.
|
|
|
|
*/
|
2007-09-19 08:50:34 +08:00
|
|
|
int shift = -(exponent + expmax-2-fmt->exponent);
|
2005-01-16 06:15:51 +08:00
|
|
|
int sh = shift % 16, wds = shift / 16;
|
|
|
|
ieee_shr(mant, sh);
|
2007-09-19 08:50:34 +08:00
|
|
|
if (ieee_round(mant, fmt->words - wds)
|
2005-01-16 06:15:51 +08:00
|
|
|
|| (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
|
|
|
|
ieee_shr(mant, 1);
|
|
|
|
if (sh == 0)
|
|
|
|
mant[0] |= 0x8000;
|
|
|
|
exponent++;
|
|
|
|
}
|
2007-09-19 08:50:34 +08:00
|
|
|
|
|
|
|
if (wds) {
|
|
|
|
for (i = fmt->words-1; i >= wds; i--)
|
|
|
|
mant[i] = mant[i-wds];
|
|
|
|
for (; i >= 0; i--)
|
|
|
|
mant[i] = 0;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
|
|
|
if (exponent > 0) {
|
|
|
|
error(ERR_NONFATAL, "overflow in floating-point constant");
|
|
|
|
return 0;
|
2007-09-19 08:50:34 +08:00
|
|
|
} else {
|
|
|
|
memset(mant, 0, 2*fmt->words);
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2007-09-19 08:50:34 +08:00
|
|
|
/* Zero */
|
|
|
|
memset(mant, 0, 2*fmt->words);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
mant[0] |= sign;
|
2007-09-19 07:39:03 +08:00
|
|
|
|
2007-09-19 08:50:34 +08:00
|
|
|
for (mp = &mant[fmt->words], i = 0; i < fmt->words; i++) {
|
|
|
|
uint16_t m = *--mp;
|
|
|
|
put(result, m);
|
|
|
|
result += 2;
|
2007-09-19 07:39:03 +08:00
|
|
|
}
|
2007-09-19 08:50:34 +08:00
|
|
|
|
|
|
|
return 1; /* success */
|
2007-09-19 07:39:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 80-bit format with 64-bit mantissa *including an explicit integer 1*
|
|
|
|
and 15-bit exponent. */
|
2007-04-14 00:47:53 +08:00
|
|
|
static int to_ldoub(char *str, int32_t sign, uint8_t *result,
|
2005-01-16 06:15:51 +08:00
|
|
|
efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
uint16_t mant[MANT_WORDS];
|
|
|
|
int32_t exponent;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
sign = (sign < 0 ? 0x8000L : 0L);
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
ieee_flconvert(str, mant, &exponent, error);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (mant[0] & 0x8000) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Non-zero.
|
|
|
|
*/
|
|
|
|
exponent--;
|
|
|
|
if (exponent >= -16383 && exponent <= 16384) {
|
|
|
|
/*
|
|
|
|
* Normalised.
|
|
|
|
*/
|
|
|
|
exponent += 16383;
|
|
|
|
if (ieee_round(mant, 4)) /* did we scale up by one? */
|
|
|
|
ieee_shr(mant, 1), mant[0] |= 0x8000, exponent++;
|
|
|
|
put(result + 8, exponent | sign);
|
|
|
|
put(result + 6, mant[0]);
|
|
|
|
put(result + 4, mant[1]);
|
|
|
|
put(result + 2, mant[2]);
|
|
|
|
put(result + 0, mant[3]);
|
|
|
|
} else if (exponent < -16383 && exponent >= -16446) {
|
|
|
|
/*
|
|
|
|
* Denormal.
|
|
|
|
*/
|
|
|
|
int shift = -(exponent + 16383);
|
|
|
|
int sh = shift % 16, wds = shift / 16;
|
|
|
|
ieee_shr(mant, sh);
|
|
|
|
if (ieee_round(mant, 4 - wds)
|
|
|
|
|| (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
|
|
|
|
ieee_shr(mant, 1);
|
|
|
|
if (sh == 0)
|
|
|
|
mant[0] |= 0x8000;
|
|
|
|
exponent++;
|
|
|
|
}
|
|
|
|
put(result + 8, sign);
|
|
|
|
put(result + 6, (wds == 0 ? mant[0] : 0));
|
|
|
|
put(result + 4, (wds <= 1 ? mant[1 - wds] : 0));
|
|
|
|
put(result + 2, (wds <= 2 ? mant[2 - wds] : 0));
|
|
|
|
put(result + 0, (wds <= 3 ? mant[3 - wds] : 0));
|
|
|
|
} else {
|
|
|
|
if (exponent > 0) {
|
|
|
|
error(ERR_NONFATAL, "overflow in floating-point constant");
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
memset(result, 0, 10);
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Zero.
|
|
|
|
*/
|
|
|
|
memset(result, 0, 10);
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-04-14 00:47:53 +08:00
|
|
|
int float_const(char *number, int32_t sign, uint8_t *result, int bytes,
|
2005-01-16 06:15:51 +08:00
|
|
|
efunc error)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-19 07:39:03 +08:00
|
|
|
switch (bytes) {
|
|
|
|
case 2:
|
2007-09-19 08:50:34 +08:00
|
|
|
return to_float(number, sign, result, &ieee_16, error);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 4:
|
2007-09-19 08:50:34 +08:00
|
|
|
return to_float(number, sign, result, &ieee_32, error);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 8:
|
2007-09-19 08:50:34 +08:00
|
|
|
return to_float(number, sign, result, &ieee_64, error);
|
2007-09-19 07:39:03 +08:00
|
|
|
case 10:
|
2005-01-16 06:15:51 +08:00
|
|
|
return to_ldoub(number, sign, result, error);
|
2007-09-19 08:50:34 +08:00
|
|
|
case 16:
|
|
|
|
return to_float(number, sign, result, &ieee_128, error);
|
2007-09-19 07:39:03 +08:00
|
|
|
default:
|
2005-01-16 06:15:51 +08:00
|
|
|
error(ERR_PANIC, "strange value %d passed to float_const", bytes);
|
|
|
|
return 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|