Remove strcmp inlines

Message ID AM5PR0802MB2610F842F677223D09E8E43783580@AM5PR0802MB2610.eurprd08.prod.outlook.com
State Committed
Headers

Commit Message

Wilco Dijkstra Feb. 14, 2017, 12:51 p.m. UTC
  ping

    
Remove the str(n)cmp inlines from string/bits/string2.h.  The strncmp
optimization seems unlikely to ever be useful, but if it occurs in
real code it should be added to GCC.  Expanding strcmp of small strings
does appear useful (benchmarking shows it is 2-3x faster), so this would
be useful to implement in GCC (PR 78809).

ChangeLog:
2015-12-12  Wilco Dijkstra  <wdijkstr@arm.com>

        * string/bits/string2.h (strcmp): Remove define.
        (__strcmp_cg): Likewise.
        (strncmp): Likewise.
--
  

Comments

Adhemerval Zanella Feb. 14, 2017, 5:23 p.m. UTC | #1
LGTM.

On 14/02/2017 10:51, Wilco Dijkstra wrote:
> ping
> 
>     
> Remove the str(n)cmp inlines from string/bits/string2.h.  The strncmp
> optimization seems unlikely to ever be useful, but if it occurs in
> real code it should be added to GCC.  Expanding strcmp of small strings
> does appear useful (benchmarking shows it is 2-3x faster), so this would
> be useful to implement in GCC (PR 78809).
> 
> ChangeLog:
> 2015-12-12  Wilco Dijkstra  <wdijkstr@arm.com>
> 
>         * string/bits/string2.h (strcmp): Remove define.
>         (__strcmp_cg): Likewise.
>         (strncmp): Likewise.
> --
> 
> diff --git a/string/bits/string2.h b/string/bits/string2.h
> index b0be5f6a49024a0bedfc37e9ec2c0356e0e4fa09..5e0339223697256fff7eba4cc32d2eb618f07713 100644
> --- a/string/bits/string2.h
> +++ b/string/bits/string2.h
> @@ -60,64 +60,6 @@
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
>  #endif
>  
> -/* Compare characters of S1 and S2.  */
> -#ifndef strcmp
> -# define strcmp(s1, s2) \
> -  __extension__                                                                      \
> -  ({ size_t __s1_len, __s2_len;                                                      \
> -     (__builtin_constant_p (s1) && __builtin_constant_p (s2)                 \
> -      && (__s1_len = strlen (s1), __s2_len = strlen (s2),                    \
> -         (!__string2_1bptr_p (s1) || __s1_len >= 4)                           \
> -         && (!__string2_1bptr_p (s2) || __s2_len >= 4))               \
> -      ? __builtin_strcmp (s1, s2)                                            \
> -      : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)                 \
> -        && (__s1_len = strlen (s1), __s1_len < 4)                             \
> -        ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
> -           ? __builtin_strcmp (s1, s2)                                        \
> -           : __strcmp_cg (s1, s2, __s1_len))                                  \
> -        : (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
> -           && (__s2_len = strlen (s2), __s2_len < 4)                          \
> -           ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1)             \
> -              ? __builtin_strcmp (s1, s2)                                     \
> -              : -__strcmp_cg (s2, s1, __s2_len))                              \
> -            : __builtin_strcmp (s1, s2)))); })
> -
> -# define __strcmp_cg(s1, s2, l1) \
> -  (__extension__ ({ const unsigned char *__s2 =                                      \
> -                     (const unsigned char *) (const char *) (s2);             \
> -                   int __result =                                             \
> -                     (((const unsigned char *) (const char *) (s1))[0]        \
> -                      - __s2[0]);                                             \
> -                   if (l1 > 0 && __result == 0)                       \
> -                     {                                                        \
> -                       __result = (((const unsigned char *)                  \
> -                                    (const char *) (s1))[1] - __s2[1]);      \
> -                       if (l1 > 1 && __result == 0)                          \
> -                         {                                                    \
> -                           __result = (((const unsigned char *)       \
> -                                        (const char *) (s1))[2] - __s2[2]);  \
> -                           if (l1 > 2 && __result == 0)               \
> -                             __result = (((const unsigned char *)             \
> -                                         (const char *)  (s1))[3]             \
> -                                         - __s2[3]);                          \
> -                         }                                                    \
> -                     }                                                        \
> -                   __result; }))
> -#endif
> -
> -
> -/* Compare N characters of S1 and S2.  */
> -#ifndef strncmp
> -# define strncmp(s1, s2, n)                                                  \
> -  (__extension__ (__builtin_constant_p (n)                                   \
> -                 && ((__builtin_constant_p (s1)                       \
> -                      && strlen (s1) < ((size_t) (n)))                        \
> -                     || (__builtin_constant_p (s2)                            \
> -                         && strlen (s2) < ((size_t) (n))))                    \
> -                 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
> -#endif
> -
> -
>  #ifndef _FORCE_INLINES
>  # undef __STRING_INLINE
>  #endif
> 
> 
> 
> 
>     
>
  
Wainer dos Santos Moschetta Feb. 20, 2017, 7:21 p.m. UTC | #2
On PowerPC front there are some progress with builtin expansion for strncmp/strcmp in GCC. Following patches were merged recently:

https://gcc.gnu.org/ml/gcc-patches/2016-12/msg01399.html

https://gcc.gnu.org/ml/gcc-patches/2017-01/msg00744.html

Also it has already been suggested not define these macros for powerpc:

https://sourceware.org/ml/libc-alpha/2016-10/msg00465.html

So if this patch isn't accepted then we could disable the macros for powerpc at least.


On 02/14/2017 10:51 AM, Wilco Dijkstra wrote:
> ping
>
>     
> Remove the str(n)cmp inlines from string/bits/string2.h.  The strncmp
> optimization seems unlikely to ever be useful, but if it occurs in
> real code it should be added to GCC.  Expanding strcmp of small strings
> does appear useful (benchmarking shows it is 2-3x faster), so this would
> be useful to implement in GCC (PR 78809).
>
> ChangeLog:
> 2015-12-12  Wilco Dijkstra  <wdijkstr@arm.com>
>
>         * string/bits/string2.h (strcmp): Remove define.
>         (__strcmp_cg): Likewise.
>         (strncmp): Likewise.
> --
>
> diff --git a/string/bits/string2.h b/string/bits/string2.h
> index b0be5f6a49024a0bedfc37e9ec2c0356e0e4fa09..5e0339223697256fff7eba4cc32d2eb618f07713 100644
> --- a/string/bits/string2.h
> +++ b/string/bits/string2.h
> @@ -60,64 +60,6 @@
>  # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
>  #endif
>  
> -/* Compare characters of S1 and S2.  */
> -#ifndef strcmp
> -# define strcmp(s1, s2) \
> -  __extension__                                                                      \
> -  ({ size_t __s1_len, __s2_len;                                                      \
> -     (__builtin_constant_p (s1) && __builtin_constant_p (s2)                 \
> -      && (__s1_len = strlen (s1), __s2_len = strlen (s2),                    \
> -         (!__string2_1bptr_p (s1) || __s1_len >= 4)                           \
> -         && (!__string2_1bptr_p (s2) || __s2_len >= 4))               \
> -      ? __builtin_strcmp (s1, s2)                                            \
> -      : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)                 \
> -        && (__s1_len = strlen (s1), __s1_len < 4)                             \
> -        ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
> -           ? __builtin_strcmp (s1, s2)                                        \
> -           : __strcmp_cg (s1, s2, __s1_len))                                  \
> -        : (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
> -           && (__s2_len = strlen (s2), __s2_len < 4)                          \
> -           ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1)             \
> -              ? __builtin_strcmp (s1, s2)                                     \
> -              : -__strcmp_cg (s2, s1, __s2_len))                              \
> -            : __builtin_strcmp (s1, s2)))); })
> -
> -# define __strcmp_cg(s1, s2, l1) \
> -  (__extension__ ({ const unsigned char *__s2 =                                      \
> -                     (const unsigned char *) (const char *) (s2);             \
> -                   int __result =                                             \
> -                     (((const unsigned char *) (const char *) (s1))[0]        \
> -                      - __s2[0]);                                             \
> -                   if (l1 > 0 && __result == 0)                       \
> -                     {                                                        \
> -                       __result = (((const unsigned char *)                  \
> -                                    (const char *) (s1))[1] - __s2[1]);      \
> -                       if (l1 > 1 && __result == 0)                          \
> -                         {                                                    \
> -                           __result = (((const unsigned char *)       \
> -                                        (const char *) (s1))[2] - __s2[2]);  \
> -                           if (l1 > 2 && __result == 0)               \
> -                             __result = (((const unsigned char *)             \
> -                                         (const char *)  (s1))[3]             \
> -                                         - __s2[3]);                          \
> -                         }                                                    \
> -                     }                                                        \
> -                   __result; }))
> -#endif
> -
> -
> -/* Compare N characters of S1 and S2.  */
> -#ifndef strncmp
> -# define strncmp(s1, s2, n)                                                  \
> -  (__extension__ (__builtin_constant_p (n)                                   \
> -                 && ((__builtin_constant_p (s1)                       \
> -                      && strlen (s1) < ((size_t) (n)))                        \
> -                     || (__builtin_constant_p (s2)                            \
> -                         && strlen (s2) < ((size_t) (n))))                    \
> -                 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
> -#endif
> -
> -
>  #ifndef _FORCE_INLINES
>  # undef __STRING_INLINE
>  #endif
>
>
>
>
>
  

Patch

diff --git a/string/bits/string2.h b/string/bits/string2.h
index b0be5f6a49024a0bedfc37e9ec2c0356e0e4fa09..5e0339223697256fff7eba4cc32d2eb618f07713 100644
--- a/string/bits/string2.h
+++ b/string/bits/string2.h
@@ -60,64 +60,6 @@ 
 # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
 #endif
 
-/* Compare characters of S1 and S2.  */
-#ifndef strcmp
-# define strcmp(s1, s2) \
-  __extension__                                                                      \
-  ({ size_t __s1_len, __s2_len;                                                      \
-     (__builtin_constant_p (s1) && __builtin_constant_p (s2)                 \
-      && (__s1_len = strlen (s1), __s2_len = strlen (s2),                    \
-         (!__string2_1bptr_p (s1) || __s1_len >= 4)                           \
-         && (!__string2_1bptr_p (s2) || __s2_len >= 4))               \
-      ? __builtin_strcmp (s1, s2)                                            \
-      : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)                 \
-        && (__s1_len = strlen (s1), __s1_len < 4)                             \
-        ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
-           ? __builtin_strcmp (s1, s2)                                        \
-           : __strcmp_cg (s1, s2, __s1_len))                                  \
-        : (__builtin_constant_p (s2) && __string2_1bptr_p (s2)                \
-           && (__s2_len = strlen (s2), __s2_len < 4)                          \
-           ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1)             \
-              ? __builtin_strcmp (s1, s2)                                     \
-              : -__strcmp_cg (s2, s1, __s2_len))                              \
-            : __builtin_strcmp (s1, s2)))); })
-
-# define __strcmp_cg(s1, s2, l1) \
-  (__extension__ ({ const unsigned char *__s2 =                                      \
-                     (const unsigned char *) (const char *) (s2);             \
-                   int __result =                                             \
-                     (((const unsigned char *) (const char *) (s1))[0]        \
-                      - __s2[0]);                                             \
-                   if (l1 > 0 && __result == 0)                       \
-                     {                                                        \
-                       __result = (((const unsigned char *)                  \
-                                    (const char *) (s1))[1] - __s2[1]);      \
-                       if (l1 > 1 && __result == 0)                          \
-                         {                                                    \
-                           __result = (((const unsigned char *)       \
-                                        (const char *) (s1))[2] - __s2[2]);  \
-                           if (l1 > 2 && __result == 0)               \
-                             __result = (((const unsigned char *)             \
-                                         (const char *)  (s1))[3]             \
-                                         - __s2[3]);                          \
-                         }                                                    \
-                     }                                                        \
-                   __result; }))
-#endif
-
-
-/* Compare N characters of S1 and S2.  */
-#ifndef strncmp
-# define strncmp(s1, s2, n)                                                  \
-  (__extension__ (__builtin_constant_p (n)                                   \
-                 && ((__builtin_constant_p (s1)                       \
-                      && strlen (s1) < ((size_t) (n)))                        \
-                     || (__builtin_constant_p (s2)                            \
-                         && strlen (s2) < ((size_t) (n))))                    \
-                 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
-#endif
-
-
 #ifndef _FORCE_INLINES
 # undef __STRING_INLINE
 #endif