atof-generic.c 16 KB

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