aboutsummaryrefslogtreecommitdiff
path: root/gas/config/atof-tahoe.c
blob: bd31eee18db19687f0ca08f884acdd28b9b4cf82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* atof_tahoe.c - turn a string into a Tahoe floating point number
   Copyright 1987, 1993, 2000 Free Software Foundation, Inc.

/* This is really a simplified version of atof_vax.c. I glommed it wholesale
   and then shaved it down. I don't even know how it works. (Don't you find
   my honesty refreshing?  Devon E Bowen <bowen@cs.buffalo.edu>

   I don't allow uppercase letters in the precision descrpitors.
   i.e. 'f' and 'd' are allowed but 'F' and 'D' aren't.  */

#include "as.h"

/* Precision in LittleNums.  */
#define MAX_PRECISION (4)
#define D_PRECISION (4)
#define F_PRECISION (2)

/* Precision in chars.  */
#define D_PRECISION_CHARS (8)
#define F_PRECISION_CHARS (4)

/* Length in LittleNums of guard bits.  */
#define GUARD (2)

static const long int mask[] =
{
  0x00000000,
  0x00000001,
  0x00000003,
  0x00000007,
  0x0000000f,
  0x0000001f,
  0x0000003f,
  0x0000007f,
  0x000000ff,
  0x000001ff,
  0x000003ff,
  0x000007ff,
  0x00000fff,
  0x00001fff,
  0x00003fff,
  0x00007fff,
  0x0000ffff,
  0x0001ffff,
  0x0003ffff,
  0x0007ffff,
  0x000fffff,
  0x001fffff,
  0x003fffff,
  0x007fffff,
  0x00ffffff,
  0x01ffffff,
  0x03ffffff,
  0x07ffffff,
  0x0fffffff,
  0x1fffffff,
  0x3fffffff,
  0x7fffffff,
  0xffffffff
};

/* Shared between flonum_gen2tahoe and next_bits.  */
static int bits_left_in_littlenum;
static LITTLENUM_TYPE *littlenum_pointer;
static LITTLENUM_TYPE *littlenum_end;

#if __STDC__ == 1

int flonum_gen2tahoe (int format_letter, FLONUM_TYPE * f,
		      LITTLENUM_TYPE * words);

#else /* not __STDC__  */

int flonum_gen2tahoe ();

#endif /* not __STDC__  */

static int
next_bits (number_of_bits)
     int number_of_bits;
{
  int return_value;

  if (littlenum_pointer < littlenum_end)
    return 0;
  if (number_of_bits >= bits_left_in_littlenum)
    {
      return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
      number_of_bits -= bits_left_in_littlenum;
      return_value <<= number_of_bits;
      bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
      littlenum_pointer--;
      if (littlenum_pointer >= littlenum_end)
	return_value |= ((*littlenum_pointer) >> (bits_left_in_littlenum)) &
	  mask[number_of_bits];
    }
  else
    {
      bits_left_in_littlenum -= number_of_bits;
      return_value = mask[number_of_bits] &
	((*littlenum_pointer) >> bits_left_in_littlenum);
    }
  return return_value;
}

static void
make_invalid_floating_point_number (words)
     LITTLENUM_TYPE *words;
{
  /* Floating Reserved Operand Code.  */
  *words = 0x8000;
}

static int			/* 0 means letter is OK.  */
what_kind_of_float (letter, precisionP, exponent_bitsP)
     /* In: lowercase please. What kind of float?  */
     char letter;

     /* Number of 16-bit words in the float.  */
     int *precisionP;

     /* Number of exponent bits.  */
     long int *exponent_bitsP;
{
  int retval;			/* 0: OK.  */

  retval = 0;
  switch (letter)
    {
    case 'f':
      *precisionP = F_PRECISION;
      *exponent_bitsP = 8;
      break;

    case 'd':
      *precisionP = D_PRECISION;
      *exponent_bitsP = 8;
      break;

    default:
      retval = 69;
      break;
    }
  return (retval);
}

/* Warning: This returns 16-bit LITTLENUMs, because that is what the
   VAX thinks in.  It is up to the caller to figure out any alignment
   problems and to conspire for the bytes/word to be emitted in the
   right order. Bigendians beware!  */

