1 /* Copyright (C) 2007, 2008 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 95 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 2, or (at your option)
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 Libgfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with Libgfortran; see the file COPYING. If not, write to
29 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA. */
35 { S_NONE, S_MINUS, S_PLUS }
38 /* Given a flag that indicates if a value is negative or not, return a
39 sign_t that gives the sign that we need to produce. */
42 calculate_sign (st_parameter_dt *dtp, int negative_flag)
49 switch (dtp->u.p.sign_status)
51 case SIGN_SP: /* Show sign. */
54 case SIGN_SS: /* Suppress sign. */
57 case SIGN_S: /* Processor defined. */
58 case SIGN_UNSPECIFIED:
59 s = options.optional_plus ? S_PLUS : S_NONE;
67 /* Output a real number according to its format which is FMT_G free. */
70 output_float (st_parameter_dt *dtp, const fnode *f, char *buffer, size_t size,
71 int sign_bit, bool zero_flag, int ndigits, int edigits)
80 /* Number of digits before the decimal point. */
82 /* Number of zeros after the decimal point. */
84 /* Number of digits after the decimal point. */
86 /* Number of zeros after the decimal point, whatever the precision. */
99 /* We should always know the field width and precision. */
101 internal_error (&dtp->common, "Unspecified precision");
103 sign = calculate_sign (dtp, sign_bit);
105 /* The following code checks the given string has punctuation in the correct
106 places. Uncomment if needed for debugging.
107 if (d != 0 && ((buffer[2] != '.' && buffer[2] != ',')
108 || buffer[ndigits + 2] != 'e'))
109 internal_error (&dtp->common, "printf is broken"); */
111 /* Read the exponent back in. */
112 e = atoi (&buffer[ndigits + 3]) + 1;
114 /* Make sure zero comes out as 0.0e0. */
118 if (compile_options.sign_zero == 1)
119 sign = calculate_sign (dtp, sign_bit);
121 sign = calculate_sign (dtp, 0);
124 /* Normalize the fractional component. */
125 buffer[2] = buffer[1];
128 /* Figure out where to place the decimal point. */
132 nbefore = e + dtp->u.p.scale_factor;
152 i = dtp->u.p.scale_factor;
153 if (d <= 0 && i == 0)
155 generate_error (&dtp->common, LIBERROR_FORMAT, "Precision not "
156 "greater than zero in format specifier 'E' or 'D'");
159 if (i <= -d || i >= d + 2)
161 generate_error (&dtp->common, LIBERROR_FORMAT, "Scale factor "
162 "out of range in format specifier 'E' or 'D'");
178 nafter = (d - i) + 1;
194 /* The exponent must be a multiple of three, with 1-3 digits before
195 the decimal point. */
204 nbefore = 3 - nbefore;
223 /* Should never happen. */
224 internal_error (&dtp->common, "Unexpected format token");
227 /* Round the value. */
228 if (nbefore + nafter == 0)
231 if (nzero_real == d && digits[0] >= '5')
233 /* We rounded to zero but shouldn't have */
240 else if (nbefore + nafter < ndigits)
242 ndigits = nbefore + nafter;
244 if (digits[i] >= '5')
246 /* Propagate the carry. */
247 for (i--; i >= 0; i--)
249 if (digits[i] != '9')
259 /* The carry overflowed. Fortunately we have some spare space
260 at the start of the buffer. We may discard some digits, but
261 this is ok because we already know they are zero. */
274 else if (ft == FMT_EN)
289 /* Calculate the format of the exponent field. */
293 for (i = abs (e); i >= 10; i /= 10)
298 /* Width not specified. Must be no more than 3 digits. */
299 if (e > 999 || e < -999)
304 if (e > 99 || e < -99)
310 /* Exponent width specified, check it is wide enough. */
311 if (edigits > f->u.real.e)
314 edigits = f->u.real.e + 2;
320 /* Pick a field size if none was specified. */
322 w = nbefore + nzero + nafter + (sign != S_NONE ? 2 : 1);
324 /* Create the ouput buffer. */
325 out = write_block (dtp, w);
329 /* Zero values always output as positive, even if the value was negative
331 for (i = 0; i < ndigits; i++)
333 if (digits[i] != '0')
338 /* The output is zero, so set the sign according to the sign bit unless
339 -fno-sign-zero was specified. */
340 if (compile_options.sign_zero == 1)
341 sign = calculate_sign (dtp, sign_bit);
343 sign = calculate_sign (dtp, 0);
346 /* Work out how much padding is needed. */
347 nblanks = w - (nbefore + nzero + nafter + edigits + 1);
351 /* Check the value fits in the specified field width. */
352 if (nblanks < 0 || edigits == -1)
358 /* See if we have space for a zero before the decimal point. */
359 if (nbefore == 0 && nblanks > 0)
367 /* Pad to full field width. */
369 if ( ( nblanks > 0 ) && !dtp->u.p.no_leading_blank)
371 memset (out, ' ', nblanks);
375 /* Output the initial sign (if any). */
378 else if (sign == S_MINUS)
381 /* Output an optional leading zero. */
385 /* Output the part before the decimal point, padding with zeros. */
388 if (nbefore > ndigits)
391 memcpy (out, digits, i);
399 memcpy (out, digits, i);
406 /* Output the decimal point. */
407 if (dtp->common.flags & IOPARM_DT_HAS_F2003)
408 *(out++) = dtp->u.p.decimal_status == DECIMAL_POINT ? '.' : ',';
412 /* Output leading zeros after the decimal point. */
415 for (i = 0; i < nzero; i++)
419 /* Output digits after the decimal point, padding with zeros. */
422 if (nafter > ndigits)
427 memcpy (out, digits, i);
436 /* Output the exponent. */
445 snprintf (buffer, size, "%+0*d", edigits, e);
447 sprintf (buffer, "%+0*d", edigits, e);
449 memcpy (out, buffer, edigits);
451 if (dtp->u.p.no_leading_blank)
454 memset( out , ' ' , nblanks );
455 dtp->u.p.no_leading_blank = 0;
459 #undef MIN_FIELD_WIDTH
463 /* Write "Infinite" or "Nan" as appropriate for the given format. */
466 write_infnan (st_parameter_dt *dtp, const fnode *f, int isnan_flag, int sign_bit)
471 if (f->format != FMT_B && f->format != FMT_O && f->format != FMT_Z)
475 /* If the field width is zero, the processor must select a width
476 not zero. 4 is chosen to allow output of '-Inf' or '+Inf' */
479 p = write_block (dtp, nb);
494 /* If the sign is negative and the width is 3, there is
495 insufficient room to output '-Inf', so output asterisks */
503 /* The negative sign is mandatory */
509 /* The positive sign is optional, but we output it for
515 /* We have room, so output 'Infinity' */
516 memcpy(p + nb - 8, "Infinity", 8);
519 /* For the case of width equals 8, there is not enough room
520 for the sign and 'Infinity' so we go with 'Inf' */
521 memcpy(p + nb - 3, "Inf", 3);
523 if (nb < 9 && nb > 3)
524 p[nb - 4] = fin; /* Put the sign in front of Inf */
526 p[nb - 9] = fin; /* Put the sign in front of Infinity */
529 memcpy(p + nb - 3, "NaN", 3);
535 /* Returns the value of 10**d. */
537 #define CALCULATE_EXP(x) \
538 inline static GFC_REAL_ ## x \
539 calculate_exp_ ## x (int d)\
542 GFC_REAL_ ## x r = 1.0;\
543 for (i = 0; i< (d >= 0 ? d : -d); i++)\
545 r = (d >= 0) ? r : 1.0 / r;\
553 #ifdef HAVE_GFC_REAL_10
557 #ifdef HAVE_GFC_REAL_16
562 /* Generate corresponding I/O format for FMT_G and output.
563 The rules to translate FMT_G to FMT_E or FMT_F from DEC fortran
564 LRM (table 11-2, Chapter 11, "I/O Formatting", P11-25) is:
566 Data Magnitude Equivalent Conversion
567 0< m < 0.1-0.5*10**(-d-1) Ew.d[Ee]
568 m = 0 F(w-n).(d-1), n' '
569 0.1-0.5*10**(-d-1)<= m < 1-0.5*10**(-d) F(w-n).d, n' '
570 1-0.5*10**(-d)<= m < 10-0.5*10**(-d+1) F(w-n).(d-1), n' '
571 10-0.5*10**(-d+1)<= m < 100-0.5*10**(-d+2) F(w-n).(d-2), n' '
572 ................ ..........
573 10**(d-1)-0.5*10**(-1)<= m <10**d-0.5 F(w-n).0,n(' ')
574 m >= 10**d-0.5 Ew.d[Ee]
576 notes: for Gw.d , n' ' means 4 blanks
577 for Gw.dEe, n' ' means e+2 blanks */
579 #define OUTPUT_FLOAT_FMT_G(x) \
581 output_float_FMT_G_ ## x (st_parameter_dt *dtp, const fnode *f, \
582 GFC_REAL_ ## x m, char *buffer, size_t size, \
583 int sign_bit, bool zero_flag, int ndigits, int edigits) \
585 int e = f->u.real.e;\
586 int d = f->u.real.d;\
587 int w = f->u.real.w;\
589 GFC_REAL_ ## x exp_d;\
593 int save_scale_factor, nb = 0;\
595 save_scale_factor = dtp->u.p.scale_factor;\
596 newf = get_mem (sizeof (fnode));\
598 exp_d = calculate_exp_ ## x (d);\
599 if ((m > 0.0 && m < 0.1 - 0.05 / exp_d) || (m >= exp_d - 0.5 ) ||\
600 ((m == 0.0) && !(compile_options.allow_std & GFC_STD_F2003)))\
602 newf->format = FMT_E;\
618 GFC_REAL_ ## x temp;\
619 mid = (low + high) / 2;\
621 temp = 0.1 * calculate_exp_ ## x (mid) - 0.5\
622 * calculate_exp_ ## x (mid - d - 1);\
627 if (ubound == lbound + 1)\
634 if (ubound == lbound + 1)\
650 newf->format = FMT_F;\
651 newf->u.real.w = f->u.real.w - nb;\
654 newf->u.real.d = d - 1;\
656 newf->u.real.d = - (mid - d - 1);\
658 dtp->u.p.scale_factor = 0;\
661 output_float (dtp, newf, buffer, size, sign_bit, zero_flag, ndigits, \
663 dtp->u.p.scale_factor = save_scale_factor;\
669 p = write_block (dtp, nb);\
672 memset (p, ' ', nb);\
676 OUTPUT_FLOAT_FMT_G(4)
678 OUTPUT_FLOAT_FMT_G(8)
680 #ifdef HAVE_GFC_REAL_10
681 OUTPUT_FLOAT_FMT_G(10)
684 #ifdef HAVE_GFC_REAL_16
685 OUTPUT_FLOAT_FMT_G(16)
688 #undef OUTPUT_FLOAT_FMT_G
691 /* Define a macro to build code for write_float. */
693 /* Note: Before output_float is called, sprintf is used to print to buffer the
694 number in the format +D.DDDDe+ddd. For an N digit exponent, this gives us
695 (MIN_FIELD_WIDTH-5)-N digits after the decimal point, plus another one
696 before the decimal point.
698 # The result will always contain a decimal point, even if no
701 - The converted value is to be left adjusted on the field boundary
703 + A sign (+ or -) always be placed before a number
705 MIN_FIELD_WIDTH minimum field width
707 * (ndigits-1) is used as the precision
709 e format: [-]d.ddde±dd where there is one digit before the
710 decimal-point character and the number of digits after it is
711 equal to the precision. The exponent always contains at least two
712 digits; if the value is zero, the exponent is 00. */
717 snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
718 "e", ndigits - 1, tmp);
721 snprintf (buffer, size, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
722 "Le", ndigits - 1, tmp);
727 sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
728 "e", ndigits - 1, tmp);
731 sprintf (buffer, "%+-#" STR(MIN_FIELD_WIDTH) ".*" \
732 "Le", ndigits - 1, tmp);
736 #define WRITE_FLOAT(x,y)\
739 tmp = * (GFC_REAL_ ## x *)source;\
740 sign_bit = signbit (tmp);\
741 if (!isfinite (tmp))\
743 write_infnan (dtp, f, isnan (tmp), sign_bit);\
746 tmp = sign_bit ? -tmp : tmp;\
747 if (f->u.real.d == 0 && f->format == FMT_F)\
754 zero_flag = (tmp == 0.0);\
758 if (f->format != FMT_G)\
759 output_float (dtp, f, buffer, size, sign_bit, zero_flag, ndigits, \
762 output_float_FMT_G_ ## x (dtp, f, tmp, buffer, size, sign_bit, \
763 zero_flag, ndigits, edigits);\
766 /* Output a real number according to its format. */
769 write_float (st_parameter_dt *dtp, const fnode *f, const char *source, int len)
772 #if defined(HAVE_GFC_REAL_16) && __LDBL_DIG__ > 18
773 # define MIN_FIELD_WIDTH 46
775 # define MIN_FIELD_WIDTH 31
777 #define STR(x) STR1(x)
780 /* This must be large enough to accurately hold any value. */
781 char buffer[MIN_FIELD_WIDTH+1];
782 int sign_bit, ndigits, edigits;
786 size = MIN_FIELD_WIDTH+1;
788 /* printf pads blanks for us on the exponent so we just need it big enough
789 to handle the largest number of exponent digits expected. */
792 if (f->format == FMT_F || f->format == FMT_EN || f->format == FMT_G
793 || ((f->format == FMT_D || f->format == FMT_E)
794 && dtp->u.p.scale_factor != 0))
796 /* Always convert at full precision to avoid double rounding. */
797 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
801 /* The number of digits is known, so let printf do the rounding. */
802 if (f->format == FMT_ES)
803 ndigits = f->u.real.d + 1;
805 ndigits = f->u.real.d;
806 if (ndigits > MIN_FIELD_WIDTH - 4 - edigits)
807 ndigits = MIN_FIELD_WIDTH - 4 - edigits;
820 #ifdef HAVE_GFC_REAL_10
825 #ifdef HAVE_GFC_REAL_16
831 internal_error (NULL, "bad real kind");