Commit f8797daa authored by Ken Thompson's avatar Ken Thompson

import/export of exact mp floating constants

new syntax for exact mp floating constants
     decimal_int ( "p" | "P" ) [ "+" | "-" ] decimal_int
     the value is decimal1 * 2^decimal2

R=r
OCL=20357
CL=20357
parent 98431dbf
...@@ -105,7 +105,7 @@ dumpexportconst(Sym *s) ...@@ -105,7 +105,7 @@ dumpexportconst(Sym *s)
Bprint(bout, "0x%llux\n", n->val.u.bval); Bprint(bout, "0x%llux\n", n->val.u.bval);
break; break;
case CTFLT: case CTFLT:
Bprint(bout, "%.17e\n", mpgetflt(n->val.u.fval)); Bprint(bout, "%F\n", n->val.u.fval);
break; break;
case CTSTR: case CTSTR:
Bprint(bout, "\"%Z\"\n", n->val.u.sval); Bprint(bout, "\"%Z\"\n", n->val.u.sval);
......
...@@ -567,6 +567,8 @@ void mpshiftfix(Mpint *a, int s); ...@@ -567,6 +567,8 @@ void mpshiftfix(Mpint *a, int s);
/* /*
* mparith3.c * mparith3.c
*/ */
int sigfig(Mpflt *a);
void mpnorm(Mpflt *a);
void mpmovefltflt(Mpflt *a, Mpflt *b); void mpmovefltflt(Mpflt *a, Mpflt *b);
void mpmovecflt(Mpflt *a, double f); void mpmovecflt(Mpflt *a, double f);
int mptestflt(Mpflt *a); int mptestflt(Mpflt *a);
...@@ -576,7 +578,6 @@ void mpdivfltflt(Mpflt *a, Mpflt *b); ...@@ -576,7 +578,6 @@ void mpdivfltflt(Mpflt *a, Mpflt *b);
void mpnegflt(Mpflt *a); void mpnegflt(Mpflt *a);
double mpgetflt(Mpflt *a); double mpgetflt(Mpflt *a);
int Fconv(Fmt*); int Fconv(Fmt*);
void mpnorm(Mpflt *a);
/* /*
* subr.c * subr.c
......
...@@ -742,6 +742,8 @@ dc: ...@@ -742,6 +742,8 @@ dc:
goto casedot; goto casedot;
if(c == 'e' || c == 'E') if(c == 'e' || c == 'E')
goto casee; goto casee;
if(c == 'p' || c == 'P')
goto casep;
ncu: ncu:
*cp = 0; *cp = 0;
...@@ -780,6 +782,22 @@ casee: ...@@ -780,6 +782,22 @@ casee:
*cp++ = c; *cp++ = c;
c = getc(); c = getc();
} }
goto caseout;
casep:
*cp++ = 'p';
c = getc();
if(c == '+' || c == '-') {
*cp++ = c;
c = getc();
}
if(!isdigit(c))
yyerror("malformed fp constant exponent");
while(isdigit(c)) {
*cp++ = c;
c = getc();
}
goto caseout;
caseout: caseout:
*cp = 0; *cp = 0;
......
...@@ -194,13 +194,14 @@ void ...@@ -194,13 +194,14 @@ void
mpatoflt(Mpflt *a, char *as) mpatoflt(Mpflt *a, char *as)
{ {
Mpflt b; Mpflt b;
int dp, c, f, ef, ex, zer; int dp, c, f, ef, ex, eb, zer;
char *s; char *s;
s = as; s = as;
dp = 0; /* digits after decimal point */ dp = 0; /* digits after decimal point */
f = 0; /* sign */ f = 0; /* sign */
ex = 0; /* exponent */ ex = 0; /* exponent */
eb = 0; /* binary point */
zer = 1; /* zero */ zer = 1; /* zero */
mpmovecflt(a, 0.0); mpmovecflt(a, 0.0);
...@@ -239,6 +240,10 @@ mpatoflt(Mpflt *a, char *as) ...@@ -239,6 +240,10 @@ mpatoflt(Mpflt *a, char *as)
dp++; dp++;
continue; continue;
case 'P':
case 'p':
eb = 1;
case 'E': case 'E':
case 'e': case 'e':
ex = 0; ex = 0;
...@@ -266,6 +271,13 @@ mpatoflt(Mpflt *a, char *as) ...@@ -266,6 +271,13 @@ mpatoflt(Mpflt *a, char *as)
break; break;
} }
if(eb) {
if(dp)
goto bad;
a->exp += ex;
goto out;
}
if(dp) if(dp)
dp--; dp--;
if(mpcmpfltc(a, 0.0) != 0) { if(mpcmpfltc(a, 0.0) != 0) {
...@@ -277,6 +289,8 @@ mpatoflt(Mpflt *a, char *as) ...@@ -277,6 +289,8 @@ mpatoflt(Mpflt *a, char *as)
mpdivfltflt(a, &b); mpdivfltflt(a, &b);
} }
} }
out:
if(f) if(f)
mpnegflt(a); mpnegflt(a);
return; return;
...@@ -407,12 +421,30 @@ int ...@@ -407,12 +421,30 @@ int
Fconv(Fmt *fp) Fconv(Fmt *fp)
{ {
char buf[500]; char buf[500];
Mpflt *fval; Mpflt *fvp, fv;
fvp = va_arg(fp->args, Mpflt*);
if(sigfig(fvp) == 0) {
snprint(buf, sizeof(buf), "0p+0");
goto out;
}
fv = *fvp;
while(fv.val.a[0] == 0) {
mpshiftfix(&fv.val, -Mpscale);
fv.exp += Mpscale;
}
while((fv.val.a[0]&1) == 0) {
mpshiftfix(&fv.val, -1);
fv.exp += 1;
}
fval = va_arg(fp->args, Mpflt*); if(fv.exp >= 0) {
if(fval->exp >= 0) snprint(buf, sizeof(buf), "%Bp+%d", &fv.val, fv.exp);
snprint(buf, sizeof(buf), "(%B*2^%d)", &fval->val, fval->exp); goto out;
else }
snprint(buf, sizeof(buf), "(%B/2^%d)", &fval->val, -fval->exp); snprint(buf, sizeof(buf), "%Bp-%d", &fv.val, -fv.exp);
out:
return fmtstrcpy(fp, buf); return fmtstrcpy(fp, buf);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment