Skip to content

Commit

Permalink
S390: Optimize strspn and wcsspn.
Browse files Browse the repository at this point in the history
This patch provides optimized versions of strspn and wcsspn with the z13
vector instructions.

ChangeLog:

	* sysdeps/s390/multiarch/strspn-c.c: New File.
	* sysdeps/s390/multiarch/strspn-vx.S: Likewise.
	* sysdeps/s390/multiarch/strspn.c: Likewise.
	* sysdeps/s390/multiarch/wcsspn-c.c: Likewise.
	* sysdeps/s390/multiarch/wcsspn-vx.S: Likewise.
	* sysdeps/s390/multiarch/wcsspn.c: Likewise.
	* wcsmbs/wcsspn.c: Use WCSSPN if defined.
	* sysdeps/s390/multiarch/Makefile (sysdep_routines): Add strspn and
	wcsspn functions.
	* sysdeps/s390/multiarch/ifunc-impl-list.c
	(__libc_ifunc_impl_list): Add ifunc test for strspn, wcsspn.
	* string/test-strspn.c: Add wcsspn support.
	* wcsmbs/test-wcsspn.c: New File.
	* wcsmbs/Makefile (strop-tests): Add wcsspn.
	* benchtests/bench-strspn.c: Add wcsspn support.
	* benchtests/bench-wcsspn.c: New File.
	* benchtests/Makefile (wcsmbs-bench): Add wcsspn.
  • Loading branch information
Stefan Liebler authored and Andreas Krebbel committed Aug 26, 2015
1 parent f40132d commit f1ffad9
Show file tree
Hide file tree
Showing 16 changed files with 823 additions and 64 deletions.
20 changes: 20 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
2015-08-26 Stefan Liebler <stli@linux.vnet.ibm.com>

* sysdeps/s390/multiarch/strspn-c.c: New File.
* sysdeps/s390/multiarch/strspn-vx.S: Likewise.
* sysdeps/s390/multiarch/strspn.c: Likewise.
* sysdeps/s390/multiarch/wcsspn-c.c: Likewise.
* sysdeps/s390/multiarch/wcsspn-vx.S: Likewise.
* sysdeps/s390/multiarch/wcsspn.c: Likewise.
* wcsmbs/wcsspn.c: Use WCSSPN if defined.
* sysdeps/s390/multiarch/Makefile (sysdep_routines): Add strspn and
wcsspn functions.
* sysdeps/s390/multiarch/ifunc-impl-list.c
(__libc_ifunc_impl_list): Add ifunc test for strspn, wcsspn.
* string/test-strspn.c: Add wcsspn support.
* wcsmbs/test-wcsspn.c: New File.
* wcsmbs/Makefile (strop-tests): Add wcsspn.
* benchtests/bench-strspn.c: Add wcsspn support.
* benchtests/bench-wcsspn.c: New File.
* benchtests/Makefile (wcsmbs-bench): Add wcsspn.

2015-08-26 Stefan Liebler <stli@linux.vnet.ibm.com>

* sysdeps/s390/multiarch/strrchr-c.c: New File.
Expand Down
2 changes: 1 addition & 1 deletion benchtests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ string-bench := bcopy bzero memccpy memchr memcmp memcpy memmem memmove \
strspn strstr strcpy_chk stpcpy_chk memrchr strsep strtok \
strcoll
wcsmbs-bench := wcslen wcsnlen wcscpy wcpcpy wcsncpy wcpncpy wcscat wcsncat \
wcscmp wcsncmp wcschr wcschrnul wcsrchr
wcscmp wcsncmp wcschr wcschrnul wcsrchr wcsspn
string-bench-all := $(string-bench) ${wcsmbs-bench}

# We have to generate locales
Expand Down
73 changes: 49 additions & 24 deletions benchtests/bench-strspn.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,47 @@
<http://www.gnu.org/licenses/>. */

#define TEST_MAIN
#define TEST_NAME "strspn"
#ifndef WIDE
# define TEST_NAME "strspn"
#else
# define TEST_NAME "wcsspn"
#endif /* WIDE */
#include "bench-string.h"

typedef size_t (*proto_t) (const char *, const char *);
size_t simple_strspn (const char *, const char *);
size_t stupid_strspn (const char *, const char *);

IMPL (stupid_strspn, 0)
IMPL (simple_strspn, 0)
IMPL (strspn, 1)
#ifndef WIDE
# define STRSPN strspn
# define CHAR char
# define SIMPLE_STRSPN simple_strspn
# define STUPID_STRSPN stupid_strspn
# define STRLEN strlen
# define STRCHR strchr
# define BIG_CHAR CHAR_MAX
# define SMALL_CHAR 127
#else
# include <wchar.h>
# define STRSPN wcsspn
# define CHAR wchar_t
# define SIMPLE_STRSPN simple_wcsspn
# define STUPID_STRSPN stupid_wcsspn
# define STRLEN wcslen
# define STRCHR wcschr
# define BIG_CHAR WCHAR_MAX
# define SMALL_CHAR 1273
#endif /* WIDE */

