ack/mach/con_float
1989-07-11 14:17:34 +00:00

166 lines
4 KiB
Plaintext

/*
(c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
/* #define NOFLOAT for machines without floating point
#define ACKFLOAT for machines using the ACK floating point software
#define IEEEFLOAT for machines using IEEE floating point format
#define OWNFLOAT to use floating point format of machine on which
the code generator runs
If none of these are defined, PDP-11 format is generated.
*/
#ifdef NOFLOAT
con_float() {
static int been_here;
if (argval != 4 && argval != 8)
fatal("bad fcon size");
fputs(".data4\t", codefile);
if (argval == 8)
fputs("0,", codefile);
fputs("0 !dummy float\n", codefile);
if ( !been_here++) {
fputs("Warning : dummy float-constant(s)\n", stderr);
}
}
#else /* NOFLOAT */
#include <ctype.h>
#ifndef OWNFLOAT
#include <flt_arith.h>
#endif /* OWNFLOAT */
con_float() {
int i, j;
char *p;
#ifdef OWNFLOAT
float fl;
double f;
double atof();
#else /* OWNFLOAT */
int overflow = 0;
flt_arith e;
char buf[8];
#endif /* OWNFLOAT */
if (argval!= 4 && argval!= 8) {
fprintf(stderr,"float constant size = %d\n",argval);
fatal("bad fcon size");
}
fprintf(codefile,"!float %s sz %d\n", str, argval);
#ifdef OWNFLOAT
f = atof(str);
if (argval == 4) {
fl = f;
p = (char *) &fl;
}
else {
p = (char *) &f;
}
fprintf(codefile, ".data1 0%o", *p++ & 0377);
for (i = argval-1; i; i--) {
fprintf(codefile,",0%o", *p++ & 0377);
}
#else /* OWNFLOAT */
flt_str2flt(str, &e);
#if IEEEFLOAT+ACKFLOAT
if (argval == 4) {
#endif
e.flt_exp += 127;
#ifndef IEEEFLOAT
e.flt_exp += 2;
#endif
if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
#if IEEEFLOAT+ACKFLOAT
if (e.flt_exp >= 255) {
overflow = 1;
e.flt_exp = 255;
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
}
if (e.flt_exp <= 0) {
#if IEEEFLOAT
flt_b64_sft(&(e.flt_mantissa), 1);
if (e.flt_exp < 0) {
flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
e.flt_exp = 0;
}
#else
e.flt_exp = 0;
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
#endif
}
#endif
buf[0] = (e.flt_sign << 7) | (e.flt_exp >> 1);
buf[1] = ((e.flt_exp&1) << 7) |
((e.flt_mantissa.flt_h_32 & 0x7fffffff) >> 24);
buf[2] = e.flt_mantissa.flt_h_32 >> 16;
buf[3] = e.flt_mantissa.flt_h_32 >> 8;
if (argval == 8) {
buf[4] = e.flt_mantissa.flt_h_32;
buf[5] = e.flt_mantissa.flt_l_32 >> 24;
buf[6] = e.flt_mantissa.flt_l_32 >> 16;
buf[7] = e.flt_mantissa.flt_l_32 >> 8;
flt_b64_sft(&(e.flt_mantissa), -56);
}
else flt_b64_sft(&(e.flt_mantissa), -24);
#if IEEEFLOAT+ACKFLOAT
}
else {
e.flt_exp += 1023;
#ifndef IEEEFLOAT
e.flt_exp += 2;
#endif
if (e.flt_mantissa.flt_h_32 == 0) e.flt_exp = 0;
if (e.flt_exp >= 2047) {
overflow = 1;
e.flt_exp = 2047;
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
}
if (e.flt_exp <= 0) {
#if IEEEFLOAT
flt_b64_sft(&(e.flt_mantissa), 1);
if (e.flt_exp < 0) {
flt_b64_sft(&(e.flt_mantissa), -e.flt_exp);
e.flt_exp = 0;
}
#else
e.flt_exp = 0;
e.flt_mantissa.flt_h_32 = e.flt_mantissa.flt_l_32 = 0;
#endif
}
buf[0] = (e.flt_sign << 7) | (e.flt_exp >> 4);
buf[1] = ((e.flt_exp & 017)<< 4) | ((e.flt_mantissa.flt_h_32 >> 27) & 017);
buf[2] = e.flt_mantissa.flt_h_32 >> 19;
buf[3] = e.flt_mantissa.flt_h_32 >> 11;
buf[4] = e.flt_mantissa.flt_h_32 >> 3;
buf[5] = (e.flt_mantissa.flt_h_32 << 5) | ((e.flt_mantissa.flt_l_32 >> 27) & 037);
buf[6] = e.flt_mantissa.flt_l_32 >> 19;
buf[7] = e.flt_mantissa.flt_l_32 >> 11;
flt_b64_sft(&(e.flt_mantissa), -53);
}
#endif
i = argval-1;
if (! overflow && (e.flt_mantissa.flt_h_32 & 0x80000000)) for (; i>=0; i--) {
if ((buf[i] &0377) != 0377) {
buf[i]++;
break;
}
else buf[i] = 0;
}
if (overflow || i < 0) {
fprintf(stderr, "Warning: overflow in floating point constant %s\n", str);
}
for (i = 0; i < argval; i++) {
fprintf(codefile,
i != 0 ? ",0%o" : ".data1 0%o",
buf[i]&0377);
}
#endif /* OWNFLOAT */
putc('\n', codefile);
}
#endif /* NOFLOAT */