diff options
author | Jeff Johnston <jjohnstn@redhat.com> | 2008-04-01 23:02:16 +0400 |
---|---|---|
committer | Jeff Johnston <jjohnstn@redhat.com> | 2008-04-01 23:02:16 +0400 |
commit | bd6e30d59aed07d0d63f8c7b9be1813300fa2225 (patch) | |
tree | d80249c6f8624cfa37c7fc53f9c2ed187ab94db0 | |
parent | 71f3a8f566b2a043e051d4943a7aee14506df171 (diff) |
2008-04-01 Patrick Mansfield <patmans@us.ibm.com>
* libc/machine/spu/memcmp.c: New file for SPU optimized memcmp.
* libc/machine/spu/strncmp.h: New file supply _strncmp_internal.
* libc/machine/spu/Makefile.am: Add memcmp.c.
* libc/machine/spu/Makefile.in: Rebuild.
* libc/machine/spu/strncmp.c: Use _strncmp_internal.
-rw-r--r-- | newlib/ChangeLog | 8 | ||||
-rw-r--r-- | newlib/libc/machine/spu/Makefile.am | 8 | ||||
-rw-r--r-- | newlib/libc/machine/spu/Makefile.in | 72 | ||||
-rw-r--r-- | newlib/libc/machine/spu/memcmp.c | 43 | ||||
-rw-r--r-- | newlib/libc/machine/spu/strncmp.c | 99 | ||||
-rw-r--r-- | newlib/libc/machine/spu/strncmp.h | 155 |
6 files changed, 257 insertions, 128 deletions
diff --git a/newlib/ChangeLog b/newlib/ChangeLog index f2435236f..914d3c686 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,5 +1,13 @@ 2008-04-01 Patrick Mansfield <patmans@us.ibm.com> + * libc/machine/spu/memcmp.c: New file for SPU optimized memcmp. + * libc/machine/spu/strncmp.h: New file supply _strncmp_internal. + * libc/machine/spu/Makefile.am: Add memcmp.c. + * libc/machine/spu/Makefile.in: Rebuild. + * libc/machine/spu/strncmp.c: Use _strncmp_internal. + +2008-04-01 Patrick Mansfield <patmans@us.ibm.com> + * configure.host: Build SPU with -DREENTRANT_SYSCALLS_PROVIDED and -DMISSING_SYSCALL_NAMES. diff --git a/newlib/libc/machine/spu/Makefile.am b/newlib/libc/machine/spu/Makefile.am index c07ee9893..e542795fa 100644 --- a/newlib/libc/machine/spu/Makefile.am +++ b/newlib/libc/machine/spu/Makefile.am @@ -12,9 +12,9 @@ lib_a_SOURCES = setjmp.S assert.c clearerr.c creat.c fclose.c feof.c \ ferror.c fflush.c fgetc.c fgetpos.c fgets.c fileno.c fiprintf.S \ fiscanf.S fopen.c fprintf.S fputc.c fputs.c fread.c freopen.c \ fscanf.S fseek.c fsetpos.c ftell.c fwrite.c getc.c getchar.c \ - gets.c iprintf.S iscanf.S memcpy.c memmove.c memset.c perror.c \ - printf.S putc.c putchar.c puts.c remove.c rename.c rewind.c \ - scanf.S setbuf.c setvbuf.c siprintf.S siscanf.S sleep.c \ + gets.c iprintf.S iscanf.S memcmp.c memcpy.c memmove.c memset.c \ + perror.c printf.S putc.c putchar.c puts.c remove.c rename.c \ + rewind.c scanf.S setbuf.c setvbuf.c siprintf.S siscanf.S sleep.c \ sniprintf.S snprintf.S sprintf.S sscanf.S stdio.c strcat.c \ strchr.c strcmp.c strcpy.c strcspn.c strlen.c strncat.c strncmp.c \ strncpy.c strpbrk.c strrchr.c strspn.c strxfrm.c tmpfile.c \ @@ -26,6 +26,6 @@ lib_a_SOURCES = setjmp.S assert.c clearerr.c creat.c fclose.c feof.c \ lib_a_CCASFLAGS = $(AM_CCASFLAGS) lib_a_CFLAGS = $(AM_CFLAGS) -ACLOCAL_AMFLAGS = -I ../../.. -I ../../../.. +ACLOCAL_AMFLAGS = -I ../../.. -I ../../../.. CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host diff --git a/newlib/libc/machine/spu/Makefile.in b/newlib/libc/machine/spu/Makefile.in index 8a7f06451..a4007ab2e 100644 --- a/newlib/libc/machine/spu/Makefile.in +++ b/newlib/libc/machine/spu/Makefile.in @@ -73,7 +73,8 @@ DIST_COMMON = $(srcdir)/../../../../config.guess \ $(srcdir)/../../../../compile $(srcdir)/../../../../compile \ $(srcdir)/../../../../compile $(srcdir)/../../../../compile \ $(srcdir)/../../../../compile $(srcdir)/../../../../compile \ - $(srcdir)/../../../../compile $(srcdir)/../../../../compile + $(srcdir)/../../../../compile $(srcdir)/../../../../compile \ + $(srcdir)/../../../../compile subdir = . ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/../../../acinclude.m4 \ @@ -103,33 +104,34 @@ am_lib_a_OBJECTS = lib_a-setjmp.$(OBJEXT) lib_a-assert.$(OBJEXT) \ lib_a-fwrite.$(OBJEXT) lib_a-getc.$(OBJEXT) \ lib_a-getchar.$(OBJEXT) lib_a-gets.$(OBJEXT) \ lib_a-iprintf.$(OBJEXT) lib_a-iscanf.$(OBJEXT) \ - lib_a-memcpy.$(OBJEXT) lib_a-memmove.$(OBJEXT) \ - lib_a-memset.$(OBJEXT) lib_a-perror.$(OBJEXT) \ - lib_a-printf.$(OBJEXT) lib_a-putc.$(OBJEXT) \ - lib_a-putchar.$(OBJEXT) lib_a-puts.$(OBJEXT) \ - lib_a-remove.$(OBJEXT) lib_a-rename.$(OBJEXT) \ - lib_a-rewind.$(OBJEXT) lib_a-scanf.$(OBJEXT) \ - lib_a-setbuf.$(OBJEXT) lib_a-setvbuf.$(OBJEXT) \ - lib_a-siprintf.$(OBJEXT) lib_a-siscanf.$(OBJEXT) \ - lib_a-sleep.$(OBJEXT) lib_a-sniprintf.$(OBJEXT) \ - lib_a-snprintf.$(OBJEXT) lib_a-sprintf.$(OBJEXT) \ - lib_a-sscanf.$(OBJEXT) lib_a-stdio.$(OBJEXT) \ - lib_a-strcat.$(OBJEXT) lib_a-strchr.$(OBJEXT) \ - lib_a-strcmp.$(OBJEXT) lib_a-strcpy.$(OBJEXT) \ - lib_a-strcspn.$(OBJEXT) lib_a-strlen.$(OBJEXT) \ - lib_a-strncat.$(OBJEXT) lib_a-strncmp.$(OBJEXT) \ - lib_a-strncpy.$(OBJEXT) lib_a-strpbrk.$(OBJEXT) \ - lib_a-strrchr.$(OBJEXT) lib_a-strspn.$(OBJEXT) \ - lib_a-strxfrm.$(OBJEXT) lib_a-tmpfile.$(OBJEXT) \ - lib_a-tmpnam.$(OBJEXT) lib_a-ungetc.$(OBJEXT) \ - lib_a-usleep.$(OBJEXT) lib_a-vfiprintf.$(OBJEXT) \ - lib_a-vfiscanf.$(OBJEXT) lib_a-vfprintf.$(OBJEXT) \ - lib_a-vfscanf.$(OBJEXT) lib_a-viprintf.$(OBJEXT) \ - lib_a-viscanf.$(OBJEXT) lib_a-vprintf.$(OBJEXT) \ - lib_a-vscanf.$(OBJEXT) lib_a-vsiprintf.$(OBJEXT) \ - lib_a-vsiscanf.$(OBJEXT) lib_a-vsniprintf.$(OBJEXT) \ - lib_a-vsnprintf.$(OBJEXT) lib_a-vsprintf.$(OBJEXT) \ - lib_a-vsscanf.$(OBJEXT) lib_a-stack_reg_va.$(OBJEXT) + lib_a-memcmp.$(OBJEXT) lib_a-memcpy.$(OBJEXT) \ + lib_a-memmove.$(OBJEXT) lib_a-memset.$(OBJEXT) \ + lib_a-perror.$(OBJEXT) lib_a-printf.$(OBJEXT) \ + lib_a-putc.$(OBJEXT) lib_a-putchar.$(OBJEXT) \ + lib_a-puts.$(OBJEXT) lib_a-remove.$(OBJEXT) \ + lib_a-rename.$(OBJEXT) lib_a-rewind.$(OBJEXT) \ + lib_a-scanf.$(OBJEXT) lib_a-setbuf.$(OBJEXT) \ + lib_a-setvbuf.$(OBJEXT) lib_a-siprintf.$(OBJEXT) \ + lib_a-siscanf.$(OBJEXT) lib_a-sleep.$(OBJEXT) \ + lib_a-sniprintf.$(OBJEXT) lib_a-snprintf.$(OBJEXT) \ + lib_a-sprintf.$(OBJEXT) lib_a-sscanf.$(OBJEXT) \ + lib_a-stdio.$(OBJEXT) lib_a-strcat.$(OBJEXT) \ + lib_a-strchr.$(OBJEXT) lib_a-strcmp.$(OBJEXT) \ + lib_a-strcpy.$(OBJEXT) lib_a-strcspn.$(OBJEXT) \ + lib_a-strlen.$(OBJEXT) lib_a-strncat.$(OBJEXT) \ + lib_a-strncmp.$(OBJEXT) lib_a-strncpy.$(OBJEXT) \ + lib_a-strpbrk.$(OBJEXT) lib_a-strrchr.$(OBJEXT) \ + lib_a-strspn.$(OBJEXT) lib_a-strxfrm.$(OBJEXT) \ + lib_a-tmpfile.$(OBJEXT) lib_a-tmpnam.$(OBJEXT) \ + lib_a-ungetc.$(OBJEXT) lib_a-usleep.$(OBJEXT) \ + lib_a-vfiprintf.$(OBJEXT) lib_a-vfiscanf.$(OBJEXT) \ + lib_a-vfprintf.$(OBJEXT) lib_a-vfscanf.$(OBJEXT) \ + lib_a-viprintf.$(OBJEXT) lib_a-viscanf.$(OBJEXT) \ + lib_a-vprintf.$(OBJEXT) lib_a-vscanf.$(OBJEXT) \ + lib_a-vsiprintf.$(OBJEXT) lib_a-vsiscanf.$(OBJEXT) \ + lib_a-vsniprintf.$(OBJEXT) lib_a-vsnprintf.$(OBJEXT) \ + lib_a-vsprintf.$(OBJEXT) lib_a-vsscanf.$(OBJEXT) \ + lib_a-stack_reg_va.$(OBJEXT) lib_a_OBJECTS = $(am_lib_a_OBJECTS) DEFAULT_INCLUDES = -I. -I$(srcdir) depcomp = @@ -260,9 +262,9 @@ lib_a_SOURCES = setjmp.S assert.c clearerr.c creat.c fclose.c feof.c \ ferror.c fflush.c fgetc.c fgetpos.c fgets.c fileno.c fiprintf.S \ fiscanf.S fopen.c fprintf.S fputc.c fputs.c fread.c freopen.c \ fscanf.S fseek.c fsetpos.c ftell.c fwrite.c getc.c getchar.c \ - gets.c iprintf.S iscanf.S memcpy.c memmove.c memset.c perror.c \ - printf.S putc.c putchar.c puts.c remove.c rename.c rewind.c \ - scanf.S setbuf.c setvbuf.c siprintf.S siscanf.S sleep.c \ + gets.c iprintf.S iscanf.S memcmp.c memcpy.c memmove.c memset.c \ + perror.c printf.S putc.c putchar.c puts.c remove.c rename.c \ + rewind.c scanf.S setbuf.c setvbuf.c siprintf.S siscanf.S sleep.c \ sniprintf.S snprintf.S sprintf.S sscanf.S stdio.c strcat.c \ strchr.c strcmp.c strcpy.c strcspn.c strlen.c strncat.c strncmp.c \ strncpy.c strpbrk.c strrchr.c strspn.c strxfrm.c tmpfile.c \ @@ -273,7 +275,7 @@ lib_a_SOURCES = setjmp.S assert.c clearerr.c creat.c fclose.c feof.c \ lib_a_CCASFLAGS = $(AM_CCASFLAGS) lib_a_CFLAGS = $(AM_CFLAGS) -ACLOCAL_AMFLAGS = -I ../../.. -I ../../../.. +ACLOCAL_AMFLAGS = -I ../../.. -I ../../../.. CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host all: all-am @@ -572,6 +574,12 @@ lib_a-gets.o: gets.c lib_a-gets.obj: gets.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-gets.obj `if test -f 'gets.c'; then $(CYGPATH_W) 'gets.c'; else $(CYGPATH_W) '$(srcdir)/gets.c'; fi` +lib_a-memcmp.o: memcmp.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-memcmp.o `test -f 'memcmp.c' || echo '$(srcdir)/'`memcmp.c + +lib_a-memcmp.obj: memcmp.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-memcmp.obj `if test -f 'memcmp.c'; then $(CYGPATH_W) 'memcmp.c'; else $(CYGPATH_W) '$(srcdir)/memcmp.c'; fi` + lib_a-memcpy.o: memcpy.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-memcpy.o `test -f 'memcpy.c' || echo '$(srcdir)/'`memcpy.c diff --git a/newlib/libc/machine/spu/memcmp.c b/newlib/libc/machine/spu/memcmp.c new file mode 100644 index 000000000..a0f5449d9 --- /dev/null +++ b/newlib/libc/machine/spu/memcmp.c @@ -0,0 +1,43 @@ +/* + (C) Copyright 2008 + International Business Machines Corporation + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the names of the copyright holders nor the names of their + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stddef.h> +#include "strncmp.h" + +/* + * Compare the two strings s1 and s2 of length n. Returns an integer less + * than, equal to, or greater than zero if s1 is found, respectively, + * to be less than, to match, or be greater than s2. + */ +int memcmp(const void *s1, const void *s2, size_t n) +{ + vec_uint4 end_v; + return _strncmp_internal (s1, s2, n, &end_v, 0 /* no zero check */); +} diff --git a/newlib/libc/machine/spu/strncmp.c b/newlib/libc/machine/spu/strncmp.c index 54d1fb9f4..d96504ca6 100644 --- a/newlib/libc/machine/spu/strncmp.c +++ b/newlib/libc/machine/spu/strncmp.c @@ -30,102 +30,17 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <spu_intrinsics.h> #include <stddef.h> -#include "vec_literal.h" +#include <spu_intrinsics.h> +#include "strncmp.h" -/* Compare the two strings s1 and s2 of length n. Returns an integer - * less than, equal to, or greater than zero if s1 is found, respectively, +/* + * Compare the two strings s1 and s2 of length n. Returns an integer less + * than, equal to, or greater than zero if s1 is found, respectively, * to be less than, to match, or be greater than s2. */ - int strncmp(const char *s1, const char *s2, size_t n) { - unsigned int offset1, offset2; - vec_int4 n_v; - vec_uint4 cnt1_v, cnt2_v, max_cnt_v; - vec_uint4 gt_v, lt_v, mask_v, end1_v, end2_v, end_v, neq_v; - vec_uint4 shift_n_v, shift_eos_v, max_shift_v; - vec_uchar16 shuffle1, shuffle2; - vec_uchar16 data1A, data1B, data1, data2A, data2B, data2; - vec_uchar16 *ptr1, *ptr2; - - data1 = data2 = spu_splats((unsigned char)0); - - ptr1 = (vec_uchar16 *)s1; - ptr2 = (vec_uchar16 *)s2; - - offset1 = (unsigned int)(ptr1) & 15; - offset2 = (unsigned int)(ptr2) & 15; - - shuffle1 = (vec_uchar16)spu_add((vec_uint4)spu_splats((unsigned char)offset1), - VEC_LITERAL(vec_uint4, 0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F)); - shuffle2 = (vec_uchar16)spu_add((vec_uint4)spu_splats((unsigned char)offset2), - VEC_LITERAL(vec_uint4, 0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F)); - data1A = *ptr1++; - data2A = *ptr2++; - - n_v = spu_promote((int)n, 0); - - do { - data1B = *ptr1++; - data2B = *ptr2++; - - /* Quadword align each of the input strings so that - * we operate on full quadwords. - */ - data1 = spu_shuffle(data1A, data1B, shuffle1); - data2 = spu_shuffle(data2A, data2B, shuffle2); - - data1A = data1B; - data2A = data2B; - - neq_v = spu_gather(spu_xor(spu_cmpeq(data1, data2), -1)); - - end1_v = spu_gather(spu_cmpeq(data1, 0)); - end2_v = spu_gather(spu_cmpeq(data2, 0)); - end_v = spu_or(end1_v, end2_v), 0; - - n_v = spu_add(n_v, -16); - - /* Repeat until either - * 1) the character count expired, - * 2) a null character is discovered in one of the input strings, or - * 3) the strings do not compare equal. - */ - } while (spu_extract(spu_and(spu_cmpeq(spu_or(end_v, neq_v), 0), spu_cmpgt(n_v, 0)), 0)); - - /* Construct a mask to eliminate characters that are not of interest - * in the comparison. Theses include characters that are beyond the - * n character count and beyond the first null character. - */ - cnt1_v = spu_cntlz(end1_v); - cnt2_v = spu_cntlz(end2_v); - - max_cnt_v = spu_sel(cnt1_v, cnt2_v, spu_cmpgt(cnt2_v, cnt1_v)); - - mask_v = spu_splats((unsigned int)0xFFFF); - - shift_n_v = spu_andc((__vector unsigned int)spu_sub(0, n_v), spu_cmpgt(n_v, -1)); - shift_eos_v = spu_sub(32, max_cnt_v); - - max_shift_v = spu_sel(shift_n_v, shift_eos_v, spu_cmpgt(shift_eos_v, shift_n_v)); - - mask_v = spu_and(spu_sl(mask_v, spu_extract(max_shift_v, 0)), mask_v); - - /* Determine if greater then or less then in the case that they are - * not equal. gt_v is either 1 (in the case s1 is greater then s2), or - * -1 (in the case that s2 is greater then s1). - */ - gt_v = spu_gather(spu_cmpgt(data1, data2)); - lt_v = spu_gather(spu_cmpgt(data2, data1)); - - gt_v = spu_sub(-1, spu_sl(spu_cmpgt(gt_v, lt_v), 1)); - - /* Construct a mask to be applied to gt_v if the strings are discovered - * to be equal. - */ - mask_v = spu_cmpeq(spu_and(neq_v, mask_v), 0); - - return (spu_extract(spu_andc(gt_v, mask_v), 0)); + vec_uint4 end_v; + return _strncmp_internal (s1, s2, n, &end_v, 1); } diff --git a/newlib/libc/machine/spu/strncmp.h b/newlib/libc/machine/spu/strncmp.h new file mode 100644 index 000000000..f464564bc --- /dev/null +++ b/newlib/libc/machine/spu/strncmp.h @@ -0,0 +1,155 @@ +/* + (C) Copyright 2001,2006,2008 + International Business Machines Corporation, + Sony Computer Entertainment, Incorporated, + Toshiba Corporation, + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the names of the copyright holders nor the names of their + contributors may be used to endorse or promote products derived from this + software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ +#include <spu_intrinsics.h> +#include <stddef.h> + +/* + * Internal _strncmp_internal for strncmp and memcmp: + * + * Compare the two strings s1 and s2 of length n. Returns an integer less + * than, equal to, or greater than zero if s1 is found, respectively, + * to be less than, to match, or be greater than s2. Check for zero bytes + * only if check_zeroes is set. In additin, the vector value *end_v will + * contain all zeroes if we hit a NUL character that caused the comparison + * to complete, *end_v is modified only if check_zeroes is set. + */ +static inline int +_strncmp_internal(const char *s1, const char *s2, size_t n, vec_uint4 + *end_v, int check_zeroes) +{ + unsigned int offset1, offset2; + vec_int4 n_v; + vec_uint4 cnt1_v, cnt2_v, max_cnt_v; + vec_uint4 gt_v, lt_v, mask_v, end1_v, end2_v, neq_v, tmp1_v; + vec_uint4 shift_n_v, shift_eos_v, max_shift_v; + vec_uchar16 shuffle1, shuffle2; + vec_uchar16 data1A, data1B, data1, data2A, data2B, data2; + vec_uchar16 *ptr1, *ptr2; + + data1 = data2 = spu_splats((unsigned char)0); + + ptr1 = (vec_uchar16 *)s1; + ptr2 = (vec_uchar16 *)s2; + + offset1 = (unsigned int)(ptr1) & 15; + offset2 = (unsigned int)(ptr2) & 15; + + shuffle1 = (vec_uchar16) + spu_add((vec_uint4)spu_splats((unsigned char)offset1), + ((vec_uint4) {0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F})); + shuffle2 = (vec_uchar16) + spu_add((vec_uint4)spu_splats((unsigned char)offset2), + ((vec_uint4) {0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F})); + data1A = *ptr1++; + data2A = *ptr2++; + + n_v = spu_promote((int)n, 0); + + do { + data1B = *ptr1++; + data2B = *ptr2++; + + /* + * Quadword align each of the input strings so that we operate on full + * quadwords. + */ + data1 = spu_shuffle(data1A, data1B, shuffle1); + data2 = spu_shuffle(data2A, data2B, shuffle2); + + data1A = data1B; + data2A = data2B; + + neq_v = spu_gather(spu_xor(spu_cmpeq(data1, data2), -1)); + + if (check_zeroes) { + end1_v = spu_gather(spu_cmpeq(data1, 0)); + end2_v = spu_gather(spu_cmpeq(data2, 0)); + *end_v = spu_or(end1_v, end2_v); + } + + n_v = spu_add(n_v, -16); + + /* + * Repeat until either + * 1) the character count expired + * 2) check_zeroes is set and a null character is discovered in one of + * the input strings + * 3) the strings do not compare equal + */ + if (check_zeroes) { + tmp1_v = spu_or(*end_v, neq_v); + tmp1_v = spu_cmpeq(tmp1_v, 0); + } else { + tmp1_v = spu_cmpeq(neq_v, 0); + } + tmp1_v = spu_and(tmp1_v, spu_cmpgt(n_v, 0)); + } while (spu_extract(tmp1_v, 0)); + + /* + * Construct a mask to eliminate characters that are not of interest + * in the comparison. + */ + mask_v = spu_splats((unsigned int)0xFFFF); + shift_n_v = + spu_andc((__vector unsigned int)spu_sub(0, n_v), spu_cmpgt(n_v, -1)); + if (check_zeroes) { + cnt1_v = spu_cntlz(end1_v); + cnt2_v = spu_cntlz(end2_v); + max_cnt_v = spu_sel(cnt1_v, cnt2_v, spu_cmpgt(cnt2_v, cnt1_v)); + shift_eos_v = spu_sub(32, max_cnt_v); + max_shift_v = + spu_sel(shift_n_v, shift_eos_v, spu_cmpgt(shift_eos_v, shift_n_v)); + mask_v = spu_and(spu_sl(mask_v, spu_extract(max_shift_v, 0)), mask_v); + } else { + mask_v = spu_and(spu_sl(mask_v, spu_extract(shift_n_v, 0)), mask_v); + } + + /* + * Determine if greater then or less then in the case that they are + * not equal. gt_v is either 1 (in the case s1 is greater then s2), or + * -1 (in the case that s2 is greater then s1). + * + * There are no byte vector math instructions, so we can't subtract + * data1 from data2. + */ + gt_v = spu_gather(spu_cmpgt(data1, data2)); + lt_v = spu_gather(spu_cmpgt(data2, data1)); + gt_v = spu_sub(-1, spu_sl(spu_cmpgt(gt_v, lt_v), 1)); + + /* + * Construct a mask to be applied to gt_v if the strings are discovered + * to be equal. + */ + mask_v = spu_cmpeq(spu_and(neq_v, mask_v), 0); + return (spu_extract(spu_andc(gt_v, mask_v), 0)); +} |