atof-vax.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* atof_vax.c - turn a Flonum into a VAX floating point number
  2. Copyright (C) 1987-2015 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 3, 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 the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. /* Precision in LittleNums. */
  18. #define MAX_PRECISION 8
  19. #define H_PRECISION 8
  20. #define G_PRECISION 4
  21. #define D_PRECISION 4
  22. #define F_PRECISION 2
  23. /* Length in LittleNums of guard bits. */
  24. #define GUARD 2
  25. int flonum_gen2vax (int, FLONUM_TYPE *, LITTLENUM_TYPE *);
  26. /* Number of chars in flonum type 'letter'. */
  27. static unsigned int
  28. atof_vax_sizeof (int letter)
  29. {
  30. int return_value;
  31. /* Permitting uppercase letters is probably a bad idea.
  32. Please use only lower-cased letters in case the upper-cased
  33. ones become unsupported! */
  34. switch (letter)
  35. {
  36. case 'f':
  37. case 'F':
  38. return_value = 4;
  39. break;
  40. case 'd':
  41. case 'D':
  42. case 'g':
  43. case 'G':
  44. return_value = 8;
  45. break;
  46. case 'h':
  47. case 'H':
  48. return_value = 16;
  49. break;
  50. default:
  51. return_value = 0;
  52. break;
  53. }
  54. return return_value;
  55. }
  56. static const long mask[] =
  57. {
  58. 0x00000000,
  59. 0x00000001,
  60. 0x00000003,
  61. 0x00000007,
  62. 0x0000000f,
  63. 0x0000001f,
  64. 0x0000003f,
  65. 0x0000007f,
  66. 0x000000ff,
  67. 0x000001ff,
  68. 0x000003ff,
  69. 0x000007ff,
  70. 0x00000fff,
  71. 0x00001fff,
  72. 0x00003fff,
  73. 0x00007fff,
  74. 0x0000ffff,
  75. 0x0001ffff,
  76. 0x0003ffff,
  77. 0x0007ffff,
  78. 0x000fffff,
  79. 0x001fffff,
  80. 0x003fffff,
  81. 0x007fffff,
  82. 0x00ffffff,
  83. 0x01ffffff,
  84. 0x03ffffff,
  85. 0x07ffffff,
  86. 0x0fffffff,
  87. 0x1fffffff,
  88. 0x3fffffff,
  89. 0x7fffffff,
  90. 0xffffffff
  91. };
  92. /* Shared between flonum_gen2vax and next_bits. */
  93. static int bits_left_in_littlenum;
  94. static LITTLENUM_TYPE *littlenum_pointer;
  95. static LITTLENUM_TYPE *littlenum_end;
  96. static int
  97. next_bits (int number_of_bits)
  98. {
  99. int return_value;
  100. if (littlenum_pointer < littlenum_end)
  101. return 0;
  102. if (number_of_bits >= bits_left_in_littlenum)
  103. {
  104. return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
  105. number_of_bits -= bits_left_in_littlenum;
  106. return_value <<= number_of_bits;
  107. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  108. littlenum_pointer--;
  109. if (littlenum_pointer >= littlenum_end)
  110. return_value |= ((*littlenum_pointer) >> (bits_left_in_littlenum)) & mask[number_of_bits];
  111. }
  112. else
  113. {
  114. bits_left_in_littlenum -= number_of_bits;
  115. return_value = mask[number_of_bits] & ((*littlenum_pointer) >> bits_left_in_littlenum);
  116. }
  117. return return_value;
  118. }
  119. static void
  120. make_invalid_floating_point_number (LITTLENUM_TYPE *words)
  121. {
  122. *words = 0x8000; /* Floating Reserved Operand Code. */
  123. }
  124. static int /* 0 means letter is OK. */
  125. what_kind_of_float (int letter, /* In: lowercase please. What kind of float? */
  126. int *precisionP, /* Number of 16-bit words in the float. */
  127. long *exponent_bitsP) /* Number of exponent bits. */
  128. {
  129. int retval;
  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. /* Warning: this returns 16-bit LITTLENUMs, because that is
  156. what the VAX thinks in. It is up to the caller to figure
  157. out any alignment problems and to conspire for the bytes/word
  158. to be emitted in the right order. Bigendians beware! */
  159. static char *
  160. atof_vax (char *str, /* Text to convert to binary. */
  161. int what_kind, /* 'd', 'f', 'g', 'h' */
  162. LITTLENUM_TYPE *words) /* Build the binary here. */
  163. {
  164. FLONUM_TYPE f;
  165. LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
  166. /* Extra bits for zeroed low-order bits.
  167. The 1st MAX_PRECISION are zeroed,
  168. the last contain flonum bits. */
  169. char *return_value;
  170. int precision; /* Number of 16-bit words in the format. */
  171. long exponent_bits;
  172. return_value = str;
  173. f.low = bits + MAX_PRECISION;
  174. f.high = NULL;
  175. f.leader = NULL;
  176. f.exponent = 0;
  177. f.sign = '\0';
  178. if (what_kind_of_float (what_kind, &precision, &exponent_bits))
  179. {
  180. return_value = NULL;
  181. make_invalid_floating_point_number (words);
  182. }
  183. if (return_value)
  184. {
  185. memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
  186. /* Use more LittleNums than seems
  187. necessary: the highest flonum may have
  188. 15 leading 0 bits, so could be useless. */
  189. f.high = f.low + precision - 1 + GUARD;
  190. if (atof_generic (&return_value, ".", "eE", &f))
  191. {
  192. make_invalid_floating_point_number (words);
  193. return_value = NULL;
  194. }
  195. else if (flonum_gen2vax (what_kind, &f, words))
  196. return_value = NULL;
  197. }
  198. return return_value;
  199. }
  200. /* In: a flonum, a vax floating point format.
  201. Out: a vax floating-point bit pattern. */
  202. int
  203. flonum_gen2vax (int format_letter, /* One of 'd' 'f' 'g' 'h'. */
  204. FLONUM_TYPE *f,
  205. LITTLENUM_TYPE *words) /* Deliver answer here. */
  206. {
  207. LITTLENUM_TYPE *lp;
  208. int precision;
  209. long exponent_bits;
  210. int return_value; /* 0 == OK. */
  211. return_value = what_kind_of_float (format_letter, &precision, &exponent_bits);
  212. if (return_value != 0)
  213. make_invalid_floating_point_number (words);
  214. else
  215. {
  216. if (f->low > f->leader)
  217. /* 0.0e0 seen. */
  218. memset (words, '\0', sizeof (LITTLENUM_TYPE) * precision);
  219. else
  220. {
  221. long exponent_1;
  222. long exponent_2;
  223. long exponent_3;
  224. long exponent_4;
  225. int exponent_skippage;
  226. LITTLENUM_TYPE word1;
  227. /* JF: Deal with new Nan, +Inf and -Inf codes. */
  228. if (f->sign != '-' && f->sign != '+')
  229. {
  230. make_invalid_floating_point_number (words);
  231. return return_value;
  232. }
  233. /* All vaxen floating_point formats (so far) have:
  234. Bit 15 is sign bit.
  235. Bits 14:n are excess-whatever exponent.
  236. Bits n-1:0 (if any) are most significant bits of fraction.
  237. Bits 15:0 of the next word are the next most significant bits.
  238. And so on for each other word.
  239. All this to be compatible with a KF11?? (Which is still faster
  240. than lots of vaxen I can think of, but it also has higher
  241. maintenance costs ... sigh).
  242. So we need: number of bits of exponent, number of bits of
  243. mantissa. */
  244. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  245. littlenum_pointer = f->leader;
  246. littlenum_end = f->low;
  247. /* Seek (and forget) 1st significant bit. */
  248. for (exponent_skippage = 0;
  249. !next_bits (1);
  250. exponent_skippage++);
  251. exponent_1 = f->exponent + f->leader + 1 - f->low;
  252. /* Radix LITTLENUM_RADIX, point just higher than f->leader. */
  253. exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  254. /* Radix 2. */
  255. exponent_3 = exponent_2 - exponent_skippage;
  256. /* Forget leading zeros, forget 1st bit. */
  257. exponent_4 = exponent_3 + (1 << (exponent_bits - 1));
  258. /* Offset exponent. */
  259. if (exponent_4 & ~mask[exponent_bits])
  260. {
  261. /* Exponent overflow. Lose immediately. */
  262. make_invalid_floating_point_number (words);
  263. /* We leave return_value alone: admit we read the
  264. number, but return a floating exception
  265. because we can't encode the number. */
  266. }
  267. else
  268. {
  269. lp = words;
  270. /* Word 1. Sign, exponent and perhaps high bits.
  271. Assume 2's complement integers. */
  272. word1 = (((exponent_4 & mask[exponent_bits]) << (15 - exponent_bits))
  273. | ((f->sign == '+') ? 0 : 0x8000)
  274. | next_bits (15 - exponent_bits));
  275. *lp++ = word1;
  276. /* The rest of the words are just mantissa bits. */
  277. for (; lp < words + precision; lp++)
  278. *lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
  279. if (next_bits (1))
  280. {
  281. /* Since the NEXT bit is a 1, round UP the mantissa.
  282. The cunning design of these hidden-1 floats permits
  283. us to let the mantissa overflow into the exponent, and
  284. it 'does the right thing'. However, we lose if the
  285. highest-order bit of the lowest-order word flips.
  286. Is that clear? */
  287. unsigned long carry;
  288. /*
  289. #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  290. Please allow at least 1 more bit in carry than is in a LITTLENUM.
  291. We need that extra bit to hold a carry during a LITTLENUM carry
  292. propagation. Another extra bit (kept 0) will assure us that we
  293. don't get a sticky sign bit after shifting right, and that
  294. permits us to propagate the carry without any masking of bits.
  295. #endif */
  296. for (carry = 1, lp--;
  297. carry && (lp >= words);
  298. lp--)
  299. {
  300. carry = *lp + carry;
  301. *lp = carry;
  302. carry >>= LITTLENUM_NUMBER_OF_BITS;
  303. }
  304. if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
  305. {
  306. make_invalid_floating_point_number (words);
  307. /* We leave return_value alone: admit we read the
  308. number, but return a floating exception
  309. because we can't encode the number. */
  310. }
  311. }
  312. }
  313. }
  314. }
  315. return return_value;
  316. }
  317. /* JF this used to be in vax.c but this looks like a better place for it. */
  318. /* In: input_line_pointer->the 1st character of a floating-point
  319. number.
  320. 1 letter denoting the type of statement that wants a
  321. binary floating point number returned.
  322. Address of where to build floating point literal.
  323. Assumed to be 'big enough'.
  324. Address of where to return size of literal (in chars).
  325. Out: Input_line_pointer->of next char after floating number.
  326. Error message, or 0.
  327. Floating point literal.
  328. Number of chars we used for the literal. */
  329. #define MAXIMUM_NUMBER_OF_LITTLENUMS 8 /* For .hfloats. */
  330. char *
  331. vax_md_atof (int what_statement_type,
  332. char *literalP,
  333. int *sizeP)
  334. {
  335. LITTLENUM_TYPE words[MAXIMUM_NUMBER_OF_LITTLENUMS];
  336. char kind_of_float;
  337. unsigned int number_of_chars;
  338. LITTLENUM_TYPE *littlenumP;
  339. switch (what_statement_type)
  340. {
  341. case 'F':
  342. case 'f':
  343. kind_of_float = 'f';
  344. break;
  345. case 'D':
  346. case 'd':
  347. kind_of_float = 'd';
  348. break;
  349. case 'g':
  350. kind_of_float = 'g';
  351. break;
  352. case 'h':
  353. kind_of_float = 'h';
  354. break;
  355. default:
  356. kind_of_float = 0;
  357. break;
  358. };
  359. if (kind_of_float)
  360. {
  361. LITTLENUM_TYPE *limit;
  362. input_line_pointer = atof_vax (input_line_pointer,
  363. kind_of_float,
  364. words);
  365. /* The atof_vax() builds up 16-bit numbers.
  366. Since the assembler may not be running on
  367. a little-endian machine, be very careful about
  368. converting words to chars. */
  369. number_of_chars = atof_vax_sizeof (kind_of_float);
  370. know (number_of_chars <= MAXIMUM_NUMBER_OF_LITTLENUMS * sizeof (LITTLENUM_TYPE));
  371. limit = words + (number_of_chars / sizeof (LITTLENUM_TYPE));
  372. for (littlenumP = words; littlenumP < limit; littlenumP++)
  373. {
  374. md_number_to_chars (literalP, *littlenumP, sizeof (LITTLENUM_TYPE));
  375. literalP += sizeof (LITTLENUM_TYPE);
  376. };
  377. }
  378. else
  379. number_of_chars = 0;
  380. *sizeP = number_of_chars;
  381. return kind_of_float ? NULL : _("Unrecognized or unsupported floating point constant");
  382. }