1 /* Copyright (c) 2009 Xilinx, Inc.  All rights reserved.
2 
3    Redistribution and use in source and binary forms, with or without
4    modification, are permitted provided that the following conditions are
5    met:
6 
7    1.  Redistributions source code must retain the above copyright notice,
8    this list of conditions and the following disclaimer.
9 
10    2.  Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 
14    3.  Neither the name of Xilinx nor the names of its contributors may be
15    used to endorse or promote products derived from this software without
16    specific prior written permission.
17 
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
19    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24    TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 
31 FUNCTION
32 	<<strcmp>>---character string compare
33 
34 INDEX
35 	strcmp
36 
37 SYNOPSIS
38 	#include <string.h>
39 	int strcmp(const char *<[a]>, const char *<[b]>);
40 
41 DESCRIPTION
42 	<<strcmp>> compares the string at <[a]> to
43 	the string at <[b]>.
44 
45 RETURNS
46 	If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
47 	<<strcmp>> returns a number greater than zero.  If the two
48 	strings match, <<strcmp>> returns zero.  If <<*<[a]>>>
49 	sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
50 	number less than zero.
51 
52 PORTABILITY
53 <<strcmp>> is ANSI C.
54 
55 <<strcmp>> requires no supporting OS subroutines.
56 
57 QUICKREF
58 	strcmp ansi pure
59 */
60 
61 #include <picolibc.h>
62 
63 #include <string.h>
64 #include <limits.h>
65 
66 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
67 #define UNALIGNED(X, Y) \
68   (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
69 
70 /* DETECTNULL returns nonzero if (long)X contains a NULL byte. */
71 #if LONG_MAX == 2147483647L
72 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
73 #else
74 #if LONG_MAX == 9223372036854775807L
75 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
76 #else
77 #error long int is not a 32bit or 64bit type.
78 #endif
79 #endif
80 
81 #ifndef DETECTNULL
82 #error long int is not a 32bit or 64bit byte
83 #endif
84 
85 int
strcmp(const char * s1,const char * s2)86 strcmp (const char *s1,
87 	const char *s2)
88 {
89 
90 #ifndef HAVE_HW_PCMP
91 
92 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
93   while (*s1 != '\0' && *s1 == *s2)
94     {
95       s1++;
96       s2++;
97     }
98 
99   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
100 #else
101   unsigned long *a1;
102   unsigned long *a2;
103 
104   /* If s1 or s2 are unaligned, then compare bytes. */
105   if (!UNALIGNED (s1, s2))
106     {
107       /* If s1 and s2 are word-aligned, compare them a word at a time. */
108       a1 = (unsigned long*)s1;
109       a2 = (unsigned long*)s2;
110       while (*a1 == *a2)
111         {
112           /* To get here, *a1 == *a2, thus if we find a null in *a1,
113 	     then the strings must be equal, so return zero.  */
114           if (DETECTNULL (*a1))
115 	    return 0;
116 
117           a1++;
118           a2++;
119         }
120 
121       /* A difference was detected in last few bytes of s1, so search bytewise */
122       s1 = (char*)a1;
123       s2 = (char*)a2;
124     }
125 
126   while (*s1 != '\0' && *s1 == *s2)
127     {
128       s1++;
129       s2++;
130     }
131   return (*(unsigned char *) s1) - (*(unsigned char *) s2);
132 #endif /* not PREFER_SIZE_OVER_SPEED */
133 
134 #else
135 
136 #include "mb_endian.h"
137 
138     __asm__ volatile ("                                          \n\
139         or      r9, r0, r0               /* Index register */\n\
140 check_alignment:                                             \n\
141         andi    r3, r5, 3                                    \n\
142         andi    r4, r6, 3                                    \n\
143         bnei    r3, try_align_args                           \n\
144         bnei    r4, regular_strcmp     /* At this point we don't have a choice */ \n\
145 cmp_loop:                                                                       \n"
146         LOAD4BYTES("r3", "r5", "r9")
147         LOAD4BYTES("r4", "r6", "r9")
148 "                                                                                      \n\
149         pcmpbf  r7, r3, r0              /* See if there is Null byte */                         \n\
150         bnei    r7, end_cmp_loop        /* IF yes (r7 > 0) use byte compares in end_cmp_loop */ \n\
151         cmpu    r7, r4, r3              /* ELSE compare whole word */                   \n\
152         bnei    r7, end_cmp                                                             \n\
153         brid    cmp_loop                                                                \n\
154         addik   r9, r9, 4               /* delay slot */                                \n\
155 end_cmp_loop:                                                                           \n\
156         lbu     r3, r5, r9              /* byte compare loop */                         \n\
157         lbu     r4, r6, r9                                                              \n\
158         cmpu    r7, r4, r3              /* Compare bytes */                             \n\
159         bnei    r7, end_cmp_early                                                       \n\
160         bneid   r3, end_cmp_loop        /* If reached null on one string, terminate */  \n\
161         addik   r9, r9, 1               /* delay slot */                        \n\
162 end_cmp_early:                                                                  \n\
163         rtsd    r15, 8                                                          \n\
164         or      r3, r0, r7                                                      \n\
165 try_align_args:                                                                 \n\
166         xor     r7, r4, r3                                                      \n\
167         bnei    r7, regular_strcmp      /* cannot align args */                 \n\
168         rsubik  r10, r3, 4              /* Number of initial bytes to align */  \n\
169 align_loop:                                                                     \n\
170         lbu     r3, r5, r9                                                      \n\
171         lbu     r4, r6, r9                                                      \n\
172         cmpu    r7, r4, r3                                                      \n\
173         bnei    r7, end_cmp                                                     \n\
174         beqi    r3, end_cmp                                                     \n\
175         addik   r10, r10, -1                                                    \n\
176         beqid   r10, cmp_loop                                                   \n\
177         addik   r9, r9, 1                                                       \n\
178         bri     align_loop                                                      \n\
179 regular_strcmp:                                                                 \n\
180         lbu     r3, r5, r9                                                      \n\
181         lbu     r4, r6, r9                                                      \n\
182         cmpu    r7, r4, r3                                                      \n\
183         bnei    r7, end_cmp                                                     \n\
184         beqi    r3, end_cmp                                                     \n\
185         brid    regular_strcmp                                                  \n\
186         addik   r9, r9, 1                                                       \n\
187 end_cmp:                                                                        \n\
188         rtsd    r15, 8                                                          \n\
189         or      r3, r0, r7              /* Return strcmp result */");
190 
191 #endif /* ! HAVE_HW_PCMP */
192 }
193 
194