|
|
1.1 root 1: /* atof_m68k.c - */
2:
3: /* Copyright (C) 1987 Free Software Foundation, Inc.
4:
5: This file is part of Gas, the GNU Assembler.
6:
7: The GNU assembler is distributed in the hope that it will be
8: useful, but WITHOUT ANY WARRANTY. No author or distributor
9: accepts responsibility to anyone for the consequences of using it
10: or for whether it serves any particular purpose or works at all,
11: unless he says so in writing. Refer to the GNU Assembler General
12: Public License for full details.
13:
14: Everyone is granted permission to copy, modify and redistribute
15: the GNU Assembler, but only under the conditions described in the
16: GNU Assembler General Public License. A copy of this license is
17: supposed to have been given to you along with the GNU Assembler
18: so you can know your rights and responsibilities. It should be
19: in a file named COPYING. Among other things, the copyright
20: notice and this notice must be preserved on all copies. */
21:
22: #include "flonum.h"
23:
24: extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
25: #define NULL (0)
26:
27: extern char EXP_CHARS[];
28: /* Precision in LittleNums. */
29: #define MAX_PRECISION (6)
30: #define F_PRECISION (2)
31: #define D_PRECISION (4)
32: #define X_PRECISION (6)
33: #define P_PRECISION (6)
34:
35: /* Length in LittleNums of guard bits. */
36: #define GUARD (2)
37:
38: int /* Number of chars in flonum type 'letter'. */
39: atof_sizeof (letter)
40: char letter;
41: {
42: int return_value;
43:
44: /*
45: * Permitting uppercase letters is probably a bad idea.
46: * Please use only lower-cased letters in case the upper-cased
47: * ones become unsupported!
48: */
49: switch (letter)
50: {
51: case 'f':
52: case 'F':
53: case 's':
54: case 'S':
55: return_value = F_PRECISION;
56: break;
57:
58: case 'd':
59: case 'D':
60: case 'r':
61: case 'R':
62: return_value = D_PRECISION;
63: break;
64:
65: case 'x':
66: case 'X':
67: return_value = X_PRECISION;
68: break;
69:
70: case 'p':
71: case 'P':
72: return_value = P_PRECISION;
73: break;
74:
75: default:
76: return_value = 0;
77: break;
78: }
79: return (return_value);
80: }
81:
82: static unsigned long int mask [] = {
83: 0x00000000,
84: 0x00000001,
85: 0x00000003,
86: 0x00000007,
87: 0x0000000f,
88: 0x0000001f,
89: 0x0000003f,
90: 0x0000007f,
91: 0x000000ff,
92: 0x000001ff,
93: 0x000003ff,
94: 0x000007ff,
95: 0x00000fff,
96: 0x00001fff,
97: 0x00003fff,
98: 0x00007fff,
99: 0x0000ffff,
100: 0x0001ffff,
101: 0x0003ffff,
102: 0x0007ffff,
103: 0x000fffff,
104: 0x001fffff,
105: 0x003fffff,
106: 0x007fffff,
107: 0x00ffffff,
108: 0x01ffffff,
109: 0x03ffffff,
110: 0x07ffffff,
111: 0x0fffffff,
112: 0x1fffffff,
113: 0x3fffffff,
114: 0x7fffffff,
115: 0xffffffff
116: };
117:
1.1.1.2 ! root 118: static int bits_left_in_littlenum;
! 119: static int littlenums_left;
! 120: static LITTLENUM_TYPE * littlenum_pointer;
! 121:
1.1 root 122: static int
1.1.1.2 ! root 123: next_bits (number_of_bits)
1.1 root 124: int number_of_bits;
125: {
126: int return_value;
127:
1.1.1.2 ! root 128: if(!littlenums_left)
! 129: return 0;
! 130: if (number_of_bits >= bits_left_in_littlenum)
1.1 root 131: {
1.1.1.2 ! root 132: return_value = mask [bits_left_in_littlenum] & *littlenum_pointer;
! 133: number_of_bits -= bits_left_in_littlenum;
1.1 root 134: return_value <<= number_of_bits;
1.1.1.2 ! root 135: if(littlenums_left) {
! 136: bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
! 137: littlenum_pointer --;
! 138: --littlenums_left;
! 139: return_value |= (*littlenum_pointer>>bits_left_in_littlenum) & mask[number_of_bits];
! 140: }
1.1 root 141: }
142: else
143: {
1.1.1.2 ! root 144: bits_left_in_littlenum -= number_of_bits;
! 145: return_value = mask [number_of_bits] & (*littlenum_pointer>>bits_left_in_littlenum);
1.1 root 146: }
147: return (return_value);
148: }
149:
150: static void
151: make_invalid_floating_point_number (words)
152: LITTLENUM_TYPE * words;
153: {
154: words[0]= ((unsigned)-1)>>1; /* Zero the leftmost bit */
155: words[1]= -1;
156: words[2]= -1;
157: words[3]= -1;
158: words[4]= -1;
159: words[5]= -1;
160: }
161:
162: /***********************************************************************\
163: * *
164: * Warning: this returns 16-bit LITTLENUMs, because that is *
165: * what the VAX thinks in. It is up to the caller to figure *
166: * out any alignment problems and to conspire for the bytes/word *
167: * to be emitted in the right order. Bigendians beware! *
168: * *
169: \***********************************************************************/
170:
171: char * /* Return pointer past text consumed. */
172: atof_m68k (str, what_kind, words)
173: char * str; /* Text to convert to binary. */
174: char what_kind; /* 'd', 'f', 'g', 'h' */
175: LITTLENUM_TYPE * words; /* Build the binary here. */
176: {
177: FLONUM_TYPE f;
178: LITTLENUM_TYPE bits [MAX_PRECISION + MAX_PRECISION + GUARD];
179: /* Extra bits for zeroed low-order bits. */
180: /* The 1st MAX_PRECISION are zeroed, */
181: /* the last contain flonum bits. */
182: char * return_value;
183: int precision; /* Number of 16-bit words in the format. */
184: long int exponent_bits;
185:
186: long int exponent_1;
187: long int exponent_2;
188: long int exponent_3;
189: long int exponent_4;
190: int exponent_skippage;
191: LITTLENUM_TYPE word1;
192: LITTLENUM_TYPE * lp;
193:
194: return_value = str;
195: f.low = bits + MAX_PRECISION;
196: f.high = NULL;
197: f.leader = NULL;
198: f.exponent = NULL;
199: f.sign = '\0';
200:
201: /* Use more LittleNums than seems */
202: /* necessary: the highest flonum may have */
203: /* 15 leading 0 bits, so could be useless. */
204:
205: bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
206:
207: switch(what_kind) {
208: case 'f':
209: case 'F':
210: case 's':
211: case 'S':
212: precision = F_PRECISION;
213: exponent_bits = 8;
214: break;
215:
216: case 'd':
217: case 'D':
218: case 'r':
219: case 'R':
220: precision = D_PRECISION;
221: exponent_bits = 11;
222: break;
223:
224: case 'x':
225: case 'X':
226: precision = X_PRECISION;
227: exponent_bits = 15;
228: break;
229:
230: case 'p':
231: case 'P':
232:
233: precision = P_PRECISION;
234: exponent_bits= -1;
235: break;
236:
237: default:
238: make_invalid_floating_point_number (words);
239: return NULL;
240: }
241:
242: f.high = f.low + precision - 1 + GUARD;
243:
244: if (atof_generic (& return_value, ".", EXP_CHARS, & f)) {
245: as_warn("Error converting floating point number (Exponent overflow?)");
246: make_invalid_floating_point_number (words);
247: return NULL;
248: }
249:
250: if (f.low > f.leader) {
251: /* 0.0e0 seen. */
252: bzero (words, sizeof(LITTLENUM_TYPE) * precision);
253: return return_value;
254: }
255:
256: /*
257: * All vaxen floating_point formats (so far) have:
258: * Bit 15 is sign bit.
259: * Bits 14:n are excess-whatever exponent.
260: * Bits n-1:0 (if any) are most significant bits of fraction.
261: * Bits 15:0 of the next word are the next most significant bits.
262: * And so on for each other word.
263: *
264: * So we need: number of bits of exponent, number of bits of
265: * mantissa.
266: */
267: bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
268: littlenum_pointer = f.leader;
1.1.1.2 ! root 269: littlenums_left = 1 + f.leader-f.low;
1.1 root 270: /* Seek (and forget) 1st significant bit */
1.1.1.2 ! root 271: for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
1.1 root 272: ;
273: exponent_1 = f.exponent + f.leader + 1 - f.low;
274: /* Radix LITTLENUM_RADIX, point just higher than f.leader. */
275: exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
276: /* Radix 2. */
277: exponent_3 = exponent_2 - exponent_skippage;
278: /* Forget leading zeros, forget 1st bit. */
279: exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
280: /* Offset exponent. */
281:
282: if (exponent_4 & ~ mask [exponent_bits]) {
283: /*
284: * Exponent overflow. Lose immediately.
285: */
286:
287: /*
288: * We leave return_value alone: admit we read the
289: * number, but return a floating exception
290: * because we can't encode the number.
291: */
292:
293: as_warn("Exponent overflow in floating-point number");
294: make_invalid_floating_point_number (words);
295: return return_value;
296: }
297: lp = words;
298:
299: /* Word 1. Sign, exponent and perhaps high bits. */
300: /* Assume 2's complement integers. */
301: word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits)) |
1.1.1.2 ! root 302: ((f.sign == '+') ? 0 : 0x8000) | next_bits (15 - exponent_bits);
1.1 root 303: * lp ++ = word1;
304:
305: /* The rest of the words are just mantissa bits. */
306: for (; lp < words + precision; lp++)
1.1.1.2 ! root 307: * lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
1.1 root 308:
1.1.1.2 ! root 309: if (next_bits (1)) {
1.1 root 310: unsigned long int carry;
311: /*
312: * Since the NEXT bit is a 1, round UP the mantissa.
313: * The cunning design of these hidden-1 floats permits
314: * us to let the mantissa overflow into the exponent, and
315: * it 'does the right thing'. However, we lose if the
316: * highest-order bit of the lowest-order word flips.
317: * Is that clear?
318: */
319:
320:
321: /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
322: Please allow at least 1 more bit in carry than is in a LITTLENUM.
323: We need that extra bit to hold a carry during a LITTLENUM carry
324: propagation. Another extra bit (kept 0) will assure us that we
325: don't get a sticky sign bit after shifting right, and that
326: permits us to propagate the carry without any masking of bits.
327: #endif */
328: for (carry = 1, lp --; carry && (lp >= words); lp --) {
329: carry = * lp + carry;
330: * lp = carry;
331: carry >>= LITTLENUM_NUMBER_OF_BITS;
332: }
333: if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
334: /* We leave return_value alone: admit we read the
335: * number, but return a floating exception
336: * because we can't encode the number.
337: */
338: make_invalid_floating_point_number (words);
339: return return_value;
340: }
341: }
342: return (return_value);
343: }
344:
1.1.1.2 ! root 345: /* This is really identical to atof_m68k except for some details */
! 346:
1.1 root 347: gen_to_words(words,precision,exponent_bits)
348: LITTLENUM_TYPE *words;
349: long int exponent_bits;
350: {
351: int return_value=0;
352:
353: long int exponent_1;
354: long int exponent_2;
355: long int exponent_3;
356: long int exponent_4;
357: int exponent_skippage;
358: LITTLENUM_TYPE word1;
359: LITTLENUM_TYPE * lp;
360:
361: if (generic_floating_point_number.low > generic_floating_point_number.leader) {
362: /* 0.0e0 seen. */
363: bzero (words, sizeof(LITTLENUM_TYPE) * precision);
364: return return_value;
365: }
366:
367: /*
368: * All vaxen floating_point formats (so far) have:
369: * Bit 15 is sign bit.
370: * Bits 14:n are excess-whatever exponent.
371: * Bits n-1:0 (if any) are most significant bits of fraction.
372: * Bits 15:0 of the next word are the next most significant bits.
373: * And so on for each other word.
374: *
375: * So we need: number of bits of exponent, number of bits of
376: * mantissa.
377: */
378: bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
379: littlenum_pointer = generic_floating_point_number.leader;
1.1.1.2 ! root 380: littlenums_left = 1+generic_floating_point_number.leader - generic_floating_point_number.low;
1.1 root 381: /* Seek (and forget) 1st significant bit */
1.1.1.2 ! root 382: for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
1.1 root 383: ;
384: exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
385: generic_floating_point_number.low;
386: /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
387: exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
388: /* Radix 2. */
389: exponent_3 = exponent_2 - exponent_skippage;
390: /* Forget leading zeros, forget 1st bit. */
391: exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
392: /* Offset exponent. */
393:
394: if (exponent_4 & ~ mask [exponent_bits]) {
395: /*
396: * Exponent overflow. Lose immediately.
397: */
398:
399: /*
400: * We leave return_value alone: admit we read the
401: * number, but return a floating exception
402: * because we can't encode the number.
403: */
404:
405: make_invalid_floating_point_number (words);
406: return return_value;
407: }
408: lp = words;
409:
410: /* Word 1. Sign, exponent and perhaps high bits. */
411: /* Assume 2's complement integers. */
412: word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits)) |
1.1.1.2 ! root 413: ((generic_floating_point_number.sign == '+') ? 0 : 0x8000) | next_bits (15 - exponent_bits);
1.1 root 414: * lp ++ = word1;
415:
416: /* The rest of the words are just mantissa bits. */
417: for (; lp < words + precision; lp++)
1.1.1.2 ! root 418: * lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
1.1 root 419:
1.1.1.2 ! root 420: if (next_bits (1)) {
1.1 root 421: unsigned long int carry;
422: /*
423: * Since the NEXT bit is a 1, round UP the mantissa.
424: * The cunning design of these hidden-1 floats permits
425: * us to let the mantissa overflow into the exponent, and
426: * it 'does the right thing'. However, we lose if the
427: * highest-order bit of the lowest-order word flips.
428: * Is that clear?
429: */
430:
431:
432: /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
433: Please allow at least 1 more bit in carry than is in a LITTLENUM.
434: We need that extra bit to hold a carry during a LITTLENUM carry
435: propagation. Another extra bit (kept 0) will assure us that we
436: don't get a sticky sign bit after shifting right, and that
437: permits us to propagate the carry without any masking of bits.
438: #endif */
439: for (carry = 1, lp --; carry && (lp >= words); lp --) {
440: carry = * lp + carry;
441: * lp = carry;
442: carry >>= LITTLENUM_NUMBER_OF_BITS;
443: }
444: if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
445: /* We leave return_value alone: admit we read the
446: * number, but return a floating exception
447: * because we can't encode the number.
448: */
449: make_invalid_floating_point_number (words);
450: return return_value;
451: }
452: }
453: return (return_value);
454: }
455:
456: /* This routine is a real kludge. Someone really should do it better, but
457: I'm too lazy, and I don't understand this stuff all too well anyway
458: (JF)
459: */
460: int_to_gen(x)
461: long x;
462: {
463: char buf[20];
464: char *bufp;
465:
466: sprintf(buf,"%ld",x);
467: bufp= &buf[0];
468: if(atof_generic(&bufp,".", EXP_CHARS, &generic_floating_point_number))
469: as_warn("Error converting number to floating point (Exponent overflow?)");
470: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.