This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] Fix up long double fphex
- From: Marek Polacek <polacek at redhat dot com>
- To: libc-alpha at sourceware dot org
- Date: Sun, 4 Mar 2012 21:42:57 +0100
- Subject: [PATCH] Fix up long double fphex
The __printf_fphex gave wrong results for long double values whose
decimal fraction is != 0. The problem is in PRINT_FPHEX_LONG_DOUBLE
macro. Here numstr and wnumstr should just be strings representating
.ieee.mantissa[01] values. But when we are saving the leading digit, we
adjust the position of numstr, but not of wnumstr. This way we output
the leading digit in the wide case twice when we do PRINT () in case
that precision > 0 later on. This patch fixes that and adds a
testcase as well.
Regtested on x86_64-linux, ok for trunk?
BTW, I think it'll be worthwhile to adjust stdio-common/tst-fphex.c
to work also with widechars (and maybe also with %A format specifier).
Should I create a bug for this addition and the hack it up?
2012-03-04 Marek Polacek <polacek@redhat.com>
[BZ #13726]
* sysdeps/ieee754/ldbl-96/printf_fphex.c: Adjust position of wnumstr.
* sysdeps/x86_64/fpu/printf_fphex.c: Likewise.
* stdio-common/Makefile (tests): Add tst-long-dbl-fphex.
* stdio-common/tst-long-dbl-fphex.c: New file.
--- libc/stdio-common/Makefile.mp 2012-03-04 18:32:54.057155127 +0100
+++ libc/stdio-common/Makefile 2012-03-04 18:39:15.160963544 +0100
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-2009, 2011 Free Software Foundation, Inc.
+# Copyright (C) 1991-2012 Free Software Foundation, Inc.
# This file is part of the GNU C Library.
# The GNU C Library is free software; you can redistribute it and/or
@@ -59,7 +59,8 @@ tests := tstscanf test_rdwr test-popen t
tst-popen tst-unlockedio tst-fmemopen2 tst-put-error tst-fgets \
tst-fwrite bug16 bug17 tst-swscanf tst-sprintf2 bug18 bug18a \
bug19 bug19a tst-popen2 scanf13 scanf14 scanf15 bug20 bug21 bug22 \
- scanf16 scanf17 tst-setvbuf1 tst-grouping bug23 bug24
+ scanf16 scanf17 tst-setvbuf1 tst-grouping bug23 bug24 \
+ tst-long-dbl-fphex
test-srcs = tst-unbputc tst-printf
--- libc/stdio-common/tst-long-dbl-fphex.c.mp 2012-03-04 19:09:18.595925212 +0100
+++ libc/stdio-common/tst-long-dbl-fphex.c 2012-03-04 19:08:07.479774042 +0100
@@ -0,0 +1,38 @@
+/* This file is part of the GNU C Library.
+ Copyright (C) 2012 Free Software Foundation, Inc.
+ Contributed by Marek Polacek <polacek@redhat.com>, 2012.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <wchar.h>
+
+/* Prototype for our test function. */
+static int do_test (void);
+
+static int
+do_test (void)
+{
+ const long double x = 24.5;
+ wchar_t a[16 * sizeof (wchar_t)];
+ swprintf (a, 16 * sizeof (wchar_t), L"%La\n", x);
+ wchar_t A[16 * sizeof (wchar_t)];
+ swprintf (A, 16 * sizeof (wchar_t), L"%LA\n", x);
+
+ return (wmemcmp (a, L"0xc.4p+1", 8) != 0
+ || wmemcmp (A, L"0XC.4P+1", 8) != 0);
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
--- libc/sysdeps/x86_64/fpu/printf_fphex.c.mp 2012-03-04 11:44:09.514108919 +0100
+++ libc/sysdeps/x86_64/fpu/printf_fphex.c 2012-03-04 19:35:42.342141139 +0100
@@ -1,5 +1,5 @@
/* Print floating point number in hexadecimal notation according to ISO C99.
- Copyright (C) 1997,1998,1999,2000,2001,2005 Free Software Foundation, Inc.
+ Copyright (C) 1997-2012 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -57,6 +57,7 @@ do { \
\
/* We use a full nibble for the leading digit. */ \
leading = *numstr++; \
+ wnumstr++; \
\
/* We have 3 bits from the mantissa in the leading nibble. \
Therefore we are here using `IEEE854_LONG_DOUBLE_BIAS + 3'. */ \
--- libc/sysdeps/ieee754/ldbl-96/printf_fphex.c.mp 2012-03-04 18:27:51.630501310 +0100
+++ libc/sysdeps/ieee754/ldbl-96/printf_fphex.c 2012-03-04 19:36:12.746205566 +0100
@@ -1,5 +1,5 @@
/* Print floating point number in hexadecimal notation according to ISO C99.
- Copyright (C) 1997, 1998, 1999, 2000, 2005 Free Software Foundation, Inc.
+ Copyright (C) 1997-2012 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -58,6 +58,7 @@ do { \
\
/* We use a full nibble for the leading digit. */ \
leading = *numstr++; \
+ wnumstr++; \
\
/* We have 3 bits from the mantissa in the leading nibble. \
Therefore we are here using `IEEE854_LONG_DOUBLE_BIAS + 3'. */ \
Marek