1 /* atof_vax.c - turn a Flonum into a VAX floating point number
2 Copyright (C) 1987-2015 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* Precision in LittleNums. */
24 #define MAX_PRECISION 8
30 /* Length in LittleNums of guard bits. */
33 int flonum_gen2vax (int, FLONUM_TYPE
*, LITTLENUM_TYPE
*);
35 /* Number of chars in flonum type 'letter'. */
38 atof_vax_sizeof (int letter
)
42 /* Permitting uppercase letters is probably a bad idea.
43 Please use only lower-cased letters in case the upper-cased
44 ones become unsupported! */
72 static const long mask
[] =
110 /* Shared between flonum_gen2vax and next_bits. */
111 static int bits_left_in_littlenum
;
112 static LITTLENUM_TYPE
*littlenum_pointer
;
113 static LITTLENUM_TYPE
*littlenum_end
;
116 next_bits (int number_of_bits
)
120 if (littlenum_pointer
< littlenum_end
)
122 if (number_of_bits
>= bits_left_in_littlenum
)
124 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
125 number_of_bits
-= bits_left_in_littlenum
;
126 return_value
<<= number_of_bits
;
127 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
129 if (littlenum_pointer
>= littlenum_end
)
130 return_value
|= ((*littlenum_pointer
) >> (bits_left_in_littlenum
)) & mask
[number_of_bits
];
134 bits_left_in_littlenum
-= number_of_bits
;
135 return_value
= mask
[number_of_bits
] & ((*littlenum_pointer
) >> bits_left_in_littlenum
);
141 make_invalid_floating_point_number (LITTLENUM_TYPE
*words
)
143 *words
= 0x8000; /* Floating Reserved Operand Code. */
147 static int /* 0 means letter is OK. */
148 what_kind_of_float (int letter
, /* In: lowercase please. What kind of float? */
149 int *precisionP
, /* Number of 16-bit words in the float. */
150 long *exponent_bitsP
) /* Number of exponent bits. */
158 *precisionP
= F_PRECISION
;
163 *precisionP
= D_PRECISION
;
168 *precisionP
= G_PRECISION
;
169 *exponent_bitsP
= 11;
173 *precisionP
= H_PRECISION
;
174 *exponent_bitsP
= 15;
184 /* Warning: this returns 16-bit LITTLENUMs, because that is
185 what the VAX thinks in. It is up to the caller to figure
186 out any alignment problems and to conspire for the bytes/word
187 to be emitted in the right order. Bigendians beware! */
190 atof_vax (char *str
, /* Text to convert to binary. */
191 int what_kind
, /* 'd', 'f', 'g', 'h' */
192 LITTLENUM_TYPE
*words
) /* Build the binary here. */
195 LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
196 /* Extra bits for zeroed low-order bits.
197 The 1st MAX_PRECISION are zeroed,
198 the last contain flonum bits. */
200 int precision
; /* Number of 16-bit words in the format. */
204 f
.low
= bits
+ MAX_PRECISION
;
210 if (what_kind_of_float (what_kind
, &precision
, &exponent_bits
))
213 make_invalid_floating_point_number (words
);
218 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
220 /* Use more LittleNums than seems
221 necessary: the highest flonum may have
222 15 leading 0 bits, so could be useless. */
223 f
.high
= f
.low
+ precision
- 1 + GUARD
;
225 if (atof_generic (&return_value
, ".", "eE", &f
))
227 make_invalid_floating_point_number (words
);
230 else if (flonum_gen2vax (what_kind
, &f
, words
))
237 /* In: a flonum, a vax floating point format.
238 Out: a vax floating-point bit pattern. */
241 flonum_gen2vax (int format_letter
, /* One of 'd' 'f' 'g' 'h'. */
243 LITTLENUM_TYPE
*words
) /* Deliver answer here. */
248 int return_value
; /* 0 == OK. */
250 return_value
= what_kind_of_float (format_letter
, &precision
, &exponent_bits
);
252 if (return_value
!= 0)
253 make_invalid_floating_point_number (words
);
257 if (f
->low
> f
->leader
)
259 memset (words
, '\0', sizeof (LITTLENUM_TYPE
) * precision
);
267 int exponent_skippage
;
268 LITTLENUM_TYPE word1
;
270 /* JF: Deal with new Nan, +Inf and -Inf codes. */
271 if (f
->sign
!= '-' && f
->sign
!= '+')
273 make_invalid_floating_point_number (words
);
277 /* All vaxen floating_point formats (so far) have:
279 Bits 14:n are excess-whatever exponent.
280 Bits n-1:0 (if any) are most significant bits of fraction.
281 Bits 15:0 of the next word are the next most significant bits.
282 And so on for each other word.
284 All this to be compatible with a KF11?? (Which is still faster
285 than lots of vaxen I can think of, but it also has higher
286 maintenance costs ... sigh).
288 So we need: number of bits of exponent, number of bits of
291 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
292 littlenum_pointer
= f
->leader
;
293 littlenum_end
= f
->low
;
294 /* Seek (and forget) 1st significant bit. */
295 for (exponent_skippage
= 0;
297 exponent_skippage
++);
299 exponent_1
= f
->exponent
+ f
->leader
+ 1 - f
->low
;
300 /* Radix LITTLENUM_RADIX, point just higher than f->leader. */
301 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
303 exponent_3
= exponent_2
- exponent_skippage
;
304 /* Forget leading zeros, forget 1st bit. */
305 exponent_4
= exponent_3
+ (1 << (exponent_bits
- 1));
306 /* Offset exponent. */
308 if (exponent_4
& ~mask
[exponent_bits
])
310 /* Exponent overflow. Lose immediately. */
311 make_invalid_floating_point_number (words
);
313 /* We leave return_value alone: admit we read the
314 number, but return a floating exception
315 because we can't encode the number. */
321 /* Word 1. Sign, exponent and perhaps high bits.
322 Assume 2's complement integers. */
323 word1
= (((exponent_4
& mask
[exponent_bits
]) << (15 - exponent_bits
))
324 | ((f
->sign
== '+') ? 0 : 0x8000)
325 | next_bits (15 - exponent_bits
));
328 /* The rest of the words are just mantissa bits. */
329 for (; lp
< words
+ precision
; lp
++)
330 *lp
= next_bits (LITTLENUM_NUMBER_OF_BITS
);
334 /* Since the NEXT bit is a 1, round UP the mantissa.
335 The cunning design of these hidden-1 floats permits
336 us to let the mantissa overflow into the exponent, and
337 it 'does the right thing'. However, we lose if the
338 highest-order bit of the lowest-order word flips.
343 #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
344 Please allow at least 1 more bit in carry than is in a LITTLENUM.
345 We need that extra bit to hold a carry during a LITTLENUM carry
346 propagation. Another extra bit (kept 0) will assure us that we
347 don't get a sticky sign bit after shifting right, and that
348 permits us to propagate the carry without any masking of bits.
350 for (carry
= 1, lp
--;
351 carry
&& (lp
>= words
);
356 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
359 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
361 make_invalid_floating_point_number (words
);
362 /* We leave return_value alone: admit we read the
363 number, but return a floating exception
364 because we can't encode the number. */
373 /* JF this used to be in vax.c but this looks like a better place for it. */
375 /* In: input_line_pointer->the 1st character of a floating-point
377 1 letter denoting the type of statement that wants a
378 binary floating point number returned.
379 Address of where to build floating point literal.
380 Assumed to be 'big enough'.
381 Address of where to return size of literal (in chars).
383 Out: Input_line_pointer->of next char after floating number.
385 Floating point literal.
386 Number of chars we used for the literal. */
388 #define MAXIMUM_NUMBER_OF_LITTLENUMS 8 /* For .hfloats. */
391 vax_md_atof (int what_statement_type
,
395 LITTLENUM_TYPE words
[MAXIMUM_NUMBER_OF_LITTLENUMS
];
397 unsigned int number_of_chars
;
398 LITTLENUM_TYPE
*littlenumP
;
400 switch (what_statement_type
)
427 LITTLENUM_TYPE
*limit
;
429 input_line_pointer
= atof_vax (input_line_pointer
,
432 /* The atof_vax() builds up 16-bit numbers.
433 Since the assembler may not be running on
434 a little-endian machine, be very careful about
435 converting words to chars. */
436 number_of_chars
= atof_vax_sizeof (kind_of_float
);
437 know (number_of_chars
<= MAXIMUM_NUMBER_OF_LITTLENUMS
* sizeof (LITTLENUM_TYPE
));
438 limit
= words
+ (number_of_chars
/ sizeof (LITTLENUM_TYPE
));
439 for (littlenumP
= words
; littlenumP
< limit
; littlenumP
++)
441 md_number_to_chars (literalP
, *littlenumP
, sizeof (LITTLENUM_TYPE
));
442 literalP
+= sizeof (LITTLENUM_TYPE
);
448 *sizeP
= number_of_chars
;
449 return kind_of_float
? NULL
: _("Unrecognized or unsupported floating point constant");