1
0
mirror of https://github.com/golang/go synced 2024-10-03 05:21:22 -06:00

cmd/gc: export constants in hexadecimal

R=golang-dev, r, rsc, iant, remyoudompheng, dave
CC=golang-dev
https://golang.org/cl/6206077
This commit is contained in:
Jan Ziak 2012-05-22 13:53:38 -04:00 committed by Russ Cox
parent 15436da232
commit fbaf59bf1e
4 changed files with 142 additions and 34 deletions

View File

@ -361,6 +361,8 @@ Vconv(Fmt *fp)
switch(v->ctype) { switch(v->ctype) {
case CTINT: case CTINT:
if((fp->flags & FmtSharp) || fmtmode == FExp)
return fmtprint(fp, "%#B", v->u.xval);
return fmtprint(fp, "%B", v->u.xval); return fmtprint(fp, "%B", v->u.xval);
case CTRUNE: case CTRUNE:
x = mpgetfix(v->u.xval); x = mpgetfix(v->u.xval);

View File

@ -1271,6 +1271,8 @@ tnum:
continue; continue;
if(cp == lexbuf+2) if(cp == lexbuf+2)
yyerror("malformed hex constant"); yyerror("malformed hex constant");
if(c == 'p')
goto casep;
goto ncu; goto ncu;
} }
} }

View File