char *				/* Return pointer past text consumed.  */
atof_tahoe (str, what_kind, words)
     char *str;			/* Text to convert to binary.  */
     char what_kind;		/* 'd', 'f', 'g', 'h' */
     LITTLENUM_TYPE *words;	/* Build the binary here.  */
{
  FLONUM_TYPE f;
  LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
  /* Extra bits for zeroed low-order bits.  */
  /* The 1st MAX_PRECISION are zeroed, the last contain flonum bits.  */
  char *return_value;
  int precision;		/* Number of 16-bit words in the format.  */
  long int exponent_bits;

  return_value = str;
  f.low = bits + MAX_PRECISION;
  f.high = NULL;
  f.leader = NULL;
  f.exponent = NULL;
  f.sign = '\0';

  if (what_kind_of_float (what_kind, &precision, &exponent_bits))
    {
      /* We lost.  */
      return_value = NULL;
      make_invalid_floating_point_number (words);
    }
  if (return_value)
    {
      memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);

      /* Use more LittleNums than seems necessary:
	 the highest flonum may have 15 leading 0 bits, so could be
	 useless.  */
      f.high = f.low + precision - 1 + GUARD;

      if (atof_generic (&return_value, ".", "eE", &f))
	{
	  make_invalid_floating_point_number (words);
	  /* We lost.  */
	  return_value = NULL;
	}
      else
	{
	  if (flonum_gen2tahoe (what_kind, &f, words))
	    return_value = NULL;
	}
    }
  return return_value;
}

/* In: a flonum, a Tahoe floating point format.
   Out: a Tahoe floating-point bit pattern.  */

