atof-ns32k.c 12 KB

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