2 * Optimized string functions
5 * Copyright IBM Corp. 2004
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
9 #define IN_ARCH_STRING_C 1
11 #include <linux/types.h>
12 #include <linux/module.h>
15 * Helper functions to find the end of a string
17 static inline char *__strend(const char *s)
19 register unsigned long r0 asm("0") = 0;
21 asm volatile ("0: srst %0,%1\n"
23 : "+d" (r0), "+a" (s) : : "cc" );
27 static inline char *__strnend(const char *s, size_t n)
29 register unsigned long r0 asm("0") = 0;
30 const char *p = s + n;
32 asm volatile ("0: srst %0,%1\n"
34 : "+d" (p), "+a" (s) : "d" (r0) : "cc" );
39 * strlen - Find the length of a string
40 * @s: The string to be sized
42 * returns the length of @s
44 size_t strlen(const char *s)
46 return __strend(s) - s;
48 EXPORT_SYMBOL(strlen);
51 * strnlen - Find the length of a length-limited string
52 * @s: The string to be sized
53 * @n: The maximum number of bytes to search
55 * returns the minimum of the length of @s and @n
57 size_t strnlen(const char * s, size_t n)
59 return __strnend(s, n) - s;
61 EXPORT_SYMBOL(strnlen);
64 * strcpy - Copy a %NUL terminated string
65 * @dest: Where to copy the string to
66 * @src: Where to copy the string from
68 * returns a pointer to @dest
70 char *strcpy(char *dest, const char *src)
72 register int r0 asm("0") = 0;
75 asm volatile ("0: mvst %0,%1\n"
77 : "+&a" (dest), "+&a" (src) : "d" (r0)
81 EXPORT_SYMBOL(strcpy);
84 * strlcpy - Copy a %NUL terminated string into a sized buffer
85 * @dest: Where to copy the string to
86 * @src: Where to copy the string from
87 * @size: size of destination buffer
89 * Compatible with *BSD: the result is always a valid
90 * NUL-terminated string that fits in the buffer (unless,
91 * of course, the buffer size is zero). It does not pad
92 * out the result like strncpy() does.
94 size_t strlcpy(char *dest, const char *src, size_t size)
96 size_t ret = __strend(src) - src;
99 size_t len = (ret >= size) ? size-1 : ret;
101 memcpy(dest, src, len);
105 EXPORT_SYMBOL(strlcpy);
108 * strncpy - Copy a length-limited, %NUL-terminated string
109 * @dest: Where to copy the string to
110 * @src: Where to copy the string from
111 * @n: The maximum number of bytes to copy
113 * The result is not %NUL-terminated if the source exceeds
116 char *strncpy(char *dest, const char *src, size_t n)
118 size_t len = __strnend(src, n) - src;
119 memset(dest + len, 0, n - len);
120 memcpy(dest, src, len);
123 EXPORT_SYMBOL(strncpy);
126 * strcat - Append one %NUL-terminated string to another
127 * @dest: The string to be appended to
128 * @src: The string to append to it
130 * returns a pointer to @dest
132 char *strcat(char *dest, const char *src)
134 register int r0 asm("0") = 0;
138 asm volatile ("0: srst %0,%1\n"
142 : "=&a" (dummy), "+a" (dest), "+a" (src)
143 : "d" (r0), "0" (0UL) : "cc", "memory" );
146 EXPORT_SYMBOL(strcat);
149 * strlcat - Append a length-limited, %NUL-terminated string to another
150 * @dest: The string to be appended to
151 * @src: The string to append to it
152 * @n: The size of the destination buffer.
154 size_t strlcat(char *dest, const char *src, size_t n)
156 size_t dsize = __strend(dest) - dest;
157 size_t len = __strend(src) - src;
158 size_t res = dsize + len;
166 memcpy(dest, src, len);
170 EXPORT_SYMBOL(strlcat);
173 * strncat - Append a length-limited, %NUL-terminated string to another
174 * @dest: The string to be appended to
175 * @src: The string to append to it
176 * @n: The maximum numbers of bytes to copy
178 * returns a pointer to @dest
180 * Note that in contrast to strncpy, strncat ensures the result is
183 char *strncat(char *dest, const char *src, size_t n)
185 size_t len = __strnend(src, n) - src;
186 char *p = __strend(dest);
192 EXPORT_SYMBOL(strncat);
195 * strcmp - Compare two strings
197 * @ct: Another string
199 * returns 0 if @cs and @ct are equal,
200 * < 0 if @cs is less than @ct
201 * > 0 if @cs is greater than @ct
203 int strcmp(const char *cs, const char *ct)
205 register int r0 asm("0") = 0;
208 asm volatile ("0: clst %2,%3\n"
215 : "+d" (ret), "+d" (r0), "+a" (cs), "+a" (ct)
219 EXPORT_SYMBOL(strcmp);
222 * strrchr - Find the last occurrence of a character in a string
223 * @s: The string to be searched
224 * @c: The character to search for
226 char * strrchr(const char * s, int c)
228 ssize_t len = __strend(s) - s;
231 if (s[len] == (char)c)
232 return (char *)s + len;
233 } while (--len >= 0);
236 EXPORT_SYMBOL(strrchr);
238 static inline int clcle(const char *s1, unsigned long l1,
239 const char *s2, unsigned long l2)
241 register unsigned long r2 asm("2") = (unsigned long) s1;
242 register unsigned long r3 asm("3") = (unsigned long) l1;
243 register unsigned long r4 asm("4") = (unsigned long) s2;
244 register unsigned long r5 asm("5") = (unsigned long) l2;
247 asm volatile ("0: clcle %1,%3,0\n"
251 : "=&d" (cc), "+a" (r2), "+a" (r3),
252 "+a" (r4), "+a" (r5) : : "cc");
257 * strstr - Find the first substring in a %NUL terminated string
258 * @s1: The string to be searched
259 * @s2: The string to search for
261 char * strstr(const char * s1,const char * s2)
265 l2 = __strend(s2) - s2;
268 l1 = __strend(s1) - s1;
272 cc = clcle(s1, l2, s2, l2);
279 EXPORT_SYMBOL(strstr);
282 * memchr - Find a character in an area of memory.
283 * @s: The memory area
284 * @c: The byte to search for
285 * @n: The size of the area.
287 * returns the address of the first occurrence of @c, or %NULL
290 void *memchr(const void *s, int c, size_t n)
292 register int r0 asm("0") = (char) c;
293 const void *ret = s + n;
295 asm volatile ("0: srst %0,%1\n"
300 : "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );
303 EXPORT_SYMBOL(memchr);
306 * memcmp - Compare two areas of memory
307 * @cs: One area of memory
308 * @ct: Another area of memory
309 * @count: The size of the area.
311 int memcmp(const void *cs, const void *ct, size_t n)
315 ret = clcle(cs, n, ct, n);
317 ret = ret == 1 ? -1 : 1;
320 EXPORT_SYMBOL(memcmp);
323 * memscan - Find a character in an area of memory.
324 * @s: The memory area
325 * @c: The byte to search for
326 * @n: The size of the area.
328 * returns the address of the first occurrence of @c, or 1 byte past
329 * the area if @c is not found
331 void *memscan(void *s, int c, size_t n)
333 register int r0 asm("0") = (char) c;
334 const void *ret = s + n;
336 asm volatile ("0: srst %0,%1\n"
338 : "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );
341 EXPORT_SYMBOL(memscan);