write_float.def 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. /* Copyright (C) 2007-2015 Free Software Foundation, Inc.
  2. Contributed by Andy Vaught
  3. Write float code factoring to this file by Jerry DeLisle
  4. F2003 I/O support contributed by Jerry DeLisle
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "config.h"
  22. typedef enum
  23. { S_NONE, S_MINUS, S_PLUS }
  24. sign_t;
  25. /* Given a flag that indicates if a value is negative or not, return a
  26. sign_t that gives the sign that we need to produce. */
  27. static sign_t
  28. calculate_sign (st_parameter_dt *dtp, int negative_flag)
  29. {
  30. sign_t s = S_NONE;
  31. if (negative_flag)
  32. s = S_MINUS;
  33. else
  34. switch (dtp->u.p.sign_status)
  35. {
  36. case SIGN_SP: /* Show sign. */
  37. s = S_PLUS;
  38. break;
  39. case SIGN_SS: /* Suppress sign. */
  40. s = S_NONE;
  41. break;
  42. case SIGN_S: /* Processor defined. */
  43. case SIGN_UNSPECIFIED:
  44. s = options.optional_plus ? S_PLUS : S_NONE;
  45. break;
  46. }
  47. return s;
  48. }
  49. /* Determine the precision except for EN format. For G format,
  50. determines an upper bound to be used for sizing the buffer. */
  51. static int
  52. determine_precision (st_parameter_dt * dtp, const fnode * f, int len)
  53. {
  54. int precision = f->u.real.d;
  55. switch (f->format)
  56. {
  57. case FMT_F:
  58. case FMT_G:
  59. precision += dtp->u.p.scale_factor;
  60. break;
  61. case FMT_ES:
  62. /* Scale factor has no effect on output. */
  63. break;
  64. case FMT_E:
  65. case FMT_D:
  66. /* See F2008 10.7.2.3.3.6 */
  67. if (dtp->u.p.scale_factor <= 0)
  68. precision += dtp->u.p.scale_factor - 1;
  69. break;
  70. default:
  71. return -1;
  72. }
  73. /* If the scale factor has a large negative value, we must do our
  74. own rounding? Use ROUND='NEAREST', which should be what snprintf
  75. is using as well. */
  76. if (precision < 0 &&
  77. (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED
  78. || dtp->u.p.current_unit->round_status == ROUND_PROCDEFINED))
  79. dtp->u.p.current_unit->round_status = ROUND_NEAREST;
  80. /* Add extra guard digits up to at least full precision when we do
  81. our own rounding. */
  82. if (dtp->u.p.current_unit->round_status != ROUND_UNSPECIFIED
  83. && dtp->u.p.current_unit->round_status != ROUND_PROCDEFINED)
  84. {
  85. precision += 2 * len + 4;
  86. if (precision < 0)
  87. precision = 0;
  88. }
  89. return precision;
  90. }
  91. /* Output a real number according to its format which is FMT_G free. */
  92. static bool
  93. output_float (st_parameter_dt *dtp, const fnode *f, char *buffer, size_t size,
  94. int nprinted, int precision, int sign_bit, bool zero_flag)
  95. {
  96. char *out;
  97. char *digits;
  98. int e, w, d, p, i;
  99. char expchar, rchar;
  100. format_token ft;
  101. /* Number of digits before the decimal point. */
  102. int nbefore;
  103. /* Number of zeros after the decimal point. */
  104. int nzero;
  105. /* Number of digits after the decimal point. */
  106. int nafter;
  107. int leadzero;
  108. int nblanks;
  109. int ndigits, edigits;
  110. sign_t sign;
  111. ft = f->format;
  112. w = f->u.real.w;
  113. d = f->u.real.d;
  114. p = dtp->u.p.scale_factor;
  115. rchar = '5';
  116. /* We should always know the field width and precision. */
  117. if (d < 0)
  118. internal_error (&dtp->common, "Unspecified precision");
  119. sign = calculate_sign (dtp, sign_bit);
  120. /* Calculate total number of digits. */
  121. if (ft == FMT_F)
  122. ndigits = nprinted - 2;
  123. else
  124. ndigits = precision + 1;
  125. /* Read the exponent back in. */
  126. if (ft != FMT_F)
  127. e = atoi (&buffer[ndigits + 3]) + 1;
  128. else
  129. e = 0;
  130. /* Make sure zero comes out as 0.0e0. */
  131. if (zero_flag)
  132. e = 0;
  133. /* Normalize the fractional component. */
  134. if (ft != FMT_F)
  135. {
  136. buffer[2] = buffer[1];
  137. digits = &buffer[2];
  138. }
  139. else
  140. digits = &buffer[1];
  141. /* Figure out where to place the decimal point. */
  142. switch (ft)
  143. {
  144. case FMT_F:
  145. nbefore = ndigits - precision;
  146. /* Make sure the decimal point is a '.'; depending on the
  147. locale, this might not be the case otherwise. */
  148. digits[nbefore] = '.';
  149. if (p != 0)
  150. {
  151. if (p > 0)
  152. {
  153. memmove (digits + nbefore, digits + nbefore + 1, p);
  154. digits[nbefore + p] = '.';
  155. nbefore += p;
  156. nafter = d - p;
  157. if (nafter < 0)
  158. nafter = 0;
  159. nafter = d;
  160. nzero = 0;
  161. }
  162. else /* p < 0 */
  163. {
  164. if (nbefore + p >= 0)
  165. {
  166. nzero = 0;
  167. memmove (digits + nbefore + p + 1, digits + nbefore + p, -p);
  168. nbefore += p;
  169. digits[nbefore] = '.';
  170. nafter = d;
  171. }
  172. else
  173. {
  174. nzero = -(nbefore + p);
  175. memmove (digits + 1, digits, nbefore);
  176. digits++;
  177. nafter = d + nbefore;
  178. nbefore = 0;
  179. }
  180. if (nzero > d)
  181. nzero = d;
  182. }
  183. }
  184. else
  185. {
  186. nzero = 0;
  187. nafter = d;
  188. }
  189. while (digits[0] == '0' && nbefore > 0)
  190. {
  191. digits++;
  192. nbefore--;
  193. ndigits--;
  194. }
  195. expchar = 0;
  196. /* If we need to do rounding ourselves, get rid of the dot by
  197. moving the fractional part. */
  198. if (dtp->u.p.current_unit->round_status != ROUND_UNSPECIFIED
  199. && dtp->u.p.current_unit->round_status != ROUND_PROCDEFINED)
  200. memmove (digits + nbefore, digits + nbefore + 1, ndigits - nbefore);
  201. break;
  202. case FMT_E:
  203. case FMT_D:
  204. i = dtp->u.p.scale_factor;
  205. if (d <= 0 && p == 0)
  206. {
  207. generate_error (&dtp->common, LIBERROR_FORMAT, "Precision not "
  208. "greater than zero in format specifier 'E' or 'D'");
  209. return false;
  210. }
  211. if (p <= -d || p >= d + 2)
  212. {
  213. generate_error (&dtp->common, LIBERROR_FORMAT, "Scale factor "
  214. "out of range in format specifier 'E' or 'D'");
  215. return false;
  216. }
  217. if (!zero_flag)
  218. e -= p;
  219. if (p < 0)
  220. {
  221. nbefore = 0;
  222. nzero = -p;
  223. nafter = d + p;
  224. }
  225. else if (p > 0)
  226. {
  227. nbefore = p;
  228. nzero = 0;
  229. nafter = (d - p) + 1;
  230. }
  231. else /* p == 0 */
  232. {
  233. nbefore = 0;
  234. nzero = 0;
  235. nafter = d;
  236. }
  237. if (ft == FMT_E)
  238. expchar = 'E';
  239. else
  240. expchar = 'D';
  241. break;
  242. case FMT_EN:
  243. /* The exponent must be a multiple of three, with 1-3 digits before
  244. the decimal point. */
  245. if (!zero_flag)
  246. e--;
  247. if (e >= 0)
  248. nbefore = e % 3;
  249. else
  250. {
  251. nbefore = (-e) % 3;
  252. if (nbefore != 0)
  253. nbefore = 3 - nbefore;
  254. }
  255. e -= nbefore;
  256. nbefore++;
  257. nzero = 0;
  258. nafter = d;
  259. expchar = 'E';
  260. break;
  261. case FMT_ES:
  262. if (!zero_flag)
  263. e--;
  264. nbefore = 1;
  265. nzero = 0;
  266. nafter = d;
  267. expchar = 'E';
  268. break;
  269. default:
  270. /* Should never happen. */
  271. internal_error (&dtp->common, "Unexpected format token");
  272. }
  273. if (zero_flag)
  274. goto skip;
  275. /* Round the value. The value being rounded is an unsigned magnitude. */
  276. switch (dtp->u.p.current_unit->round_status)
  277. {
  278. /* For processor defined and unspecified rounding we use
  279. snprintf to print the exact number of digits needed, and thus
  280. let snprintf handle the rounding. On system claiming support
  281. for IEEE 754, this ought to be round to nearest, ties to
  282. even, corresponding to the Fortran ROUND='NEAREST'. */
  283. case ROUND_PROCDEFINED:
  284. case ROUND_UNSPECIFIED:
  285. case ROUND_ZERO: /* Do nothing and truncation occurs. */
  286. goto skip;
  287. case ROUND_UP:
  288. if (sign_bit)
  289. goto skip;
  290. goto updown;
  291. case ROUND_DOWN:
  292. if (!sign_bit)
  293. goto skip;
  294. goto updown;
  295. case ROUND_NEAREST:
  296. /* Round compatible unless there is a tie. A tie is a 5 with
  297. all trailing zero's. */
  298. i = nafter + nbefore;
  299. if (digits[i] == '5')
  300. {
  301. for(i++ ; i < ndigits; i++)
  302. {
  303. if (digits[i] != '0')
  304. goto do_rnd;
  305. }
  306. /* It is a tie so round to even. */
  307. switch (digits[nafter + nbefore - 1])
  308. {
  309. case '1':
  310. case '3':
  311. case '5':
  312. case '7':
  313. case '9':
  314. /* If odd, round away from zero to even. */
  315. break;
  316. default:
  317. /* If even, skip rounding, truncate to even. */
  318. goto skip;
  319. }
  320. }
  321. /* Fall through. */
  322. /* The ROUND_COMPATIBLE is rounding away from zero when there is a tie. */
  323. case ROUND_COMPATIBLE:
  324. rchar = '5';
  325. goto do_rnd;
  326. }
  327. updown:
  328. rchar = '0';
  329. if (ft != FMT_F && w > 0 && d == 0 && p == 0)
  330. nbefore = 1;
  331. /* Scan for trailing zeros to see if we really need to round it. */
  332. for(i = nbefore + nafter; i < ndigits; i++)
  333. {
  334. if (digits[i] != '0')
  335. goto do_rnd;
  336. }
  337. goto skip;
  338. do_rnd:
  339. if (nbefore + nafter == 0)
  340. /* Handle the case Fw.0 and value < 1.0 */
  341. {
  342. ndigits = 0;
  343. if (digits[0] >= rchar)
  344. {
  345. /* We rounded to zero but shouldn't have */
  346. nbefore = 1;
  347. digits--;
  348. digits[0] = '1';
  349. ndigits = 1;
  350. }
  351. }
  352. else if (nbefore + nafter < ndigits)
  353. {
  354. i = ndigits = nbefore + nafter;
  355. if (digits[i] >= rchar)
  356. {
  357. /* Propagate the carry. */
  358. for (i--; i >= 0; i--)
  359. {
  360. if (digits[i] != '9')
  361. {
  362. digits[i]++;
  363. break;
  364. }
  365. digits[i] = '0';
  366. }
  367. if (i < 0)
  368. {
  369. /* The carry overflowed. Fortunately we have some spare
  370. space at the start of the buffer. We may discard some
  371. digits, but this is ok because we already know they are
  372. zero. */
  373. digits--;
  374. digits[0] = '1';
  375. if (ft == FMT_F)
  376. {
  377. if (nzero > 0)
  378. {
  379. nzero--;
  380. nafter++;
  381. }
  382. else
  383. nbefore++;
  384. }
  385. else if (ft == FMT_EN)
  386. {
  387. nbefore++;
  388. if (nbefore == 4)
  389. {
  390. nbefore = 1;
  391. e += 3;
  392. }
  393. }
  394. else
  395. e++;
  396. }
  397. }
  398. }
  399. skip:
  400. /* Calculate the format of the exponent field. */
  401. if (expchar && !(dtp->u.p.g0_no_blanks && e == 0))
  402. {
  403. edigits = 1;
  404. for (i = abs (e); i >= 10; i /= 10)
  405. edigits++;
  406. if (f->u.real.e < 0)
  407. {
  408. /* Width not specified. Must be no more than 3 digits. */
  409. if (e > 999 || e < -999)
  410. edigits = -1;
  411. else
  412. {
  413. edigits = 4;
  414. if (e > 99 || e < -99)
  415. expchar = ' ';
  416. }
  417. }
  418. else
  419. {
  420. /* Exponent width specified, check it is wide enough. */
  421. if (edigits > f->u.real.e)
  422. edigits = -1;
  423. else
  424. edigits = f->u.real.e + 2;
  425. }
  426. }
  427. else
  428. edigits = 0;
  429. /* Scan the digits string and count the number of zeros. If we make it
  430. all the way through the loop, we know the value is zero after the
  431. rounding completed above. */
  432. int hasdot = 0;
  433. for (i = 0; i < ndigits + hasdot; i++)
  434. {
  435. if (digits[i] == '.')
  436. hasdot = 1;
  437. else if (digits[i] != '0')
  438. break;
  439. }
  440. /* To format properly, we need to know if the rounded result is zero and if
  441. so, we set the zero_flag which may have been already set for
  442. actual zero. */
  443. if (i == ndigits + hasdot)
  444. {
  445. zero_flag = true;
  446. /* The output is zero, so set the sign according to the sign bit unless
  447. -fno-sign-zero was specified. */
  448. if (compile_options.sign_zero == 1)
  449. sign = calculate_sign (dtp, sign_bit);
  450. else
  451. sign = calculate_sign (dtp, 0);
  452. }
  453. /* Pick a field size if none was specified, taking into account small
  454. values that may have been rounded to zero. */
  455. if (w <= 0)
  456. {
  457. if (zero_flag)
  458. w = d + (sign != S_NONE ? 2 : 1) + (d == 0 ? 1 : 0);
  459. else
  460. {
  461. w = nbefore + nzero + nafter + (sign != S_NONE ? 2 : 1);
  462. w = w == 1 ? 2 : w;
  463. }
  464. }
  465. /* Work out how much padding is needed. */
  466. nblanks = w - (nbefore + nzero + nafter + edigits + 1);
  467. if (sign != S_NONE)
  468. nblanks--;
  469. if (dtp->u.p.g0_no_blanks)
  470. {
  471. w -= nblanks;
  472. nblanks = 0;
  473. }
  474. /* Create the ouput buffer. */
  475. out = write_block (dtp, w);
  476. if (out == NULL)
  477. return false;
  478. /* Check the value fits in the specified field width. */
  479. if (nblanks < 0 || edigits == -1 || w == 1 || (w == 2 && sign != S_NONE))
  480. {
  481. if (unlikely (is_char4_unit (dtp)))
  482. {
  483. gfc_char4_t *out4 = (gfc_char4_t *) out;
  484. memset4 (out4, '*', w);
  485. return false;
  486. }
  487. star_fill (out, w);
  488. return false;
  489. }
  490. /* See if we have space for a zero before the decimal point. */
  491. if (nbefore == 0 && nblanks > 0)
  492. {
  493. leadzero = 1;
  494. nblanks--;
  495. }
  496. else
  497. leadzero = 0;
  498. /* For internal character(kind=4) units, we duplicate the code used for
  499. regular output slightly modified. This needs to be maintained
  500. consistent with the regular code that follows this block. */
  501. if (unlikely (is_char4_unit (dtp)))
  502. {
  503. gfc_char4_t *out4 = (gfc_char4_t *) out;
  504. /* Pad to full field width. */
  505. if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
  506. {
  507. memset4 (out4, ' ', nblanks);
  508. out4 += nblanks;
  509. }
  510. /* Output the initial sign (if any). */
  511. if (sign == S_PLUS)
  512. *(out4++) = '+';
  513. else if (sign == S_MINUS)
  514. *(out4++) = '-';
  515. /* Output an optional leading zero. */
  516. if (leadzero)
  517. *(out4++) = '0';
  518. /* Output the part before the decimal point, padding with zeros. */
  519. if (nbefore > 0)
  520. {
  521. if (nbefore > ndigits)
  522. {
  523. i = ndigits;
  524. memcpy4 (out4, digits, i);
  525. ndigits = 0;
  526. while (i < nbefore)
  527. out4[i++] = '0';
  528. }
  529. else
  530. {
  531. i = nbefore;
  532. memcpy4 (out4, digits, i);
  533. ndigits -= i;
  534. }
  535. digits += i;
  536. out4 += nbefore;
  537. }
  538. /* Output the decimal point. */
  539. *(out4++) = dtp->u.p.current_unit->decimal_status
  540. == DECIMAL_POINT ? '.' : ',';
  541. if (ft == FMT_F
  542. && (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED
  543. || dtp->u.p.current_unit->round_status == ROUND_PROCDEFINED))
  544. digits++;
  545. /* Output leading zeros after the decimal point. */
  546. if (nzero > 0)
  547. {
  548. for (i = 0; i < nzero; i++)
  549. *(out4++) = '0';
  550. }
  551. /* Output digits after the decimal point, padding with zeros. */
  552. if (nafter > 0)
  553. {
  554. if (nafter > ndigits)
  555. i = ndigits;
  556. else
  557. i = nafter;
  558. memcpy4 (out4, digits, i);
  559. while (i < nafter)
  560. out4[i++] = '0';
  561. digits += i;
  562. ndigits -= i;
  563. out4 += nafter;
  564. }
  565. /* Output the exponent. */
  566. if (expchar && !(dtp->u.p.g0_no_blanks && e == 0))
  567. {
  568. if (expchar != ' ')
  569. {
  570. *(out4++) = expchar;
  571. edigits--;
  572. }
  573. snprintf (buffer, size, "%+0*d", edigits, e);
  574. memcpy4 (out4, buffer, edigits);
  575. }
  576. if (dtp->u.p.no_leading_blank)
  577. {
  578. out4 += edigits;
  579. memset4 (out4, ' ' , nblanks);
  580. dtp->u.p.no_leading_blank = 0;
  581. }
  582. return true;
  583. } /* End of character(kind=4) internal unit code. */
  584. /* Pad to full field width. */
  585. if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
  586. {
  587. memset (out, ' ', nblanks);
  588. out += nblanks;
  589. }
  590. /* Output the initial sign (if any). */
  591. if (sign == S_PLUS)
  592. *(out++) = '+';
  593. else if (sign == S_MINUS)
  594. *(out++) = '-';
  595. /* Output an optional leading zero. */
  596. if (leadzero)
  597. *(out++) = '0';
  598. /* Output the part before the decimal point, padding with zeros. */
  599. if (nbefore > 0)
  600. {
  601. if (nbefore > ndigits)
  602. {
  603. i = ndigits;
  604. memcpy (out, digits, i);
  605. ndigits = 0;
  606. while (i < nbefore)
  607. out[i++] = '0';
  608. }
  609. else
  610. {
  611. i = nbefore;
  612. memcpy (out, digits, i);
  613. ndigits -= i;
  614. }
  615. digits += i;
  616. out += nbefore;
  617. }
  618. /* Output the decimal point. */
  619. *(out++) = dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? '.' : ',';
  620. if (ft == FMT_F
  621. && (dtp->u.p.current_unit->round_status == ROUND_UNSPECIFIED
  622. || dtp->u.p.current_unit->round_status == ROUND_PROCDEFINED))
  623. digits++;
  624. /* Output leading zeros after the decimal point. */
  625. if (nzero > 0)
  626. {
  627. for (i = 0; i < nzero; i++)
  628. *(out++) = '0';
  629. }
  630. /* Output digits after the decimal point, padding with zeros. */
  631. if (nafter > 0)
  632. {
  633. if (nafter > ndigits)
  634. i = ndigits;
  635. else
  636. i = nafter;
  637. memcpy (out, digits, i);
  638. while (i < nafter)
  639. out[i++] = '0';
  640. digits += i;
  641. ndigits -= i;
  642. out += nafter;
  643. }
  644. /* Output the exponent. */
  645. if (expchar && !(dtp->u.p.g0_no_blanks && e == 0))
  646. {
  647. if (expchar != ' ')
  648. {
  649. *(out++) = expchar;
  650. edigits--;
  651. }
  652. snprintf (buffer, size, "%+0*d", edigits, e);
  653. memcpy (out, buffer, edigits);
  654. }
  655. if (dtp->u.p.no_leading_blank)
  656. {
  657. out += edigits;
  658. memset( out , ' ' , nblanks );
  659. dtp->u.p.no_leading_blank = 0;
  660. }
  661. return true;
  662. }
  663. /* Write "Infinite" or "Nan" as appropriate for the given format. */
  664. static void
  665. write_infnan (st_parameter_dt *dtp, const fnode *f, int isnan_flag, int sign_bit)
  666. {
  667. char * p, fin;
  668. int nb = 0;
  669. sign_t sign;
  670. int mark;
  671. if (f->format != FMT_B && f->format != FMT_O && f->format != FMT_Z)
  672. {
  673. sign = calculate_sign (dtp, sign_bit);
  674. mark = (sign == S_PLUS || sign == S_MINUS) ? 8 : 7;
  675. nb = f->u.real.w;
  676. /* If the field width is zero, the processor must select a width
  677. not zero. 4 is chosen to allow output of '-Inf' or '+Inf' */
  678. if ((nb == 0) || dtp->u.p.g0_no_blanks)
  679. {
  680. if (isnan_flag)
  681. nb = 3;
  682. else
  683. nb = (sign == S_PLUS || sign == S_MINUS) ? 4 : 3;
  684. }
  685. p = write_block (dtp, nb);
  686. if (p == NULL)
  687. return;
  688. if (nb < 3)
  689. {
  690. if (unlikely (is_char4_unit (dtp)))
  691. {
  692. gfc_char4_t *p4 = (gfc_char4_t *) p;
  693. memset4 (p4, '*', nb);
  694. }
  695. else
  696. memset (p, '*', nb);
  697. return;
  698. }
  699. if (unlikely (is_char4_unit (dtp)))
  700. {
  701. gfc_char4_t *p4 = (gfc_char4_t *) p;
  702. memset4 (p4, ' ', nb);
  703. }
  704. else
  705. memset(p, ' ', nb);
  706. if (!isnan_flag)
  707. {
  708. if (sign_bit)
  709. {
  710. /* If the sign is negative and the width is 3, there is
  711. insufficient room to output '-Inf', so output asterisks */
  712. if (nb == 3)
  713. {
  714. if (unlikely (is_char4_unit (dtp)))
  715. {
  716. gfc_char4_t *p4 = (gfc_char4_t *) p;
  717. memset4 (p4, '*', nb);
  718. }
  719. else
  720. memset (p, '*', nb);
  721. return;
  722. }
  723. /* The negative sign is mandatory */
  724. fin = '-';
  725. }
  726. else
  727. /* The positive sign is optional, but we output it for
  728. consistency */
  729. fin = '+';
  730. if (unlikely (is_char4_unit (dtp)))
  731. {
  732. gfc_char4_t *p4 = (gfc_char4_t *) p;
  733. if (nb > mark)
  734. /* We have room, so output 'Infinity' */
  735. memcpy4 (p4 + nb - 8, "Infinity", 8);
  736. else
  737. /* For the case of width equals mark, there is not enough room
  738. for the sign and 'Infinity' so we go with 'Inf' */
  739. memcpy4 (p4 + nb - 3, "Inf", 3);
  740. if (sign == S_PLUS || sign == S_MINUS)
  741. {
  742. if (nb < 9 && nb > 3)
  743. /* Put the sign in front of Inf */
  744. p4[nb - 4] = (gfc_char4_t) fin;
  745. else if (nb > 8)
  746. /* Put the sign in front of Infinity */
  747. p4[nb - 9] = (gfc_char4_t) fin;
  748. }
  749. return;
  750. }
  751. if (nb > mark)
  752. /* We have room, so output 'Infinity' */
  753. memcpy(p + nb - 8, "Infinity", 8);
  754. else
  755. /* For the case of width equals 8, there is not enough room
  756. for the sign and 'Infinity' so we go with 'Inf' */
  757. memcpy(p + nb - 3, "Inf", 3);
  758. if (sign == S_PLUS || sign == S_MINUS)
  759. {
  760. if (nb < 9 && nb > 3)
  761. p[nb - 4] = fin; /* Put the sign in front of Inf */
  762. else if (nb > 8)
  763. p[nb - 9] = fin; /* Put the sign in front of Infinity */
  764. }
  765. }
  766. else
  767. {
  768. if (unlikely (is_char4_unit (dtp)))
  769. {
  770. gfc_char4_t *p4 = (gfc_char4_t *) p;
  771. memcpy4 (p4 + nb - 3, "NaN", 3);
  772. }
  773. else
  774. memcpy(p + nb - 3, "NaN", 3);
  775. }
  776. return;
  777. }
  778. }
  779. /* Returns the value of 10**d. */
  780. #define CALCULATE_EXP(x) \
  781. static GFC_REAL_ ## x \
  782. calculate_exp_ ## x (int d)\
  783. {\
  784. int i;\
  785. GFC_REAL_ ## x r = 1.0;\
  786. for (i = 0; i< (d >= 0 ? d : -d); i++)\
  787. r *= 10;\
  788. r = (d >= 0) ? r : 1.0 / r;\
  789. return r;\
  790. }
  791. CALCULATE_EXP(4)
  792. CALCULATE_EXP(8)
  793. #ifdef HAVE_GFC_REAL_10
  794. CALCULATE_EXP(10)
  795. #endif
  796. #ifdef HAVE_GFC_REAL_16
  797. CALCULATE_EXP(16)
  798. #endif
  799. #undef CALCULATE_EXP
  800. /* Define a macro to build code for write_float. */
  801. /* Note: Before output_float is called, snprintf is used to print to buffer the
  802. number in the format +D.DDDDe+ddd.
  803. # The result will always contain a decimal point, even if no
  804. digits follow it
  805. - The converted value is to be left adjusted on the field boundary
  806. + A sign (+ or -) always be placed before a number
  807. * prec is used as the precision
  808. e format: [-]d.ddde±dd where there is one digit before the
  809. decimal-point character and the number of digits after it is
  810. equal to the precision. The exponent always contains at least two
  811. digits; if the value is zero, the exponent is 00. */
  812. #define TOKENPASTE(x, y) TOKENPASTE2(x, y)
  813. #define TOKENPASTE2(x, y) x ## y
  814. #define DTOA(suff,prec,val) TOKENPASTE(DTOA2,suff)(prec,val)
  815. #define DTOA2(prec,val) \
  816. snprintf (buffer, size, "%+-#.*e", (prec), (val))
  817. #define DTOA2L(prec,val) \
  818. snprintf (buffer, size, "%+-#.*Le", (prec), (val))
  819. #if defined(GFC_REAL_16_IS_FLOAT128)
  820. #define DTOA2Q(prec,val) \
  821. __qmath_(quadmath_snprintf) (buffer, size, "%+-#.*Qe", (prec), (val))
  822. #endif
  823. #define FDTOA(suff,prec,val) TOKENPASTE(FDTOA2,suff)(prec,val)
  824. /* For F format, we print to the buffer with f format. */
  825. #define FDTOA2(prec,val) \
  826. snprintf (buffer, size, "%+-#.*f", (prec), (val))
  827. #define FDTOA2L(prec,val) \
  828. snprintf (buffer, size, "%+-#.*Lf", (prec), (val))
  829. #if defined(GFC_REAL_16_IS_FLOAT128)
  830. #define FDTOA2Q(prec,val) \
  831. __qmath_(quadmath_snprintf) (buffer, size, "%+-#.*Qf", \
  832. (prec), (val))
  833. #endif
  834. #if defined(GFC_REAL_16_IS_FLOAT128)
  835. #define ISFINITE2Q(val) finiteq(val)
  836. #endif
  837. #define ISFINITE2(val) isfinite(val)
  838. #define ISFINITE2L(val) isfinite(val)
  839. #define ISFINITE(suff,val) TOKENPASTE(ISFINITE2,suff)(val)
  840. #if defined(GFC_REAL_16_IS_FLOAT128)
  841. #define SIGNBIT2Q(val) signbitq(val)
  842. #endif
  843. #define SIGNBIT2(val) signbit(val)
  844. #define SIGNBIT2L(val) signbit(val)
  845. #define SIGNBIT(suff,val) TOKENPASTE(SIGNBIT2,suff)(val)
  846. #if defined(GFC_REAL_16_IS_FLOAT128)
  847. #define ISNAN2Q(val) isnanq(val)
  848. #endif
  849. #define ISNAN2(val) isnan(val)
  850. #define ISNAN2L(val) isnan(val)
  851. #define ISNAN(suff,val) TOKENPASTE(ISNAN2,suff)(val)
  852. /* Generate corresponding I/O format for FMT_G and output.
  853. The rules to translate FMT_G to FMT_E or FMT_F from DEC fortran
  854. LRM (table 11-2, Chapter 11, "I/O Formatting", P11-25) is:
  855. Data Magnitude Equivalent Conversion
  856. 0< m < 0.1-0.5*10**(-d-1) Ew.d[Ee]
  857. m = 0 F(w-n).(d-1), n' '
  858. 0.1-0.5*10**(-d-1)<= m < 1-0.5*10**(-d) F(w-n).d, n' '
  859. 1-0.5*10**(-d)<= m < 10-0.5*10**(-d+1) F(w-n).(d-1), n' '
  860. 10-0.5*10**(-d+1)<= m < 100-0.5*10**(-d+2) F(w-n).(d-2), n' '
  861. ................ ..........
  862. 10**(d-1)-0.5*10**(-1)<= m <10**d-0.5 F(w-n).0,n(' ')
  863. m >= 10**d-0.5 Ew.d[Ee]
  864. notes: for Gw.d , n' ' means 4 blanks
  865. for Gw.dEe, n' ' means e+2 blanks
  866. for rounding modes adjustment, r, See Fortran F2008 10.7.5.2.2
  867. the asm volatile is required for 32-bit x86 platforms. */
  868. #define OUTPUT_FLOAT_FMT_G(x,y) \
  869. static void \
  870. output_float_FMT_G_ ## x (st_parameter_dt *dtp, const fnode *f, \
  871. GFC_REAL_ ## x m, char *buffer, size_t size, \
  872. int sign_bit, bool zero_flag, int comp_d) \
  873. { \
  874. int e = f->u.real.e;\
  875. int d = f->u.real.d;\
  876. int w = f->u.real.w;\
  877. fnode newf;\
  878. GFC_REAL_ ## x exp_d, r = 0.5, r_sc;\
  879. int low, high, mid;\
  880. int ubound, lbound;\
  881. char *p, pad = ' ';\
  882. int save_scale_factor, nb = 0;\
  883. bool result;\
  884. int nprinted, precision;\
  885. volatile GFC_REAL_ ## x temp;\
  886. \
  887. save_scale_factor = dtp->u.p.scale_factor;\
  888. \
  889. switch (dtp->u.p.current_unit->round_status)\
  890. {\
  891. case ROUND_ZERO:\
  892. r = sign_bit ? 1.0 : 0.0;\
  893. break;\
  894. case ROUND_UP:\
  895. r = 1.0;\
  896. break;\
  897. case ROUND_DOWN:\
  898. r = 0.0;\
  899. break;\
  900. default:\
  901. break;\
  902. }\
  903. \
  904. exp_d = calculate_exp_ ## x (d);\
  905. r_sc = (1 - r / exp_d);\
  906. temp = 0.1 * r_sc;\
  907. if ((m > 0.0 && ((m < temp) || (r >= (exp_d - m))))\
  908. || ((m == 0.0) && !(compile_options.allow_std\
  909. & (GFC_STD_F2003 | GFC_STD_F2008)))\
  910. || d == 0)\
  911. { \
  912. newf.format = FMT_E;\
  913. newf.u.real.w = w;\
  914. newf.u.real.d = d - comp_d;\
  915. newf.u.real.e = e;\
  916. nb = 0;\
  917. precision = determine_precision (dtp, &newf, x);\
  918. nprinted = DTOA(y,precision,m); \
  919. goto finish;\
  920. }\
  921. \
  922. mid = 0;\
  923. low = 0;\
  924. high = d + 1;\
  925. lbound = 0;\
  926. ubound = d + 1;\
  927. \
  928. while (low <= high)\
  929. { \
  930. mid = (low + high) / 2;\
  931. \
  932. temp = (calculate_exp_ ## x (mid - 1) * r_sc);\
  933. \
  934. if (m < temp)\
  935. { \
  936. ubound = mid;\
  937. if (ubound == lbound + 1)\
  938. break;\
  939. high = mid - 1;\
  940. }\
  941. else if (m > temp)\
  942. { \
  943. lbound = mid;\
  944. if (ubound == lbound + 1)\
  945. { \
  946. mid ++;\
  947. break;\
  948. }\
  949. low = mid + 1;\
  950. }\
  951. else\
  952. {\
  953. mid++;\
  954. break;\
  955. }\
  956. }\
  957. \
  958. nb = e <= 0 ? 4 : e + 2;\
  959. nb = nb >= w ? w - 1 : nb;\
  960. newf.format = FMT_F;\
  961. newf.u.real.w = w - nb;\
  962. newf.u.real.d = m == 0.0 ? d - 1 : -(mid - d - 1) ;\
  963. dtp->u.p.scale_factor = 0;\
  964. precision = determine_precision (dtp, &newf, x); \
  965. nprinted = FDTOA(y,precision,m); \
  966. \
  967. finish:\
  968. result = output_float (dtp, &newf, buffer, size, nprinted, precision,\
  969. sign_bit, zero_flag);\
  970. dtp->u.p.scale_factor = save_scale_factor;\
  971. \
  972. \
  973. if (nb > 0 && !dtp->u.p.g0_no_blanks)\
  974. {\
  975. p = write_block (dtp, nb);\
  976. if (p == NULL)\
  977. return;\
  978. if (!result)\
  979. pad = '*';\
  980. if (unlikely (is_char4_unit (dtp)))\
  981. {\
  982. gfc_char4_t *p4 = (gfc_char4_t *) p;\
  983. memset4 (p4, pad, nb);\
  984. }\
  985. else \
  986. memset (p, pad, nb);\
  987. }\
  988. }\
  989. OUTPUT_FLOAT_FMT_G(4,)
  990. OUTPUT_FLOAT_FMT_G(8,)
  991. #ifdef HAVE_GFC_REAL_10
  992. OUTPUT_FLOAT_FMT_G(10,L)
  993. #endif
  994. #ifdef HAVE_GFC_REAL_16
  995. # ifdef GFC_REAL_16_IS_FLOAT128
  996. OUTPUT_FLOAT_FMT_G(16,Q)
  997. #else
  998. OUTPUT_FLOAT_FMT_G(16,L)
  999. #endif
  1000. #endif
  1001. #undef OUTPUT_FLOAT_FMT_G
  1002. /* EN format is tricky since the number of significant digits depends
  1003. on the magnitude. Solve it by first printing a temporary value and
  1004. figure out the number of significant digits from the printed
  1005. exponent. Values y, 0.95*10.0**e <= y <10.0**e, are rounded to
  1006. 10.0**e even when the final result will not be rounded to 10.0**e.
  1007. For these values the exponent returned by atoi has to be decremented
  1008. by one. The values y in the ranges
  1009. (1000.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*(n+1))
  1010. (100.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+2)
  1011. (10.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+1)
  1012. are correctly rounded respectively to 1.0...0*10.0*(3*(n+1)),
  1013. 100.0...0*10.0*(3*n), and 10.0...0*10.0*(3*n), where 0...0
  1014. represents d zeroes, by the lines 279 to 297. */
  1015. #define EN_PREC(x,y)\
  1016. {\
  1017. volatile GFC_REAL_ ## x tmp, one = 1.0;\
  1018. tmp = * (GFC_REAL_ ## x *)source;\
  1019. if (ISFINITE (y,tmp))\
  1020. {\
  1021. nprinted = DTOA(y,0,tmp);\
  1022. int e = atoi (&buffer[4]);\
  1023. if (buffer[1] == '1')\
  1024. {\
  1025. tmp = (calculate_exp_ ## x (-e)) * tmp;\
  1026. tmp = one - (tmp < 0 ? -tmp : tmp); \
  1027. if (tmp > 0)\
  1028. e = e - 1;\
  1029. }\
  1030. nbefore = e%3;\
  1031. if (nbefore < 0)\
  1032. nbefore = 3 + nbefore;\
  1033. }\
  1034. else\
  1035. nprinted = -1;\
  1036. }\
  1037. static int
  1038. determine_en_precision (st_parameter_dt *dtp, const fnode *f,
  1039. const char *source, int len)
  1040. {
  1041. int nprinted;
  1042. char buffer[10];
  1043. const size_t size = 10;
  1044. int nbefore; /* digits before decimal point - 1. */
  1045. switch (len)
  1046. {
  1047. case 4:
  1048. EN_PREC(4,)
  1049. break;
  1050. case 8:
  1051. EN_PREC(8,)
  1052. break;
  1053. #ifdef HAVE_GFC_REAL_10
  1054. case 10:
  1055. EN_PREC(10,L)
  1056. break;
  1057. #endif
  1058. #ifdef HAVE_GFC_REAL_16
  1059. case 16:
  1060. # ifdef GFC_REAL_16_IS_FLOAT128
  1061. EN_PREC(16,Q)
  1062. # else
  1063. EN_PREC(16,L)
  1064. # endif
  1065. break;
  1066. #endif
  1067. default:
  1068. internal_error (NULL, "bad real kind");
  1069. }
  1070. if (nprinted == -1)
  1071. return -1;
  1072. int prec = f->u.real.d + nbefore;
  1073. if (dtp->u.p.current_unit->round_status != ROUND_UNSPECIFIED
  1074. && dtp->u.p.current_unit->round_status != ROUND_PROCDEFINED)
  1075. prec += 2 * len + 4;
  1076. return prec;
  1077. }
  1078. #define WRITE_FLOAT(x,y)\
  1079. {\
  1080. GFC_REAL_ ## x tmp;\
  1081. tmp = * (GFC_REAL_ ## x *)source;\
  1082. sign_bit = SIGNBIT (y,tmp);\
  1083. if (!ISFINITE (y,tmp))\
  1084. { \
  1085. write_infnan (dtp, f, ISNAN (y,tmp), sign_bit);\
  1086. return;\
  1087. }\
  1088. tmp = sign_bit ? -tmp : tmp;\
  1089. zero_flag = (tmp == 0.0);\
  1090. if (f->format == FMT_G)\
  1091. output_float_FMT_G_ ## x (dtp, f, tmp, buffer, size, sign_bit, \
  1092. zero_flag, comp_d);\
  1093. else\
  1094. {\
  1095. if (f->format == FMT_F)\
  1096. nprinted = FDTOA(y,precision,tmp); \
  1097. else\
  1098. nprinted = DTOA(y,precision,tmp); \
  1099. output_float (dtp, f, buffer, size, nprinted, precision,\
  1100. sign_bit, zero_flag);\
  1101. }\
  1102. }\
  1103. /* Output a real number according to its format. */
  1104. static void
  1105. write_float (st_parameter_dt *dtp, const fnode *f, const char *source, \
  1106. int len, int comp_d)
  1107. {
  1108. int sign_bit, nprinted;
  1109. int precision; /* Precision for snprintf call. */
  1110. bool zero_flag;
  1111. if (f->format != FMT_EN)
  1112. precision = determine_precision (dtp, f, len);
  1113. else
  1114. precision = determine_en_precision (dtp, f, source, len);
  1115. /* 4932 is the maximum exponent of long double and quad precision, 3
  1116. extra characters for the sign, the decimal point, and the
  1117. trailing null, and finally some extra digits depending on the
  1118. requested precision. */
  1119. const size_t size = 4932 + 3 + precision;
  1120. #define BUF_STACK_SZ 5000
  1121. char buf_stack[BUF_STACK_SZ];
  1122. char *buffer;
  1123. if (size > BUF_STACK_SZ)
  1124. buffer = xmalloc (size);
  1125. else
  1126. buffer = buf_stack;
  1127. switch (len)
  1128. {
  1129. case 4:
  1130. WRITE_FLOAT(4,)
  1131. break;
  1132. case 8:
  1133. WRITE_FLOAT(8,)
  1134. break;
  1135. #ifdef HAVE_GFC_REAL_10
  1136. case 10:
  1137. WRITE_FLOAT(10,L)
  1138. break;
  1139. #endif
  1140. #ifdef HAVE_GFC_REAL_16
  1141. case 16:
  1142. # ifdef GFC_REAL_16_IS_FLOAT128
  1143. WRITE_FLOAT(16,Q)
  1144. # else
  1145. WRITE_FLOAT(16,L)
  1146. # endif
  1147. break;
  1148. #endif
  1149. default:
  1150. internal_error (NULL, "bad real kind");
  1151. }
  1152. if (size > BUF_STACK_SZ)
  1153. free (buffer);
  1154. }