atof-i386.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /* atof_i386.c -- turn a Flonum into an i386 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. #include "flonum.h"
  16. #ifdef USG
  17. #define bzero(s,n) memset(s,0,n)
  18. #endif
  19. extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
  20. #define NULL (0)
  21. extern char EXP_CHARS[];
  22. /* Precision in LittleNums. */
  23. #define MAX_PRECISION (6)
  24. #define F_PRECISION (2)
  25. #define D_PRECISION (4)
  26. #define X_PRECISION (5)
  27. /* Length in LittleNums of guard bits. */
  28. #define GUARD (2)
  29. int /* Number of chars in flonum type 'letter'. */
  30. atof_sizeof (letter)
  31. char letter;
  32. {
  33. int return_value;
  34. /*
  35. * Permitting uppercase letters is probably a bad idea.
  36. * Please use only lower-cased letters in case the upper-cased
  37. * ones become unsupported!
  38. */
  39. switch (letter)
  40. {
  41. case 'f':
  42. case 'F':
  43. return_value = F_PRECISION;
  44. break;
  45. case 'd':
  46. case 'D':
  47. return_value = D_PRECISION;
  48. break;
  49. case 'x':
  50. case 'X':
  51. return_value = X_PRECISION;
  52. break;
  53. default:
  54. return_value = 0;
  55. break;
  56. }
  57. return (return_value);
  58. }
  59. static unsigned 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 bits_left_in_littlenum;
  95. static int littlenums_left;
  96. static LITTLENUM_TYPE * littlenum_pointer;
  97. static int
  98. next_bits (number_of_bits)
  99. int number_of_bits;
  100. {
  101. int return_value;
  102. if(!littlenums_left)
  103. return 0;
  104. if (number_of_bits >= bits_left_in_littlenum) {
  105. return_value = mask [bits_left_in_littlenum] & *littlenum_pointer;
  106. number_of_bits -= bits_left_in_littlenum;
  107. return_value <<= number_of_bits;
  108. if(littlenums_left) {
  109. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
  110. littlenum_pointer --;
  111. --littlenums_left;
  112. return_value |=
  113. (*littlenum_pointer>>bits_left_in_littlenum) & mask[number_of_bits];
  114. }
  115. } else {
  116. bits_left_in_littlenum -= number_of_bits;
  117. return_value =
  118. mask [number_of_bits] & (*littlenum_pointer>>bits_left_in_littlenum);
  119. }
  120. return (return_value);
  121. }
  122. static void
  123. make_invalid_floating_point_number (words, precision)
  124. LITTLENUM_TYPE * words;
  125. int precision;
  126. {
  127. bzero (words, precision * sizeof (LITTLENUM_TYPE));
  128. switch (precision) {
  129. case F_PRECISION:
  130. words[0] = 0xffc0; break;
  131. case D_PRECISION:
  132. words[0] = 0xfff8; break;
  133. case X_PRECISION:
  134. words[0] = 0xffff; words[1] = 0xc000; break;
  135. }
  136. }
  137. /***********************************************************************\
  138. * *
  139. * Warning: this returns 16-bit LITTLENUMs, because that is *
  140. * what the VAX thinks in. It is up to the caller to figure *
  141. * out any alignment problems and to conspire for the bytes/word *
  142. * to be emitted in the right order. Bigendians beware! *
  143. * *
  144. \***********************************************************************/
  145. char * /* Return pointer past text consumed. */
  146. atof_i386 (str, what_kind, words)
  147. char * str; /* Text to convert to binary. */
  148. char what_kind; /* 'd', 'f', 'g', 'h' */
  149. LITTLENUM_TYPE * words; /* Build the binary here. */
  150. {
  151. FLONUM_TYPE f;
  152. LITTLENUM_TYPE bits [MAX_PRECISION + MAX_PRECISION + GUARD];
  153. /* Extra bits for zeroed low-order bits. */
  154. /* The 1st MAX_PRECISION are zeroed, */
  155. /* the last contain flonum bits. */
  156. char * return_value;
  157. int precision; /* Number of 16-bit words in the format. */
  158. long int exponent_bits;
  159. long int exponent_1;
  160. long int exponent_2;
  161. long int exponent_3;
  162. long int exponent_4;
  163. int exponent_skippage;
  164. LITTLENUM_TYPE word1;
  165. LITTLENUM_TYPE * lp;
  166. return_value = str;
  167. f.low = bits + MAX_PRECISION;
  168. f.high = NULL;
  169. f.leader = NULL;
  170. f.exponent = NULL;
  171. f.sign = '\0';
  172. /* Use more LittleNums than seems */
  173. /* necessary: the highest flonum may have */
  174. /* 15 leading 0 bits, so could be useless. */
  175. bzero (bits, sizeof(LITTLENUM_TYPE) * MAX_PRECISION);
  176. switch(what_kind) {
  177. case 'f':
  178. case 'F':
  179. precision = F_PRECISION;
  180. exponent_bits = 8;
  181. break;
  182. case 'd':
  183. case 'D':
  184. precision = D_PRECISION;
  185. exponent_bits = 11;
  186. break;
  187. case 'x':
  188. case 'X':
  189. precision = X_PRECISION;
  190. exponent_bits = 15;
  191. break;
  192. default:
  193. make_invalid_floating_point_number (words, precision);
  194. return NULL;
  195. }
  196. f.high = f.low + precision - 1 + GUARD;
  197. if (atof_generic (& return_value, ".", EXP_CHARS, & f)) {
  198. as_warn("Error converting floating point number (Exponent overflow?)");
  199. make_invalid_floating_point_number (words, precision);
  200. return NULL;
  201. }
  202. if (f.low > f.leader) {
  203. /* 0.0e0 seen. */
  204. bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  205. return return_value;
  206. }
  207. if(f.sign!='+' && f.sign!='-') {
  208. make_invalid_floating_point_number(words,precision);
  209. return NULL;
  210. }
  211. /*
  212. * All vaxen floating_point formats (so far) have:
  213. * Bit 15 is sign bit.
  214. * Bits 14:n are excess-whatever exponent.
  215. * Bits n-1:0 (if any) are most significant bits of fraction.
  216. * Bits 15:0 of the next word are the next most significant bits.
  217. * And so on for each other word.
  218. *
  219. * So we need: number of bits of exponent, number of bits of
  220. * mantissa.
  221. */
  222. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  223. littlenum_pointer = f.leader;
  224. littlenums_left = 1 + f.leader-f.low;
  225. if (precision != X_PRECISION) {
  226. /* Seek (and forget) 1st significant bit */
  227. for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++) ;
  228. } else {
  229. /* Dont seek (and forget) 1st significant bit for X format */
  230. for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++) ;
  231. exponent_skippage--;
  232. bits_left_in_littlenum++;
  233. }
  234. exponent_1 = f.exponent + f.leader + 1 - f.low;
  235. /* Radix LITTLENUM_RADIX, point just higher than f.leader. */
  236. exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  237. /* Radix 2. */
  238. exponent_3 = exponent_2 - exponent_skippage;
  239. /* Forget leading zeros, forget 1st bit. */
  240. exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  241. /* Offset exponent. */
  242. if (exponent_4 & ~ mask [exponent_bits]) {
  243. /*
  244. * Exponent overflow. Lose immediately.
  245. */
  246. /*
  247. * We leave return_value alone: admit we read the
  248. * number, but return a floating exception
  249. * because we can't encode the number.
  250. */
  251. as_warn("Exponent overflow in floating-point number");
  252. make_invalid_floating_point_number (words, precision);
  253. return return_value;
  254. }
  255. lp = words;
  256. /* Word 1. Sign, exponent and perhaps high bits. */
  257. /* Assume 2's complement integers. */
  258. word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits));
  259. word1 |= ((f.sign == '+') ? 0 : 0x8000);
  260. word1 |= next_bits (15 - exponent_bits);
  261. * lp ++ = word1;
  262. /* The rest of the words are just mantissa bits. */
  263. for (; lp < words + precision; lp++)
  264. * lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
  265. if (next_bits (1)) {
  266. unsigned long int carry;
  267. /*
  268. * Since the NEXT bit is a 1, round UP the mantissa.
  269. * The cunning design of these hidden-1 floats permits
  270. * us to let the mantissa overflow into the exponent, and
  271. * it 'does the right thing'. However, we lose if the
  272. * highest-order bit of the lowest-order word flips.
  273. * Is that clear?
  274. */
  275. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  276. Please allow at least 1 more bit in carry than is in a LITTLENUM.
  277. We need that extra bit to hold a carry during a LITTLENUM carry
  278. propagation. Another extra bit (kept 0) will assure us that we
  279. don't get a sticky sign bit after shifting right, and that
  280. permits us to propagate the carry without any masking of bits.
  281. #endif */
  282. for (carry = 1, lp --; carry && (lp >= words); lp --) {
  283. carry = * lp + carry;
  284. * lp = carry;
  285. carry >>= LITTLENUM_NUMBER_OF_BITS;
  286. }
  287. if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  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. make_invalid_floating_point_number (words, precision);
  293. return return_value;
  294. }
  295. }
  296. return (return_value);
  297. }
  298. /* This is really identical to atof_m68k except for some details */
  299. gen_to_words(words,precision,exponent_bits)
  300. LITTLENUM_TYPE *words;
  301. long int exponent_bits;
  302. {
  303. int return_value=0;
  304. long int exponent_1;
  305. long int exponent_2;
  306. long int exponent_3;
  307. long int exponent_4;
  308. int exponent_skippage;
  309. LITTLENUM_TYPE word1;
  310. LITTLENUM_TYPE * lp;
  311. if (generic_floating_point_number.low > generic_floating_point_number.leader) {
  312. /* 0.0e0 seen. */
  313. bzero (words, sizeof(LITTLENUM_TYPE) * precision);
  314. return return_value;
  315. }
  316. /*
  317. * All vaxen floating_point formats (so far) have:
  318. * Bit 15 is sign bit.
  319. * Bits 14:n are excess-whatever exponent.
  320. * Bits n-1:0 (if any) are most significant bits of fraction.
  321. * Bits 15:0 of the next word are the next most significant bits.
  322. * And so on for each other word.
  323. *
  324. * So we need: number of bits of exponent, number of bits of
  325. * mantissa.
  326. */
  327. bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
  328. littlenum_pointer = generic_floating_point_number.leader;
  329. littlenums_left = 1+generic_floating_point_number.leader - generic_floating_point_number.low;
  330. /* Seek (and forget) 1st significant bit */
  331. for (exponent_skippage = 0;! next_bits(1); exponent_skippage ++)
  332. ;
  333. exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader + 1 -
  334. generic_floating_point_number.low;
  335. /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
  336. exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
  337. /* Radix 2. */
  338. exponent_3 = exponent_2 - exponent_skippage;
  339. /* Forget leading zeros, forget 1st bit. */
  340. exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
  341. /* Offset exponent. */
  342. if (exponent_4 & ~ mask [exponent_bits]) {
  343. /*
  344. * Exponent overflow. Lose immediately.
  345. */
  346. /*
  347. * We leave return_value alone: admit we read the
  348. * number, but return a floating exception
  349. * because we can't encode the number.
  350. */
  351. make_invalid_floating_point_number (words, precision);
  352. return return_value;
  353. }
  354. lp = words;
  355. /* Word 1. Sign, exponent and perhaps high bits. */
  356. /* Assume 2's complement integers. */
  357. word1 = ((exponent_4 & mask [exponent_bits]) << (15 - exponent_bits));
  358. word1 |= ((generic_floating_point_number.sign == '+') ? 0 : 0x8000);
  359. word1 |= next_bits (15 - exponent_bits);
  360. * lp ++ = word1;
  361. /* The rest of the words are just mantissa bits. */
  362. for (; lp < words + precision; lp++)
  363. * lp = next_bits (LITTLENUM_NUMBER_OF_BITS);
  364. if (next_bits (1)) {
  365. unsigned long int carry;
  366. /*
  367. * Since the NEXT bit is a 1, round UP the mantissa.
  368. * The cunning design of these hidden-1 floats permits
  369. * us to let the mantissa overflow into the exponent, and
  370. * it 'does the right thing'. However, we lose if the
  371. * highest-order bit of the lowest-order word flips.
  372. * Is that clear?
  373. */
  374. /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
  375. Please allow at least 1 more bit in carry than is in a LITTLENUM.
  376. We need that extra bit to hold a carry during a LITTLENUM carry
  377. propagation. Another extra bit (kept 0) will assure us that we
  378. don't get a sticky sign bit after shifting right, and that
  379. permits us to propagate the carry without any masking of bits.
  380. #endif */
  381. for (carry = 1, lp --; carry && (lp >= words); lp --) {
  382. carry = * lp + carry;
  383. * lp = carry;
  384. carry >>= LITTLENUM_NUMBER_OF_BITS;
  385. }
  386. if ( (word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)) ) {
  387. /* We leave return_value alone: admit we read the
  388. * number, but return a floating exception
  389. * because we can't encode the number.
  390. */
  391. make_invalid_floating_point_number (words, precision);
  392. return return_value;
  393. }
  394. }
  395. return (return_value);
  396. }
  397. /* This routine is a real kludge. Someone really should do it better, but
  398. I'm too lazy, and I don't understand this stuff all too well anyway
  399. (JF)
  400. */
  401. int_to_gen(x)
  402. long x;
  403. {
  404. char buf[20];
  405. char *bufp;
  406. sprintf(buf,"%ld",x);
  407. bufp= &buf[0];
  408. if(atof_generic(&bufp,".", EXP_CHARS, &generic_floating_point_number))
  409. as_warn("Error converting number to floating point (Exponent overflow?)");
  410. }