atof-generic.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* atof_generic.c - turn a string of digits into a Flonum
  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, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. 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. #include "safe-ctype.h"
  18. #ifndef FALSE
  19. #define FALSE (0)
  20. #endif
  21. #ifndef TRUE
  22. #define TRUE (1)
  23. #endif
  24. #ifdef TRACE
  25. static void flonum_print (const FLONUM_TYPE *);
  26. #endif
  27. #define ASSUME_DECIMAL_MARK_IS_DOT
  28. /***********************************************************************\
  29. * *
  30. * Given a string of decimal digits , with optional decimal *
  31. * mark and optional decimal exponent (place value) of the *
  32. * lowest_order decimal digit: produce a floating point *
  33. * number. The number is 'generic' floating point: our *
  34. * caller will encode it for a specific machine architecture. *
  35. * *
  36. * Assumptions *
  37. * uses base (radix) 2 *
  38. * this machine uses 2's complement binary integers *
  39. * target flonums use " " " " *
  40. * target flonums exponents fit in a long *
  41. * *
  42. \***********************************************************************/
  43. /*
  44. Syntax:
  45. <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
  46. <optional-sign> ::= '+' | '-' | {empty}
  47. <decimal-number> ::= <integer>
  48. | <integer> <radix-character>
  49. | <integer> <radix-character> <integer>
  50. | <radix-character> <integer>
  51. <optional-exponent> ::= {empty}
  52. | <exponent-character> <optional-sign> <integer>
  53. <integer> ::= <digit> | <digit> <integer>
  54. <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  55. <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
  56. <radix-character> ::= {one character from "string_of_decimal_marks"}
  57. */
  58. int
  59. atof_generic (/* return pointer to just AFTER number we read. */
  60. char **address_of_string_pointer,
  61. /* At most one per number. */
  62. const char *string_of_decimal_marks,
  63. const char *string_of_decimal_exponent_marks,
  64. FLONUM_TYPE *address_of_generic_floating_point_number)
  65. {
  66. int return_value; /* 0 means OK. */
  67. char *first_digit;
  68. unsigned int number_of_digits_before_decimal;
  69. unsigned int number_of_digits_after_decimal;
  70. long decimal_exponent;
  71. unsigned int number_of_digits_available;
  72. char digits_sign_char;
  73. /*
  74. * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
  75. * It would be simpler to modify the string, but we don't; just to be nice
  76. * to caller.
  77. * We need to know how many digits we have, so we can allocate space for
  78. * the digits' value.
  79. */
  80. char *p;
  81. char c;
  82. int seen_significant_digit;
  83. #ifdef ASSUME_DECIMAL_MARK_IS_DOT
  84. gas_assert (string_of_decimal_marks[0] == '.'
  85. && string_of_decimal_marks[1] == 0);
  86. #define IS_DECIMAL_MARK(c) ((c) == '.')
  87. #else
  88. #define IS_DECIMAL_MARK(c) (0 != strchr (string_of_decimal_marks, (c)))
  89. #endif
  90. first_digit = *address_of_string_pointer;
  91. c = *first_digit;
  92. if (c == '-' || c == '+')
  93. {
  94. digits_sign_char = c;
  95. first_digit++;
  96. }
  97. else
  98. digits_sign_char = '+';
  99. switch (first_digit[0])
  100. {
  101. case 'n':
  102. case 'N':
  103. if (!strncasecmp ("nan", first_digit, 3))
  104. {
  105. address_of_generic_floating_point_number->sign = 0;
  106. address_of_generic_floating_point_number->exponent = 0;
  107. address_of_generic_floating_point_number->leader =
  108. address_of_generic_floating_point_number->low;
  109. *address_of_string_pointer = first_digit + 3;
  110. return 0;
  111. }
  112. break;
  113. case 'i':
  114. case 'I':
  115. if (!strncasecmp ("inf", first_digit, 3))
  116. {
  117. address_of_generic_floating_point_number->sign =
  118. digits_sign_char == '+' ? 'P' : 'N';
  119. address_of_generic_floating_point_number->exponent = 0;
  120. address_of_generic_floating_point_number->leader =
  121. address_of_generic_floating_point_number->low;
  122. first_digit += 3;
  123. if (!strncasecmp ("inity", first_digit, 5))
  124. first_digit += 5;
  125. *address_of_string_pointer = first_digit;
  126. return 0;
  127. }
  128. break;
  129. }
  130. number_of_digits_before_decimal = 0;
  131. number_of_digits_after_decimal = 0;
  132. decimal_exponent = 0;
  133. seen_significant_digit = 0;
  134. for (p = first_digit;
  135. (((c = *p) != '\0')
  136. && (!c || !IS_DECIMAL_MARK (c))
  137. && (!c || !strchr (string_of_decimal_exponent_marks, c)));
  138. p++)
  139. {
  140. if (ISDIGIT (c))
  141. {
  142. if (seen_significant_digit || c > '0')
  143. {
  144. ++number_of_digits_before_decimal;
  145. seen_significant_digit = 1;
  146. }
  147. else
  148. {
  149. first_digit++;
  150. }
  151. }
  152. else
  153. {
  154. break; /* p -> char after pre-decimal digits. */
  155. }
  156. } /* For each digit before decimal mark. */
  157. #ifndef OLD_FLOAT_READS
  158. /* Ignore trailing 0's after the decimal point. The original code here
  159. * (ifdef'd out) does not do this, and numbers like
  160. * 4.29496729600000000000e+09 (2**31)
  161. * come out inexact for some reason related to length of the digit
  162. * string.
  163. */
  164. if (c && IS_DECIMAL_MARK (c))
  165. {
  166. unsigned int zeros = 0; /* Length of current string of zeros */
  167. for (p++; (c = *p) && ISDIGIT (c); p++)
  168. {
  169. if (c == '0')
  170. {
  171. zeros++;
  172. }
  173. else
  174. {
  175. number_of_digits_after_decimal += 1 + zeros;
  176. zeros = 0;
  177. }
  178. }
  179. }
  180. #else
  181. if (c && IS_DECIMAL_MARK (c))
  182. {
  183. for (p++;
  184. (((c = *p) != '\0')
  185. && (!c || !strchr (string_of_decimal_exponent_marks, c)));
  186. p++)
  187. {
  188. if (ISDIGIT (c))
  189. {
  190. /* This may be retracted below. */
  191. number_of_digits_after_decimal++;
  192. if ( /* seen_significant_digit || */ c > '0')
  193. {
  194. seen_significant_digit = TRUE;
  195. }
  196. }
  197. else
  198. {
  199. if (!seen_significant_digit)
  200. {
  201. number_of_digits_after_decimal = 0;
  202. }
  203. break;
  204. }
  205. } /* For each digit after decimal mark. */
  206. }
  207. while (number_of_digits_after_decimal
  208. && first_digit[number_of_digits_before_decimal
  209. + number_of_digits_after_decimal] == '0')
  210. --number_of_digits_after_decimal;
  211. #endif
  212. if (flag_m68k_mri)
  213. {
  214. while (c == '_')
  215. c = *++p;
  216. }
  217. if (c && strchr (string_of_decimal_exponent_marks, c))
  218. {
  219. char digits_exponent_sign_char;
  220. c = *++p;
  221. if (flag_m68k_mri)
  222. {
  223. while (c == '_')
  224. c = *++p;
  225. }
  226. if (c && strchr ("+-", c))
  227. {
  228. digits_exponent_sign_char = c;
  229. c = *++p;
  230. }
  231. else
  232. {
  233. digits_exponent_sign_char = '+';
  234. }
  235. for (; (c); c = *++p)
  236. {
  237. if (ISDIGIT (c))
  238. {
  239. decimal_exponent = decimal_exponent * 10 + c - '0';
  240. /*
  241. * BUG! If we overflow here, we lose!
  242. */
  243. }
  244. else
  245. {
  246. break;
  247. }
  248. }
  249. if (digits_exponent_sign_char == '-')
  250. {
  251. decimal_exponent = -decimal_exponent;
  252. }
  253. }
  254. *address_of_string_pointer = p;
  255. number_of_digits_available =
  256. number_of_digits_before_decimal + number_of_digits_after_decimal;
  257. return_value = 0;
  258. if (number_of_digits_available == 0)
  259. {
  260. address_of_generic_floating_point_number->exponent = 0; /* Not strictly necessary */
  261. address_of_generic_floating_point_number->leader
  262. = -1 + address_of_generic_floating_point_number->low;
  263. address_of_generic_floating_point_number->sign = digits_sign_char;
  264. /* We have just concocted (+/-)0.0E0 */
  265. }
  266. else
  267. {
  268. int count; /* Number of useful digits left to scan. */
  269. LITTLENUM_TYPE *digits_binary_low;
  270. unsigned int precision;
  271. unsigned int maximum_useful_digits;
  272. unsigned int number_of_digits_to_use;
  273. unsigned int more_than_enough_bits_for_digits;
  274. unsigned int more_than_enough_littlenums_for_digits;
  275. unsigned int size_of_digits_in_littlenums;
  276. unsigned int size_of_digits_in_chars;
  277. FLONUM_TYPE power_of_10_flonum;
  278. FLONUM_TYPE digits_flonum;
  279. precision = (address_of_generic_floating_point_number->high
  280. - address_of_generic_floating_point_number->low
  281. + 1); /* Number of destination littlenums. */
  282. /* Includes guard bits (two littlenums worth) */
  283. maximum_useful_digits = (((precision - 2))
  284. * ( (LITTLENUM_NUMBER_OF_BITS))
  285. * 1000000 / 3321928)
  286. + 2; /* 2 :: guard digits. */
  287. if (number_of_digits_available > maximum_useful_digits)
  288. {
  289. number_of_digits_to_use = maximum_useful_digits;
  290. }
  291. else
  292. {
  293. number_of_digits_to_use = number_of_digits_available;
  294. }
  295. /* Cast these to SIGNED LONG first, otherwise, on systems with
  296. LONG wider than INT (such as Alpha OSF/1), unsignedness may
  297. cause unexpected results. */
  298. decimal_exponent += ((long) number_of_digits_before_decimal
  299. - (long) number_of_digits_to_use);
  300. more_than_enough_bits_for_digits
  301. = (number_of_digits_to_use * 3321928 / 1000000 + 1);
  302. more_than_enough_littlenums_for_digits
  303. = (more_than_enough_bits_for_digits
  304. / LITTLENUM_NUMBER_OF_BITS)
  305. + 2;
  306. /* Compute (digits) part. In "12.34E56" this is the "1234" part.
  307. Arithmetic is exact here. If no digits are supplied then this
  308. part is a 0 valued binary integer. Allocate room to build up
  309. the binary number as littlenums. We want this memory to
  310. disappear when we leave this function. Assume no alignment
  311. problems => (room for n objects) == n * (room for 1
  312. object). */
  313. size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits;
  314. size_of_digits_in_chars = size_of_digits_in_littlenums
  315. * sizeof (LITTLENUM_TYPE);
  316. digits_binary_low = (LITTLENUM_TYPE *)
  317. alloca (size_of_digits_in_chars);
  318. memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
  319. /* Digits_binary_low[] is allocated and zeroed. */
  320. /*
  321. * Parse the decimal digits as if * digits_low was in the units position.
  322. * Emit a binary number into digits_binary_low[].
  323. *
  324. * Use a large-precision version of:
  325. * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
  326. */
  327. for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
  328. {
  329. c = *p;
  330. if (ISDIGIT (c))
  331. {
  332. /*
  333. * Multiply by 10. Assume can never overflow.
  334. * Add this digit to digits_binary_low[].
  335. */
  336. long carry;
  337. LITTLENUM_TYPE *littlenum_pointer;
  338. LITTLENUM_TYPE *littlenum_limit;
  339. littlenum_limit = digits_binary_low
  340. + more_than_enough_littlenums_for_digits
  341. - 1;
  342. carry = c - '0'; /* char -> binary */
  343. for (littlenum_pointer = digits_binary_low;
  344. littlenum_pointer <= littlenum_limit;
  345. littlenum_pointer++)
  346. {
  347. long work;
  348. work = carry + 10 * (long) (*littlenum_pointer);
  349. *littlenum_pointer = work & LITTLENUM_MASK;
  350. carry = work >> LITTLENUM_NUMBER_OF_BITS;
  351. }
  352. if (carry != 0)
  353. {
  354. /*
  355. * We have a GROSS internal error.
  356. * This should never happen.
  357. */
  358. as_fatal (_("failed sanity check"));
  359. }
  360. }
  361. else
  362. {
  363. ++count; /* '.' doesn't alter digits used count. */
  364. }
  365. }
  366. /*
  367. * Digits_binary_low[] properly encodes the value of the digits.
  368. * Forget about any high-order littlenums that are 0.
  369. */
  370. while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0
  371. && size_of_digits_in_littlenums >= 2)
  372. size_of_digits_in_littlenums--;
  373. digits_flonum.low = digits_binary_low;
  374. digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1;
  375. digits_flonum.leader = digits_flonum.high;
  376. digits_flonum.exponent = 0;
  377. /*
  378. * The value of digits_flonum . sign should not be important.
  379. * We have already decided the output's sign.
  380. * We trust that the sign won't influence the other parts of the number!
  381. * So we give it a value for these reasons:
  382. * (1) courtesy to humans reading/debugging
  383. * these numbers so they don't get excited about strange values
  384. * (2) in future there may be more meaning attached to sign,
  385. * and what was
  386. * harmless noise may become disruptive, ill-conditioned (or worse)
  387. * input.
  388. */
  389. digits_flonum.sign = '+';
  390. {
  391. /*
  392. * Compute the mantssa (& exponent) of the power of 10.
  393. * If successful, then multiply the power of 10 by the digits
  394. * giving return_binary_mantissa and return_binary_exponent.
  395. */
  396. LITTLENUM_TYPE *power_binary_low;
  397. int decimal_exponent_is_negative;
  398. /* This refers to the "-56" in "12.34E-56". */
  399. /* FALSE: decimal_exponent is positive (or 0) */
  400. /* TRUE: decimal_exponent is negative */
  401. FLONUM_TYPE temporary_flonum;
  402. LITTLENUM_TYPE *temporary_binary_low;
  403. unsigned int size_of_power_in_littlenums;
  404. unsigned int size_of_power_in_chars;
  405. size_of_power_in_littlenums = precision;
  406. /* Precision has a built-in fudge factor so we get a few guard bits. */
  407. decimal_exponent_is_negative = decimal_exponent < 0;
  408. if (decimal_exponent_is_negative)
  409. {
  410. decimal_exponent = -decimal_exponent;
  411. }
  412. /* From now on: the decimal exponent is > 0. Its sign is separate. */
  413. size_of_power_in_chars = size_of_power_in_littlenums
  414. * sizeof (LITTLENUM_TYPE) + 2;
  415. power_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
  416. temporary_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
  417. memset ((char *) power_binary_low, '\0', size_of_power_in_chars);
  418. *power_binary_low = 1;
  419. power_of_10_flonum.exponent = 0;
  420. power_of_10_flonum.low = power_binary_low;
  421. power_of_10_flonum.leader = power_binary_low;
  422. power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1;
  423. power_of_10_flonum.sign = '+';
  424. temporary_flonum.low = temporary_binary_low;
  425. temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1;
  426. /*
  427. * (power) == 1.
  428. * Space for temporary_flonum allocated.
  429. */
  430. /*
  431. * ...
  432. *
  433. * WHILE more bits
  434. * DO find next bit (with place value)
  435. * multiply into power mantissa
  436. * OD
  437. */
  438. {
  439. int place_number_limit;
  440. /* Any 10^(2^n) whose "n" exceeds this */
  441. /* value will fall off the end of */
  442. /* flonum_XXXX_powers_of_ten[]. */
  443. int place_number;
  444. const FLONUM_TYPE *multiplicand; /* -> 10^(2^n) */
  445. place_number_limit = table_size_of_flonum_powers_of_ten;
  446. multiplicand = (decimal_exponent_is_negative
  447. ? flonum_negative_powers_of_ten
  448. : flonum_positive_powers_of_ten);
  449. for (place_number = 1;/* Place value of this bit of exponent. */
  450. decimal_exponent;/* Quit when no more 1 bits in exponent. */
  451. decimal_exponent >>= 1, place_number++)
  452. {
  453. if (decimal_exponent & 1)
  454. {
  455. if (place_number > place_number_limit)
  456. {
  457. /* The decimal exponent has a magnitude so great
  458. that our tables can't help us fragment it.
  459. Although this routine is in error because it
  460. can't imagine a number that big, signal an
  461. error as if it is the user's fault for
  462. presenting such a big number. */
  463. return_value = ERROR_EXPONENT_OVERFLOW;
  464. /* quit out of loop gracefully */
  465. decimal_exponent = 0;
  466. }
  467. else
  468. {
  469. #ifdef TRACE
  470. printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
  471. place_number);
  472. flonum_print (&power_of_10_flonum);
  473. (void) putchar ('\n');
  474. #endif
  475. #ifdef TRACE
  476. printf ("multiplier:\n");
  477. flonum_print (multiplicand + place_number);
  478. (void) putchar ('\n');
  479. #endif
  480. flonum_multip (multiplicand + place_number,
  481. &power_of_10_flonum, &temporary_flonum);
  482. #ifdef TRACE
  483. printf ("after multiply:\n");
  484. flonum_print (&temporary_flonum);
  485. (void) putchar ('\n');
  486. #endif
  487. flonum_copy (&temporary_flonum, &power_of_10_flonum);
  488. #ifdef TRACE
  489. printf ("after copy:\n");
  490. flonum_print (&power_of_10_flonum);
  491. (void) putchar ('\n');
  492. #endif
  493. } /* If this bit of decimal_exponent was computable.*/
  494. } /* If this bit of decimal_exponent was set. */
  495. } /* For each bit of binary representation of exponent */
  496. #ifdef TRACE
  497. printf ("after computing power_of_10_flonum:\n");
  498. flonum_print (&power_of_10_flonum);
  499. (void) putchar ('\n');
  500. #endif
  501. }
  502. }
  503. /*
  504. * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
  505. * It may be the number 1, in which case we don't NEED to multiply.
  506. *
  507. * Multiply (decimal digits) by power_of_10_flonum.
  508. */
  509. flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number);
  510. /* Assert sign of the number we made is '+'. */
  511. address_of_generic_floating_point_number->sign = digits_sign_char;
  512. }
  513. return return_value;
  514. }
  515. #ifdef TRACE
  516. static void
  517. flonum_print (f)
  518. const FLONUM_TYPE *f;
  519. {
  520. LITTLENUM_TYPE *lp;
  521. char littlenum_format[10];
  522. sprintf (littlenum_format, " %%0%dx", sizeof (LITTLENUM_TYPE) * 2);
  523. #define print_littlenum(LP) (printf (littlenum_format, LP))
  524. printf ("flonum @%p %c e%ld", f, f->sign, f->exponent);
  525. if (f->low < f->high)
  526. for (lp = f->high; lp >= f->low; lp--)
  527. print_littlenum (*lp);
  528. else
  529. for (lp = f->low; lp <= f->high; lp++)
  530. print_littlenum (*lp);
  531. printf ("\n");
  532. fflush (stdout);
  533. }
  534. #endif
  535. /* end of atof_generic.c */