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 not contained in the string pointed to by s2.
40 */
strcspn(const char * s1,const char * s2)41 size_t strcspn(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
67 ptr2 = (vec_uchar16 *)s2;
68 data2 = *ptr2;
69 data2 = spu_shuffle(data2, data2, initial_splat);
70 ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
71 splat = initial_splat;
72
73 match = spu_cmpeq(data1, 0);
74
75 /* For each character of s2, compare agains a quadword of s1,
76 * accumulating match success in the variable match.
77 */
78 while (spu_extract((vec_uint4)data2, 0)) {
79 match = spu_or(match, spu_cmpeq(data1, data2));
80
81 splat = spu_and((vec_uchar16)(spu_add((vec_uint4)splat, VEC_SPLAT_U32(0x01010101))), 0xF);
82
83 data2 = *ptr2;
84 data2 = spu_shuffle(data2, data2, splat);
85 ptr2 = (vec_uchar16 *)((unsigned int)(ptr2) + 1);
86 }
87
88 cnt = spu_extract(spu_cntlz(spu_gather(match)), 0);
89 len = (len - 16) + cnt;
90
91 dataA = dataB;
92 dataB = *ptr1++;
93 } while (cnt == 32);
94
95 return (len);
96 }
97