This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[v3] Fix some issues related to DR117


Hi,

the below, tested x86-linux, fixes some long standing issues related to
DR117. In particular:

    1- We had a wrong logic to check for oct and hex formatting, to wit
(see also the res. of 117):

          (__fmt & ios_base::oct || __fmt & ios_base::hex)

          instead of:

          (__fmt == ios_base::oct || __fmt == ios_base::hex)

    2- We took care of long and long long inside the inserters instead
of inside the facets (22.2.2.2.2/p5-7)

See also the corresponding testcases.

In fixing those problems I made sure not to introduce performance
regressions: as a matter of fact, I'm seeing measurable improvements
(order 7-8%) on x86, for testcases like ofstream_insert_int.cc in our
performance testsuite.

Committed to mainline.

Paolo.

///////////////
2005-07-11  Paolo Carlini  <pcarlini@suse.de>

	* include/bits/ostream.tcc (basic_ostream<>::operator<<(long),
	basic_ostream<>::operator<<(long long)): Don't deal with oct
	and hex and casts to unsigned here...
	* include/bits/locale_facets.tcc (__int_to_char(_CharT*, long,
	const _CharT*, ios_base::fmtflags), __int_to_char(_CharT*, long
	long, const _CharT*, ios_base::fmtflags)): ... do that here,
	instead, as per Table 57.
	(num_put<>::_M_insert_int): Tidy treatment of numeric base and
	sign.
	* include/std/std_ostream.h (operator<<(short), operator<<(int)):
	Adjust logic, as per the letter of the resolution of DR117 [WP].
	* testsuite/22_locale/num_put/put/char/10.cc: New.
	* testsuite/22_locale/num_put/put/wchar_t/10.cc: Likewise.
	* testsuite/27_io/basic_ostream/inserters_arithmetic/char/7.cc:
	Likewise.
	* testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/7.cc:
	Likewise.
diff -urN libstdc++-v3-orig/include/bits/locale_facets.tcc libstdc++-v3/include/bits/locale_facets.tcc
--- libstdc++-v3-orig/include/bits/locale_facets.tcc	2005-06-29 13:48:50.000000000 +0200
+++ libstdc++-v3/include/bits/locale_facets.tcc	2005-07-11 11:34:46.000000000 +0200
@@ -820,15 +820,19 @@
       __len = static_cast<int>(__w);
     }
 
-  // Forwarding functions to peel signed from unsigned integer types.
+  // Forwarding functions to peel signed from unsigned integer types and
+  // either cast or compute the absolute value for the former, depending
+  // on __basefield.
   template<typename _CharT>
     inline int
     __int_to_char(_CharT* __bufend, long __v, const _CharT* __lit,
 		  ios_base::fmtflags __flags)
     {
-      unsigned long __ul = static_cast<unsigned long>(__v);
-      if (__v < 0)
-	__ul = -__ul;
+      unsigned long __ul = __v;
+      const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
+      if (__builtin_expect(__basefield != ios_base::oct
+			   && __basefield != ios_base::hex, true))
+	__ul = __v < 0 ? -__v : __ul;
       return __int_to_char(__bufend, __ul, __lit, __flags, false);
     }
 
@@ -844,9 +848,11 @@
     __int_to_char(_CharT* __bufend, long long __v, const _CharT* __lit,
 		  ios_base::fmtflags __flags)
     {
-      unsigned long long __ull = static_cast<unsigned long long>(__v);
-      if (__v < 0)
-	__ull = -__ull;
+      unsigned long long __ull = __v;
+      const ios_base::fmtflags __basefield = __flags & ios_base::basefield;
+      if (__builtin_expect(__basefield != ios_base::oct
+			   && __basefield != ios_base::hex, true))
+	__ull = __v < 0 ? -__v : __ull;
       return __int_to_char(__bufend, __ull, __lit, __flags, false);
     }
 
@@ -966,16 +972,11 @@
 	    else if (__v)
 	      *--__cs = __lit[__num_base::_S_ominus], ++__len;
 	  }
