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