atof-vax.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /* atof_vax.c - turn a Flonum into a VAX floating point number
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. /* JF added these two for md_atof() */
  16. #include "as.h"
  17. #include "read.h"
  18. #include "flonum.h"
  19. /* Precision in LittleNums. */
  20. #define MAX_PRECISION (8)
  21. #define H_PRECISION (8)
  22. #define G_PRECISION (4)
  23. #define D_PRECISION (4)
  24. #define F_PRECISION (2)
  25. /* Length in LittleNums of guard bits. */
  26. #define GUARD (2)
  27. int /* Number of chars in flonum type 'letter'. */
  28. atof_vax_sizeof (letter)
  29. char letter;
  30. {
  31. int return_value;
  32. /*
  33. * Permitting uppercase letters is probably a bad idea.
  34. * Please use only lower-cased letters in case the upper-cased
  35. * ones become unsupported!
  36. */
  37. switch (letter)
  38. {
  39. case 'f':
  40. case 'F':
  41. return_value = 4;
  42. break;
  43. case 'd':
  44. case 'D':
  45. case 'g':
  46. case 'G':
  47. return_value = 8;
  48. break;
  49. case 'h':
  50. case 'H':
  51. return_value = 16;
  52. break;
  53. default:
  54. return_value = 0;
  55. break;
  56. }
  57. return (return_value);
  58. } /* atof_vax_sizeof */
  59. static long int mask [] = {
  60. 0x00000000,
  61. 0x00000001,
  62. 0x00000003,
  63. 0x00000007,
  64. 0x0000000f,
  65. 0x0000001f,
  66. 0x0000003f,
  67. 0x0000007f,
  68. 0x000000ff,
  69. 0x000001ff,
  70. 0x000003ff,
  71. 0x000007ff,
  72. 0x00000fff,
  73. 0x00001fff,
  74. 0x00003fff,
  75. 0x00007fff,
  76. 0x0000ffff,
  77. 0x0001ffff,
  78. 0x0003ffff,
  79. 0x0007ffff,
  80. 0x000fffff,
  81. 0x001fffff,
  82. 0x003fffff,
  83. 0x007fffff,
  84. 0x00ffffff,
  85. 0x01ffffff,
  86. 0x03ffffff,
  87. 0x07ffffff,
  88. 0x0fffffff,
  89. 0x1fffffff,
  90. 0x3fffffff,
  91. 0x7fffffff,
  92. 0xffffffff
  93. };
  94. static int
  95. next_bits (number_of_bits, address_of_bits_left_in_littlenum, address_of_littlenum_pointer)
  96. int number_of_bits;
  97. int * address_of_bits_left_in_littlenum;
  98. LITTLENUM_TYPE ** address_of_littlenum_pointer;
  99. {
  100. int return_value;
  101. if (number_of_bits >= (* address_of_bits_left_in_littlenum))
  102. {
  103. return_value = mask [(* address_of_bits_left_in_littlenum)] & * (* address_of_littlenum_pointer);
  104. number_of_bits -= (* address_of_bits_left_in_littlenum);
  105. return_value <<= number_of_bits;
  106. (* address_of_bits_left_in_littlenum) = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  107. (* address_of_littlenum_pointer) --;
  108. return_value |= ( (* (* address_of_littlenum_pointer)) >> ((* address_of_bits_left_in_littlenum)) ) & mask [number_of_bits];
  109. }
  110. else
  111. {
  112. (* address_of_bits_left_in_littlenum) -= number_of_bits;
  113. return_value = mask [number_of_bits] & ( (* (* address_of_littlenum_pointer)) >> (* address_of_bits_left_in_littlenum));
  114. }
  115. return (return_value);
  116. }
  117. static void
  118. make_invalid_floating_point_number (words)
  119. LITTLENUM_TYPE * words;
  120. {
  121. * words = 0x8000; /* Floating Reserved Operand Code */
  122. }
  123. static int /* 0 means letter is OK. */
  124. what_kind_of_float (letter, precisionP, exponent_bitsP)
  125. char letter; /* In: lowercase please. What kind of float? */
  126. int * precisionP; /* Number of 16-bit words in the float. */
  127. long int * exponent_bitsP; /* Number of exponent bits. */
  128. {
  129. int retval; /* 0: OK. */
  130. retval = 0;
  131. switch (letter)
  132. {
  133. case 'f':
  134. * precisionP = F_PRECISION;
  135. * exponent_bitsP = 8;
  136. break;
  137. case 'd':
  138. * precisionP = D_PRECISION;
  139. * exponent_bitsP = 8;
  140. break;
  141. case 'g':
  142. * precisionP = G_PRECISION;
  143. * exponent_bitsP = 11;
  144. break;
  145. case 'h':
  146. * precisionP = H_PRECISION;
  147. * exponent_bitsP = 15;
  148. break;
  149. default:
  150. retval = 69;
  151. break;
  152. }
  153. return (retval);
  154. }
  155. /***********************************************************************\
  156. * *
  157. * Warning: this returns 16-bit LITTLENUMs, because that is *
  158. * what the VAX thinks in. It is up to the caller to figure *
  159. * out any alignment problems and to conspire for the bytes/word *
  160. * to be emitted in the right order. Bigendians beware! *
  161. * *
  162. \***********************************************************************/
  163. char * /* Return pointer past text consumed. */
  164. atof_vax (str, what_kind, words)
  165. char * str; /* Text to convert to binary. */
  166. char what_kind; /* 'd', 'f', 'g', 'h' */
  167. LITTLENUM_TYPE * words; /* Build the binary here. */
  168. {
  169. FLONUM_TYPE f;
  170. LITTLENUM_TYPE bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  171. /* Extra bits for zeroed low-order bits. */
  172. /* The 1st MAX_PRECISION are zeroed, */
  173. /* the last contain flonum bits. */
  174. char * return_value;
  175. int precision; /* Number of 16-bit words in the format. */
  176. long int exponent_bits;
  177. return_value = str;
  178. f . low = bits + MAX_PRECISION;
  179. f . high = NULL;
  180. f . leader = NULL;
  181. f . exponent = NULL;
  182. f . sign = '\0';
  183. if (what_kind_of_float (what_kind, & precision, & exponent_bits))
  184. {
  185. return_value = NULL; /* We lost. */
  186. make_invalid_floating_point_number (words);
  187. }
  188. if (return_value)
  189. {
  190. bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  191. /* Use more LittleNums than seems */
  192. /* necessary: the highest flonum may have */
  193. /* 15 leading 0 bits, so could be useless. */
  194. f . high = f . low + precision - 1 + GUARD;
  195. if (atof_generic (& return_value, ".", "eE", & f))
  196. {
  197. make_invalid_floating_point_number (words);
  198. return_value = NULL; /* we lost */
  199. }
  200. else
  201. {
  202. if (flonum_gen2vax (what_kind, & f, words))
  203. {
  204. return_value = NULL;
  205. }
  206. }
  207. }
  208. return (return_value);
  209. }
  210. /*
  211. * In: a flonum, a vax floating point format.
  212. * Out: a vax floating-point bit pattern.
  213. */
  214. int /* 0: OK. */
  215. flonum_gen2vax (format_letter, f, words)
  216. char format_letter; /* One of 'd' 'f' 'g' 'h'. */
  217. FLONUM_TYPE * f;
  218. LITTLENUM_TYPE * words; /* Deliver answer here. */
  219. {
  220. int bits_left_in_littlenum;
  221. LITTLENUM_TYPE * littlenum_pointer;
  222. LITTLENUM_TYPE * lp;
  223. int precision;
  224. long int exponent_bits;
  225. int return_value; /* 0 == OK. */
  226. return_value = what_kind_of_float (format_letter, & precision, & exponent_bits);
  227. if (return_value != 0)
  228. {
  229. make_invalid_floating_point_number (words);
  230. }
  231. else
  232. {
  233. if (f -> low > f -> leader)
  234. {
  235. /* 0.0e0 seen. */
  236. bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  237. }
  238. else
  239. {
  240. long int exponent_1;
  241. long int exponent_2;
  242. long int exponent_3;
  243. long int exponent_4;
  244. int exponent_skippage;
  245. LITTLENUM_TYPE word1;
  246. /* JF: Deal with new Nan, +Inf and -Inf codes */
  247. if(f->sign!='-' && f->sign!='+') {
  248. make_invalid_floating_point_number(words);
  249. return return_value;
  250. }
  251. /*
  252. * All vaxen floating_point formats (so far) have:
  253. * Bit 15 is sign bit.
  254. * Bits 14:n are excess-whatever exponent.
  255. * Bits n-1:0 (if any) are most significant bits of fraction.
  256. * Bits 15:0 of the next word are the next most significant bits.
  257. * And so on for each other word.
  258. *
  259. * All this to be compatible with a KF11?? (Which is still faster
  260. * than lots of vaxen I can think of, but it also has higher
  261. * maintenance costs ... sigh).
  262. *
  263. * So we need: number of bits of exponent, number of bits of
  264. * mantissa.
  265. */
  266. #ifdef NEVER /******* This zeroing seems redundant - Dean 3may86 **********/
  267. /*
  268. * No matter how few bits we got back from the atof()
  269. * routine, add enough zero littlenums so the rest of the
  270. * code won't run out of "significant" bits in the mantissa.
  271. */
  272. {
  273. LITTLENUM_TYPE * ltp;
  274. for (ltp = f -> leader + 1;
  275. ltp <= f -> low + precision;
  276. ltp ++)
  277. {
  278. * ltp = 0;
  279. }
  280. }
  281. #endif
  282. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  283. littlenum_pointer = f -> leader;
  284. /* Seek (and forget) 1st significant bit */
  285. for (exponent_skippage = 0;
  286. ! next_bits(1, &bits_left_in_littlenum, &littlenum_pointer);
  287. exponent_skippage ++)
  288. {
  289. }
  290. exponent_1 = f -> exponent + f -> leader + 1 - f -> low;
  291. /* Radix LITTLENUM_RADIX, point just higher than f -> leader. */
  292. exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  293. /* Radix 2. */
  294. exponent_3 = exponent_2 - exponent_skippage;
  295. /* Forget leading zeros, forget 1st bit. */
  296. exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
  297. /* Offset exponent. */
  298. if (exponent_4 & ~ mask [exponent_bits])
  299. {
  300. /*
  301. * Exponent overflow. Lose immediately.
  302. */
  303. make_invalid_floating_point_number (words);
  304. /*
  305. * We leave return_value alone: admit we read the
  306. * number, but return a floating exception
  307. * because we can't encode the number.
  308. */
  309. }
  310. else
  311. {
  312. lp = words;
  313. /* Word 1. Sign, exponent and perhaps high bits. */
  314. /* Assume 2's complement integers. */
  315. word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits))
  316. | ((f -> sign == '+') ? 0 : 0x8000)
  317. | next_bits (15 - exponent_bits, &bits_left_in_littlenum, &littlenum_pointer);
  318. * lp ++ = word1;
  319. /* The rest of the words are just mantissa bits. */
  320. for (; lp < words + precision; lp++)
  321. {
  322. * lp = next_bits (LITTLENUM_NUMBER_OF_BITS, &bits_left_in_littlenum, &littlenum_pointer);
  323. }
  324. if (next_bits (1, &bits_left_in_littlenum, &littlenum_pointer))
  325. {
  326. /*
  327. * Since the NEXT bit is a 1, round UP the mantissa.
  328. * The cunning design of these hidden-1 floats permits
  329. * us to let the mantissa overflow into the exponent, and
  330. * it 'does the right thing'. However, we lose if the
  331. * highest-order bit of the lowest-order word flips.
  332. * Is that clear?
  333. */
  334. unsigned long int carry;
  335. /*
  336. #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  337. Please allow at least 1 more bit in carry than is in a LITTLENUM.
  338. We need that extra bit to hold a carry during a LITTLENUM carry
  339. propagation. Another extra bit (kept 0) will assure us that we
  340. don't get a sticky sign bit after shifting right, and that
  341. permits us to propagate the carry without any masking of bits.
  342. #endif
  343. */
  344. for (carry = 1, lp --;
  345. carry && (lp >= words);
  346. lp --)
  347. {
  348. carry = * lp + carry;
  349. * lp = carry;
  350. carry >>= LITTLENUM_NUMBER_OF_BITS;
  351. }
  352. if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) )
  353. {
  354. make_invalid_floating_point_number (words);
  355. /*
  356. * We leave return_value alone: admit we read the
  357. * number, but return a floating exception
  358. * because we can't encode the number.
  359. */
  360. }
  361. } /* if (we needed to round up) */
  362. } /* if (exponent overflow) */
  363. } /* if (0.0e0) */
  364. } /* if (float_type was OK) */
  365. return (return_value);
  366. }
  367. /* JF this used to be in vax.c but this looks like a better place for it */
  368. /*
  369. * md_atof()
  370. *
  371. * In: input_line_pointer -> the 1st character of a floating-point
  372. * number.
  373. * 1 letter denoting the type of statement that wants a
  374. * binary floating point number returned.
  375. * Address of where to build floating point literal.
  376. * Assumed to be 'big enough'.
  377. * Address of where to return size of literal (in chars).
  378. *
  379. * Out: Input_line_pointer -> of next char after floating number.
  380. * Error message, or "".
  381. * Floating point literal.
  382. * Number of chars we used for the literal.
  383. */
  384. int atof_vax_sizeof();
  385. #define MAXIMUM_NUMBER_OF_LITTLENUMS (8) /* For .hfloats. */
  386. char *
  387. md_atof (what_statement_type, literalP, sizeP)
  388. char what_statement_type;
  389. char * literalP;
  390. int * sizeP;
  391. {
  392. LITTLENUM_TYPE words [MAXIMUM_NUMBER_OF_LITTLENUMS];
  393. register char kind_of_float;
  394. register int number_of_chars;
  395. register LITTLENUM_TYPE * littlenum_pointer;
  396. switch (what_statement_type)
  397. {
  398. case 'F': /* .float */
  399. case 'f': /* .ffloat */
  400. kind_of_float = 'f';
  401. break;
  402. case 'D': /* .double */
  403. case 'd': /* .dfloat */
  404. kind_of_float = 'd';
  405. break;
  406. case 'g': /* .gfloat */
  407. kind_of_float = 'g';
  408. break;
  409. case 'h': /* .hfloat */
  410. kind_of_float = 'h';
  411. break;
  412. default:
  413. kind_of_float = 0;
  414. break;
  415. };
  416. if (kind_of_float)
  417. {
  418. register LITTLENUM_TYPE * limit;
  419. char * atof_vax();
  420. input_line_pointer = atof_vax (input_line_pointer,
  421. kind_of_float,
  422. words);
  423. /*
  424. * The atof_vax() builds up 16-bit numbers.
  425. * Since the assembler may not be running on
  426. * a little-endian machine, be very careful about
  427. * converting words to chars.
  428. */
  429. number_of_chars = atof_vax_sizeof (kind_of_float);
  430. know( number_of_chars <= MAXIMUM_NUMBER_OF_LITTLENUMS * sizeof(LITTLENUM_TYPE) );
  431. limit = words + (number_of_chars / sizeof(LITTLENUM_TYPE));
  432. for (littlenum_pointer = words;
  433. littlenum_pointer < limit;
  434. littlenum_pointer ++)
  435. {
  436. md_number_to_chars (literalP, * littlenum_pointer, sizeof(LITTLENUM_TYPE));
  437. literalP += sizeof(LITTLENUM_TYPE);
  438. };
  439. }
  440. else
  441. {
  442. number_of_chars = 0;
  443. };
  444. * sizeP = number_of_chars;
  445. return (kind_of_float ? "" : "Bad call to md_atof()");
  446. } /* md_atof() */
  447. /* atof_vax.c */