1 /* atof_vax.c - turn a Flonum into a VAX floating point number
2 Copyright (C) 1987, 1992, 93, 95, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 static int atof_vax_sizeof PARAMS ((int));
24 static int next_bits PARAMS ((int));
25 static void make_invalid_floating_point_number PARAMS ((LITTLENUM_TYPE *));
26 static int what_kind_of_float PARAMS ((int, int *, long *));
27 static char *atof_vax PARAMS ((char *, int, LITTLENUM_TYPE *));
29 /* Precision in LittleNums. */
30 #define MAX_PRECISION (8)
31 #define H_PRECISION (8)
32 #define G_PRECISION (4)
33 #define D_PRECISION (4)
34 #define F_PRECISION (2)
36 /* Length in LittleNums of guard bits. */
39 int flonum_gen2vax PARAMS ((int format_letter, FLONUM_TYPE * f,
40 LITTLENUM_TYPE * words));
42 /* Number of chars in flonum type 'letter'. */
44 atof_vax_sizeof (letter)
50 * Permitting uppercase letters is probably a bad idea.
51 * Please use only lower-cased letters in case the upper-cased
52 * ones become unsupported!
77 return (return_value);
78 } /* atof_vax_sizeof */
80 static const long mask[] =
118 /* Shared between flonum_gen2vax and next_bits */
119 static int bits_left_in_littlenum;
120 static LITTLENUM_TYPE *littlenum_pointer;
121 static LITTLENUM_TYPE *littlenum_end;
124 next_bits (number_of_bits)
129 if (littlenum_pointer < littlenum_end)
131 if (number_of_bits >= bits_left_in_littlenum)
133 return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
134 number_of_bits -= bits_left_in_littlenum;
135 return_value <<= number_of_bits;
136 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
138 if (littlenum_pointer >= littlenum_end)
139 return_value |= ((*littlenum_pointer) >> (bits_left_in_littlenum)) & mask[number_of_bits];
143 bits_left_in_littlenum -= number_of_bits;
144 return_value = mask[number_of_bits] & ((*littlenum_pointer) >> bits_left_in_littlenum);
146 return (return_value);
150 make_invalid_floating_point_number (words)
151 LITTLENUM_TYPE *words;
153 *words = 0x8000; /* Floating Reserved Operand Code */
156 static int /* 0 means letter is OK. */
157 what_kind_of_float (letter, precisionP, exponent_bitsP)
158 int letter; /* In: lowercase please. What kind of float? */
159 int *precisionP; /* Number of 16-bit words in the float. */
160 long *exponent_bitsP; /* Number of exponent bits. */
162 int retval; /* 0: OK. */
168 *precisionP = F_PRECISION;
173 *precisionP = D_PRECISION;
178 *precisionP = G_PRECISION;
179 *exponent_bitsP = 11;
183 *precisionP = H_PRECISION;
184 *exponent_bitsP = 15;
194 /***********************************************************************\
196 * Warning: this returns 16-bit LITTLENUMs, because that is *
197 * what the VAX thinks in. It is up to the caller to figure *
198 * out any alignment problems and to conspire for the bytes/word *
199 * to be emitted in the right order. Bigendians beware! *
201 \***********************************************************************/
203 static char * /* Return pointer past text consumed. */
204 atof_vax (str, what_kind, words)
205 char *str; /* Text to convert to binary. */
206 int what_kind; /* 'd', 'f', 'g', 'h' */
207 LITTLENUM_TYPE *words; /* Build the binary here. */
210 LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
211 /* Extra bits for zeroed low-order bits. */
212 /* The 1st MAX_PRECISION are zeroed, */
213 /* the last contain flonum bits. */
215 int precision; /* Number of 16-bit words in the format. */
219 f.low = bits + MAX_PRECISION;
225 if (what_kind_of_float (what_kind, &precision, &exponent_bits))
227 return_value = NULL; /* We lost. */
228 make_invalid_floating_point_number (words);
233 memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
235 /* Use more LittleNums than seems */
236 /* necessary: the highest flonum may have */
237 /* 15 leading 0 bits, so could be useless. */
238 f.high = f.low + precision - 1 + GUARD;
240 if (atof_generic (&return_value, ".", "eE", &f))
242 make_invalid_floating_point_number (words);
243 return_value = NULL; /* we lost */
247 if (flonum_gen2vax (what_kind, &f, words))
253 return (return_value);
257 * In: a flonum, a vax floating point format.
258 * Out: a vax floating-point bit pattern.
262 flonum_gen2vax (format_letter, f, words)
263 char format_letter; /* One of 'd' 'f' 'g' 'h'. */
265 LITTLENUM_TYPE *words; /* Deliver answer here. */
270 int return_value; /* 0 == OK. */
272 return_value = what_kind_of_float (format_letter, &precision, &exponent_bits);
274 if (return_value != 0)
276 make_invalid_floating_point_number (words);
280 if (f->low > f->leader)
283 memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
291 int exponent_skippage;
292 LITTLENUM_TYPE word1;
294 /* JF: Deal with new Nan, +Inf and -Inf codes */
295 if (f->sign != '-' && f->sign != '+')
297 make_invalid_floating_point_number (words);
301 * All vaxen floating_point formats (so far) have:
302 * Bit 15 is sign bit.
303 * Bits 14:n are excess-whatever exponent.
304 * Bits n-1:0 (if any) are most significant bits of fraction.
305 * Bits 15:0 of the next word are the next most significant bits.
306 * And so on for each other word.
308 * All this to be compatible with a KF11?? (Which is still faster
309 * than lots of vaxen I can think of, but it also has higher
310 * maintenance costs ... sigh).
312 * So we need: number of bits of exponent, number of bits of
316 #ifdef NEVER /******* This zeroing seems redundant - Dean 3may86 **********/
318 * No matter how few bits we got back from the atof()
319 * routine, add enough zero littlenums so the rest of the
320 * code won't run out of "significant" bits in the mantissa.
324 for (ltp = f->leader + 1;
325 ltp <= f->low + precision;
333 bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
334 littlenum_pointer = f->leader;
335 littlenum_end = f->low;
336 /* Seek (and forget) 1st significant bit */
337 for (exponent_skippage = 0;
339 exponent_skippage++);;
341 exponent_1 = f->exponent + f->leader + 1 - f->low;
342 /* Radix LITTLENUM_RADIX, point just higher than f->leader. */
343 exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
345 exponent_3 = exponent_2 - exponent_skippage;
346 /* Forget leading zeros, forget 1st bit. */
347 exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
348 /* Offset exponent. */
350 if (exponent_4 & ~mask[exponent_bits])
353 * Exponent overflow. Lose immediately.
356 make_invalid_floating_point_number (words);
359 * We leave return_value alone: admit we read the
360 * number, but return a floating exception
361 * because we can't encode the number.
368 /* Word 1. Sign, exponent and perhaps high bits. */
369 /* Assume 2's complement integers. */
370 word1 = (((exponent_4 & mask[exponent_bits]) << (15 - exponent_bits))
371 | ((f->sign == '+') ? 0 : 0x8000)
372 | next_bits (15 - exponent_bits));
375 /* The rest of the words are just mantissa bits. */
376 for (; lp < words + precision; lp++)
378 *lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
384 * Since the NEXT bit is a 1, round UP the mantissa.
385 * The cunning design of these hidden-1 floats permits
386 * us to let the mantissa overflow into the exponent, and
387 * it 'does the right thing'. However, we lose if the
388 * highest-order bit of the lowest-order word flips.
395 #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
396 Please allow at least 1 more bit in carry than is in a LITTLENUM.
397 We need that extra bit to hold a carry during a LITTLENUM carry
398 propagation. Another extra bit (kept 0) will assure us that we
399 don't get a sticky sign bit after shifting right, and that
400 permits us to propagate the carry without any masking of bits.
403 for (carry = 1, lp--;
404 carry && (lp >= words);
409 carry >>= LITTLENUM_NUMBER_OF_BITS;
412 if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
414 make_invalid_floating_point_number (words);
416 * We leave return_value alone: admit we read the
417 * number, but return a floating exception
418 * because we can't encode the number.
421 } /* if (we needed to round up) */
422 } /* if (exponent overflow) */
424 } /* if (float_type was OK) */
425 return (return_value);
426 } /* 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 ? 0 : _("Bad call to md_atof()");
517 /* end of atof-vax.c */