1 /* Copyright (C) 2007, 2008, 2009, 2010, 2011 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
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
30 { S_NONE, S_MINUS, S_PLUS }
33 /* Given a flag that indicates if a value is negative or not, return a
34 sign_t that gives the sign that we need to produce. */
37 calculate_sign (st_parameter_dt *dtp, int negative_flag)
44 switch (dtp->u.p.sign_status)
46 case SIGN_SP: /* Show sign. */
49 case SIGN_SS: /* Suppress sign. */
52 case SIGN_S: /* Processor defined. */
53 case SIGN_UNSPECIFIED:
54 s = options.optional_plus ? S_PLUS : S_NONE;
62 /* Output a real number according to its format which is FMT_G free. */
65 output_float (st_parameter_dt *dtp, const fnode *f, char *buffer, size_t size,
66 int sign_bit, bool zero_flag, int ndigits, int edigits)
75 /* Number of digits before the decimal point. */
77 /* Number of zeros after the decimal point. */
79 /* Number of digits after the decimal point. */
81 /* Number of zeros after the decimal point, whatever the precision. */
95 /* We should always know the field width and precision. */
97 internal_error (&dtp->common, "Unspecified precision");
99 sign = calculate_sign (dtp, sign_bit);
101 /* The following code checks the given string has punctuation in the correct
102 places. Uncomment if needed for debugging.
103 if (d != 0 && ((buffer[2] != '.' && buffer[2] != ',')
104 || buffer[ndigits + 2] != 'e'))
105 internal_error (&dtp->common, "printf is broken"); */
107 /* Read the exponent back in. */
108 e = atoi (&buffer[ndigits + 3]) + 1;
110 /* Make sure zero comes out as 0.0e0. */
114 /* Normalize the fractional component. */
115 buffer[2] = buffer[1];
118 /* Figure out where to place the decimal point. */
122 if (d == 0 && e <= 0 && dtp->u.p.scale_factor == 0)
124 memmove (digits + 1, digits, ndigits - 1);
129 nbefore = e + dtp->u.p.scale_factor;
149 i = dtp->u.p.scale_factor;
150 if (d <= 0 && i == 0)
152 generate_error (&dtp->common, LIBERROR_FORMAT, "Precision not "
153 "greater than zero in format specifier 'E' or 'D'");
156 if (i <= -d || i >= d + 2)
158 generate_error (&dtp->common, LIBERROR_FORMAT, "Scale factor "
159 "out of range in format specifier 'E' or 'D'");
175 nafter = (d - i) + 1;
191 /* The exponent must be a multiple of three, with 1-3 digits before
192 the decimal point. */
201 nbefore = 3 - nbefore;
220 /* Should never happen. */
221 internal_error (&dtp->common, "Unexpected format token");
224 /* Round the value. The value being rounded is an unsigned magnitude.
225 The ROUND_COMPATIBLE is rounding away from zero when there is a tie. */
226 switch (dtp->u.p.current_unit->round_status)
228 case ROUND_ZERO: /* Do nothing and truncation occurs. */
241 /* Round compatible unless there is a tie. A tie is a 5 with
242 all trailing zero's. */
243 i = nafter + nbefore;
244 if (digits[i] == '5')
246 for(i++ ; i < ndigits; i++)
248 if (digits[i] != '0')
251 /* It is a tie so round to even. */
252 switch (digits[nafter + nbefore - 1])
259 /* If odd, round away from zero to even. */
262 /* If even, skip rounding, truncate to even. */
267 case ROUND_PROCDEFINED:
268 case ROUND_UNSPECIFIED:
269 case ROUND_COMPATIBLE:
271 /* Just fall through and do the actual rounding. */
276 if (nbefore + nafter == 0)
279 if (nzero_real == d && digits[0] >= rchar)
281 /* We rounded to zero but shouldn't have */
288 else if (nbefore + nafter < ndigits)
290 ndigits = nbefore + nafter;
292 if (digits[i] >= rchar)
294 /* Propagate the carry. */
295 for (i--; i >= 0; i--)
297 if (digits[i] != '9')
307 /* The carry overflowed. Fortunately we have some spare
308 space at the start of the buffer. We may discard some
309 digits, but this is ok because we already know they are
323 else if (ft == FMT_EN)
340 /* Calculate the format of the exponent field. */
344 for (i = abs (e); i >= 10; i /= 10)
349 /* Width not specified. Must be no more than 3 digits. */
350 if (e > 999 || e < -999)
355 if (e > 99 || e < -99)
361 /* Exponent width specified, check it is wide enough. */
362 if (edigits > f->u.real.e)
365 edigits = f->u.real.e + 2;
371 /* Scan the digits string and count the number of zeros. If we make it
372 all the way through the loop, we know the value is zero after the
373 rounding completed above. */
374 for (i = 0; i < ndigits; i++)
376 if (digits[i] != '0')
380 /* To format properly, we need to know if the rounded result is zero and if
381 so, we set the zero_flag which may have been already set for
386 /* The output is zero, so set the sign according to the sign bit unless
387 -fno-sign-zero was specified. */
388 if (compile_options.sign_zero == 1)
389 sign = calculate_sign (dtp, sign_bit);
391 sign = calculate_sign (dtp, 0);
394 /* Pick a field size if none was specified, taking into account small
395 values that may have been rounded to zero. */
399 w = d + (sign != S_NONE ? 2 : 1) + (d == 0 ? 1 : 0);
402 w = nbefore + nzero + nafter + (sign != S_NONE ? 2 : 1);
407 /* Work out how much padding is needed. */
408 nblanks = w - (nbefore + nzero + nafter + edigits + 1);
412 if (dtp->u.p.g0_no_blanks)
418 /* Create the ouput buffer. */
419 out = write_block (dtp, w);
423 /* Check the value fits in the specified field width. */
424 if (nblanks < 0 || edigits == -1 || w == 1 || (w == 2 && sign != S_NONE))
426 if (unlikely (is_char4_unit (dtp)))
428 gfc_char4_t *out4 = (gfc_char4_t *) out;
429 memset4 (out4, '*', w);
436 /* See if we have space for a zero before the decimal point. */
437 if (nbefore == 0 && nblanks > 0)
445 /* For internal character(kind=4) units, we duplicate the code used for
446 regular output slightly modified. This needs to be maintained
447 consistent with the regular code that follows this block. */
448 if (unlikely (is_char4_unit (dtp)))
450 gfc_char4_t *out4 = (gfc_char4_t *) out;
451 /* Pad to full field width. */
453 if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
455 memset4 (out4, ' ', nblanks);
459 /* Output the initial sign (if any). */
462 else if (sign == S_MINUS)
465 /* Output an optional leading zero. */
469 /* Output the part before the decimal point, padding with zeros. */
472 if (nbefore > ndigits)
475 memcpy4 (out4, digits, i);
483 memcpy4 (out4, digits, i);
491 /* Output the decimal point. */
492 *(out4++) = dtp->u.p.current_unit->decimal_status
493 == DECIMAL_POINT ? '.' : ',';
495 /* Output leading zeros after the decimal point. */
498 for (i = 0; i < nzero; i++)
502 /* Output digits after the decimal point, padding with zeros. */
505 if (nafter > ndigits)
510 memcpy4 (out4, digits, i);
519 /* Output the exponent. */
528 snprintf (buffer, size, "%+0*d", edigits, e);
530 sprintf (buffer, "%+0*d", edigits, e);
532 memcpy4 (out4, buffer, edigits);
535 if (dtp->u.p.no_leading_blank)
538 memset4 (out4, ' ' , nblanks);
539 dtp->u.p.no_leading_blank = 0;
542 } /* End of character(kind=4) internal unit code. */
544 /* Pad to full field width. */
546 if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
548 memset (out, ' ', nblanks);
552 /* Output the initial sign (if any). */
555 else if (sign == S_MINUS)
558 /* Output an optional leading zero. */
562 /* Output the part before the decimal point, padding with zeros. */
565 if (nbefore > ndigits)
568 memcpy (out, digits, i);
576 memcpy (out, digits, i);
584 /* Output the decimal point. */
585 *(out++) = dtp->u.p.current_unit->decimal_status == DECIMAL_POINT ? '.' : ',';
587 /* Output leading zeros after the decimal point. */
590 for (i = 0; i < nzero; i++)
594 /* Output digits after the decimal point, padding with zeros. */
597 if (nafter > ndigits)
602 memcpy (out, digits, i);
611 /* Output the exponent. */
620 snprintf (buffer, size, "%+0*d", edigits, e);
622 sprintf (buffer, "%+0*d", edigits, e);
624 memcpy (out, buffer, edigits);
627 if (dtp->u.p.no_leading_blank)
630 memset( out , ' ' , nblanks );
631 dtp->u.p.no_leading_blank = 0;
636 #undef MIN_FIELD_WIDTH
641 /* Write "Infinite" or "Nan" as appropriate for the given format. */
644 write_infnan (st_parameter_dt *dtp, const fnode *f, int isnan_flag, int sign_bit)
651 if (f->format != FMT_B && f->format != FMT_O && f->format != FMT_Z)
653 sign = calculate_sign (dtp, sign_bit);
654 mark = (sign == S_PLUS || sign == S_MINUS) ? 8 : 7;
658 /* If the field width is zero, the processor must select a width
659 not zero. 4 is chosen to allow output of '-Inf' or '+Inf' */
666 nb = (sign == S_PLUS || sign == S_MINUS) ? 4 : 3;
668 p = write_block (dtp, nb);
673 if (unlikely (is_char4_unit (dtp)))
675 gfc_char4_t *p4 = (gfc_char4_t *) p;
676 memset4 (p4, '*', nb);
683 if (unlikely (is_char4_unit (dtp)))
685 gfc_char4_t *p4 = (gfc_char4_t *) p;
686 memset4 (p4, ' ', nb);
695 /* If the sign is negative and the width is 3, there is
696 insufficient room to output '-Inf', so output asterisks */
699 if (unlikely (is_char4_unit (dtp)))
701 gfc_char4_t *p4 = (gfc_char4_t *) p;
702 memset4 (p4, '*', nb);
708 /* The negative sign is mandatory */
712 /* The positive sign is optional, but we output it for
716 if (unlikely (is_char4_unit (dtp)))
718 gfc_char4_t *p4 = (gfc_char4_t *) p;
721 /* We have room, so output 'Infinity' */
722 memcpy4 (p4 + nb - 8, "Infinity", 8);
724 /* For the case of width equals mark, there is not enough room
725 for the sign and 'Infinity' so we go with 'Inf' */
726 memcpy4 (p4 + nb - 3, "Inf", 3);
728 if (sign == S_PLUS || sign == S_MINUS)
730 if (nb < 9 && nb > 3)
731 /* Put the sign in front of Inf */
732 p4[nb - 4] = (gfc_char4_t) fin;
734 /* Put the sign in front of Infinity */
735 p4[nb - 9] = (gfc_char4_t) fin;
741 /* We have room, so output 'Infinity' */
742 memcpy(p + nb - 8, "Infinity", 8);
744 /* For the case of width equals 8, there is not enough room
745 for the sign and 'Infinity' so we go with 'Inf' */
746 memcpy(p + nb - 3, "Inf", 3);
748 if (sign == S_PLUS || sign == S_MINUS)
750 if (nb < 9 && nb > 3)
751 p[nb - 4] = fin; /* Put the sign in front of Inf */
753 p[nb - 9] = fin; /* Put the sign in front of Infinity */
758 if (unlikely (is_char4_unit (dtp)))
760 gfc_char4_t *p4 = (gfc_char4_t *) p;
761 memcpy4 (p4 + nb - 3, "NaN", 3);
764 memcpy(p + nb - 3, "NaN", 3);
771 /* Returns the value of 10**d. */
773 #define CALCULATE_EXP(x) \
774 inline static GFC_REAL_ ## x \
775 calculate_exp_ ## x (int d)\
778 GFC_REAL_ ## x r = 1.0;\
779 for (i = 0; i< (d >= 0 ? d : -d); i++)\
781 r = (d >= 0) ? r : 1.0 / r;\
789 #ifdef HAVE_GFC_REAL_10
793 #ifdef HAVE_GFC_REAL_16
798 /* Generate corresponding I/O format for FMT_G and output.
799 The rules to translate FMT_G to FMT_E or FMT_F from DEC fortran
800 LRM (table 11-2, Chapter 11, "I/O Formatting", P11-25) is:
802 Data Magnitude Equivalent Conversion
803 0< m < 0.1-0.5*10**(-d-1) Ew.d[Ee]
804 m = 0 F(w-n).(d-1), n' '
805 0.1-0.5*10**(-d-1)<= m < 1-0.5*10**(-d) F(w-n).d, n' '
806 1-0.5*10**(-d)<= m < 10-0.5*10**(-d+1) F(w-n).(d-1), n' '
807 10-0.5*10**(-d+1)<= m < 100-0.5*10**(-d+2) F(w-n).(d-2), n' '
808 ................ ..........
809 10**(d-1)-0.5*10**(-1)<= m <10**d-0.5 F(w-n).0,n(' ')
810 m >= 10**d-0.5 Ew.d[Ee]
812 notes: for Gw.d , n' ' means 4 blanks
813 for Gw.dEe, n' ' means e+2 blanks */
815 #define OUTPUT_FLOAT_FMT_G(x) \
817 output_float_FMT_G_ ## x (st_parameter_dt *dtp, const fnode *f, \
818 GFC_REAL_ ## x m, char *buffer, size_t size, \
819 int sign_bit, bool zero_flag, int ndigits, int edigits) \
821 int e = f->u.real.e;\
822 int d = f->u.real.d;\
823 int w = f->u.real.w;\
825 GFC_REAL_ ## x rexp_d;\
829 int save_scale_factor, nb = 0;\
832 save_scale_factor = dtp->u.p.scale_factor;\
833 newf = (fnode *) get_mem (sizeof (fnode));\
835 rexp_d = calculate_exp_ ## x (-d);\
836 if ((m > 0.0 && m < 0.1 - 0.05 * rexp_d) || (rexp_d * (m + 0.5) >= 1.0) ||\
837 ((m == 0.0) && !(compile_options.allow_std & GFC_STD_F2003)))\
839 newf->format = FMT_E;\
855 GFC_REAL_ ## x temp;\
856 mid = (low + high) / 2;\
858 temp = (calculate_exp_ ## x (mid - 1) * (1 - 0.5 * rexp_d));\
863 if (ubound == lbound + 1)\
870 if (ubound == lbound + 1)\
891 nb = nb >= w ? 0 : nb;\
892 newf->format = FMT_F;\
893 newf->u.real.w = f->u.real.w - nb;\
896 newf->u.real.d = d - 1;\
898 newf->u.real.d = - (mid - d - 1);\
900 dtp->u.p.scale_factor = 0;\
903 result = output_float (dtp, newf, buffer, size, sign_bit, zero_flag, \
905 dtp->u.p.scale_factor = save_scale_factor;\
909 if (nb > 0 && !dtp->u.p.g0_no_blanks)\
911 p = write_block (dtp, nb);\
914 if (result == FAILURE)\
916 if (unlikely (is_char4_unit (dtp)))\
918 gfc_char4_t *p4 = (gfc_char4_t *) p;\
919 memset4 (p4, pad, nb);\
922 memset (p, pad, nb);\
926 OUTPUT_FLOAT_FMT_G(4)
928 OUTPUT_FLOAT_FMT_G(8)
930 #ifdef HAVE_GFC_REAL_10
931 OUTPUT_FLOAT_FMT_G(10)
934 #ifdef HAVE_GFC_REAL_16
935 OUTPUT_FLOAT_FMT_G(16)
938 #undef OUTPUT_FLOAT_FMT_G
941 /* Define a macro to build code for write_float. */
943 /* Note: Before output_float is called, sprintf is used to print to buffer the
944 number in the format +D.DDDDe+ddd. For an N digit exponent, this gives us
945 (MIN_FIELD_WIDTH-5)-N digits after the decimal point, plus another one
946 before the decimal point.
948 # The result will always contain a decimal point, even if no
951 - The converted value is to be left adjusted on the field boundary
953 + A sign (+ or -) always be placed before a number
955 MIN_FIELD_WIDTH minimum field width
957 * (ndigits-1) is used as the precision
959 e format: [-]d.ddde±dd where there is one digit before the
960 decimal-point character and the number of digits after it is
961 equal to the precision. The exponent always contains at least two
962 digits; if the value is zero, the exponent is 00. */
967 snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
968 "e", ndigits - 1, tmp);
971 snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
972 "Le", ndigits - 1, tmp);
977 sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
978 "e", ndigits - 1, tmp);
981 sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
982 "Le", ndigits - 1, tmp);
986 #if defined(GFC_REAL_16_IS_FLOAT128)
988 __qmath_(quadmath_snprintf) (buffer, sizeof buffer, \
989 "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
990 "Qe", ndigits - 1, tmp);
993 #define WRITE_FLOAT(x,y)\
996 tmp = * (GFC_REAL_ ## x *)source;\
997 sign_bit = signbit (tmp);\
998 if (!isfinite (tmp))\
1000 write_infnan (dtp, f, isnan (tmp), sign_bit);\
1003 tmp = sign_bit ? -tmp : tmp;\
1004 zero_flag = (tmp == 0.0);\
1008 if (f->format != FMT_G)\
1009 output_float (dtp, f, buffer, size, sign_bit, zero_flag, ndigits, \
1012 output_float_FMT_G_ ## x (dtp, f, tmp, buffer, size, sign_bit, \
1013 zero_flag, ndigits, edigits);\
1016 /* Output a real number according to its format. */
1019 write_float (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
1022 #if defined(HAVE_GFC_REAL_16) || __LDBL_DIG__ > 18
1023 # define MIN_FIELD_WIDTH 46
1025 # define MIN_FIELD_WIDTH 31
1027 #define STR(x) STR1(x)
1030 /* This must be large enough to accurately hold any value. */
1031 char buffer[MIN_FIELD_WIDTH+1];
1032 int sign_bit, ndigits, edigits;
1036 size = MIN_FIELD_WIDTH+1;
1038 /* printf pads blanks for us on the exponent so we just need it big enough
1039 to handle the largest number of exponent digits expected. */
1042 if (f->format == FMT_F || f->format == FMT_EN || f->format == FMT_G
1043 || ((f->format == FMT_D || f->format == FMT_E)
1044 && dtp->u.p.scale_factor != 0))
1046 /* Always convert at full precision to avoid double rounding. */
1047 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
1051 /* The number of digits is known, so let printf do the rounding. */
1052 if (f->format == FMT_ES)
1053 ndigits = f->u.real.d + 1;
1055 ndigits = f->u.real.d;
1056 if (ndigits > MIN_FIELD_WIDTH - 4 - edigits)
1057 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
1070 #ifdef HAVE_GFC_REAL_10
1075 #ifdef HAVE_GFC_REAL_16
1077 # ifdef GFC_REAL_16_IS_FLOAT128
1085 internal_error (NULL, "bad real kind");