From patchwork Fri Mar 7 22:05:56 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Dominique_d=27Humi=C3=A8res?= X-Patchwork-Id: 328092 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 968162C00BA for ; Sat, 8 Mar 2014 09:06:23 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; q=dns; s= default; b=Vf9VA6jng17cnKJ+mERwGK3gNMIlPfXq1wA3WvMvpC1JjPbXU4aei QzHFkLMWn/1QdTzrcOd5UykZsfrUVaMGQUARpZjfyF5Etnw4eoblf6owcr7LNAAt NGJT9twlnctjL8A6vVnni7PEl2n0fwsDDqq/+fRLtiyGfXutwrK3qs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s=default; bh=10+uSKS+qq8OLvwXtbVP2lkmqpc=; b=HWzd0vTGoDc6sYXsx+9tLfZvMshU lfE20TLBRgPLmXeRVw85dY8LFzsJKXzVMnW9RCuj5QNrnOg7cX65Q4Z3TxuXT5E0 1SaJMNJaiYvv0oBapu0FW3NC7OhHH6+uha3Zv7oEoxVojIO9zCz+oJjGCTNbgZWY t7GUQQ6D086X+Vg= Received: (qmail 4485 invoked by alias); 7 Mar 2014 22:06:06 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 4243 invoked by uid 89); 7 Mar 2014 22:06:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: nef2.ens.fr Received: from nef2.ens.fr (HELO nef2.ens.fr) (129.199.96.40) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 07 Mar 2014 22:06:00 +0000 Received: from mailhost.lps.ens.fr (tournesol.lps.ens.fr [129.199.120.1]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id s27M5v2L015758 ; Fri, 7 Mar 2014 23:05:57 +0100 (CET) X-Envelope-To: gcc-patches@gcc.gnu.org Received: from localhost (localhost [127.0.0.1]) by mailhost.lps.ens.fr (Postfix) with ESMTP id 58B0B104; Fri, 7 Mar 2014 23:05:57 +0100 (CET) Received: from mailhost.lps.ens.fr ([127.0.0.1]) by localhost (tournesol.lps.ens.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JRfrCZqPaLn6; Fri, 7 Mar 2014 23:05:57 +0100 (CET) Received: from [192.168.1.12] (log78-1-82-242-47-10.fbx.proxad.net [82.242.47.10]) by mailhost.lps.ens.fr (Postfix) with ESMTPSA id D6009F7; Fri, 7 Mar 2014 23:05:56 +0100 (CET) Mime-Version: 1.0 (Mac OS X Mail 7.2 \(1874\)) Subject: [patch, libgfortran] PR60128 Wrong ouput using en edit descriptor From: =?iso-8859-1?Q?Dominique_d=27Humi=E8res?= In-Reply-To: <201403072156.s27LudBg013793@nef2.ens.fr> Date: Fri, 7 Mar 2014 23:05:56 +0100 Cc: gcc-patches@gcc.gnu.org Message-Id: References: <201403072156.s27LudBg013793@nef2.ens.fr> To: fortran@gcc.gnu.org Hi all! Patch for pr60128. It is basically the patch posted at = http://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D60128#c7. I have made tmp = volatile (although I did not see any differences between -m32 and -m64) = and added a comment (please feel free to improve the wording). I have = also fixed a double space and removed the unused variable nzero_real. I have regtested 4.8.4 and trunk on x86_64-apple-darwin13 and trunk on = powerpc-apple-darwin9. Dominique 2014-03-08 Dominique d'Humieres PR libgfortran/60128 * io/write_float.def (output_float): Remove unused variable nzero_real. Replace a double space with a single one. (determine_en_precision): Fix wrong handling of the EN format. PR libfortran/60128 * gfortran.dg/fmt_en.f90: New test. ! { dg-do run } ! PR60128 Invalid outputs with EN descriptors ! Test case provided by Walt Brainerd. program pr60128 implicit none integer :: n_tst = 0, n_cnt = 0 ! Original test. call checkfmt("(en15.2)", -.44444, " -444.44E-03") ! Test for the bug in comment 6. call checkfmt("(en15.0)", 1.0, " 1.E+00") call checkfmt("(en15.0)", 1.00000012, " 1.E+00") call checkfmt("(en15.0)", 0.99999994, " 1.E+00") call checkfmt("(en15.0)", 10.0, " 10.E+00") call checkfmt("(en15.0)", 10.0000010, " 10.E+00") call checkfmt("(en15.0)", 9.99999905, " 10.E+00") call checkfmt("(en15.0)", 100.0, " 100.E+00") call checkfmt("(en15.0)", 100.000008, " 100.E+00") call checkfmt("(en15.0)", 99.9999924, " 100.E+00") call checkfmt("(en15.0)", 1000.0, " 1.E+03") call checkfmt("(en15.0)", 1000.00006, " 1.E+03") call checkfmt("(en15.0)", 999.999939, " 1.E+03") call checkfmt("(en15.0)", 9.5, " 10.E+00") call checkfmt("(en15.0)", 9.50000095, " 10.E+00") call checkfmt("(en15.0)", 9.49999905, " 9.E+00") call checkfmt("(en15.0)", 99.5, " 100.E+00") call checkfmt("(en15.0)", 99.5000076, " 100.E+00") call checkfmt("(en15.0)", 99.4999924, " 99.E+00") call checkfmt("(en15.0)", 999.5, " 1.E+03") call checkfmt("(en15.0)", 999.500061, " 1.E+03") call checkfmt("(en15.0)", 999.499939, " 999.E+00") call checkfmt("(en15.0)", 9500.0, " 10.E+03") call checkfmt("(en15.0)", 9500.00098, " 10.E+03") call checkfmt("(en15.0)", 9499.99902, " 9.E+03") call checkfmt("(en15.1)", 9950.0, " 10.0E+03") call checkfmt("(en15.2)", 9995.0, " 10.00E+03") call checkfmt("(en15.3)", 9999.5, " 10.000E+03") call checkfmt("(en15.1)", 9.5, " 9.5E+00") call checkfmt("(en15.1)", 9.50000095, " 9.5E+00") call checkfmt("(en15.1)", 9.49999905, " 9.5E+00") call checkfmt("(en15.1)", 0.099951, " 100.0E-03") call checkfmt("(en15.1)", 0.009951, " 10.0E-03") call checkfmt("(en15.1)", 0.000999951," 1.0E-03") call checkfmt("(en15.0)", -1.0, " -1.E+00") call checkfmt("(en15.0)", -1.00000012, " -1.E+00") call checkfmt("(en15.0)", -0.99999994, " -1.E+00") call checkfmt("(en15.0)", -10.0, " -10.E+00") call checkfmt("(en15.0)", -10.0000010, " -10.E+00") call checkfmt("(en15.0)", -9.99999905, " -10.E+00") call checkfmt("(en15.0)", -100.0, " -100.E+00") call checkfmt("(en15.0)", -100.000008, " -100.E+00") call checkfmt("(en15.0)", -99.9999924, " -100.E+00") call checkfmt("(en15.0)", -1000.0, " -1.E+03") call checkfmt("(en15.0)", -1000.00006, " -1.E+03") call checkfmt("(en15.0)", -999.999939, " -1.E+03") call checkfmt("(en15.0)", -9.5, " -10.E+00") call checkfmt("(en15.0)", -9.50000095, " -10.E+00") call checkfmt("(en15.0)", -9.49999905, " -9.E+00") call checkfmt("(en15.0)", -99.5, " -100.E+00") call checkfmt("(en15.0)", -99.5000076, " -100.E+00") call checkfmt("(en15.0)", -99.4999924, " -99.E+00") call checkfmt("(en15.0)", -999.5, " -1.E+03") call checkfmt("(en15.0)", -999.500061, " -1.E+03") call checkfmt("(en15.0)", -999.499939, " -999.E+00") call checkfmt("(en15.0)", -9500.0, " -10.E+03") call checkfmt("(en15.0)", -9500.00098, " -10.E+03") call checkfmt("(en15.0)", -9499.99902, " -9.E+03") call checkfmt("(en15.1)", -9950.0, " -10.0E+03") call checkfmt("(en15.2)", -9995.0, " -10.00E+03") call checkfmt("(en15.3)", -9999.5, " -10.000E+03") call checkfmt("(en15.1)", -9.5, " -9.5E+00") call checkfmt("(en15.1)", -9.50000095, " -9.5E+00") call checkfmt("(en15.1)", -9.49999905, " -9.5E+00") call checkfmt("(en15.1)", -0.099951, " -100.0E-03") call checkfmt("(en15.1)", -0.009951, " -10.0E-03") call checkfmt("(en15.1)", -0.000999951," -1.0E-03") call checkfmt("(en15.1)", 987350., " 987.4E+03") call checkfmt("(en15.2)", 98735., " 98.74E+03") call checkfmt("(en15.3)", 9873.5, " 9.874E+03") call checkfmt("(en15.1)", 987650., " 987.6E+03") call checkfmt("(en15.2)", 98765., " 98.76E+03") call checkfmt("(en15.3)", 9876.5, " 9.876E+03") call checkfmt("(en15.1)", 3.125E-02, " 31.2E-03") call checkfmt("(en15.1)", 9.375E-02, " 93.8E-03") call checkfmt("(en15.2)", 1.5625E-02, " 15.62E-03") call checkfmt("(en15.2)", 4.6875E-02, " 46.88E-03") call checkfmt("(en15.3)", 7.8125E-03, " 7.812E-03") call checkfmt("(en15.3)", 2.34375E-02, " 23.438E-03") call checkfmt("(en15.3)", 9.765625E-04," 976.562E-06") call checkfmt("(en15.6)", 2.9296875E-03," 2.929688E-03") call checkfmt("(en15.1)", -987350., " -987.4E+03") call checkfmt("(en15.2)", -98735., " -98.74E+03") call checkfmt("(en15.3)", -9873.5, " -9.874E+03") call checkfmt("(en15.1)", -987650., " -987.6E+03") call checkfmt("(en15.2)", -98765., " -98.76E+03") call checkfmt("(en15.3)", -9876.5, " -9.876E+03") call checkfmt("(en15.1)", -3.125E-02, " -31.2E-03") call checkfmt("(en15.1)", -9.375E-02, " -93.8E-03") call checkfmt("(en15.2)", -1.5625E-02, " -15.62E-03") call checkfmt("(en15.2)", -4.6875E-02, " -46.88E-03") call checkfmt("(en15.3)", -7.8125E-03, " -7.812E-03") call checkfmt("(en15.3)", -2.34375E-02, " -23.438E-03") call checkfmt("(en15.3)", -9.765625E-04," -976.562E-06") call checkfmt("(en15.6)", -2.9296875E-03," -2.929688E-03") print *, n_tst, n_cnt if (n_cnt /= 0) call abort contains subroutine checkfmt(fmt, x, cmp) use ISO_FORTRAN_ENV implicit none integer, parameter :: j(size(real_kinds)+4)=[REAL_KINDS, [4, 4, 4, 4]] integer :: i character(len=*), intent(in) :: fmt real, intent(in) :: x character(len=*), intent(in) :: cmp character(len=20) :: s do i=1,size(real_kinds) if (i == 1) then write(s, fmt) real(x,kind=j(1)) else if (i == 2) then write(s, fmt) real(x,kind=j(2)) else if (i == 3) then write(s, fmt) real(x,kind=j(3)) else if (i == 4) then write(s, fmt) real(x,kind=j(4)) end if n_tst = n_tst + 1 if (s /= cmp) then print "(a,1x,a,' expected: ',1x,a)", fmt, s, cmp n_cnt = n_cnt + 1 end if end do end subroutine end program --Apple-Mail=_1DBE7B5C-A706-447E-AF87-1D205882FD33 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=us-ascii --Apple-Mail=_1DBE7B5C-A706-447E-AF87-1D205882FD33-- --Apple-Mail=_A4A12A44-95D9-4ED2-935A-B85167B6BE6F-- --- ../_clean/libgfortran/io/write_float.def 2014-01-21 08:30:57.000000000 +0100 +++ libgfortran/io/write_float.def 2014-03-04 16:13:05.000000000 +0100 @@ -125,8 +125,6 @@ output_float (st_parameter_dt *dtp, cons int nzero; /* Number of digits after the decimal point. */ int nafter; - /* Number of zeros after the decimal point, whatever the precision. */ - int nzero_real; int leadzero; int nblanks; int ndigits, edigits; @@ -138,7 +136,6 @@ output_float (st_parameter_dt *dtp, cons p = dtp->u.p.scale_factor; rchar = '5'; - nzero_real = -1; /* We should always know the field width and precision. */ if (d < 0) @@ -191,7 +188,7 @@ output_float (st_parameter_dt *dtp, cons if (nafter < 0) nafter = 0; nafter = d; - nzero = nzero_real = 0; + nzero = 0; } else /* p < 0 */ { @@ -211,14 +208,13 @@ output_float (st_parameter_dt *dtp, cons nafter = d + nbefore; nbefore = 0; } - nzero_real = nzero; if (nzero > d) nzero = d; } } else { - nzero = nzero_real = 0; + nzero = 0; nafter = d; } @@ -373,7 +369,7 @@ output_float (st_parameter_dt *dtp, cons updown: rchar = '0'; - if (ft != FMT_F && w > 0 && d == 0 && p == 0) + if (ft != FMT_F && w > 0 && d == 0 && p == 0) nbefore = 1; /* Scan for trailing zeros to see if we really need to round it. */ for(i = nbefore + nafter; i < ndigits; i++) @@ -1153,17 +1149,39 @@ OUTPUT_FLOAT_FMT_G(16,L) /* EN format is tricky since the number of significant digits depends on the magnitude. Solve it by first printing a temporary value and figure out the number of significant digits from the printed - exponent. */ - -#define EN_PREC(x,y)\ -{\ - GFC_REAL_ ## x tmp; \ - tmp = * (GFC_REAL_ ## x *)source; \ - if (ISFINITE (y,tmp)) \ - nprinted = DTOA(y,0,tmp); \ - else\ - nprinted = -1;\ -}\ + exponent. Values y, 0.95*10.0**e <= y <10.0**e, are rounded to + 10.0**e even when the final result will not be rounded to 10.0**e. + For these values the exponent returned by atoi has to be decremented + by one. The values y in the ranges + (1000.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*(n+1)) + (100.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+2) + (10.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+1) + are correctly rounded respectively to 1.0...0*10.0*(3*(n+1)), + 100.0...0*10.0*(3*n), and 10.0...0*10.0*(3*n), where 0...0 + represents d zeroes, by the lines 279 to 297. */ + +#define EN_PREC(x,y) \ +{ \ + volatile GFC_REAL_ ## x tmp, one = 1.0; \ + tmp = * (GFC_REAL_ ## x *)source; \ + if (ISFINITE (y,tmp)) \ + { \ + nprinted = DTOA(y,0,tmp); \ + int e = atoi (&buffer[4]); \ + if (buffer[1] == '1') \ + { \ + tmp = (calculate_exp_ ## x (-e)) * tmp; \ + tmp = one - (tmp < 0 ? -tmp : tmp); \ + if (tmp > 0) \ + e = e - 1; \ + } \ + nbefore = e%3; \ + if (nbefore < 0) \ + nbefore = 3 + nbefore; \ + } \ + else \ + nprinted = -1; \ +} \ static int determine_en_precision (st_parameter_dt *dtp, const fnode *f, @@ -1172,6 +1190,7 @@ determine_en_precision (st_parameter_dt int nprinted; char buffer[10]; const size_t size = 10; + int nbefore; /* digits before decimal point - 1. */ switch (len) { @@ -1204,16 +1223,6 @@ determine_en_precision (st_parameter_dt if (nprinted == -1) return -1; - int e = atoi (&buffer[5]); - int nbefore; /* digits before decimal point - 1. */ - if (e >= 0) - nbefore = e % 3; - else - { - nbefore = (-e) % 3; - if (nbefore != 0) - nbefore = 3 - nbefore; - } int prec = f->u.real.d + nbefore; if (dtp->u.p.current_unit->round_status != ROUND_UNSPECIFIED && dtp->u.p.current_unit->round_status != ROUND_PROCDEFINED) --- ../4.8_clean/libgfortran/io/write_float.def 2014-02-15 16:51:18.000000000 +0100 +++ libgfortran/io/write_float.def 2014-03-04 17:27:50.000000000 +0100 @@ -125,8 +125,6 @@ output_float (st_parameter_dt *dtp, cons int nzero; /* Number of digits after the decimal point. */ int nafter; - /* Number of zeros after the decimal point, whatever the precision. */ - int nzero_real; int leadzero; int nblanks; int ndigits, edigits; @@ -138,7 +136,6 @@ output_float (st_parameter_dt *dtp, cons p = dtp->u.p.scale_factor; rchar = '5'; - nzero_real = -1; /* We should always know the field width and precision. */ if (d < 0) @@ -191,7 +188,7 @@ output_float (st_parameter_dt *dtp, cons if (nafter < 0) nafter = 0; nafter = d; - nzero = nzero_real = 0; + nzero = 0; } else /* p < 0 */ { @@ -211,14 +208,13 @@ output_float (st_parameter_dt *dtp, cons nafter = d + nbefore; nbefore = 0; } - nzero_real = nzero; if (nzero > d) nzero = d; } } else { - nzero = nzero_real = 0; + nzero = 0; nafter = d; } @@ -373,7 +369,7 @@ output_float (st_parameter_dt *dtp, cons updown: rchar = '0'; - if (ft != FMT_F && w > 0 && d == 0 && p == 0) + if (ft != FMT_F && w > 0 && d == 0 && p == 0) nbefore = 1; /* Scan for trailing zeros to see if we really need to round it. */ for(i = nbefore + nafter; i < ndigits; i++) @@ -1125,17 +1121,39 @@ OUTPUT_FLOAT_FMT_G(16,L) /* EN format is tricky since the number of significant digits depends on the magnitude. Solve it by first printing a temporary value and figure out the number of significant digits from the printed - exponent. */ - -#define EN_PREC(x,y)\ -{\ - GFC_REAL_ ## x tmp; \ - tmp = * (GFC_REAL_ ## x *)source; \ - if (isfinite (tmp)) \ - nprinted = DTOA(y,0,tmp); \ - else\ - nprinted = -1;\ -}\ + exponent. Values y, 0.95*10.0**e <= y <10.0**e, are rounded to + 10.0**e even when the final result will not be rounded t 10.0**e. + For these values the exponent returned by atoi has to be decremented + by one. The values y in the ranges + (1000.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*(n+1)) + (100.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+2) + (10.0-0.5*10.0**(-d))*10.0**(3*n) <= y < 10.0*(3*n+1) + are correctly rounded respectively to 1.0...0*10.0*(3*(n+1)), + 100.0...0*10.0*(3*n), and 10.0...0*10.0*(3*n), where 0...0 + represents d zeroes, by the lines 279 to 297. */ + +#define EN_PREC(x,y) \ +{ \ + volatile GFC_REAL_ ## x tmp, one = 1.0; \ + tmp = * (GFC_REAL_ ## x *)source; \ + if (isfinite (tmp)) \ + { \ + nprinted = DTOA(y,0,tmp); \ + int e = atoi (&buffer[4]); \ + if (buffer[1] == '1') \ + { \ + tmp = (calculate_exp_ ## x (-e)) * tmp; \ + tmp = one - (tmp < 0 ? -tmp : tmp); \ + if (tmp > 0) \ + e = e - 1; \ + } \ + nbefore = e%3; \ + if (nbefore < 0) \ + nbefore = 3 + nbefore; \ + } \ + else \ + nprinted = -1; \ +} \ static int determine_en_precision (st_parameter_dt *dtp, const fnode *f, @@ -1144,6 +1162,7 @@ determine_en_precision (st_parameter_dt int nprinted; char buffer[10]; const size_t size = 10; + int nbefore; /* digits before decimal point - 1. */ switch (len) { @@ -1176,16 +1195,6 @@ determine_en_precision (st_parameter_dt if (nprinted == -1) return -1; - int e = atoi (&buffer[5]); - int nbefore; /* digits before decimal point - 1. */ - if (e >= 0) - nbefore = e % 3; - else - { - nbefore = (-e) % 3; - if (nbefore != 0) - nbefore = 3 - nbefore; - } int prec = f->u.real.d + nbefore; if (dtp->u.p.current_unit->round_status != ROUND_UNSPECIFIED && dtp->u.p.current_unit->round_status != ROUND_PROCDEFINED)