speeded up a bit for converting 0.0 to string

This commit is contained in:
ceriel 1989-11-27 17:25:55 +00:00
parent b91af798bc
commit 862f83571f
4 changed files with 47 additions and 34 deletions

View file

@ -8,7 +8,7 @@
#include "misc.h" #include "misc.h"
#include <em_arith.h> #include <em_arith.h>
flt_arith2flt(n, e) flt_arith2flt(n, e, uns)
register arith n; register arith n;
register flt_arith *e; register flt_arith *e;
{ {
@ -16,7 +16,7 @@ flt_arith2flt(n, e)
*/ */
register int i; register int i;
if (n < 0) { if (!uns && n < 0) {
e->flt_sign = 1; e->flt_sign = 1;
n = -n; n = -n;
} }
@ -27,16 +27,11 @@ flt_arith2flt(n, e)
return; return;
} }
e->flt_exp = 63; e->flt_exp = 63;
if (n < 0) {
/* n = MINARITH */
n = 0x40000000;
while ((n << 1) > 0) n <<= 1;
e->flt_exp++;
}
for (i = 64; i > 0 && n != 0; i--) { for (i = 64; i > 0 && n != 0; i--) {
flt_b64_sft(&(e->flt_mantissa),1); flt_b64_sft(&(e->flt_mantissa),1);
e->m1 |= (n & 1) << 31; e->m1 |= (n & 1) << 31;
n >>= 1; n = (n >> 1) & ~(0x80 << 8*(sizeof(arith)-1));
} }
if (i > 0) { if (i > 0) {

View file

@ -61,12 +61,14 @@ extern int flt_status;
.B int flt_status; .B int flt_status;
.PP .PP
.B #include <em_arith.h> .B #include <em_arith.h>
.B flt_arith2flt(n, e) .B flt_arith2flt(n, e, uns)
.B arith n; .B arith n;
.B flt_arith *e; .B flt_arith *e;
.B int uns;
.PP .PP
.B arith flt_flt2arith(e) .B arith flt_flt2arith(e, uns)
.B flt_arith *e; .B flt_arith *e;
.B int uns;
.PP .PP
.B flt_b64_sft(m, n) .B flt_b64_sft(m, n)
.B struct flt_mantissa *m; .B struct flt_mantissa *m;
@ -184,14 +186,20 @@ converts the number
.I n .I n
to the floating point format used in this package and returns the result to the floating point format used in this package and returns the result
in in
.IR e . .IR e . If the
.I uns
flag is set, the number
.I n
is regarded as an unsigned.
.PP .PP
.B flt_flt2arith .B flt_flt2arith
truncates the number indicated by truncates the number indicated by
.I e .I e
to the largest integer value smaller than or equal to the number indicated by to the largest integer value smaller than or equal to the number indicated by
.IR e . .IR e .
It returns this value. It returns this value. If the
.I uns
flag is set, the result is to be regarded as unsigned.
.PP .PP
Before each operation, the Before each operation, the
.I flt_status .I flt_status

View file

@ -9,15 +9,21 @@
#include <em_arith.h> #include <em_arith.h>
arith arith
flt_flt2arith(e) flt_flt2arith(e, uns)
register flt_arith *e; register flt_arith *e;
{ {
/* Convert the flt_arith "n" to an arith. /* Convert the flt_arith "n" to an arith.
*/ */
arith n; arith n;
struct flt_mantissa a; struct flt_mantissa a;
register int i;
if (uns) uns = 1;
flt_status = 0; flt_status = 0;
if (e->flt_sign && uns) {
flt_status = FLT_UNFL;
return 0;
}
if (e->flt_exp < 0) { if (e->flt_exp < 0) {
/* absolute value of result < 1. /* absolute value of result < 1.
Return value only depends on sign: Return value only depends on sign:
@ -25,31 +31,33 @@ flt_flt2arith(e)
return -e->flt_sign; return -e->flt_sign;
} }
if (e->flt_exp > (8*sizeof(arith)-2)) { if (e->flt_exp > 8*sizeof(arith)-2 + uns) {
/* probably overflow, but there is one exception: /* probably overflow, but there is one exception:
*/ */
if (e->flt_sign) { if (e->flt_sign &&
n = 0x80; e->flt_exp == 8*sizeof(arith)-1 &&
while (n << 8) n <<= 8;
if (e->flt_exp == 8*sizeof(arith)-1 &&
e->m2 == 0 && e->m2 == 0 &&
e->m1 == 0x80000000) { e->m1 == 0x80000000) {
/* No overflow in this case */ /* No overflow in this case */
flt_status = 0;
} }
else flt_status = FLT_OVFL; else {
return n; flt_status = FLT_OVFL;
e->flt_exp = 8*sizeof(arith)-2 + uns + e->flt_sign;
if (e->flt_sign) {
e->m1 = 0x80000000;
e->m2 = 0;
}
else {
e->m1 = 0xFFFFFFFF;
e->m2 = 0xFFFFFFFF;
} }
n = 0x7F;
while ((n << 8) > 0) {
n <<= 8;
n |= 0xFF;
} }
return n;
} }
a = e->flt_mantissa; a = e->flt_mantissa;
flt_b64_sft(&a, 63-e->flt_exp); flt_b64_sft(&a, 63-e->flt_exp);
n = a.flt_l_32 | ((a.flt_h_32 << 16) << 16); n = a.flt_l_32 | ((a.flt_h_32 << 16) << 16);
/* not << 32; this could be an undefined operation */ /* not << 32; this could be an undefined operation */
return n; return e->flt_sign ? -n : n;
} }

View file

@ -347,7 +347,7 @@ flt_ecvt(e, ndigit, decpt, sign)
(*decpt)++; /* because now value in [1.0, 10.0) */ (*decpt)++; /* because now value in [1.0, 10.0) */
} }
while (p <= pe) { while (p <= pe) {
if (e->flt_exp >= 0) { if (e->flt_exp >= 0 && e->m1 != 0) {
flt_arith x; flt_arith x;
x.m2 = 0; x.flt_exp = e->flt_exp; x.m2 = 0; x.flt_exp = e->flt_exp;
@ -355,9 +355,11 @@ flt_ecvt(e, ndigit, decpt, sign)
x.m1 = (e->m1 >> 1) & 0x7FFFFFFF; x.m1 = (e->m1 >> 1) & 0x7FFFFFFF;
x.m1 = x.m1>>(30-e->flt_exp); x.m1 = x.m1>>(30-e->flt_exp);
*p++ = (x.m1) + '0'; *p++ = (x.m1) + '0';
if (x.m1) {
x.m1 = x.m1 << (31-e->flt_exp); x.m1 = x.m1 << (31-e->flt_exp);
flt_add(e, &x, e); flt_add(e, &x, e);
} }
}
else *p++ = '0'; else *p++ = '0';
flt_mul(e, &s10pow[1], e); flt_mul(e, &s10pow[1], e);
} }