typedef size_t (*proto_t) (const CHAR *, const CHAR *);
size_t SIMPLE_STRSPN (const CHAR *, const CHAR *);
size_t STUPID_STRSPN (const CHAR *, const CHAR *);

IMPL (STUPID_STRSPN, 0)
IMPL (SIMPLE_STRSPN, 0)
IMPL (STRSPN, 1)

size_t
simple_strspn (const char *s, const char *acc)
SIMPLE_STRSPN (const CHAR *s, const CHAR *acc)
{
const char *r, *str = s;
char c;
const CHAR *r, *str = s;
CHAR c;

while ((c = *s++) != '\0')
{
Expand All @@ -46,9 +71,9 @@ simple_strspn (const char *s, const char *acc)
}

size_t
stupid_strspn (const char *s, const char *acc)
STUPID_STRSPN (const CHAR *s, const CHAR *acc)
{
size_t ns = strlen (s), nacc = strlen (acc);
size_t ns = STRLEN (s), nacc = STRLEN (acc);
size_t i, j;

for (i = 0; i < ns; ++i)
Expand All @@ -63,7 +88,7 @@ stupid_strspn (const char *s, const char *acc)
}

static void
do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res)
do_one_test (impl_t *impl, const CHAR *s, const CHAR *acc, size_t exp_res)
{
size_t res = CALL (impl, s, acc), i, iters = INNER_LOOP_ITERS;
timing_t start, stop, cur;
Expand Down Expand Up @@ -92,34 +117,34 @@ static void
do_test (size_t align, size_t pos, size_t len)
{
size_t i;
char *acc, *s;
CHAR *acc, *s;

align &= 7;
if (align + pos + 10 >= page_size || len > 240 || ! len)
if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || ! len)
return;

acc = (char *) (buf2 + (random () & 255));
s = (char *) (buf1 + align);
acc = (CHAR *) (buf2) + (random () & 255);
s = (CHAR *) (buf1) + align;

for (i = 0; i < len; ++i)
{
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = 1 + (random () & 127);
acc[i] = 1 + (random () & SMALL_CHAR);
}
acc[len] = '\0';

for (i = 0; i < pos; ++i)
s[i] = acc[random () % len];
s[pos] = random () & 255;
if (strchr (acc, s[pos]))
s[pos] = random () & BIG_CHAR;
if (STRCHR (acc, s[pos]))
s[pos] = '\0';
else
{
for (i = pos + 1; i < pos + 10; ++i)
s[i] = random () & 255;
s[i] = random () & BIG_CHAR;
s[i] = '\0';
}

Expand Down
20 changes: 20 additions & 0 deletions benchtests/bench-wcsspn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Measure wcsspn functions.
Copyright (C) 2015 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
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/>. */

#define WIDE 1
#include "bench-strspn.c"
99 changes: 63 additions & 36 deletions string/test-strspn.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,49 @@
<http://www.gnu.org/licenses/>. */

#define TEST_MAIN
#define TEST_NAME "strspn"
#ifndef WIDE
# define TEST_NAME "strspn"
#else
# define TEST_NAME "wcsspn"
#endif /* WIDE */
#include "test-string.h"

typedef size_t (*proto_t) (const char *, const char *);
size_t simple_strspn (const char *, const char *);
size_t stupid_strspn (const char *, const char *);

IMPL (stupid_strspn, 0)
IMPL (simple_strspn, 0)
IMPL (strspn, 1)
#ifndef WIDE
# define STRSPN strspn
# define CHAR char
# define UCHAR unsigned char
# define SIMPLE_STRSPN simple_strspn
# define STUPID_STRSPN stupid_strspn
# define STRLEN strlen
# define STRCHR strchr
# define BIG_CHAR CHAR_MAX
# define SMALL_CHAR 127
#else
# include <wchar.h>
# define STRSPN wcsspn
# define CHAR wchar_t
# define UCHAR wchar_t
# define SIMPLE_STRSPN simple_wcsspn
# define STUPID_STRSPN stupid_wcsspn
# define STRLEN wcslen
# define STRCHR wcschr
# define BIG_CHAR WCHAR_MAX
# define SMALL_CHAR 1273
#endif /* WIDE */

typedef size_t (*proto_t) (const CHAR *, const CHAR *);
size_t SIMPLE_STRSPN (const CHAR *, const CHAR *);
size_t STUPID_STRSPN (const CHAR *, const CHAR *);

IMPL (STUPID_STRSPN, 0)
IMPL (SIMPLE_STRSPN, 0)
IMPL (STRSPN, 1)

size_t
simple_strspn (const char *s, const char *acc)
SIMPLE_STRSPN (const CHAR *s, const CHAR *acc)
{
const char *r, *str = s;
char c;
const CHAR *r, *str = s;
CHAR c;

while ((c = *s++) != '\0')
{
Expand All @@ -47,9 +74,9 @@ simple_strspn (const char *s, const char *acc)
}

size_t
stupid_strspn (const char *s, const char *acc)
STUPID_STRSPN (const CHAR *s, const CHAR *acc)
{
size_t ns = strlen (s), nacc = strlen (acc);
size_t ns = STRLEN (s), nacc = STRLEN (acc);
size_t i, j;

for (i = 0; i < ns; ++i)
Expand All @@ -64,7 +91,7 @@ stupid_strspn (const char *s, const char *acc)
}

static void
do_one_test (impl_t *impl, const char *s, const char *acc, size_t exp_res)
do_one_test (impl_t *impl, const CHAR *s, const CHAR *acc, size_t exp_res)
{
size_t res = CALL (impl, s, acc);
if (res != exp_res)
Expand All @@ -80,34 +107,34 @@ static void
do_test (size_t align, size_t pos, size_t len)
{
size_t i;
char *acc, *s;
CHAR *acc, *s;

align &= 7;
if (align + pos + 10 >= page_size || len > 240 || ! len)
if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240 || ! len)
return;

acc = (char *) (buf2 + (random () & 255));
s = (char *) (buf1 + align);
acc = (CHAR *) (buf2) + (random () & 255);
s = (CHAR *) (buf1) + align;

for (i = 0; i < len; ++i)
{
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = 1 + (random () & 127);
acc[i] = 1 + (random () & SMALL_CHAR);
}
acc[len] = '\0';

for (i = 0; i < pos; ++i)
s[i] = acc[random () % len];
s[pos] = random () & 255;
if (strchr (acc, s[pos]))
s[pos] = random () & BIG_CHAR;
if (STRCHR (acc, s[pos]))
s[pos] = '\0';
else
{
for (i = pos + 1; i < pos + 10; ++i)
s[i] = random () & 255;
s[i] = random () & BIG_CHAR;
s[i] = '\0';
}

Expand All @@ -119,8 +146,8 @@ static void
do_random_tests (void)
{
size_t i, j, n, align, pos, alen, len;
unsigned char *p = buf1 + page_size - 512;
unsigned char *acc;
UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
UCHAR *acc;

for (n = 0; n < ITERATIONS; n++)
{
Expand All @@ -138,14 +165,14 @@ do_random_tests (void)
len = random () & 511;
if (len + align >= 512)
len = 511 - align - (random () & 7);
acc = buf2 + page_size - alen - 1 - (random () & 7);
acc = (UCHAR *) (buf2 + page_size) - alen - 1 - (random () & 7);
for (i = 0; i < alen; ++i)
{
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = random () & 255;
acc[i] = random () & BIG_CHAR;
if (!acc[i])
acc[i] = 1 + (random () & 127);
acc[i] = 1 + (random () & SMALL_CHAR);
}
acc[i] = '\0';
j = (pos > len ? pos : len) + align + 64;
Expand All @@ -158,23 +185,23 @@ do_random_tests (void)
p[i] = '\0';
else if (i == pos + align)
{
p[i] = random () & 255;
if (strchr ((char *) acc, p[i]))
p[i] = random () & BIG_CHAR;
if (STRCHR ((CHAR *) acc, p[i]))
p[i] = '\0';
}
else if (i < align || i > pos + align)
p[i] = random () & 255;
p[i] = random () & BIG_CHAR;
else
p[i] = acc [random () % alen];
}

FOR_EACH_IMPL (impl, 1)
if (CALL (impl, (char *) (p + align),
(char *) acc) != (pos < len ? pos : len))
if (CALL (impl, (CHAR *) (p + align),
(CHAR *) acc) != (pos < len ? pos : len))
{
error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %zd != %zd",
n, impl->name, align, acc, alen, pos, len,
CALL (impl, (char *) (p + align), (char *) acc),
CALL (impl, (CHAR *) (p + align), (CHAR *) acc),
(pos < len ? pos : len));
ret = 1;
}
Expand Down
6 changes: 4 additions & 2 deletions sysdeps/s390/multiarch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ sysdep_routines += strlen strlen-vx strlen-c \
strncmp strncmp-vx strncmp-c \
strchr strchr-vx strchr-c \
strchrnul strchrnul-vx strchrnul-c \
strrchr strrchr-vx strrchr-c
strrchr strrchr-vx strrchr-c \
strspn strspn-vx strspn-c
endif

ifeq ($(subdir),wcsmbs)
Expand All @@ -27,5 +28,6 @@ sysdep_routines += wcslen wcslen-vx wcslen-c \
wcsncmp wcsncmp-vx wcsncmp-c \
wcschr wcschr-vx wcschr-c \
wcschrnul wcschrnul-vx wcschrnul-c \
wcsrchr wcsrchr-vx wcsrchr-c
wcsrchr wcsrchr-vx wcsrchr-c \
wcsspn wcsspn-vx wcsspn-c
endif
3 changes: 3 additions & 0 deletions sysdeps/s390/multiarch/ifunc-impl-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ __libc_ifunc_impl_list (const char *name, struct libc_ifunc_impl *array,
IFUNC_VX_IMPL (strrchr);
IFUNC_VX_IMPL (wcsrchr);

IFUNC_VX_IMPL (strspn);
IFUNC_VX_IMPL (wcsspn);

#endif /* HAVE_S390_VX_ASM_SUPPORT */

return i;
Expand Down
Loading

0 comments on commit f1ffad9

Please sign in to comment.