source: trunk/libs/newlib/src/newlib/libc/machine/spu/strrchr.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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