1 /* atof_vax.c - turn a Flonum into a VAX floating point number
2 Copyright 1987, 1992, 1993, 1995, 1997, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 static int atof_vax_sizeof PARAMS ((int));
25 static int next_bits PARAMS ((int));
26 static void make_invalid_floating_point_number PARAMS ((LITTLENUM_TYPE *));
27 static int what_kind_of_float PARAMS ((int, int *, long *));
28 static char *atof_vax PARAMS ((char *, int, LITTLENUM_TYPE *));
30 /* Precision in LittleNums. */
31 #define MAX_PRECISION (8)
32 #define H_PRECISION (8)
33 #define G_PRECISION (4)
34 #define D_PRECISION (4)
35 #define F_PRECISION (2)
37 /* Length in LittleNums of guard bits. */
40 int flonum_gen2vax PARAMS ((int format_letter, FLONUM_TYPE * f,
41 LITTLENUM_TYPE * words));
43 /* Number of chars in flonum type 'letter'. */
45 atof_vax_sizeof (letter)
51 * Permitting uppercase letters is probably a bad idea.
52 * Please use only lower-cased letters in case the upper-cased
53 * ones become unsupported!
78 return (return_value);
79 } /* atof_vax_sizeof */
81 static const long mask[] =
119 /* Shared between flonum_gen2vax and next_bits */
120 static int bits_left_in_littlenum;
121 static LITTLENUM_TYPE *littlenum_pointer;
122 static LITTLENUM_TYPE *littlenum_end;
125 next_bits (number_of_bits)
130 if (littlenum_pointer < littlenum_end)
132 if (number_of_bits >= bits_left_in_littlenum)
134 return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
135 number_of_bits -= bits_left_in_littlenum;
136 return_value <<= number_of_bits;
137 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
139 if (littlenum_pointer >= littlenum_end)
140 return_value |= ((*littlenum_pointer) >> (bits_left_in_littlenum)) & mask[number_of_bits];
144 bits_left_in_littlenum -= number_of_bits;
145 return_value = mask[number_of_bits] & ((*littlenum_pointer) >> bits_left_in_littlenum);
147 return (return_value);
151 make_invalid_floating_point_number (words)
152 LITTLENUM_TYPE *words;
154 *words = 0x8000; /* Floating Reserved Operand Code */
157 static int /* 0 means letter is OK. */
158 what_kind_of_float (letter, precisionP, exponent_bitsP)
159 int letter; /* In: lowercase please. What kind of float? */
160 int *precisionP; /* Number of 16-bit words in the float. */
161 long *exponent_bitsP; /* Number of exponent bits. */
163 int retval; /* 0: OK. */
169 *precisionP = F_PRECISION;
174 *precisionP = D_PRECISION;
179 *precisionP = G_PRECISION;
180 *exponent_bitsP = 11;
184 *precisionP = H_PRECISION;
185 *exponent_bitsP = 15;
195 /***********************************************************************\
197 * Warning: this returns 16-bit LITTLENUMs, because that is *
198 * what the VAX thinks in. It is up to the caller to figure *
199 * out any alignment problems and to conspire for the bytes/word *
200 * to be emitted in the right order. Bigendians beware! *
202 \***********************************************************************/
204 static char * /* Return pointer past text consumed. */
205 atof_vax (str, what_kind, words)
206 char *str; /* Text to convert to binary. */
207 int what_kind; /* 'd', 'f', 'g', 'h' */
208 LITTLENUM_TYPE *words; /* Build the binary here. */
211 LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
212 /* Extra bits for zeroed low-order bits. */
213 /* The 1st MAX_PRECISION are zeroed, */
214 /* the last contain flonum bits. */
216 int precision; /* Number of 16-bit words in the format. */
220 f.low = bits + MAX_PRECISION;
226 if (what_kind_of_float (what_kind, &precision, &exponent_bits))
228 return_value = NULL; /* We lost. */
229 make_invalid_floating_point_number (words);
234 memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
236 /* Use more LittleNums than seems */
237 /* necessary: the highest flonum may have */
238 /* 15 leading 0 bits, so could be useless. */
239 f.high = f.low + precision - 1 + GUARD;
241 if (atof_generic (&return_value, ".", "eE", &f))
243 make_invalid_floating_point_number (words);
244 return_value = NULL; /* we lost */
248 if (flonum_gen2vax (what_kind, &f, words))
254 return (return_value);
258 * In: a flonum, a vax floating point format.
259 * Out: a vax floating-point bit pattern.
263 flonum_gen2vax (format_letter, f, words)
264 int format_letter; /* One of 'd' 'f' 'g' 'h'. */
266 LITTLENUM_TYPE *words; /* Deliver answer here. */
271 int return_value; /* 0 == OK. */
273 return_value = what_kind_of_float (format_letter, &precision, &exponent_bits);
275 if (return_value != 0)
277 make_invalid_floating_point_number (words);
281 if (f->low > f->leader)
284 memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
292 int exponent_skippage;
293 LITTLENUM_TYPE word1;
295 /* JF: Deal with new Nan, +Inf and -Inf codes */
296 if (f->sign != '-' && f->sign != '+')
298 make_invalid_floating_point_number (words);
302 * All vaxen floating_point formats (so far) have:
303 * Bit 15 is sign bit.
304 * Bits 14:n are excess-whatever exponent.
305 * Bits n-1:0 (if any) are most significant bits of fraction.
306 * Bits 15:0 of the next word are the next most significant bits.
307 * And so on for each other word.
309 * All this to be compatible with a KF11?? (Which is still faster
310 * than lots of vaxen I can think of, but it also has higher
311 * maintenance costs ... sigh).
313 * So we need: number of bits of exponent, number of bits of
317 #ifdef NEVER /******* This zeroing seems redundant - Dean 3may86 **********/
319 * No matter how few bits we got back from the atof()
320 * routine, add enough zero littlenums so the rest of the
321 * code won't run out of "significant" bits in the mantissa.
325 for (ltp = f->leader + 1;
326 ltp <= f->low + precision;
334 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
335 littlenum_pointer = f->leader;
336 littlenum_end = f->low;
337 /* Seek (and forget) 1st significant bit */
338 for (exponent_skippage = 0;
340 exponent_skippage++);;
342 exponent_1 = f->exponent + f->leader + 1 - f->low;
343 /* Radix LITTLENUM_RADIX, point just higher than f->leader. */
344 exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
346 exponent_3 = exponent_2 - exponent_skippage;
347 /* Forget leading zeros, forget 1st bit. */
348 exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
349 /* Offset exponent. */
351 if (exponent_4 & ~mask[exponent_bits])
354 * Exponent overflow. Lose immediately.
357 make_invalid_floating_point_number (words);
360 * We leave return_value alone: admit we read the
361 * number, but return a floating exception
362 * because we can't encode the number.
369 /* Word 1. Sign, exponent and perhaps high bits. */
370 /* Assume 2's complement integers. */
371 word1 = (((exponent_4 & mask[exponent_bits]) << (15 - exponent_bits))
372 | ((f->sign == '+') ? 0 : 0x8000)
373 | next_bits (15 - exponent_bits));
376 /* The rest of the words are just mantissa bits. */
377 for (; lp < words + precision; lp++)
379 *lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
385 * Since the NEXT bit is a 1, round UP the mantissa.
386 * The cunning design of these hidden-1 floats permits
387 * us to let the mantissa overflow into the exponent, and
388 * it 'does the right thing'. However, we lose if the
389 * highest-order bit of the lowest-order word flips.
396 #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
397 Please allow at least 1 more bit in carry than is in a LITTLENUM.
398 We need that extra bit to hold a carry during a LITTLENUM carry
399 propagation. Another extra bit (kept 0) will assure us that we
400 don't get a sticky sign bit after shifting right, and that
401 permits us to propagate the carry without any masking of bits.
404 for (carry = 1, lp--;
405 carry && (lp >= words);
410 carry >>= LITTLENUM_NUMBER_OF_BITS;
413 if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
415 make_invalid_floating_point_number (words);
417 * We leave return_value alone: admit we read the
418 * number, but return a floating exception
419 * because we can't encode the number.
422 } /* if (we needed to round up) */
423 } /* if (exponent overflow) */
425 } /* if (float_type was OK) */
426 return (return_value);
427 } /* flonum_gen2vax() */
429 /* JF this used to be in vax.c but this looks like a better place for it */
434 * In: input_line_pointer->the 1st character of a floating-point
436 * 1 letter denoting the type of statement that wants a
437 * binary floating point number returned.
438 * Address of where to build floating point literal.
439 * Assumed to be 'big enough'.
440 * Address of where to return size of literal (in chars).
442 * Out: Input_line_pointer->of next char after floating number.
443 * Error message, or 0.
444 * Floating point literal.
445 * Number of chars we used for the literal.
448 #define MAXIMUM_NUMBER_OF_LITTLENUMS (8) /* For .hfloats. */
451 md_atof (what_statement_type, literalP, sizeP)
452 int what_statement_type;
456 LITTLENUM_TYPE words[MAXIMUM_NUMBER_OF_LITTLENUMS];
457 register char kind_of_float;
458 register int number_of_chars;
459 register LITTLENUM_TYPE *littlenumP;
461 switch (what_statement_type)
463 case 'F': /* .float */
464 case 'f': /* .ffloat */
468 case 'D': /* .double */
469 case 'd': /* .dfloat */
473 case 'g': /* .gfloat */
477 case 'h': /* .hfloat */
488 register LITTLENUM_TYPE *limit;
490 input_line_pointer = atof_vax (input_line_pointer,
494 * The atof_vax() builds up 16-bit numbers.
495 * Since the assembler may not be running on
496 * a little-endian machine, be very careful about
497 * converting words to chars.
499 number_of_chars = atof_vax_sizeof (kind_of_float);
500 know (number_of_chars <= MAXIMUM_NUMBER_OF_LITTLENUMS * sizeof (LITTLENUM_TYPE));
501 limit = words + (number_of_chars / sizeof (LITTLENUM_TYPE));
502 for (littlenumP = words; littlenumP < limit; littlenumP++)
504 md_number_to_chars (literalP, *littlenumP, sizeof (LITTLENUM_TYPE));
505 literalP += sizeof (LITTLENUM_TYPE);
513 *sizeP = number_of_chars;
514 return kind_of_float ? NULL : _("Bad call to md_atof()");
517 /* end of atof-vax.c */