-	else if (__basefield == ios_base::oct)
+	else if (__flags & ios_base::showbase && __v)
 	  {
-	    // Octal.
-	    if (__flags & ios_base::showbase && __v)
+	    if (__basefield == ios_base::oct)
 	      *--__cs = __lit[__num_base::_S_odigits], ++__len;
-	  }
-	else
-	  {
-	    // Hex.
-	    if (__flags & ios_base::showbase && __v)
+	    else
 	      {
 		// 'x' or 'X'
 		const bool __uppercase = __flags & ios_base::uppercase;
@@ -1213,8 +1214,8 @@
   template<typename _CharT, typename _OutIter>
     _OutIter
     num_put<_CharT, _OutIter>::
-    do_put(iter_type __s, ios_base& __b, char_type __fill, long long __v) const
-    { return _M_insert_int(__s, __b, __fill, __v); }
+    do_put(iter_type __s, ios_base& __io, char_type __fill, long long __v) const
+    { return _M_insert_int(__s, __io, __fill, __v); }
 
   template<typename _CharT, typename _OutIter>
     _OutIter
diff -urN libstdc++-v3-orig/include/bits/ostream.tcc libstdc++-v3/include/bits/ostream.tcc
--- libstdc++-v3-orig/include/bits/ostream.tcc	2004-11-24 05:11:10.000000000 +0100
+++ libstdc++-v3/include/bits/ostream.tcc	2005-07-11 10:06:14.000000000 +0200
@@ -1,6 +1,6 @@
 // ostream classes -*- C++ -*-
 
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
+// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
 // Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
@@ -130,19 +130,8 @@
 	  ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
 	  try
 	    {
-	      bool __b = false;
-	      const char_type __c = this->fill();
-	      const ios_base::fmtflags __fmt = (this->flags()
-						& ios_base::basefield);
 	      const __num_put_type& __np = __check_facet(this->_M_num_put);
-	      if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
-		{
-		  const unsigned long __l = static_cast<unsigned long>(__n);
-		  __b = __np.put(*this, *this, __c, __l).failed();
-		}
-	      else
-		__b = __np.put(*this, *this, __c, __n).failed();
-	      if (__b)
+	      if (__np.put(*this, *this, this->fill(), __n).failed())
 		__err |= ios_base::badbit;
 	    }
 	  catch(...)
@@ -188,20 +177,8 @@
 	  ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
 	  try
 	    {
-	      bool __b = false;
-	      const char_type __c = this->fill();
-	      const ios_base::fmtflags __fmt = (this->flags()
-						& ios_base::basefield);
 	      const __num_put_type& __np = __check_facet(this->_M_num_put);
-	      if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
-		{
-		  const unsigned long long __l = (static_cast<
-						  unsigned long long>(__n));
-		  __b = __np.put(*this, *this, __c, __l).failed();
-		}
-	      else
-		__b = __np.put(*this, *this, __c, __n).failed();
-	      if (__b)
+	      if (__np.put(*this, *this, this->fill(), __n).failed())
 		__err |= ios_base::badbit;
 	    }
 	  catch(...)
diff -urN libstdc++-v3-orig/include/std/std_ostream.h libstdc++-v3/include/std/std_ostream.h
--- libstdc++-v3-orig/include/std/std_ostream.h	2004-11-24 05:11:21.000000000 +0100
+++ libstdc++-v3/include/std/std_ostream.h	2005-07-11 10:06:14.000000000 +0200
@@ -175,9 +175,9 @@
       __ostream_type& 
       operator<<(short __n)
       { 
-	ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
-	if (__fmt & ios_base::oct || __fmt & ios_base::hex)
-	  return this->operator<<(static_cast<unsigned long>
+	const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
+	if (__fmt == ios_base::oct || __fmt == ios_base::hex)
+	  return this->operator<<(static_cast<long>
 				  (static_cast<unsigned short>(__n)));
 	else
 	  return this->operator<<(static_cast<long>(__n));
@@ -190,9 +190,9 @@
       __ostream_type& 
       operator<<(int __n)
       { 
-	ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
-	if (__fmt & ios_base::oct || __fmt & ios_base::hex)
-	  return this->operator<<(static_cast<unsigned long>
+	const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
+	if (__fmt == ios_base::oct || __fmt == ios_base::hex)
+	  return this->operator<<(static_cast<long>
 				  (static_cast<unsigned int>(__n)));
 	else
 	  return this->operator<<(static_cast<long>(__n));
diff -urN libstdc++-v3-orig/testsuite/22_locale/num_put/put/char/10.cc libstdc++-v3/testsuite/22_locale/num_put/put/char/10.cc
--- libstdc++-v3-orig/testsuite/22_locale/num_put/put/char/10.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_put/put/char/10.cc	2005-07-11 10:06:14.000000000 +0200
@@ -0,0 +1,68 @@
+// 2005-07-11  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2005 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This 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 General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.2.2.1  num_put members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  locale loc_c = locale::classic();
+
+  const string empty;
+
+  stringstream ss;
+  ss.imbue(loc_c);
+  const num_put<char>& np = use_facet<num_put<char> >(ss.getloc()); 
+
+  long l = -1;
+  unsigned long ul = 0;
+
+  ss.setf(ios::hex, ios::basefield);
+  np.put(ss.rdbuf(), ss, '+', l);
+  VERIFY( ss.str() != "1" );
+  ss >> ul;
+  VERIFY( ul == static_cast<unsigned long>(l) );
+
+#ifdef _GLIBCXX_USE_LONG_LONG  
+  long long ll = -1LL;
+  unsigned long long ull = 0ULL;
+
+  ss.str(empty);
+  ss.clear();
+  np.put(ss.rdbuf(), ss, '+', ll);
+  VERIFY( ss.str() != "1" );
+  ss >> ull;
+  VERIFY( ull == static_cast<unsigned long long>(ll) );
+#endif
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
+
+
diff -urN libstdc++-v3-orig/testsuite/22_locale/num_put/put/wchar_t/10.cc libstdc++-v3/testsuite/22_locale/num_put/put/wchar_t/10.cc
--- libstdc++-v3-orig/testsuite/22_locale/num_put/put/wchar_t/10.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/22_locale/num_put/put/wchar_t/10.cc	2005-07-11 10:06:14.000000000 +0200
@@ -0,0 +1,68 @@
+// 2005-07-11  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2005 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This 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 General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 22.2.2.2.1  num_put members
+
+#include <locale>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  locale loc_c = locale::classic();
+
+  const wstring empty;
+
+  wstringstream ss;
+  ss.imbue(loc_c);
+  const num_put<wchar_t>& np = use_facet<num_put<wchar_t> >(ss.getloc()); 
+
+  long l = -1;
+  unsigned long ul = 0;
+
+  ss.setf(ios::hex, ios::basefield);
+  np.put(ss.rdbuf(), ss, L'+', l);
+  VERIFY( ss.str() != L"1" );
+  ss >> ul;
+  VERIFY( ul == static_cast<unsigned long>(l) );
+
+#ifdef _GLIBCXX_USE_LONG_LONG  
+  long long ll = -1LL;
+  unsigned long long ull = 0ULL;
+
+  ss.str(empty);
+  ss.clear();
+  np.put(ss.rdbuf(), ss, L'+', ll);
+  VERIFY( ss.str() != L"1" );
+  ss >> ull;
+  VERIFY( ull == static_cast<unsigned long long>(ll) );
+#endif
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
+
+
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_ostream/inserters_arithmetic/char/7.cc libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/7.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_ostream/inserters_arithmetic/char/7.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/7.cc	2005-07-11 10:06:14.000000000 +0200
@@ -0,0 +1,68 @@
+// 2005-07-11  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2005 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This 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 General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.6.2.5.2  Arithmetic inserters
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  stringstream ostr1, ostr2, ostr3, ostr4;
+
+  ostr1.setf(ios_base::oct);
+  ostr1.setf(ios_base::hex);
+
+  short s = -1;
+  ostr1 << s;
+  VERIFY( ostr1.str() == "-1" );
+
+  ostr2.setf(ios_base::oct);
+  ostr2.setf(ios_base::hex);
+
+  int i = -1;
+  ostr2 << i;
+  VERIFY( ostr2.str() == "-1" );
+
+  ostr3.setf(ios_base::oct);
+  ostr3.setf(ios_base::hex);
+
+  long l = -1;
+  ostr3 << l;
+  VERIFY( ostr3.str() == "-1" );
+
+#ifdef _GLIBCXX_USE_LONG_LONG
+  ostr4.setf(ios_base::oct);
+  ostr4.setf(ios_base::hex);
+
+  long long ll = -1LL;
+  ostr4 << ll;
+  VERIFY( ostr4.str() == "-1" );
+#endif
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
diff -urN libstdc++-v3-orig/testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/7.cc libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/7.cc
--- libstdc++-v3-orig/testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/7.cc	1970-01-01 01:00:00.000000000 +0100
+++ libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/wchar_t/7.cc	2005-07-11 10:06:14.000000000 +0200
@@ -0,0 +1,68 @@
+// 2005-07-11  Paolo Carlini  <pcarlini@suse.de>
+
+// Copyright (C) 2005 Free Software Foundation
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// This 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 General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING.  If not, write to the Free
+// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// USA.
+
+// 27.6.2.5.2  Arithmetic inserters
+
+#include <sstream>
+#include <testsuite_hooks.h>
+
+void test01()
+{
+  using namespace std;
+  bool test __attribute__((unused)) = true;
+
+  wstringstream ostr1, ostr2, ostr3, ostr4;
+
+  ostr1.setf(ios_base::oct);
+  ostr1.setf(ios_base::hex);
+
+  short s = -1;
+  ostr1 << s;
+  VERIFY( ostr1.str() == L"-1" );
+
+  ostr2.setf(ios_base::oct);
+  ostr2.setf(ios_base::hex);
+
+  int i = -1;
+  ostr2 << i;
+  VERIFY( ostr2.str() == L"-1" );
+
+  ostr3.setf(ios_base::oct);
+  ostr3.setf(ios_base::hex);
+
+  long l = -1;
+  ostr3 << l;
+  VERIFY( ostr3.str() == L"-1" );
+
+#ifdef _GLIBCXX_USE_LONG_LONG
+  ostr4.setf(ios_base::oct);
+  ostr4.setf(ios_base::hex);
+
+  long long ll = -1LL;
+  ostr4 << ll;
+  VERIFY( ostr4.str() == L"-1" );
+#endif
+}
+
+int main()
+{
+  test01();
+  return 0;
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]