source: trunk/libs/newlib/src/newlib/libc/machine/spu/strncmp.h

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

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

File size: 5.5 KB
Line 
1/*
2  (C) Copyright 2001,2006,2008
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/*
37 * Internal _strncmp_internal for strncmp and memcmp:
38 *
39 * Compare the two strings s1 and s2 of length n. Returns an integer less
40 * than, equal to, or greater than zero if s1 is found, respectively,
41 * to be less than, to match, or be greater than s2. Check for zero bytes
42 * only if check_zeroes is set. In additin, the vector value *end_v will
43 * contain all zeroes if we hit a NUL character that caused the comparison
44 * to complete, *end_v is modified only if check_zeroes is set.
45 */
46static inline int
47_strncmp_internal(const char *s1, const char *s2, size_t n, vec_uint4
48                  *end_v, int check_zeroes)
49{
50  unsigned int offset1, offset2;
51  vec_int4 n_v;
52  vec_uint4 cnt1_v, cnt2_v, max_cnt_v;
53  vec_uint4 gt_v, lt_v, mask_v, end1_v, end2_v, neq_v, tmp1_v;
54  vec_uint4 shift_n_v, shift_eos_v, max_shift_v;
55  vec_uchar16 shuffle1, shuffle2;
56  vec_uchar16 data1A, data1B, data1, data2A, data2B, data2;
57  vec_uchar16 *ptr1, *ptr2;
58
59  data1 = data2 = spu_splats((unsigned char)0);
60
61  ptr1 = (vec_uchar16 *)s1;
62  ptr2 = (vec_uchar16 *)s2;
63
64  offset1 = (unsigned int)(ptr1) & 15;
65  offset2 = (unsigned int)(ptr2) & 15;
66
67  shuffle1 = (vec_uchar16)
68    spu_add((vec_uint4)spu_splats((unsigned char)offset1),
69    ((vec_uint4) {0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F}));
70  shuffle2 = (vec_uchar16)
71    spu_add((vec_uint4)spu_splats((unsigned char)offset2),
72    ((vec_uint4) {0x00010203, 0x04050607, 0x08090A0B, 0x0C0D0E0F}));
73  data1A = *ptr1++;
74  data2A = *ptr2++;
75
76  n_v = spu_promote((int)n, 0);
77
78  do {
79    data1B = *ptr1++;
80    data2B = *ptr2++;
81
82    /*
83     * Quadword align each of the input strings so that we operate on full
84     * quadwords.
85     */
86    data1 = spu_shuffle(data1A, data1B, shuffle1);
87    data2 = spu_shuffle(data2A, data2B, shuffle2);
88
89    data1A = data1B;
90    data2A = data2B;
91
92    neq_v = spu_gather(spu_xor(spu_cmpeq(data1, data2), -1));
93
94    if (check_zeroes) {
95      end1_v = spu_gather(spu_cmpeq(data1, 0));
96      end2_v = spu_gather(spu_cmpeq(data2, 0));
97      *end_v = spu_or(end1_v, end2_v);
98    }
99
100    n_v = spu_add(n_v, -16);
101
102    /*
103     * Repeat until either
104     * 1) the character count expired
105     * 2) check_zeroes is set and a null character is discovered in one of
106     *    the input strings
107     * 3) the strings do not compare equal
108     */
109    if (check_zeroes) {
110      tmp1_v = spu_or(*end_v, neq_v);
111      tmp1_v = spu_cmpeq(tmp1_v, 0);
112    } else {
113      tmp1_v = spu_cmpeq(neq_v, 0);
114    }
115    tmp1_v = spu_and(tmp1_v, spu_cmpgt(n_v, 0));
116  } while (spu_extract(tmp1_v, 0));
117
118  /*
119   * Construct a mask to eliminate characters that are not of interest
120   * in the comparison.
121   */
122  mask_v = spu_splats((unsigned int)0xFFFF);
123  shift_n_v =
124          spu_andc((__vector unsigned int)spu_sub(0, n_v), spu_cmpgt(n_v, -1));
125  if (check_zeroes) {
126    cnt1_v = spu_cntlz(end1_v);
127    cnt2_v = spu_cntlz(end2_v);
128    max_cnt_v = spu_sel(cnt1_v, cnt2_v, spu_cmpgt(cnt2_v, cnt1_v));
129    shift_eos_v = spu_sub(32, max_cnt_v);
130    max_shift_v =
131      spu_sel(shift_n_v, shift_eos_v, spu_cmpgt(shift_eos_v, shift_n_v));
132    mask_v = spu_and(spu_sl(mask_v, spu_extract(max_shift_v, 0)), mask_v);
133  } else {
134    mask_v = spu_and(spu_sl(mask_v, spu_extract(shift_n_v, 0)), mask_v);
135  }
136
137  /*
138   * Determine if greater then or less then in the case that they are
139   * not equal. gt_v is either 1 (in the case s1 is greater then s2), or
140   * -1 (in the case that s2 is greater then s1).
141   *
142   * There are no byte vector math instructions, so we can't subtract
143   * data1 from data2.
144   */
145  gt_v = spu_gather(spu_cmpgt(data1, data2));
146  lt_v = spu_gather(spu_cmpgt(data2, data1));
147  gt_v = spu_sub(-1, spu_sl(spu_cmpgt(gt_v, lt_v), 1));
148
149  /*
150   * Construct a mask to be applied to gt_v if the strings are discovered
151   * to be equal.
152   */
153  mask_v = spu_cmpeq(spu_and(neq_v, mask_v), 0);
154  return (spu_extract(spu_andc(gt_v, mask_v), 0));
155}
Note: See TracBrowser for help on using the repository browser.