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 /* Locates the first occurance in string pointed to by s1 of
38 * any character in the string pointed to by s2. A null pointer
39 * is returned if no character in s2 occurs in s1.
40 */
strpbrk(const char * s1,const char * s2)41 char * strpbrk(const char *s1, const char *s2)
42 {
43 unsigned int offset;
44 vec_uint4 cnt;
45 vec_uchar16 shuffle, match, initial_splat, splat, eos;
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 dataA = *ptr1++;
55 dataB = *ptr1++;
56
57 initial_splat = spu_splats((unsigned char)((unsigned int)(s2) & 0xF));
58
59 /* For each quadword of the string s1.
60 */
61 do {
62 data1 = spu_shuffle(dataA, dataB, shuffle);
63
64 eos = match = spu_cmpeq(data1, 0);
65
66 ptr2 = (vec_uchar16 *)s2;
67 data2 = *ptr2;
68 data2 = spu_shuffle(data2, data2, initial_splat);
69 ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
70 splat = initial_splat;
71
72 /* For each character of s2, compare agains a quadword of s1,
73 * accumulating match success in the variable match.
74 */
75 while (spu_extract((vec_uint4)data2, 0)) {
76 match = spu_or(match, spu_cmpeq(data1, data2));
77
78 splat = spu_and((vec_uchar16)(spu_add((vec_uint4)splat, VEC_SPLAT_U32(0x01010101))), 0xF);
79
80 data2 = *ptr2;
81 data2 = spu_shuffle(data2, data2, splat);
82 ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
83 }
84
85 cnt = spu_cntlz(spu_gather(match));
86
87 dataA = dataB;
88 dataB = *ptr1++;
89 } while (spu_extract(cnt, 0) == 32);
90
91 /* Compute the first match pointer, zeroing it (NIL) if it is the end of
92 * string.
93 */
94 return ((char *)spu_extract(spu_andc(spu_add(spu_add(spu_promote((unsigned int)(ptr1), 0), -64), cnt),
95 spu_cmpeq(cnt, spu_cntlz(spu_gather(eos)))), 0));
96 }
97