This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc 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]

[PATCH] PowerPC - Optimization for str[n]casecmp functions


This patch provides throughput boost for the strcasecmp/strncasecmp
functions for POWER7 for both ppc32 (25%) and ppc64 (40%), POWER6
performance does not change and POWER5 shows an increase for ppc64 (15%).
The optimization is done by manually unrolling the test loop to
avoid Load-Hit-Store stalls caused by a test followed by a load.

---

2011-08-15  Adhemerval Zanella  <azanella@linux.vnet.ibm.com>

        * sysdeps/powerpc/strcasecmp: Optimization for POWER7.
        * sysdeps/powerpc/strcasecmp: Likewise.


diff --git a/sysdeps/powerpc/strcasecmp.c b/sysdeps/powerpc/strcasecmp.c
new file mode 100644
index 0000000..4e55c79
--- /dev/null
+++ b/sysdeps/powerpc/strcasecmp.c
@@ -0,0 +1,85 @@
+/* Copyright (C) 2011 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Adhemerval Zanella <azanella@br.ibm.com>, 2011
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <string.h>
+
+#ifndef _LIBC
+# define __strcasecmp strcasecmp
+# define TOLOWER(Ch) tolower (Ch)
+#else
+# include <locale/localeinfo.h>
+# ifdef USE_IN_EXTENDED_LOCALE_MODEL
+#  define __strcasecmp __strcasecmp_l
+# endif
+# define TOLOWER(Ch) __tolower_l ((Ch), loc)
+#endif
+
+#ifdef USE_IN_EXTENDED_LOCALE_MODEL
+# define LOCALE_PARAM , loc
+# define LOCALE_PARAM_DECL , __locale_t loc;
+#else
+# define LOCALE_PARAM
+# define LOCALE_PARAM_DECL
+#endif
+
+/* Compare S1 and S2, ignoring case, returning less than, equal to or
+   greater than zero if S1 is lexicographically less than,
+   equal to or greater than S2.  */
+int
+__strcasecmp (const char *s1, const char *s2 LOCALE_PARAM_DECL LOCALE_PARAM)
+{
+#if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
+  __locale_t loc = _NL_CURRENT_LOCALE;
+#endif
+  const unsigned char *p1 = (const unsigned char *) s1;
+  const unsigned char *p2 = (const unsigned char *) s2;
+  int result;
+
+  if (p1 == p2)
+    return 0;
+
+  /* Byte-by-byte comparison: the loop is unrolled to
+     reduce LHS due a conditional followed by a load. */
+  while (1)
+    {
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0'))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0'))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0'))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0'))
+	break;
+    }
+
+  return result;
+}
+#ifndef __strcasecmp
+libc_hidden_def (__strcasecmp)
+weak_alias (__strcasecmp, strcasecmp)
+#endif
diff --git a/sysdeps/powerpc/strncase.c b/sysdeps/powerpc/strncase.c
new file mode 100644
index 0000000..3e039a3
--- /dev/null
+++ b/sysdeps/powerpc/strncase.c
@@ -0,0 +1,87 @@
+/* Compare at most N characters of two strings ignoring the case.
+   Copyright (C) 2011 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Adhemerval Zanella <azanella@br.ibm.com>, 2011
+
+   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, write to the Free
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <string.h>
+
+#ifndef _LIBC
+# define __strncasecmp strncasecmp
+# define TOLOWER(Ch) tolower (Ch)
+#else
+# include <locale/localeinfo.h>
+# ifdef USE_IN_EXTENDED_LOCALE_MODEL
+#  define __strncasecmp __strncasecmp_l
+# endif
+# define TOLOWER(Ch) __tolower_l ((Ch), loc)
+#endif
+
+#ifdef USE_IN_EXTENDED_LOCALE_MODEL
+# define LOCALE_PARAM , loc
+# define LOCALE_PARAM_DECL , __locale_t loc;
+#else
+# define LOCALE_PARAM
+# define LOCALE_PARAM_DECL
+#endif
+
+/* Compare no more than N characters of S1 and S2,
+   ignoring case, returning less than, equal to or
+   greater than zero if S1 is lexicographically less
+   than, equal to or greater than S2.  */
+int
+__strncasecmp (const char *s1, const char *s2, size_t n
+  LOCALE_PARAM_DECL)
+{
+#if defined _LIBC && !defined USE_IN_EXTENDED_LOCALE_MODEL
+  __locale_t loc = _NL_CURRENT_LOCALE;
+#endif
+  const unsigned char *p1 = (const unsigned char *) s1;
+  const unsigned char *p2 = (const unsigned char *) s2;
+  int result;
+
+  if (p1 == p2 || n == 0)
+    return 0;
+
+  /* Byte-by-byte comparison: the loop is unrolled to
+     reduce LHS due a conditional followed by a load. */
+  while (1)
+    {
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0') || (--n == 0))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0') || (--n == 0))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0') || (--n == 0))
+	break;
+      result = TOLOWER (*p1) - TOLOWER (*p2++);
+      if ((result != 0) || (*p1++ == '\0') || (--n == 0))
+	break;
+    }
+
+  return result;
+}
+#ifndef __strncasecmp
+weak_alias (__strncasecmp, strncasecmp)
+#endif
-- 
1.7.1



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