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 <picolibc.h>
34 
35 #include <spu_intrinsics.h>
36 #include <stddef.h>
37 #include "vec_literal.h"
38 
39 /* Scans the string pointed to by s for the character c and
40  * returns a pointer to the last occurance of c. If
41  * c is not found, then NULL is returned.
42  */
strrchr(const char * s,int c)43 char * strrchr(const char *s, int c)
44 {
45   int nskip;
46   vec_uchar16 *ptr, data, vc;
47   vec_uint4 cmp_c, cmp_0, cmp;
48   vec_uint4 res_ptr, res_cmp;
49   vec_uint4 mask, result;
50   vec_uint4 one = spu_splats(0xffffU);
51   /* Scan memory array a quadword at a time. Skip leading
52    * mis-aligned bytes.
53    */
54   ptr = (vec_uchar16 *)s;
55 
56   nskip = -((unsigned int)(ptr) & 15);
57   mask = spu_rlmask(one, nskip);
58 
59   vc = spu_splats((unsigned char)(c));
60 
61   data = *ptr++;
62   ptr = (vec_uchar16 *)((unsigned int)ptr & ~15);
63 
64   cmp_c = spu_and(spu_gather(spu_cmpeq(data, vc)), mask);
65   cmp_0 = spu_and(spu_gather(spu_cmpeq(data, 0)), mask);
66 
67   res_ptr = spu_splats(0U);
68   res_cmp = spu_splats(0U);
69 
70   while (spu_extract(cmp_0, 0) == 0) {
71     cmp = spu_cmpeq(cmp_c, 0);
72 
73     res_ptr = spu_sel(spu_promote((unsigned int)(ptr), 0), res_ptr, cmp);
74     res_cmp = spu_sel(cmp_c, res_cmp, cmp);
75 
76     data = *ptr++;
77 
78     cmp_c = spu_gather(spu_cmpeq(data, vc));
79     cmp_0 = spu_gather(spu_cmpeq(data, 0));
80 
81     cmp = spu_cmpeq(cmp_c, 0);
82   }
83 
84   /* Compute the location of the last character before termination
85    * character.
86    *
87    * First mask off compare results following the first termination character.
88    */
89   mask = spu_sl(one, 31 - spu_extract(spu_cntlz(cmp_0), 0));
90   cmp_c = spu_and(cmp_c, mask);
91 
92   /* Conditionally update res_ptr and res_cmd if a match was found in the last
93    * quadword.
94    */
95   cmp = spu_cmpeq(cmp_c, 0);
96 
97   res_ptr = spu_sel(spu_promote((unsigned int)(ptr), 0), res_ptr, cmp);
98   res_cmp = spu_sel(cmp_c, res_cmp, cmp);
99 
100   /* Bit reserve res_cmp for locating last occurance.
101    */
102   mask = spu_cmpeq(res_cmp, 0);
103 
104   res_cmp = (vec_uint4)spu_maskb(spu_extract(res_cmp, 0));
105   res_cmp = spu_gather((vec_uchar16)spu_shuffle(res_cmp, res_cmp,
106 						VEC_LITERAL(vec_uchar16,
107 							    15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)));
108 
109   /* Compute the location (ptr) of the last occurance of c. If no
110    * occurance was found (ie, element 0 of res_cmp == 0, then return
111    * NULL.
112    */
113   result = spu_sub(spu_add(res_ptr, 15), spu_cntlz(res_cmp));
114   result = spu_andc(result, mask);
115 
116   return ((char *)spu_extract(result, 0));
117 }
118