| 1 | /* atof_tahoe.c - turn a string into a Tahoe floating point number
|
|---|
| 2 | Copyright 1987, 1993, 2000 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | /* This is really a simplified version of atof_vax.c. I glommed it wholesale
|
|---|
| 5 | and then shaved it down. I don't even know how it works. (Don't you find
|
|---|
| 6 | my honesty refreshing? Devon E Bowen <bowen@cs.buffalo.edu>
|
|---|
| 7 |
|
|---|
| 8 | I don't allow uppercase letters in the precision descrpitors.
|
|---|
| 9 | i.e. 'f' and 'd' are allowed but 'F' and 'D' aren't. */
|
|---|
| 10 |
|
|---|
| 11 | #include "as.h"
|
|---|
| 12 |
|
|---|
| 13 | /* Precision in LittleNums. */
|
|---|
| 14 | #define MAX_PRECISION (4)
|
|---|
| 15 | #define D_PRECISION (4)
|
|---|
| 16 | #define F_PRECISION (2)
|
|---|
| 17 |
|
|---|
| 18 | /* Precision in chars. */
|
|---|
| 19 | #define D_PRECISION_CHARS (8)
|
|---|
| 20 | #define F_PRECISION_CHARS (4)
|
|---|
| 21 |
|
|---|
| 22 | /* Length in LittleNums of guard bits. */
|
|---|
| 23 | #define GUARD (2)
|
|---|
| 24 |
|
|---|
| 25 | static const long int mask[] =
|
|---|
| 26 | {
|
|---|
| 27 | 0x00000000,
|
|---|
| 28 | 0x00000001,
|
|---|
| 29 | 0x00000003,
|
|---|
| 30 | 0x00000007,
|
|---|
| 31 | 0x0000000f,
|
|---|
| 32 | 0x0000001f,
|
|---|
| 33 | 0x0000003f,
|
|---|
| 34 | 0x0000007f,
|
|---|
| 35 | 0x000000ff,
|
|---|
| 36 | 0x000001ff,
|
|---|
| 37 | 0x000003ff,
|
|---|
| 38 | 0x000007ff,
|
|---|
| 39 | 0x00000fff,
|
|---|
| 40 | 0x00001fff,
|
|---|
| 41 | 0x00003fff,
|
|---|
| 42 | 0x00007fff,
|
|---|
| 43 | 0x0000ffff,
|
|---|
| 44 | 0x0001ffff,
|
|---|
| 45 | 0x0003ffff,
|
|---|
| 46 | 0x0007ffff,
|
|---|
| 47 | 0x000fffff,
|
|---|
| 48 | 0x001fffff,
|
|---|
| 49 | 0x003fffff,
|
|---|
| 50 | 0x007fffff,
|
|---|
| 51 | 0x00ffffff,
|
|---|
| 52 | 0x01ffffff,
|
|---|
| 53 | 0x03ffffff,
|
|---|
| 54 | 0x07ffffff,
|
|---|
| 55 | 0x0fffffff,
|
|---|
| 56 | 0x1fffffff,
|
|---|
| 57 | 0x3fffffff,
|
|---|
| 58 | 0x7fffffff,
|
|---|
| 59 | 0xffffffff
|
|---|
| 60 | };
|
|---|
| 61 | |
|---|
| 62 |
|
|---|
| 63 | /* Shared between flonum_gen2tahoe and next_bits. */
|
|---|
| 64 | static int bits_left_in_littlenum;
|
|---|
| 65 | static LITTLENUM_TYPE *littlenum_pointer;
|
|---|
| 66 | static LITTLENUM_TYPE *littlenum_end;
|
|---|
| 67 |
|
|---|
| 68 | #if __STDC__ == 1
|
|---|
| 69 |
|
|---|
| 70 | int flonum_gen2tahoe (int format_letter, FLONUM_TYPE * f,
|
|---|
| 71 | LITTLENUM_TYPE * words);
|
|---|
| 72 |
|
|---|
| 73 | #else /* not __STDC__ */
|
|---|
| 74 |
|
|---|
| 75 | int flonum_gen2tahoe ();
|
|---|
| 76 |
|
|---|
| 77 | #endif /* not __STDC__ */
|
|---|
| 78 |
|
|---|
| 79 | static int
|
|---|
| 80 | next_bits (number_of_bits)
|
|---|
| 81 | int number_of_bits;
|
|---|
| 82 | {
|
|---|
| 83 | int return_value;
|
|---|
| 84 |
|
|---|
| 85 | if (littlenum_pointer < littlenum_end)
|
|---|
| 86 | return 0;
|
|---|
| 87 | if (number_of_bits >= bits_left_in_littlenum)
|
|---|
| 88 | {
|
|---|
| 89 | return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
|
|---|
| 90 | number_of_bits -= bits_left_in_littlenum;
|
|---|
| 91 | return_value <<= number_of_bits;
|
|---|
| 92 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
|
|---|
| 93 | littlenum_pointer--;
|
|---|
| 94 | if (littlenum_pointer >= littlenum_end)
|
|---|
| 95 | return_value |= ((*littlenum_pointer) >> (bits_left_in_littlenum)) &
|
|---|
| 96 | mask[number_of_bits];
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | bits_left_in_littlenum -= number_of_bits;
|
|---|
| 101 | return_value = mask[number_of_bits] &
|
|---|
| 102 | ((*littlenum_pointer) >> bits_left_in_littlenum);
|
|---|
| 103 | }
|
|---|
| 104 | return return_value;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | static void
|
|---|
| 108 | make_invalid_floating_point_number (words)
|
|---|
| 109 | LITTLENUM_TYPE *words;
|
|---|
| 110 | {
|
|---|
| 111 | /* Floating Reserved Operand Code. */
|
|---|
| 112 | *words = 0x8000;
|
|---|
| 113 | }
|
|---|
| 114 | |
|---|
| 115 |
|
|---|
| 116 | static int /* 0 means letter is OK. */
|
|---|
| 117 | what_kind_of_float (letter, precisionP, exponent_bitsP)
|
|---|
| 118 | /* In: lowercase please. What kind of float? */
|
|---|
| 119 | char letter;
|
|---|
| 120 |
|
|---|
| 121 | /* Number of 16-bit words in the float. */
|
|---|
| 122 | int *precisionP;
|
|---|
| 123 |
|
|---|
| 124 | /* Number of exponent bits. */
|
|---|
| 125 | long int *exponent_bitsP;
|
|---|
| 126 | {
|
|---|
| 127 | int retval; /* 0: OK. */
|
|---|
| 128 |
|
|---|
| 129 | retval = 0;
|
|---|
| 130 | switch (letter)
|
|---|
| 131 | {
|
|---|
| 132 | case 'f':
|
|---|
| 133 | *precisionP = F_PRECISION;
|
|---|
| 134 | *exponent_bitsP = 8;
|
|---|
| 135 | break;
|
|---|
| 136 |
|
|---|
| 137 | case 'd':
|
|---|
| 138 | *precisionP = D_PRECISION;
|
|---|
| 139 | *exponent_bitsP = 8;
|
|---|
| 140 | break;
|
|---|
| 141 |
|
|---|
| 142 | default:
|
|---|
| 143 | retval = 69;
|
|---|
| 144 | break;
|
|---|
| 145 | }
|
|---|
| 146 | return (retval);
|
|---|
| 147 | }
|
|---|
| 148 | |
|---|
| 149 |
|
|---|
| 150 | /* Warning: This returns 16-bit LITTLENUMs, because that is what the
|
|---|
| 151 | VAX thinks in. It is up to the caller to figure out any alignment
|
|---|
| 152 | problems and to conspire for the bytes/word to be emitted in the
|
|---|
| 153 | right order. Bigendians beware! */
|
|---|
| 154 |
|
|---|
| 155 | char * /* Return pointer past text consumed. */
|
|---|
| 156 | atof_tahoe (str, what_kind, words)
|
|---|
| 157 | char *str; /* Text to convert to binary. */
|
|---|
| 158 | char what_kind; /* 'd', 'f', 'g', 'h' */
|
|---|
| 159 | LITTLENUM_TYPE *words; /* Build the binary here. */
|
|---|
| 160 | {
|
|---|
| 161 | FLONUM_TYPE f;
|
|---|
| 162 | LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
|
|---|
| 163 | /* Extra bits for zeroed low-order bits. */
|
|---|
| 164 | /* The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */
|
|---|
| 165 | char *return_value;
|
|---|
| 166 | int precision; /* Number of 16-bit words in the format. */
|
|---|
| 167 | long int exponent_bits;
|
|---|
| 168 |
|
|---|
| 169 | return_value = str;
|
|---|
| 170 | f.low = bits + MAX_PRECISION;
|
|---|
| 171 | f.high = NULL;
|
|---|
| 172 | f.leader = NULL;
|
|---|
| 173 | f.exponent = NULL;
|
|---|
| 174 | f.sign = '\0';
|
|---|
| 175 |
|
|---|
| 176 | if (what_kind_of_float (what_kind, &precision, &exponent_bits))
|
|---|
| 177 | {
|
|---|
| 178 | /* We lost. */
|
|---|
| 179 | return_value = NULL;
|
|---|
| 180 | make_invalid_floating_point_number (words);
|
|---|
| 181 | }
|
|---|
| 182 | if (return_value)
|
|---|
| 183 | {
|
|---|
| 184 | memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
|
|---|
| 185 |
|
|---|
| 186 | /* Use more LittleNums than seems necessary:
|
|---|
| 187 | the highest flonum may have 15 leading 0 bits, so could be
|
|---|
| 188 | useless. */
|
|---|
| 189 | f.high = f.low + precision - 1 + GUARD;
|
|---|
| 190 |
|
|---|
| 191 | if (atof_generic (&return_value, ".", "eE", &f))
|
|---|
| 192 | {
|
|---|
| 193 | make_invalid_floating_point_number (words);
|
|---|
| 194 | /* We lost. */
|
|---|
| 195 | return_value = NULL;
|
|---|
| 196 | }
|
|---|
| 197 | else
|
|---|
| 198 | {
|
|---|
| 199 | if (flonum_gen2tahoe (what_kind, &f, words))
|
|---|
| 200 | return_value = NULL;
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | return return_value;
|
|---|
| 204 | }
|
|---|
| 205 | |
|---|
| 206 |
|
|---|
| 207 | /* In: a flonum, a Tahoe floating point format.
|
|---|
| 208 | Out: a Tahoe floating-point bit pattern. */
|
|---|
| 209 |
|
|---|
| 210 | int /* 0: OK. */
|
|---|
| 211 | flonum_gen2tahoe (format_letter, f, words)
|
|---|
| 212 | char format_letter; /* One of 'd' 'f'. */
|
|---|
| 213 | FLONUM_TYPE *f;
|
|---|
| 214 | LITTLENUM_TYPE *words; /* Deliver answer here. */
|
|---|
| 215 | {
|
|---|
| 216 | LITTLENUM_TYPE *lp;
|
|---|
| 217 | int precision;
|
|---|
| 218 | long int exponent_bits;
|
|---|
| 219 | int return_value; /* 0 == OK. */
|
|---|
| 220 |
|
|---|
| 221 | return_value =
|
|---|
| 222 | what_kind_of_float (format_letter, &precision, &exponent_bits);
|
|---|
| 223 | if (return_value != 0)
|
|---|
| 224 | {
|
|---|
| 225 | make_invalid_floating_point_number (words);
|
|---|
| 226 | }
|
|---|
| 227 | else
|
|---|
| 228 | {
|
|---|
| 229 | if (f->low > f->leader)
|
|---|
| 230 | {
|
|---|
| 231 | /* 0.0e0 seen. */
|
|---|
| 232 | memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
|
|---|
| 233 | }
|
|---|
| 234 | else
|
|---|
| 235 | {
|
|---|
| 236 | long int exponent_1;
|
|---|
| 237 | long int exponent_2;
|
|---|
| 238 | long int exponent_3;
|
|---|
| 239 | long int exponent_4;
|
|---|
| 240 | int exponent_skippage;
|
|---|
| 241 | LITTLENUM_TYPE word1;
|
|---|
| 242 |
|
|---|
| 243 | /* JF: Deal with new Nan, +Inf and -Inf codes. */
|
|---|
| 244 | if (f->sign != '-' && f->sign != '+')
|
|---|
| 245 | {
|
|---|
| 246 | make_invalid_floating_point_number (words);
|
|---|
| 247 | return return_value;
|
|---|
| 248 | }
|
|---|
| 249 | /* All tahoe floating_point formats have:
|
|---|
| 250 | Bit 15 is sign bit.
|
|---|
| 251 | Bits 14:n are excess-whatever exponent.
|
|---|
| 252 | Bits n-1:0 (if any) are most significant bits of fraction.
|
|---|
| 253 | Bits 15:0 of the next word are the next most significant bits.
|
|---|
| 254 | And so on for each other word.
|
|---|
| 255 |
|
|---|
| 256 | So we need: number of bits of exponent, number of bits of
|
|---|
| 257 | mantissa. */
|
|---|
| 258 |
|
|---|
| 259 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
|
|---|
| 260 | littlenum_pointer = f->leader;
|
|---|
| 261 | littlenum_end = f->low;
|
|---|
| 262 |
|
|---|
| 263 | /* Seek (and forget) 1st significant bit. */
|
|---|
| 264 | for (exponent_skippage = 0;
|
|---|
| 265 | !next_bits (1);
|
|---|
| 266 | exponent_skippage++)
|
|---|
| 267 | ;
|
|---|
| 268 |
|
|---|
| 269 | exponent_1 = f->exponent + f->leader + 1 - f->low;
|
|---|
| 270 |
|
|---|
| 271 | /* Radix LITTLENUM_RADIX, point just higher than f -> leader. */
|
|---|
| 272 | exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
|
|---|
| 273 |
|
|---|
| 274 | /* Radix 2. */
|
|---|
| 275 | exponent_3 = exponent_2 - exponent_skippage;
|
|---|
| 276 |
|
|---|
| 277 | /* Forget leading zeros, forget 1st bit. */
|
|---|
| 278 | exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
|
|---|
| 279 |
|
|---|
| 280 | /* Offset exponent. */
|
|---|
| 281 |
|
|---|
| 282 | if (exponent_4 & ~mask[exponent_bits])
|
|---|
| 283 | {
|
|---|
| 284 | /* Exponent overflow. Lose immediately. */
|
|---|
| 285 |
|
|---|
| 286 | make_invalid_floating_point_number (words);
|
|---|
| 287 |
|
|---|
| 288 | /* We leave return_value alone: admit we read the
|
|---|
| 289 | number, but return a floating exception because we
|
|---|
| 290 | can't encode the number. */
|
|---|
| 291 | }
|
|---|
| 292 | else
|
|---|
| 293 | {
|
|---|
| 294 | lp = words;
|
|---|
| 295 |
|
|---|
| 296 | /* Word 1. Sign, exponent and perhaps high bits. */
|
|---|
| 297 | /* Assume 2's complement integers. */
|
|---|
| 298 | word1 = ((exponent_4 & mask[exponent_bits])
|
|---|
| 299 | << (15 - exponent_bits))
|
|---|
| 300 | | ((f->sign == '+') ? 0 : 0x8000)
|
|---|
| 301 | | next_bits (15 - exponent_bits);
|
|---|
| 302 | *lp++ = word1;
|
|---|
| 303 |
|
|---|
| 304 | /* The rest of the words are just mantissa bits. */
|
|---|
| 305 | for (; lp < words + precision; lp++)
|
|---|
| 306 | *lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
|
|---|
| 307 |
|
|---|
| 308 | if (next_bits (1))
|
|---|
| 309 | {
|
|---|
| 310 | /* Since the NEXT bit is a 1, round UP the mantissa.
|
|---|
| 311 | The cunning design of these hidden-1 floats permits
|
|---|
| 312 | us to let the mantissa overflow into the exponent, and
|
|---|
| 313 | it 'does the right thing'. However, we lose if the
|
|---|
| 314 | highest-order bit of the lowest-order word flips.
|
|---|
| 315 | Is that clear? */
|
|---|
| 316 |
|
|---|
| 317 | unsigned long int carry;
|
|---|
| 318 |
|
|---|
| 319 | /* #if (sizeof(carry)) < ((sizeof(bits[0]) *
|
|---|
| 320 | BITS_PER_CHAR) + 2) Please allow at least 1 more
|
|---|
| 321 | bit in carry than is in a LITTLENUM. We need
|
|---|
| 322 | that extra bit to hold a carry during a LITTLENUM
|
|---|
| 323 | carry propagation. Another extra bit (kept 0)
|
|---|
| 324 | will assure us that we don't get a sticky sign
|
|---|
| 325 | bit after shifting right, and that permits us to
|
|---|
| 326 | propagate the carry without any masking of bits.
|
|---|
| 327 | #endif */
|
|---|
| 328 | for (carry = 1, lp--;
|
|---|
| 329 | carry && (lp >= words);
|
|---|
| 330 | lp--)
|
|---|
| 331 | {
|
|---|
| 332 | carry = *lp + carry;
|
|---|
| 333 | *lp = carry;
|
|---|
| 334 | carry >>= LITTLENUM_NUMBER_OF_BITS;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | if ((word1 ^ *words)
|
|---|
| 338 | & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
|
|---|
| 339 | {
|
|---|
| 340 | make_invalid_floating_point_number (words);
|
|---|
| 341 | /* We leave return_value alone: admit we read
|
|---|
| 342 | the number, but return a floating exception
|
|---|
| 343 | because we can't encode the number. */
|
|---|
| 344 | }
|
|---|
| 345 | } /* if (we needed to round up) */
|
|---|
| 346 | } /* if (exponent overflow) */
|
|---|
| 347 | } /* if (0.0e0) */
|
|---|
| 348 | } /* if (float_type was OK) */
|
|---|
| 349 | return return_value;
|
|---|
| 350 | }
|
|---|
| 351 | |
|---|
| 352 |
|
|---|
| 353 | /* In: input_line_pointer -> the 1st character of a floating-point
|
|---|
| 354 | * number.
|
|---|
| 355 | * 1 letter denoting the type of statement that wants a
|
|---|
| 356 | * binary floating point number returned.
|
|---|
| 357 | * Address of where to build floating point literal.
|
|---|
| 358 | * Assumed to be 'big enough'.
|
|---|
| 359 | * Address of where to return size of literal (in chars).
|
|---|
| 360 | *
|
|---|
| 361 | * Out: Input_line_pointer -> of next char after floating number.
|
|---|
| 362 | * Error message, or 0.
|
|---|
| 363 | * Floating point literal.
|
|---|
| 364 | * Number of chars we used for the literal. */
|
|---|
| 365 |
|
|---|
| 366 | char *
|
|---|
| 367 | md_atof (what_statement_type, literalP, sizeP)
|
|---|
| 368 | char what_statement_type;
|
|---|
| 369 | char *literalP;
|
|---|
| 370 | int *sizeP;
|
|---|
| 371 | {
|
|---|
| 372 | LITTLENUM_TYPE words[MAX_PRECISION];
|
|---|
| 373 | register char kind_of_float;
|
|---|
| 374 | register int number_of_chars;
|
|---|
| 375 | register LITTLENUM_TYPE *littlenum_pointer;
|
|---|
| 376 |
|
|---|
| 377 | switch (what_statement_type)
|
|---|
| 378 | {
|
|---|
| 379 | case 'f': /* .ffloat */
|
|---|
| 380 | case 'd': /* .dfloat */
|
|---|
| 381 | kind_of_float = what_statement_type;
|
|---|
| 382 | break;
|
|---|
| 383 |
|
|---|
| 384 | default:
|
|---|
| 385 | kind_of_float = 0;
|
|---|
| 386 | break;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | if (kind_of_float)
|
|---|
| 390 | {
|
|---|
| 391 | register LITTLENUM_TYPE *limit;
|
|---|
| 392 |
|
|---|
| 393 | input_line_pointer = atof_tahoe (input_line_pointer,
|
|---|
| 394 | kind_of_float,
|
|---|
| 395 | words);
|
|---|
| 396 | /* The atof_tahoe() builds up 16-bit numbers.
|
|---|
| 397 | Since the assembler may not be running on
|
|---|
| 398 | a different-endian machine, be very careful about
|
|---|
| 399 | converting words to chars. */
|
|---|
| 400 | number_of_chars = (kind_of_float == 'f' ? F_PRECISION_CHARS :
|
|---|
| 401 | (kind_of_float == 'd' ? D_PRECISION_CHARS : 0));
|
|---|
| 402 | know (number_of_chars <= MAX_PRECISION * sizeof (LITTLENUM_TYPE));
|
|---|
| 403 | limit = words + (number_of_chars / sizeof (LITTLENUM_TYPE));
|
|---|
| 404 | for (littlenum_pointer = words;
|
|---|
| 405 | littlenum_pointer < limit;
|
|---|
| 406 | littlenum_pointer++)
|
|---|
| 407 | {
|
|---|
| 408 | md_number_to_chars (literalP, *littlenum_pointer,
|
|---|
| 409 | sizeof (LITTLENUM_TYPE));
|
|---|
| 410 | literalP += sizeof (LITTLENUM_TYPE);
|
|---|
| 411 | }
|
|---|
| 412 | }
|
|---|
| 413 | else
|
|---|
| 414 | {
|
|---|
| 415 | number_of_chars = 0;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | *sizeP = number_of_chars;
|
|---|
| 419 | return kind_of_float ? 0 : _("Bad call to md_atof()");
|
|---|
| 420 | }
|
|---|