#include "as.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "expr.h"
#include "md.h"
#include "atof-ieee.h"
#include "messages.h"
#define MAX_PRECISION (6)
#define F_PRECISION (2)
#define D_PRECISION (4)
#define X_PRECISION (6)
#define P_PRECISION (6)
#define GUARD (2)
static uint32_t mask [] = {
0x00000000,
0x00000001,
0x00000003,
0x00000007,
0x0000000f,
0x0000001f,
0x0000003f,
0x0000007f,
0x000000ff,
0x000001ff,
0x000003ff,
0x000007ff,
0x00000fff,
0x00001fff,
0x00003fff,
0x00007fff,
0x0000ffff,
0x0001ffff,
0x0003ffff,
0x0007ffff,
0x000fffff,
0x001fffff,
0x003fffff,
0x007fffff,
0x00ffffff,
0x01ffffff,
0x03ffffff,
0x07ffffff,
0x0fffffff,
0x1fffffff,
0x3fffffff,
0x7fffffff,
0xffffffff
};
static int bits_left_in_littlenum;
static int littlenums_left;
static LITTLENUM_TYPE *littlenum_pointer;
static
int
next_bits(
int number_of_bits)
{
int return_value;
if(!littlenums_left)
return 0;
if (number_of_bits >= bits_left_in_littlenum)
{
return_value = mask [bits_left_in_littlenum] & *littlenum_pointer;
number_of_bits -= bits_left_in_littlenum;
return_value <<= number_of_bits;
if(--littlenums_left) {
bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
littlenum_pointer --;
return_value |= (*littlenum_pointer>>bits_left_in_littlenum) & mask[number_of_bits];
}
}
else
{
bits_left_in_littlenum -= number_of_bits;
return_value = mask [number_of_bits] & (*littlenum_pointer>>bits_left_in_littlenum);
}
return (return_value);
}
static
void
unget_bits(
int num)
{
if(!littlenums_left) {
++littlenum_pointer;
++littlenums_left;
bits_left_in_littlenum=num;
} else if(bits_left_in_littlenum+num>LITTLENUM_NUMBER_OF_BITS) {
bits_left_in_littlenum= num-(LITTLENUM_NUMBER_OF_BITS-bits_left_in_littlenum);
++littlenum_pointer;
++littlenums_left;
} else
bits_left_in_littlenum+=num;
}
static
void
make_invalid_floating_point_number(
LITTLENUM_TYPE *words)
{
as_warn("cannot create floating-point number");
words[0]= (LITTLENUM_TYPE)(((unsigned)-1)>>1);
words[1]= -1;
words[2]= -1;
words[3]= -1;
words[4]= -1;
words[5]= -1;
}
char *
atof_ieee(
char *str,
char what_kind,
LITTLENUM_TYPE *words)
{
static LITTLENUM_TYPE bits [MAX_PRECISION + MAX_PRECISION + GUARD];
char * return_value;
int precision;
int32_t exponent_bits;
return_value = str;
generic_floating_point_number.low = bits + MAX_PRECISION;
generic_floating_point_number.high = NULL;
generic_floating_point_number.leader = NULL;
generic_floating_point_number.exponent = 0;
generic_floating_point_number.sign = '\0';
memset(bits, '\0', sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
switch(what_kind) {
case 'f':
case 'F':
case 's':
case 'S':
precision = F_PRECISION;
exponent_bits = 8;
break;
case 'd':
case 'D':
case 'r':
case 'R':
precision = D_PRECISION;
exponent_bits = 11;
break;
case 'x':
case 'X':
case 'e':
case 'E':
precision = X_PRECISION;
exponent_bits = 15;
break;
case 'p':
case 'P':
precision = P_PRECISION;
exponent_bits= -1;
break;
default:
make_invalid_floating_point_number (words);
return NULL;
}
generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD;
if (atof_generic (& return_value, ".", md_EXP_CHARS, & generic_floating_point_number)) {
#ifdef NeXT_MOD
if(precision==F_PRECISION) {
words[0]=0x7f80;
words[1]=0;
} else {
words[0]=0x7ff0;
words[1]=0;
words[2]=0;
words[3]=0;
}
if(generic_floating_point_number.sign=='-')
words[0] |= 0x8000;
return return_value;
#else
make_invalid_floating_point_number (words);
return NULL;
#endif
}
gen_to_words(words, precision, exponent_bits);
return return_value;
}
int
gen_to_words(
LITTLENUM_TYPE *words,
int precision,
int exponent_bits)
{
int return_value=0;
int32_t exponent_1;
int32_t exponent_2;
int32_t exponent_3;
int32_t exponent_4;
int exponent_skippage;
LITTLENUM_TYPE word1;
LITTLENUM_TYPE * lp;
if (generic_floating_point_number.low > generic_floating_point_number.leader) {
if(generic_floating_point_number.sign=='+')
words[0]=0x0000;
else
words[0]=0x8000;
memset(&words[1], '\0', sizeof(LITTLENUM_TYPE) * (precision-1));
return return_value;
}
if(generic_floating_point_number.sign==0) {
if(precision==F_PRECISION) {
words[0]=0x7fff;
words[1]=0xffff;
} else {
words[0]=0x7fff;
words[1]=0xffff;
words[2]=0xffff;
words[3]=0xffff;
}
return return_value;
} else if(generic_floating_point_number.sign=='P') {
if(precision==F_PRECISION) {
words[0]=0x7f80;
words[1]=0;
} else {
words[0]=0x7ff0;
words[1]=0;
words[2]=0;
words[3]=0;
}
return return_value;
} else if(generic_floating_point_number.sign=='N') {
if(precision==F_PRECISION) {
words[0]=0xff80;
words[1]=0x0;
} else {
words[0]=0xfff0;
words[1]=0x0;
words[2]=0x0;
words[3]=0x0;
}
return return_value;
}
bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
littlenum_pointer = generic_floating_point_number.leader;
littlenums_left = 1+generic_floating_point_number.leader - generic_floating_point_number.low;
for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
;
exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
generic_floating_point_number.low;
exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
exponent_3 = exponent_2 - exponent_skippage;
exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
lp = words;
word1 = (generic_floating_point_number.sign == '+') ? 0 : (1<<(LITTLENUM_NUMBER_OF_BITS-1));
if(exponent_4<1 && exponent_4>=-62) {
int prec_bits;
int num_bits;
unget_bits(1);
num_bits= -exponent_4;
prec_bits=LITTLENUM_NUMBER_OF_BITS*precision-(exponent_bits+1+num_bits);
if(precision==X_PRECISION && exponent_bits==15)
prec_bits-=LITTLENUM_NUMBER_OF_BITS+1;
if(num_bits>=LITTLENUM_NUMBER_OF_BITS-exponent_bits) {
num_bits-=(LITTLENUM_NUMBER_OF_BITS-1)-exponent_bits;
*lp++=word1;
if(num_bits+exponent_bits+1>=precision*LITTLENUM_NUMBER_OF_BITS) {
#ifdef NeXT_MOD
if(precision==F_PRECISION) {
words[0]=0x7f80;
words[1]=0;
} else {
words[0]=0x7ff0;
words[1]=0;
words[2]=0;
words[3]=0;
}
if(generic_floating_point_number.sign=='-')
words[0] |= 0x8000;
return return_value;
#else
make_invalid_floating_point_number(words);
return return_value;
#endif
}
if(precision==X_PRECISION && exponent_bits==15) {
*lp++=0;
*lp++=0;
num_bits-=LITTLENUM_NUMBER_OF_BITS-1;
}
while(num_bits>=LITTLENUM_NUMBER_OF_BITS) {
num_bits-=LITTLENUM_NUMBER_OF_BITS;
*lp++=0;
}
if(num_bits)
*lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-(num_bits));
} else {
if(precision==X_PRECISION && exponent_bits==15) {
*lp++=word1;
*lp++=0;
if(num_bits==LITTLENUM_NUMBER_OF_BITS) {
*lp++=0;
*lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1);
} else if(num_bits==LITTLENUM_NUMBER_OF_BITS-1)
*lp++=0;
else
*lp++=next_bits(LITTLENUM_NUMBER_OF_BITS-1-num_bits);
num_bits=0;
} else {
word1|= next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - (exponent_bits+num_bits));
*lp++=word1;
}
}
while(lp<words+precision)
*lp++=next_bits(LITTLENUM_NUMBER_OF_BITS);
#ifdef NeXT_MOD
#else
#endif
if(next_bits(1)) {
--lp;
if(prec_bits>LITTLENUM_NUMBER_OF_BITS) {
int n = 0;
int tmp_bits;
n=0;
tmp_bits=prec_bits;
while(tmp_bits>LITTLENUM_NUMBER_OF_BITS) {
if(lp[n]!=(LITTLENUM_TYPE)-1)
break;
--n;
tmp_bits-=LITTLENUM_NUMBER_OF_BITS;
}
#ifndef NeXT_MOD
if(tmp_bits>LITTLENUM_NUMBER_OF_BITS ||
(lp[n]&mask[tmp_bits])!=mask[tmp_bits])
#endif
{
uint32_t carry;
for (carry = 1; carry && (lp >= words); lp --) {
carry = * lp + carry;
* lp = carry;
carry >>= LITTLENUM_NUMBER_OF_BITS;
}
}
}
else
#ifdef NeXT_MOD
*lp = *lp + 1;
#else
else if((*lp&mask[prec_bits])!=mask[prec_bits])
*lp++;
#endif
}
return return_value;
} else if (exponent_4 & ~ mask [exponent_bits]) {
#ifdef NeXT_MOD
if(precision==F_PRECISION) {
words[0]=0x7f80;
words[1]=0;
} else {
words[0]=0x7ff0;
words[1]=0;
words[2]=0;
words[3]=0;
}
if(generic_floating_point_number.sign=='-')
words[0] |= 0x8000;
#else
make_invalid_floating_point_number (words);
#endif
return return_value;
} else {
word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits))
| next_bits ((LITTLENUM_NUMBER_OF_BITS-1) - exponent_bits);
}
* lp ++ = word1;
if(exponent_bits==15 && precision==X_PRECISION) {
*lp++=0;
*lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS - 1) |
next_bits(LITTLENUM_NUMBER_OF_BITS - 1);
}
while(lp < words + precision)
*lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
if (next_bits (1)) {
uint32_t carry;
for (carry = 1, lp --; carry && (lp >= words); lp --) {
carry = * lp + carry;
* lp = carry;
carry >>= LITTLENUM_NUMBER_OF_BITS;
}
if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
*words&= ~ (1 << (LITTLENUM_NUMBER_OF_BITS - 1));
}
}
return (return_value);
}
void
int_to_gen(
int x)
{
char buf[20];
char *bufp;
sprintf(buf,"%d",x);
bufp= &buf[0];
if(atof_generic(&bufp,".", md_EXP_CHARS, &generic_floating_point_number))
as_fatal("Error converting number to floating point (Exponent overflow?)");
}
#ifdef TEST
char *
print_gen(gen)
FLONUM_TYPE *gen;
{
FLONUM_TYPE f;
LITTLENUM_TYPE arr[10];
double dv;
float fv;
static char sbuf[40];
if(gen) {
f=generic_floating_point_number;
generic_floating_point_number= *gen;
}
gen_to_words(&arr[0],4,11);
memcpy(&dv, &arr[0], sizeof(double));
sprintf(sbuf,"%x %x %x %x %.14G ",arr[0],arr[1],arr[2],arr[3],dv);
gen_to_words(&arr[0],2,8);
memcpy(&fv, &arr[0], sizeof(float));
sprintf(sbuf+strlen(sbuf),"%x %x %.12g\n",arr[0],arr[1],fv);
if(gen)
generic_floating_point_number=f;
return sbuf;
}
#endif