1 /*
2   (C) Copyright 2001,2006,
3   International Business Machines Corporation,
4   Sony Computer Entertainment, Incorporated,
5   Toshiba Corporation,
6 
7   All rights reserved.
8 
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice,
13   this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17     * Neither the names of the copyright holders nor the names of their
18   contributors may be used to endorse or promote products derived from this
19   software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31   POSSIBILITY OF SUCH DAMAGE.
32 */
33 #include <spu_intrinsics.h>
34 #include <stddef.h>
35 #include "vec_literal.h"
36 
37 /* Computes the length of the maximum initial segement
38  * of the string pointed to by s1 which consists entirely
39  * of characters from the string pointed to by s2.
40  */
strspn(const char * s1,const char * s2)41 size_t strspn(const char *s1, const char *s2)
42 {
43   size_t len, cnt;
44   unsigned int offset;
45   vec_uchar16 shuffle, match, initial_splat, splat;
46   vec_uchar16 data1, data2, dataA, dataB, *ptr1, *ptr2;
47 
48   ptr1 = (vec_uchar16 *)s1;
49 
50   offset = (unsigned int)(s1) & 15;
51   shuffle = (vec_uchar16)spu_add((vec_uint4)spu_splats((unsigned char) offset),
52 				 VEC_LITERAL(vec_uint4, 0x0010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F));
53 
54   len = 0;
55 
56   dataA = *ptr1++;
57   dataB = *ptr1++;
58 
59   initial_splat = spu_splats((unsigned char)((unsigned int)(s2) & 0xF));
60 
61   /* For each quadword of the string s1.
62    */
63   do {
64     data1 = spu_shuffle(dataA, dataB, shuffle);
65 
66     match = spu_splats((unsigned char)0);
67 
68     ptr2 = (vec_uchar16 *)s2;
69     data2 = *ptr2;
70     data2 = spu_shuffle(data2, data2, initial_splat);
71     ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
72     splat = initial_splat;
73 
74     /* For each character of s2, compare agains a quadword of s1,
75      * accumulating match success in the variable match.
76      */
77     while (spu_extract((vec_uint4)data2, 0)) {
78       match = spu_or(match, spu_cmpeq(data1, data2));
79 
80       splat = spu_and((vec_uchar16)(spu_add((vec_uint4)splat, VEC_SPLAT_U32(0x01010101))), 0xF);
81 
82       data2 = *ptr2;
83       data2 = spu_shuffle(data2, data2, splat);
84       ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
85     }
86 
87     cnt = spu_extract(spu_cntlz(spu_gather(spu_xor(match, -1))), 0);
88     len = (len - 16) + cnt;
89 
90     dataA = dataB;
91     dataB = *ptr1++;
92   } while (cnt == 32);
93 
94   return (len);
95 }
96