| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#include "gdtoa.h" |
| 53 |
#include <stdio.h> |
| 54 |
#include <stdlib.h> |
| 55 |
#include <string.h> |
| 56 |
|
| 57 |
extern int getround ANSI((int,char*)); |
| 58 |
|
| 59 |
static char ibuf[2048], obuf[2048], obuf1[2048]; |
| 60 |
#undef _0 |
| 61 |
#undef _1 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#ifdef IEEE_MC68k |
| 66 |
#define _0 0 |
| 67 |
#define _1 1 |
| 68 |
#define _2 2 |
| 69 |
#define _3 3 |
| 70 |
#endif |
| 71 |
#ifdef IEEE_8087 |
| 72 |
#define _0 3 |
| 73 |
#define _1 2 |
| 74 |
#define _2 1 |
| 75 |
#define _3 0 |
| 76 |
#endif |
| 77 |
|
| 78 |
#define U (unsigned long) |
| 79 |
|
| 80 |
int |
| 81 |
main(Void) |
| 82 |
{ |
| 83 |
char *s, *s1, *se, *se1; |
| 84 |
int Ltest, i, dItry, ndig = 0, nik, nike, r = 1; |
| 85 |
union { long double d; ULong bits[4]; } u, v[2], w; |
| 86 |
|
| 87 |
w.bits[0] = w.bits[3] = 0; |
| 88 |
w.d = 1.; |
| 89 |
u.d = 3.; |
| 90 |
w.d = w.d / u.d; |
| 91 |
Ltest = sizeof(long double) == 16 && w.bits[0] && w.bits[3]; |
| 92 |
while( (s = fgets(ibuf, sizeof(ibuf), stdin)) !=0) { |
| 93 |
while(*s <= ' ') |
| 94 |
if (!*s++) |
| 95 |
continue; |
| 96 |
dItry = 0; |
| 97 |
switch(*s) { |
| 98 |
case 'r': |
| 99 |
r = getround(r, s); |
| 100 |
continue; |
| 101 |
case 'n': |
| 102 |
i = s[1]; |
| 103 |
if (i <= ' ' || (i >= '0' && i <= '9')) { |
| 104 |
ndig = atoi(s+1); |
| 105 |
continue; |
| 106 |
} |
| 107 |
break; |
| 108 |
case '#': |
| 109 |
|
| 110 |
|
| 111 |
u.bits[_0] = (ULong)strtoul(s1 = s+1, &se, 16); |
| 112 |
if (se > s1) { |
| 113 |
u.bits[_1] = (ULong)strtoul(s1 = se, &se, 16); |
| 114 |
if (se > s1) { |
| 115 |
u.bits[_2] = (ULong)strtoul(s1 = se, &se, 16); |
| 116 |
if (se > s1) |
| 117 |
u.bits[_3] = (ULong)strtoul(s1 = se, &se, 16); |
| 118 |
} |
| 119 |
} |
| 120 |
printf("\nInput: %s", ibuf); |
| 121 |
printf(" --> f = #%lx %lx %lx %lx\n", U u.bits[_0], |
| 122 |
U u.bits[_1], U u.bits[_2], U u.bits[_3]); |
| 123 |
i = 0; |
| 124 |
goto fmt_test; |
| 125 |
} |
| 126 |
dItry = 1; |
| 127 |
printf("\nInput: %s", ibuf); |
| 128 |
i = strtorQ(ibuf, &se, r, u.bits); |
| 129 |
if (r == 1 && (strtopQ(ibuf,&se1,v[0].bits) != i |
| 130 |
|| se != se1 || memcmp(u.bits, v[0].bits, 16))) |
| 131 |
printf("***strtoQ and strtorQ disagree!!\n:"); |
| 132 |
printf("\nstrtoQ consumes %d bytes and returns %d\n", |
| 133 |
(int)(se-ibuf), i); |
| 134 |
printf("with bits = #%lx %lx %lx %lx\n", |
| 135 |
U u.bits[_0], U u.bits[_1], U u.bits[_2], U u.bits[_3]); |
| 136 |
fmt_test: |
| 137 |
if (Ltest) |
| 138 |
printf("printf(\"%%.35Lg\") gives %.35Lg\n", u.d); |
| 139 |
se = g_Qfmt(obuf, u.bits, ndig, sizeof(obuf)); |
| 140 |
printf("g_Qfmt(%d) gives %d bytes: \"%s\"\n\n", ndig, |
| 141 |
(int)(se-obuf), se ? obuf : "<null>"); |
| 142 |
se1 = g_Qfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), 0); |
| 143 |
if (se1 - obuf1 != se - obuf || strcmp(obuf, obuf1)) |
| 144 |
printf("Botch: g_Qfmt_p gives \"%s\" rather than \"%s\"\n", |
| 145 |
obuf1, obuf); |
| 146 |
if (!dItry) |
| 147 |
continue; |
| 148 |
printf("strtoIQ returns %d,", |
| 149 |
strtoIQ(ibuf, &se, v[0].bits, v[1].bits)); |
| 150 |
printf(" consuming %d bytes.\n", (int)(se-ibuf)); |
| 151 |
if (!memcmp(v[0].bits, v[1].bits, 16)) { |
| 152 |
if (!memcpy(u.bits, v[0].bits, 16)) |
| 153 |
printf("fI[0] == fI[1] == strtoQ\n"); |
| 154 |
else { |
| 155 |
printf("fI[0] == fI[1] = #%lx %lx %lx %lx\n", |
| 156 |
U v[0].bits[_0], U v[0].bits[_1], |
| 157 |
U v[0].bits[_2], U v[0].bits[_3]); |
| 158 |
if (Ltest) |
| 159 |
printf("= %.35Lg\n", v[0].d); |
| 160 |
} |
| 161 |
} |
| 162 |
else { |
| 163 |
printf("fI[0] = #%lx %lx %lx %lx\n", |
| 164 |
U v[0].bits[_0], U v[0].bits[_1], |
| 165 |
U v[0].bits[_2], U v[0].bits[_3]); |
| 166 |
if (Ltest) |
| 167 |
printf("= %.35Lg\n", v[0].d); |
| 168 |
printf("fI[1] = #%lx %lx %lx %lx\n", |
| 169 |
U v[1].bits[_0], U v[1].bits[_1], |
| 170 |
U v[1].bits[_2], U v[1].bits[_3]); |
| 171 |
if (Ltest) |
| 172 |
printf("= %.35Lg\n", v[1].d); |
| 173 |
if (!memcmp(v[0].bits, u.bits, 16)) |
| 174 |
printf("fI[0] == strtod\n"); |
| 175 |
else if (!memcmp(v[1].bits, u.bits, 16)) |
| 176 |
printf("fI[1] == strtod\n"); |
| 177 |
else |
| 178 |
printf("**** Both differ from strtod ****\n"); |
| 179 |
} |
| 180 |
printf("\n"); |
| 181 |
switch(i & STRTOG_Retmask) { |
| 182 |
case STRTOG_Infinite: |
| 183 |
for(nik = 0; nik < 6; ++nik) { |
| 184 |
se1 = g_Qfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), nik); |
| 185 |
printf("g_Qfmt_p(...,%d): \"%s\"\n", nik, obuf1); |
| 186 |
} |
| 187 |
break; |
| 188 |
case STRTOG_NaN: |
| 189 |
case STRTOG_NaNbits: |
| 190 |
for(i = 0; i < 3; ++i) |
| 191 |
for(nik = 6*i, nike = nik + 3; nik < nike; ++nik) { |
| 192 |
se1 = g_Qfmt_p(obuf1, u.bits, ndig, sizeof(obuf1), nik); |
| 193 |
printf("g_Qfmt_p(...,%d): \"%s\"\n", nik, obuf1); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
return 0; |
| 198 |
} |