int				/* 0: OK.  */
flonum_gen2tahoe (format_letter, f, words)
     char format_letter;	/* One of 'd' 'f'.  */
     FLONUM_TYPE *f;
     LITTLENUM_TYPE *words;	/* Deliver answer here.  */
{
  LITTLENUM_TYPE *lp;
  int precision;
  long int exponent_bits;
  int return_value;		/* 0 == OK.  */

  return_value =
    what_kind_of_float (format_letter, &precision, &exponent_bits);
  if (return_value != 0)
    {
      make_invalid_floating_point_number (words);
    }
  else
    {
      if (f->low > f->leader)
	{
	  /* 0.0e0 seen.  */
	  memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
	}
      else
	{
	  long int exponent_1;
	  long int exponent_2;
	  long int exponent_3;
	  long int exponent_4;
	  int exponent_skippage;
	  LITTLENUM_TYPE word1;

	  /* JF: Deal with new Nan, +Inf and -Inf codes.  */
	  if (f->sign != '-' && f->sign != '+')
	    {
	      make_invalid_floating_point_number (words);
	      return return_value;
	    }
	  /* All tahoe floating_point formats have:
	     Bit 15 is sign bit.
	     Bits 14:n are excess-whatever exponent.
	     Bits n-1:0 (if any) are most significant bits of fraction.
	     Bits 15:0 of the next word are the next most significant bits.
	     And so on for each other word.

	     So we need: number of bits of exponent, number of bits of
	     mantissa.  */

	  bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
	  littlenum_pointer = f->leader;
	  littlenum_end = f->low;

	  /* Seek (and forget) 1st significant bit.  */
	  for (exponent_skippage = 0;
	       !next_bits (1);
	       exponent_skippage++)
	    ;

	  exponent_1 = f->exponent + f->leader + 1 - f->low;

	  /* Radix LITTLENUM_RADIX, point just higher than f -> leader.  */
	  exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;

	  /* Radix 2.  */
	  exponent_3 = exponent_2 - exponent_skippage;

	  /* Forget leading zeros, forget 1st bit.  */
	  exponent_4 = exponent_3 + (1 << (exponent_bits - 1));

	  /* Offset exponent.  */

	  if (exponent_4 & ~mask[exponent_bits])
	    {
	      /* Exponent overflow. Lose immediately.  */

	      make_invalid_floating_point_number (words);

	      /* We leave return_value alone: admit we read the
	        number, but return a floating exception because we
	        can't encode the number.  */
	    }
	  else
	    {
	      lp = words;

	      /* Word 1.  Sign, exponent and perhaps high bits.  */
	      /* Assume 2's complement integers.  */
	      word1 = ((exponent_4 & mask[exponent_bits])
		       << (15 - exponent_bits))
		| ((f->sign == '+') ? 0 : 0x8000)
		| next_bits (15 - exponent_bits);
	      *lp++ = word1;

	      /* The rest of the words are just mantissa bits.  */
	      for (; lp < words + precision; lp++)
		*lp = next_bits (LITTLENUM_NUMBER_OF_BITS);

	      if (next_bits (1))
		{
		  /* Since the NEXT bit is a 1, round UP the mantissa.
		     The cunning design of these hidden-1 floats permits
		     us to let the mantissa overflow into the exponent, and
		     it 'does the right thing'. However, we lose if the
		     highest-order bit of the lowest-order word flips.
		     Is that clear?  */

		  unsigned long int carry;

		  /* #if (sizeof(carry)) < ((sizeof(bits[0]) *
		     BITS_PER_CHAR) + 2) Please allow at least 1 more
		     bit in carry than is in a LITTLENUM.  We need
		     that extra bit to hold a carry during a LITTLENUM
		     carry propagation. Another extra bit (kept 0)
		     will assure us that we don't get a sticky sign
		     bit after shifting right, and that permits us to
		     propagate the carry without any masking of bits.
		     #endif  */
		  for (carry = 1, lp--;
		       carry && (lp >= words);
		       lp--)
		    {
		      carry = *lp + carry;
		      *lp = carry;
		      carry >>= LITTLENUM_NUMBER_OF_BITS;
		    }

		  if ((word1 ^ *words)
		      & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
		    {
		      make_invalid_floating_point_number (words);
		      /* We leave return_value alone: admit we read
			 the number, but return a floating exception
			 because we can't encode the number.  */
		    }
		}		/* if (we needed to round up)  */
	    }			/* if (exponent overflow)  */
	}			/* if (0.0e0)  */
    }				/* if (float_type was OK)  */
  return return_value;
}

/* In:	input_line_pointer -> the 1st character of a floating-point
 *		number.
 *	1 letter denoting the type of statement that wants a
 *		binary floating point number returned.
 *	Address of where to build floating point literal.
 *		Assumed to be 'big enough'.
 *	Address of where to return size of literal (in chars).
 *
 * Out:	Input_line_pointer -> of next char after floating number.
 *	Error message, or 0.
 *	Floating point literal.
 *	Number of chars we used for the literal.  */

char *
md_atof (what_statement_type, literalP, sizeP)
     char what_statement_type;
     char *literalP;
     int *sizeP;
{
  LITTLENUM_TYPE words[MAX_PRECISION];
  register char kind_of_float;
  register int number_of_chars;
  register LITTLENUM_TYPE *littlenum_pointer;

  switch (what_statement_type)
    {
    case 'f':			/* .ffloat  */
    case 'd':			/* .dfloat  */
      kind_of_float = what_statement_type;
      break;

    default:
      kind_of_float = 0;
      break;
    }

  if (kind_of_float)
    {
      register LITTLENUM_TYPE *limit;

      input_line_pointer = atof_tahoe (input_line_pointer,
				       kind_of_float,
				       words);
      /* The atof_tahoe() builds up 16-bit numbers.
	 Since the assembler may not be running on
	 a different-endian machine, be very careful about
	 converting words to chars.  */
      number_of_chars = (kind_of_float == 'f' ? F_PRECISION_CHARS :
			 (kind_of_float == 'd' ? D_PRECISION_CHARS : 0));
      know (number_of_chars <= MAX_PRECISION * sizeof (LITTLENUM_TYPE));
      limit = words + (number_of_chars / sizeof (LITTLENUM_TYPE));
      for (littlenum_pointer = words;
	   littlenum_pointer < limit;
	   littlenum_pointer++)
	{
	  md_number_to_chars (literalP, *littlenum_pointer,
			      sizeof (LITTLENUM_TYPE));
	  literalP += sizeof (LITTLENUM_TYPE);
	}
    }
  else
    {
      number_of_chars = 0;
    }

  *sizeP = number_of_chars;
  return kind_of_float ? 0 : _("Bad call to md_atof()");
}