@ -232,16 +232,78 @@ mppow10flt(Mpflt *a, int p)
mpmulcflt(a, 10); mpmulcflt(a, 10);
} }
static void
mphextofix(Mpint *a, char *s, int n)
{
char *hexdigitp, *end, c;
long d;
int bit;
while(*s == '0') {
s++;
n--;
}
// overflow
if(4*n > Mpscale*Mpprec) {
a->ovf = 1;
return;
}
end = s+n-1;
for(hexdigitp=end; hexdigitp>=s; hexdigitp--) {
c = *hexdigitp;
if(c >= '0' && c <= '9')
d = c-'0';
else if(c >= 'A' && c <= 'F')
d = c-'A'+10;
else
d = c-'a'+10;
bit = 4*(end - hexdigitp);
while(d > 0) {
if(d & 1)
a->a[bit/Mpscale] |= (long)1 << (bit%Mpscale);
bit++;
d = d >> 1;
}
}
}
// //
// floating point input // floating point input
// required syntax is [+-]d*[.]d*[e[+-]d*] // required syntax is [+-]d*[.]d*[e[+-]d*] or [+-]0xH*[e[+-]d*]
// //
void void
mpatoflt(Mpflt *a, char *as) mpatoflt(Mpflt *a, char *as)
{ {
Mpflt b; Mpflt b;
int dp, c, f, ef, ex, eb; int dp, c, f, ef, ex, eb, base;
char *s; char *s, *start;
while(*as == ' ' || *as == '\t')
as++;
/* determine base */
s = as;
base = -1;
while(base == -1) {
switch(c = *s++) {
case '-':
case '+':
break;
case '0':
if(*s == 'x')
base = 16;
else
base = 10;
break;
default:
base = 10;
}
}
s = as; s = as;
dp = 0; /* digits after decimal point */ dp = 0; /* digits after decimal point */
@ -250,6 +312,37 @@ mpatoflt(Mpflt *a, char *as)
eb = 0; /* binary point */ eb = 0; /* binary point */
mpmovecflt(a, 0.0); mpmovecflt(a, 0.0);
if(base == 16) {
start = nil;
for(;;) {
c = *s;
if(c == '-') {
f = 1;
s++;
}
else if(c == '+') {
s++;
}
else if(c == '0' && s[1] == 'x') {
s += 2;
start = s;
}
else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
s++;
}
else {
break;
}
}
if(start == nil)
goto bad;
mphextofix(&a->val, start, s-start);
if(a->val.ovf)
goto bad;
a->exp = 0;
mpnorm(a);
}
for(;;) { for(;;) {
switch(c = *s++) { switch(c = *s++) {
default: default:
@ -264,6 +357,8 @@ mpatoflt(Mpflt *a, char *as)
continue; continue;
case '.': case '.':
if(base == 16)
goto bad;
dp = 1; dp = 1;
continue; continue;
@ -355,7 +450,7 @@ void
mpatofix(Mpint *a, char *as) mpatofix(Mpint *a, char *as)
{ {
int c, f; int c, f;
char *s; char *s, *s0;
s = as; s = as;
f = 0; f = 0;
@ -402,28 +497,19 @@ oct:
goto out; goto out;
hex: hex:
c = *s++; s0 = s;
c = *s;
while(c) { while(c) {
if(c >= '0' && c <= '9') { if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
mpmulcfix(a, 16); s++;
mpaddcfix(a, c-'0'); c = *s;
c = *s++;
continue;
}
if(c >= 'a' && c <= 'f') {
mpmulcfix(a, 16);
mpaddcfix(a, c+10-'a');
c = *s++;
continue;
}
if(c >= 'A' && c <= 'F') {
mpmulcfix(a, 16);
mpaddcfix(a, c+10-'A');
c = *s++;
continue; continue;
} }
goto bad; goto bad;
} }
mphextofix(a, s0, s-s0);
if(a->ovf)
goto bad;
out: out:
if(f) if(f)
@ -439,8 +525,8 @@ int
Bconv(Fmt *fp) Bconv(Fmt *fp)
{ {
char buf[500], *p; char buf[500], *p;
Mpint *xval, q, r, ten; Mpint *xval, q, r, ten, sixteen;
int f; int f, digit;
xval = va_arg(fp->args, Mpint*); xval = va_arg(fp->args, Mpint*);
mpmovefixfix(&q, xval); mpmovefixfix(&q, xval);
@ -449,16 +535,34 @@ Bconv(Fmt *fp)
f = 1; f = 1;
mpnegfix(&q); mpnegfix(&q);
} }
mpmovecfix(&ten, 10);
p = &buf[sizeof(buf)]; p = &buf[sizeof(buf)];
*--p = 0; *--p = 0;
if(fp->flags & FmtSharp) {
// Hexadecimal
mpmovecfix(&sixteen, 16);
for(;;) {
mpdivmodfixfix(&q, &r, &q, &sixteen);
digit = mpgetfix(&r);
if(digit < 10)
*--p = digit + '0';
else
*--p = digit - 10 + 'A';
if(mptestfix(&q) <= 0)
break;
}
*--p = 'x';
*--p = '0';
} else {
// Decimal
mpmovecfix(&ten, 10);
for(;;) { for(;;) {
mpdivmodfixfix(&q, &r, &q, &ten); mpdivmodfixfix(&q, &r, &q, &ten);
*--p = mpgetfix(&r) + '0'; *--p = mpgetfix(&r) + '0';
if(mptestfix(&q) <= 0) if(mptestfix(&q) <= 0)
break; break;
} }
}
if(f) if(f)
*--p = '-'; *--p = '-';
return fmtstrcpy(fp, p); return fmtstrcpy(fp, p);
@ -501,10 +605,10 @@ Fconv(Fmt *fp)
} }
if(fv.exp >= 0) { if(fv.exp >= 0) {
snprint(buf, sizeof(buf), "%Bp+%d", &fv.val, fv.exp); snprint(buf, sizeof(buf), "%#Bp+%d", &fv.val, fv.exp);
goto out; goto out;
} }
snprint(buf, sizeof(buf), "%Bp-%d", &fv.val, -fv.exp); snprint(buf, sizeof(buf), "%#Bp-%d", &fv.val, -fv.exp);
out: out:
return fmtstrcpy(fp, buf); return fmtstrcpy(fp, buf);

View File

@ -664,7 +664,7 @@ func (p *gcParser) parseInt() (sign, val string) {
func (p *gcParser) parseNumber() Const { func (p *gcParser) parseNumber() Const {
// mantissa // mantissa
sign, val := p.parseInt() sign, val := p.parseInt()
mant, ok := new(big.Int).SetString(sign+val, 10) mant, ok := new(big.Int).SetString(sign+val, 0)
assert(ok) assert(ok)
if p.lit == "p" { if p.lit == "p" {