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
36 /* Scans the string pointed to by s for the character c and
37 * returns a pointer to the first occurance of c. If
38 * c is not found, then NULL is returned.
39 */
strchr(const char * s,int c)40 char *strchr(const char *s, int c)
41 {
42 unsigned int cmp, skip;
43 vec_uchar16 *ptr, data, vc;
44 vec_uint4 cmp_c, cmp_0;
45 vec_uint4 result;
46 vec_uint4 mask;
47 vec_uint4 one = spu_splats(0xffffU);
48 /* Scan memory array a quadword at a time. Skip leading
49 * mis-aligned bytes.
50 */
51 ptr = (vec_uchar16 *)s;
52
53 skip = (unsigned int)(ptr) & 15;
54 mask = spu_rlmask(one, -skip);
55
56 vc = spu_splats((unsigned char)(c));
57
58 data = *ptr++;
59
60 cmp_c = spu_and(spu_gather(spu_cmpeq(data, vc)), mask);
61 cmp_0 = spu_and(spu_gather(spu_cmpeq(data, 0)), mask);
62
63 cmp = spu_extract(spu_or(cmp_c, cmp_0), 0);
64
65 while (cmp == 0) {
66 data = *ptr++;
67 cmp_c = spu_gather(spu_cmpeq(data, vc));
68 cmp_0 = spu_gather(spu_cmpeq(data, 0));
69
70 cmp = spu_extract(spu_or(cmp_c, cmp_0), 0);
71 }
72
73 /* Compute the location of the first character. If it is beyond
74 * the end of the string, then return NULL.
75 */
76 result = spu_add(spu_promote((unsigned int)ptr - (skip+32), 0),
77 spu_cntlz(spu_promote(cmp, 0)));
78
79 result = spu_andc(result, spu_cmpgt(cmp_0, cmp_c));
80
81 return ((char *)spu_extract(result, 0));
82 }
83