Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* atof_ieee.c - turn a Flonum into an IEEE floating point number |
82704155 | 2 | Copyright (C) 1987-2019 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
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 | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
9 | any later version. |
10 | ||
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. | |
15 | ||
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 | |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 RH |
20 | |
21 | #include "as.h" | |
22 | ||
23 | /* Flonums returned here. */ | |
24 | extern FLONUM_TYPE generic_floating_point_number; | |
25 | ||
2d484c7f | 26 | /* Precision in LittleNums. */ |
252b5132 | 27 | /* Don't count the gap in the m68k extended precision format. */ |
ea1562b3 | 28 | #define MAX_PRECISION 5 |
5312fe52 | 29 | #define H_PRECISION 1 |
ea1562b3 NC |
30 | #define F_PRECISION 2 |
31 | #define D_PRECISION 4 | |
32 | #define X_PRECISION 5 | |
33 | #define P_PRECISION 5 | |
252b5132 | 34 | |
2d484c7f | 35 | /* Length in LittleNums of guard bits. */ |
ea1562b3 | 36 | #define GUARD 2 |
252b5132 | 37 | |
3a18fa4f | 38 | #ifndef TC_LARGEST_EXPONENT_IS_NORMAL |
580a832e | 39 | #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0 |
e103941e NC |
40 | #endif |
41 | ||
252b5132 RH |
42 | static const unsigned long mask[] = |
43 | { | |
44 | 0x00000000, | |
45 | 0x00000001, | |
46 | 0x00000003, | |
47 | 0x00000007, | |
48 | 0x0000000f, | |
49 | 0x0000001f, | |
50 | 0x0000003f, | |
51 | 0x0000007f, | |
52 | 0x000000ff, | |
53 | 0x000001ff, | |
54 | 0x000003ff, | |
55 | 0x000007ff, | |
56 | 0x00000fff, | |
57 | 0x00001fff, | |
58 | 0x00003fff, | |
59 | 0x00007fff, | |
60 | 0x0000ffff, | |
61 | 0x0001ffff, | |
62 | 0x0003ffff, | |
63 | 0x0007ffff, | |
64 | 0x000fffff, | |
65 | 0x001fffff, | |
66 | 0x003fffff, | |
67 | 0x007fffff, | |
68 | 0x00ffffff, | |
69 | 0x01ffffff, | |
70 | 0x03ffffff, | |
71 | 0x07ffffff, | |
72 | 0x0fffffff, | |
73 | 0x1fffffff, | |
74 | 0x3fffffff, | |
75 | 0x7fffffff, | |
76 | 0xffffffff, | |
77 | }; | |
78 | \f | |
252b5132 RH |
79 | static int bits_left_in_littlenum; |
80 | static int littlenums_left; | |
81 | static LITTLENUM_TYPE *littlenum_pointer; | |
82 | ||
83 | static int | |
ea1562b3 | 84 | next_bits (int number_of_bits) |
252b5132 RH |
85 | { |
86 | int return_value; | |
87 | ||
88 | if (!littlenums_left) | |
ea1562b3 NC |
89 | return 0; |
90 | ||
252b5132 RH |
91 | if (number_of_bits >= bits_left_in_littlenum) |
92 | { | |
93 | return_value = mask[bits_left_in_littlenum] & *littlenum_pointer; | |
94 | number_of_bits -= bits_left_in_littlenum; | |
95 | return_value <<= number_of_bits; | |
96 | ||
97 | if (--littlenums_left) | |
98 | { | |
99 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits; | |
100 | --littlenum_pointer; | |
2d484c7f KH |
101 | return_value |= |
102 | (*littlenum_pointer >> bits_left_in_littlenum) | |
103 | & mask[number_of_bits]; | |
252b5132 RH |
104 | } |
105 | } | |
106 | else | |
107 | { | |
108 | bits_left_in_littlenum -= number_of_bits; | |
2d484c7f KH |
109 | return_value = |
110 | mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum); | |
252b5132 | 111 | } |
2d484c7f | 112 | return return_value; |
252b5132 RH |
113 | } |
114 | ||
2d484c7f KH |
115 | /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */ |
116 | ||
252b5132 | 117 | static void |
ea1562b3 | 118 | unget_bits (int num) |
252b5132 RH |
119 | { |
120 | if (!littlenums_left) | |
121 | { | |
122 | ++littlenum_pointer; | |
123 | ++littlenums_left; | |
124 | bits_left_in_littlenum = num; | |
125 | } | |
126 | else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS) | |
127 | { | |
2d484c7f KH |
128 | bits_left_in_littlenum = |
129 | num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum); | |
252b5132 RH |
130 | ++littlenum_pointer; |
131 | ++littlenums_left; | |
132 | } | |
133 | else | |
134 | bits_left_in_littlenum += num; | |
135 | } | |
136 | ||
137 | static void | |
ea1562b3 | 138 | make_invalid_floating_point_number (LITTLENUM_TYPE *words) |
252b5132 RH |
139 | { |
140 | as_bad (_("cannot create floating-point number")); | |
2d484c7f KH |
141 | /* Zero the leftmost bit. */ |
142 | words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; | |
252b5132 RH |
143 | words[1] = (LITTLENUM_TYPE) -1; |
144 | words[2] = (LITTLENUM_TYPE) -1; | |
145 | words[3] = (LITTLENUM_TYPE) -1; | |
146 | words[4] = (LITTLENUM_TYPE) -1; | |
147 | words[5] = (LITTLENUM_TYPE) -1; | |
148 | } | |
149 | \f | |
2d484c7f KH |
150 | /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to |
151 | figure out any alignment problems and to conspire for the | |
152 | bytes/word to be emitted in the right order. Bigendians beware! */ | |
252b5132 RH |
153 | |
154 | /* Note that atof-ieee always has X and P precisions enabled. it is up | |
155 | to md_atof to filter them out if the target machine does not support | |
156 | them. */ | |
157 | ||
2d484c7f KH |
158 | /* Returns pointer past text consumed. */ |
159 | ||
252b5132 | 160 | char * |
ea1562b3 | 161 | atof_ieee (char *str, /* Text to convert to binary. */ |
499ac353 | 162 | int what_kind, /* 'd', 'f', 'x', 'p'. */ |
ea1562b3 | 163 | LITTLENUM_TYPE *words) /* Build the binary here. */ |
252b5132 | 164 | { |
2d484c7f KH |
165 | /* Extra bits for zeroed low-order bits. |
166 | The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */ | |
252b5132 RH |
167 | static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD]; |
168 | char *return_value; | |
2d484c7f | 169 | /* Number of 16-bit words in the format. */ |
252b5132 RH |
170 | int precision; |
171 | long exponent_bits; | |
172 | FLONUM_TYPE save_gen_flonum; | |
173 | ||
174 | /* We have to save the generic_floating_point_number because it | |
175 | contains storage allocation about the array of LITTLENUMs where | |
176 | the value is actually stored. We will allocate our own array of | |
177 | littlenums below, but have to restore the global one on exit. */ | |
178 | save_gen_flonum = generic_floating_point_number; | |
179 | ||
180 | return_value = str; | |
181 | generic_floating_point_number.low = bits + MAX_PRECISION; | |
182 | generic_floating_point_number.high = NULL; | |
183 | generic_floating_point_number.leader = NULL; | |
184 | generic_floating_point_number.exponent = 0; | |
185 | generic_floating_point_number.sign = '\0'; | |
186 | ||
187 | /* Use more LittleNums than seems necessary: the highest flonum may | |
2d484c7f | 188 | have 15 leading 0 bits, so could be useless. */ |
252b5132 RH |
189 | |
190 | memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION); | |
191 | ||
192 | switch (what_kind) | |
193 | { | |
5312fe52 BW |
194 | case 'h': |
195 | case 'H': | |
196 | precision = H_PRECISION; | |
197 | exponent_bits = 5; | |
198 | break; | |
199 | ||
252b5132 RH |
200 | case 'f': |
201 | case 'F': | |
202 | case 's': | |
203 | case 'S': | |
204 | precision = F_PRECISION; | |
205 | exponent_bits = 8; | |
206 | break; | |
207 | ||
208 | case 'd': | |
209 | case 'D': | |
210 | case 'r': | |
211 | case 'R': | |
212 | precision = D_PRECISION; | |
213 | exponent_bits = 11; | |
214 | break; | |
215 | ||
216 | case 'x': | |
217 | case 'X': | |
218 | case 'e': | |
219 | case 'E': | |
220 | precision = X_PRECISION; | |
221 | exponent_bits = 15; | |
222 | break; | |
223 | ||
224 | case 'p': | |
225 | case 'P': | |
252b5132 RH |
226 | precision = P_PRECISION; |
227 | exponent_bits = -1; | |
228 | break; | |
229 | ||
230 | default: | |
231 | make_invalid_floating_point_number (words); | |
232 | return (NULL); | |
233 | } | |
234 | ||
235 | generic_floating_point_number.high | |
236 | = generic_floating_point_number.low + precision - 1 + GUARD; | |
237 | ||
238 | if (atof_generic (&return_value, ".", EXP_CHARS, | |
239 | &generic_floating_point_number)) | |
240 | { | |
241 | make_invalid_floating_point_number (words); | |
ea1562b3 | 242 | return NULL; |
252b5132 RH |
243 | } |
244 | gen_to_words (words, precision, exponent_bits); | |
245 | ||
246 | /* Restore the generic_floating_point_number's storage alloc (and | |
247 | everything else). */ | |
248 | generic_floating_point_number = save_gen_flonum; | |
249 | ||
250 | return return_value; | |
251 | } | |
252 | ||
253 | /* Turn generic_floating_point_number into a real float/double/extended. */ | |
2d484c7f | 254 | |
252b5132 | 255 | int |
ea1562b3 | 256 | gen_to_words (LITTLENUM_TYPE *words, int precision, long exponent_bits) |
252b5132 RH |
257 | { |
258 | int return_value = 0; | |
259 | ||
260 | long exponent_1; | |
261 | long exponent_2; | |
262 | long exponent_3; | |
263 | long exponent_4; | |
264 | int exponent_skippage; | |
265 | LITTLENUM_TYPE word1; | |
266 | LITTLENUM_TYPE *lp; | |
267 | LITTLENUM_TYPE *words_end; | |
268 | ||
269 | words_end = words + precision; | |
270 | #ifdef TC_M68K | |
271 | if (precision == X_PRECISION) | |
272 | /* On the m68k the extended precision format has a gap of 16 bits | |
273 | between the exponent and the mantissa. */ | |
274 | words_end++; | |
275 | #endif | |
276 | ||
277 | if (generic_floating_point_number.low > generic_floating_point_number.leader) | |
278 | { | |
2d484c7f | 279 | /* 0.0e0 seen. */ |
252b5132 RH |
280 | if (generic_floating_point_number.sign == '+') |
281 | words[0] = 0x0000; | |
282 | else | |
283 | words[0] = 0x8000; | |
284 | memset (&words[1], '\0', | |
285 | (words_end - words - 1) * sizeof (LITTLENUM_TYPE)); | |
2d484c7f | 286 | return return_value; |
252b5132 RH |
287 | } |
288 | ||
2d484c7f | 289 | /* NaN: Do the right thing. */ |
252b5132 RH |
290 | if (generic_floating_point_number.sign == 0) |
291 | { | |
580a832e | 292 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
5312fe52 BW |
293 | as_warn (_("NaNs are not supported by this target")); |
294 | ||
295 | if (precision == H_PRECISION) | |
296 | { | |
297 | words[0] = 0x7fff; | |
298 | } | |
299 | else if (precision == F_PRECISION) | |
252b5132 RH |
300 | { |
301 | words[0] = 0x7fff; | |
302 | words[1] = 0xffff; | |
303 | } | |
304 | else if (precision == X_PRECISION) | |
305 | { | |
306 | #ifdef TC_M68K | |
307 | words[0] = 0x7fff; | |
308 | words[1] = 0; | |
309 | words[2] = 0xffff; | |
310 | words[3] = 0xffff; | |
311 | words[4] = 0xffff; | |
312 | words[5] = 0xffff; | |
2d484c7f | 313 | #else /* ! TC_M68K */ |
252b5132 RH |
314 | #ifdef TC_I386 |
315 | words[0] = 0xffff; | |
316 | words[1] = 0xc000; | |
317 | words[2] = 0; | |
318 | words[3] = 0; | |
319 | words[4] = 0; | |
2d484c7f | 320 | #else /* ! TC_I386 */ |
252b5132 | 321 | abort (); |
2d484c7f KH |
322 | #endif /* ! TC_I386 */ |
323 | #endif /* ! TC_M68K */ | |
252b5132 RH |
324 | } |
325 | else | |
326 | { | |
327 | words[0] = 0x7fff; | |
328 | words[1] = 0xffff; | |
329 | words[2] = 0xffff; | |
330 | words[3] = 0xffff; | |
331 | } | |
332 | return return_value; | |
333 | } | |
334 | else if (generic_floating_point_number.sign == 'P') | |
335 | { | |
580a832e | 336 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
5312fe52 | 337 | as_warn (_("Infinities are not supported by this target")); |
e103941e | 338 | |
2d484c7f | 339 | /* +INF: Do the right thing. */ |
5312fe52 BW |
340 | if (precision == H_PRECISION) |
341 | { | |
342 | words[0] = 0x7c00; | |
343 | } | |
344 | else if (precision == F_PRECISION) | |
252b5132 RH |
345 | { |
346 | words[0] = 0x7f80; | |
347 | words[1] = 0; | |
348 | } | |
349 | else if (precision == X_PRECISION) | |
350 | { | |
351 | #ifdef TC_M68K | |
352 | words[0] = 0x7fff; | |
353 | words[1] = 0; | |
354 | words[2] = 0; | |
355 | words[3] = 0; | |
356 | words[4] = 0; | |
357 | words[5] = 0; | |
2d484c7f | 358 | #else /* ! TC_M68K */ |
252b5132 RH |
359 | #ifdef TC_I386 |
360 | words[0] = 0x7fff; | |
361 | words[1] = 0x8000; | |
362 | words[2] = 0; | |
363 | words[3] = 0; | |
364 | words[4] = 0; | |
2d484c7f | 365 | #else /* ! TC_I386 */ |
252b5132 | 366 | abort (); |
2d484c7f KH |
367 | #endif /* ! TC_I386 */ |
368 | #endif /* ! TC_M68K */ | |
252b5132 RH |
369 | } |
370 | else | |
371 | { | |
372 | words[0] = 0x7ff0; | |
373 | words[1] = 0; | |
374 | words[2] = 0; | |
375 | words[3] = 0; | |
376 | } | |
2d484c7f | 377 | return return_value; |
252b5132 RH |
378 | } |
379 | else if (generic_floating_point_number.sign == 'N') | |
380 | { | |
580a832e | 381 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
5312fe52 | 382 | as_warn (_("Infinities are not supported by this target")); |
e103941e | 383 | |
2d484c7f | 384 | /* Negative INF. */ |
5312fe52 BW |
385 | if (precision == H_PRECISION) |
386 | { | |
387 | words[0] = 0xfc00; | |
388 | } | |
389 | else if (precision == F_PRECISION) | |
252b5132 RH |
390 | { |
391 | words[0] = 0xff80; | |
392 | words[1] = 0x0; | |
393 | } | |
394 | else if (precision == X_PRECISION) | |
395 | { | |
396 | #ifdef TC_M68K | |
397 | words[0] = 0xffff; | |
398 | words[1] = 0; | |
399 | words[2] = 0; | |
400 | words[3] = 0; | |
401 | words[4] = 0; | |
402 | words[5] = 0; | |
2d484c7f | 403 | #else /* ! TC_M68K */ |
252b5132 RH |
404 | #ifdef TC_I386 |
405 | words[0] = 0xffff; | |
406 | words[1] = 0x8000; | |
407 | words[2] = 0; | |
408 | words[3] = 0; | |
409 | words[4] = 0; | |
2d484c7f | 410 | #else /* ! TC_I386 */ |
252b5132 | 411 | abort (); |
2d484c7f KH |
412 | #endif /* ! TC_I386 */ |
413 | #endif /* ! TC_M68K */ | |
252b5132 RH |
414 | } |
415 | else | |
416 | { | |
417 | words[0] = 0xfff0; | |
418 | words[1] = 0x0; | |
419 | words[2] = 0x0; | |
420 | words[3] = 0x0; | |
421 | } | |
2d484c7f | 422 | return return_value; |
252b5132 | 423 | } |
2d484c7f KH |
424 | |
425 | /* The floating point formats we support have: | |
426 | Bit 15 is sign bit. | |
427 | Bits 14:n are excess-whatever exponent. | |
428 | Bits n-1:0 (if any) are most significant bits of fraction. | |
429 | Bits 15:0 of the next word(s) are the next most significant bits. | |
430 | ||
431 | So we need: number of bits of exponent, number of bits of | |
432 | mantissa. */ | |
252b5132 RH |
433 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS; |
434 | littlenum_pointer = generic_floating_point_number.leader; | |
435 | littlenums_left = (1 | |
436 | + generic_floating_point_number.leader | |
437 | - generic_floating_point_number.low); | |
2d484c7f KH |
438 | |
439 | /* Seek (and forget) 1st significant bit. */ | |
5bb3703f | 440 | for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage); |
252b5132 RH |
441 | exponent_1 = (generic_floating_point_number.exponent |
442 | + generic_floating_point_number.leader | |
443 | + 1 | |
444 | - generic_floating_point_number.low); | |
2d484c7f | 445 | |
252b5132 | 446 | /* Radix LITTLENUM_RADIX, point just higher than |
2d484c7f | 447 | generic_floating_point_number.leader. */ |
252b5132 | 448 | exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS; |
2d484c7f KH |
449 | |
450 | /* Radix 2. */ | |
252b5132 | 451 | exponent_3 = exponent_2 - exponent_skippage; |
2d484c7f KH |
452 | |
453 | /* Forget leading zeros, forget 1st bit. */ | |
252b5132 | 454 | exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2); |
252b5132 | 455 | |
2d484c7f | 456 | /* Offset exponent. */ |
252b5132 RH |
457 | lp = words; |
458 | ||
2d484c7f | 459 | /* Word 1. Sign, exponent and perhaps high bits. */ |
252b5132 RH |
460 | word1 = ((generic_floating_point_number.sign == '+') |
461 | ? 0 | |
462 | : (1 << (LITTLENUM_NUMBER_OF_BITS - 1))); | |
463 | ||
2d484c7f | 464 | /* Assume 2's complement integers. */ |
252b5132 RH |
465 | if (exponent_4 <= 0) |
466 | { | |
467 | int prec_bits; | |
468 | int num_bits; | |
469 | ||
470 | unget_bits (1); | |
471 | num_bits = -exponent_4; | |
2d484c7f KH |
472 | prec_bits = |
473 | LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits); | |
252b5132 RH |
474 | #ifdef TC_I386 |
475 | if (precision == X_PRECISION && exponent_bits == 15) | |
476 | { | |
477 | /* On the i386 a denormalized extended precision float is | |
478 | shifted down by one, effectively decreasing the exponent | |
479 | bias by one. */ | |
480 | prec_bits -= 1; | |
481 | num_bits += 1; | |
482 | } | |
483 | #endif | |
484 | ||
485 | if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits) | |
486 | { | |
2d484c7f | 487 | /* Bigger than one littlenum. */ |
252b5132 RH |
488 | num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits; |
489 | *lp++ = word1; | |
2d484c7f KH |
490 | if (num_bits + exponent_bits + 1 |
491 | > precision * LITTLENUM_NUMBER_OF_BITS) | |
252b5132 | 492 | { |
2d484c7f | 493 | /* Exponent overflow. */ |
252b5132 | 494 | make_invalid_floating_point_number (words); |
2d484c7f | 495 | return return_value; |
252b5132 RH |
496 | } |
497 | #ifdef TC_M68K | |
498 | if (precision == X_PRECISION && exponent_bits == 15) | |
499 | *lp++ = 0; | |
500 | #endif | |
501 | while (num_bits >= LITTLENUM_NUMBER_OF_BITS) | |
502 | { | |
503 | num_bits -= LITTLENUM_NUMBER_OF_BITS; | |
504 | *lp++ = 0; | |
505 | } | |
506 | if (num_bits) | |
507 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits)); | |
508 | } | |
509 | else | |
510 | { | |
511 | if (precision == X_PRECISION && exponent_bits == 15) | |
512 | { | |
513 | *lp++ = word1; | |
514 | #ifdef TC_M68K | |
515 | *lp++ = 0; | |
516 | #endif | |
517 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits); | |
518 | } | |
519 | else | |
520 | { | |
2d484c7f KH |
521 | word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) |
522 | - (exponent_bits + num_bits)); | |
252b5132 RH |
523 | *lp++ = word1; |
524 | } | |
525 | } | |
526 | while (lp < words_end) | |
527 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); | |
528 | ||
2d484c7f | 529 | /* Round the mantissa up, but don't change the number. */ |
252b5132 RH |
530 | if (next_bits (1)) |
531 | { | |
532 | --lp; | |
19b34177 | 533 | if (prec_bits >= LITTLENUM_NUMBER_OF_BITS) |
252b5132 RH |
534 | { |
535 | int n = 0; | |
536 | int tmp_bits; | |
537 | ||
538 | n = 0; | |
539 | tmp_bits = prec_bits; | |
540 | while (tmp_bits > LITTLENUM_NUMBER_OF_BITS) | |
541 | { | |
542 | if (lp[n] != (LITTLENUM_TYPE) - 1) | |
543 | break; | |
544 | --n; | |
545 | tmp_bits -= LITTLENUM_NUMBER_OF_BITS; | |
546 | } | |
19b34177 AS |
547 | if (tmp_bits > LITTLENUM_NUMBER_OF_BITS |
548 | || (lp[n] & mask[tmp_bits]) != mask[tmp_bits] | |
549 | || (prec_bits != (precision * LITTLENUM_NUMBER_OF_BITS | |
550 | - exponent_bits - 1) | |
551 | #ifdef TC_I386 | |
552 | /* An extended precision float with only the integer | |
553 | bit set would be invalid. That must be converted | |
554 | to the smallest normalized number. */ | |
555 | && !(precision == X_PRECISION | |
556 | && prec_bits == (precision * LITTLENUM_NUMBER_OF_BITS | |
557 | - exponent_bits - 2)) | |
558 | #endif | |
559 | )) | |
252b5132 RH |
560 | { |
561 | unsigned long carry; | |
562 | ||
563 | for (carry = 1; carry && (lp >= words); lp--) | |
564 | { | |
565 | carry = *lp + carry; | |
566 | *lp = carry; | |
567 | carry >>= LITTLENUM_NUMBER_OF_BITS; | |
568 | } | |
569 | } | |
570 | else | |
571 | { | |
572 | /* This is an overflow of the denormal numbers. We | |
573 | need to forget what we have produced, and instead | |
574 | generate the smallest normalized number. */ | |
575 | lp = words; | |
576 | word1 = ((generic_floating_point_number.sign == '+') | |
577 | ? 0 | |
578 | : (1 << (LITTLENUM_NUMBER_OF_BITS - 1))); | |
579 | word1 |= (1 | |
580 | << ((LITTLENUM_NUMBER_OF_BITS - 1) | |
581 | - exponent_bits)); | |
582 | *lp++ = word1; | |
19b34177 AS |
583 | #ifdef TC_I386 |
584 | /* Set the integer bit in the extended precision format. | |
585 | This cannot happen on the m68k where the mantissa | |
586 | just overflows into the integer bit above. */ | |
587 | if (precision == X_PRECISION) | |
588 | *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS - 1); | |
589 | #endif | |
252b5132 RH |
590 | while (lp < words_end) |
591 | *lp++ = 0; | |
592 | } | |
593 | } | |
19b34177 | 594 | else |
252b5132 RH |
595 | *lp += 1; |
596 | } | |
597 | ||
598 | return return_value; | |
599 | } | |
e103941e | 600 | else if ((unsigned long) exponent_4 > mask[exponent_bits] |
580a832e | 601 | || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision) |
e103941e | 602 | && (unsigned long) exponent_4 == mask[exponent_bits])) |
252b5132 | 603 | { |
2d484c7f KH |
604 | /* Exponent overflow. Lose immediately. */ |
605 | ||
606 | /* We leave return_value alone: admit we read the | |
607 | number, but return a floating exception | |
608 | because we can't encode the number. */ | |
252b5132 RH |
609 | make_invalid_floating_point_number (words); |
610 | return return_value; | |
611 | } | |
612 | else | |
613 | { | |
614 | word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits)) | |
615 | | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits); | |
616 | } | |
617 | ||
618 | *lp++ = word1; | |
619 | ||
620 | /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the | |
2d484c7f | 621 | middle. Either way, it is then followed by a 1 bit. */ |
252b5132 RH |
622 | if (exponent_bits == 15 && precision == X_PRECISION) |
623 | { | |
624 | #ifdef TC_M68K | |
625 | *lp++ = 0; | |
626 | #endif | |
627 | *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1) | |
628 | | next_bits (LITTLENUM_NUMBER_OF_BITS - 1)); | |
629 | } | |
630 | ||
2d484c7f | 631 | /* The rest of the words are just mantissa bits. */ |
252b5132 RH |
632 | while (lp < words_end) |
633 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); | |
634 | ||
635 | if (next_bits (1)) | |
636 | { | |
637 | unsigned long carry; | |
2d484c7f KH |
638 | /* Since the NEXT bit is a 1, round UP the mantissa. |
639 | The cunning design of these hidden-1 floats permits | |
640 | us to let the mantissa overflow into the exponent, and | |
641 | it 'does the right thing'. However, we lose if the | |
642 | highest-order bit of the lowest-order word flips. | |
643 | Is that clear? */ | |
252b5132 RH |
644 | |
645 | /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2) | |
646 | Please allow at least 1 more bit in carry than is in a LITTLENUM. | |
647 | We need that extra bit to hold a carry during a LITTLENUM carry | |
648 | propagation. Another extra bit (kept 0) will assure us that we | |
649 | don't get a sticky sign bit after shifting right, and that | |
650 | permits us to propagate the carry without any masking of bits. | |
651 | #endif */ | |
44877466 | 652 | for (carry = 1, lp--; carry; lp--) |
252b5132 RH |
653 | { |
654 | carry = *lp + carry; | |
655 | *lp = carry; | |
656 | carry >>= LITTLENUM_NUMBER_OF_BITS; | |
44877466 ILT |
657 | if (lp == words) |
658 | break; | |
252b5132 RH |
659 | } |
660 | if (precision == X_PRECISION && exponent_bits == 15) | |
661 | { | |
662 | /* Extended precision numbers have an explicit integer bit | |
663 | that we may have to restore. */ | |
664 | if (lp == words) | |
665 | { | |
666 | #ifdef TC_M68K | |
667 | /* On the m68k there is a gap of 16 bits. We must | |
2d484c7f | 668 | explicitly propagate the carry into the exponent. */ |
252b5132 RH |
669 | words[0] += words[1]; |
670 | words[1] = 0; | |
671 | lp++; | |
672 | #endif | |
2d484c7f | 673 | /* Put back the integer bit. */ |
252b5132 RH |
674 | lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1); |
675 | } | |
2d484c7f | 676 | } |
252b5132 RH |
677 | if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1))) |
678 | { | |
2d484c7f KH |
679 | /* We leave return_value alone: admit we read the number, |
680 | but return a floating exception because we can't encode | |
681 | the number. */ | |
252b5132 | 682 | *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1)); |
252b5132 RH |
683 | } |
684 | } | |
2d484c7f | 685 | return return_value; |
252b5132 RH |
686 | } |
687 | ||
252b5132 RH |
688 | #ifdef TEST |
689 | char * | |
690 | print_gen (gen) | |
691 | FLONUM_TYPE *gen; | |
692 | { | |
693 | FLONUM_TYPE f; | |
694 | LITTLENUM_TYPE arr[10]; | |
695 | double dv; | |
696 | float fv; | |
697 | static char sbuf[40]; | |
698 | ||
699 | if (gen) | |
700 | { | |
701 | f = generic_floating_point_number; | |
702 | generic_floating_point_number = *gen; | |
703 | } | |
704 | gen_to_words (&arr[0], 4, 11); | |
705 | memcpy (&dv, &arr[0], sizeof (double)); | |
706 | sprintf (sbuf, "%x %x %x %x %.14G ", arr[0], arr[1], arr[2], arr[3], dv); | |
707 | gen_to_words (&arr[0], 2, 8); | |
708 | memcpy (&fv, &arr[0], sizeof (float)); | |
709 | sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv); | |
710 | ||
711 | if (gen) | |
2d484c7f | 712 | generic_floating_point_number = f; |
252b5132 RH |
713 | |
714 | return (sbuf); | |
715 | } | |
499ac353 NC |
716 | #endif |
717 | ||
499ac353 NC |
718 | /* This is a utility function called from various tc-*.c files. It |
719 | is here in order to reduce code duplication. | |
3739860c | 720 | |
499ac353 NC |
721 | Turn a string at input_line_pointer into a floating point constant |
722 | of type TYPE (a character found in the FLT_CHARS macro), and store | |
723 | it as LITTLENUMS in the bytes buffer LITP. The number of chars | |
724 | emitted is stored in *SIZEP. BIG_WORDIAN is TRUE if the littlenums | |
725 | should be emitted most significant littlenum first. | |
726 | ||
727 | An error message is returned, or a NULL pointer if everything went OK. */ | |
728 | ||
6d4af3c2 | 729 | const char * |
499ac353 NC |
730 | ieee_md_atof (int type, |
731 | char *litP, | |
732 | int *sizeP, | |
733 | bfd_boolean big_wordian) | |
734 | { | |
735 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
736 | LITTLENUM_TYPE *wordP; | |
737 | char *t; | |
738 | int prec = 0; | |
252b5132 | 739 | |
499ac353 NC |
740 | if (strchr (FLT_CHARS, type) != NULL) |
741 | { | |
742 | switch (type) | |
743 | { | |
5312fe52 BW |
744 | case 'H': |
745 | case 'h': | |
746 | prec = H_PRECISION; | |
747 | break; | |
748 | ||
499ac353 NC |
749 | case 'f': |
750 | case 'F': | |
751 | case 's': | |
752 | case 'S': | |
753 | prec = F_PRECISION; | |
754 | break; | |
755 | ||
756 | case 'd': | |
757 | case 'D': | |
758 | case 'r': | |
759 | case 'R': | |
760 | prec = D_PRECISION; | |
761 | break; | |
762 | ||
763 | case 't': | |
764 | case 'T': | |
765 | prec = X_PRECISION; | |
766 | type = 'x'; /* This is what atof_ieee() understands. */ | |
767 | break; | |
768 | ||
769 | case 'x': | |
770 | case 'X': | |
771 | case 'p': | |
772 | case 'P': | |
773 | #ifdef TC_M68K | |
774 | /* Note: on the m68k there is a gap of 16 bits (one littlenum) | |
775 | between the exponent and mantissa. Hence the precision is | |
776 | 6 and not 5. */ | |
777 | prec = P_PRECISION + 1; | |
778 | #else | |
779 | prec = P_PRECISION; | |
252b5132 | 780 | #endif |
499ac353 NC |
781 | break; |
782 | ||
783 | default: | |
784 | break; | |
785 | } | |
786 | } | |
787 | /* The 'f' and 'd' types are always recognised, even if the target has | |
788 | not put them into the FLT_CHARS macro. This is because the 'f' type | |
789 | can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the | |
790 | 'd' type from the .dc.d, .dbc.d or .double pseudo-ops. | |
791 | ||
33eaf5de | 792 | The 'x' type is not implicitly recognised however, even though it can |
499ac353 NC |
793 | be generated by the .dc.x and .dbc.x pseudo-ops because not all targets |
794 | can support floating point values that big. ie the target has to | |
795 | explicitly allow them by putting them into FLT_CHARS. */ | |
796 | else if (type == 'f') | |
797 | prec = F_PRECISION; | |
798 | else if (type == 'd') | |
799 | prec = D_PRECISION; | |
800 | ||
801 | if (prec == 0) | |
802 | { | |
803 | *sizeP = 0; | |
804 | return _("Unrecognized or unsupported floating point constant"); | |
805 | } | |
806 | ||
9c2799c2 | 807 | gas_assert (prec <= MAX_LITTLENUMS); |
499ac353 NC |
808 | |
809 | t = atof_ieee (input_line_pointer, type, words); | |
810 | if (t) | |
811 | input_line_pointer = t; | |
812 | ||
813 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
814 | ||
815 | if (big_wordian) | |
816 | { | |
817 | for (wordP = words; prec --;) | |
818 | { | |
819 | md_number_to_chars (litP, (valueT) (* wordP ++), sizeof (LITTLENUM_TYPE)); | |
820 | litP += sizeof (LITTLENUM_TYPE); | |
821 | } | |
822 | } | |
823 | else | |
824 | { | |
825 | for (wordP = words + prec; prec --;) | |
826 | { | |
827 | md_number_to_chars (litP, (valueT) (* -- wordP), sizeof (LITTLENUM_TYPE)); | |
828 | litP += sizeof (LITTLENUM_TYPE); | |
829 | } | |
830 | } | |
831 | ||
832 | return NULL; | |
833 | } |