atof-generic.c 19 KB

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