1 // SPDX-License-Identifier: GPL-2.0
3 * Optimized string functions
6 * Copyright IBM Corp. 2004
10 #define IN_ARCH_STRING_C 1
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/export.h>
17 * Helper functions to find the end of a string
19 static inline char *__strend(const char *s)
21 register unsigned long r0 asm("0") = 0;
23 asm volatile ("0: srst %0,%1\n"
25 : "+d" (r0), "+a" (s) : : "cc", "memory");
29 static inline char *__strnend(const char *s, size_t n)
31 register unsigned long r0 asm("0") = 0;
32 const char *p = s + n;
34 asm volatile ("0: srst %0,%1\n"
36 : "+d" (p), "+a" (s) : "d" (r0) : "cc", "memory");
41 * strlen - Find the length of a string
42 * @s: The string to be sized
44 * returns the length of @s
46 size_t strlen(const char *s)
48 return __strend(s) - s;
50 EXPORT_SYMBOL(strlen);
53 * strnlen - Find the length of a length-limited string
54 * @s: The string to be sized
55 * @n: The maximum number of bytes to search
57 * returns the minimum of the length of @s and @n
59 size_t strnlen(const char * s, size_t n)
61 return __strnend(s, n) - s;
63 EXPORT_SYMBOL(strnlen);
66 * strcpy - Copy a %NUL terminated string
67 * @dest: Where to copy the string to
68 * @src: Where to copy the string from
70 * returns a pointer to @dest
72 char *strcpy(char *dest, const char *src)
74 register int r0 asm("0") = 0;
77 asm volatile ("0: mvst %0,%1\n"
79 : "+&a" (dest), "+&a" (src) : "d" (r0)
83 EXPORT_SYMBOL(strcpy);
86 * strlcpy - Copy a %NUL terminated string into a sized buffer
87 * @dest: Where to copy the string to
88 * @src: Where to copy the string from
89 * @size: size of destination buffer
91 * Compatible with *BSD: the result is always a valid
92 * NUL-terminated string that fits in the buffer (unless,
93 * of course, the buffer size is zero). It does not pad
94 * out the result like strncpy() does.
96 size_t strlcpy(char *dest, const char *src, size_t size)
98 size_t ret = __strend(src) - src;
101 size_t len = (ret >= size) ? size-1 : ret;
103 memcpy(dest, src, len);
107 EXPORT_SYMBOL(strlcpy);
110 * strncpy - Copy a length-limited, %NUL-terminated string
111 * @dest: Where to copy the string to
112 * @src: Where to copy the string from
113 * @n: The maximum number of bytes to copy
115 * The result is not %NUL-terminated if the source exceeds
118 char *strncpy(char *dest, const char *src, size_t n)
120 size_t len = __strnend(src, n) - src;
121 memset(dest + len, 0, n - len);
122 memcpy(dest, src, len);
125 EXPORT_SYMBOL(strncpy);
128 * strcat - Append one %NUL-terminated string to another
129 * @dest: The string to be appended to
130 * @src: The string to append to it
132 * returns a pointer to @dest
134 char *strcat(char *dest, const char *src)
136 register int r0 asm("0") = 0;
140 asm volatile ("0: srst %0,%1\n"
144 : "=&a" (dummy), "+a" (dest), "+a" (src)
145 : "d" (r0), "0" (0UL) : "cc", "memory" );
148 EXPORT_SYMBOL(strcat);
151 * strlcat - Append a length-limited, %NUL-terminated string to another
152 * @dest: The string to be appended to
153 * @src: The string to append to it
154 * @n: The size of the destination buffer.
156 size_t strlcat(char *dest, const char *src, size_t n)
158 size_t dsize = __strend(dest) - dest;
159 size_t len = __strend(src) - src;
160 size_t res = dsize + len;
168 memcpy(dest, src, len);
172 EXPORT_SYMBOL(strlcat);
175 * strncat - Append a length-limited, %NUL-terminated string to another
176 * @dest: The string to be appended to
177 * @src: The string to append to it
178 * @n: The maximum numbers of bytes to copy
180 * returns a pointer to @dest
182 * Note that in contrast to strncpy, strncat ensures the result is
185 char *strncat(char *dest, const char *src, size_t n)
187 size_t len = __strnend(src, n) - src;
188 char *p = __strend(dest);
194 EXPORT_SYMBOL(strncat);
197 * strcmp - Compare two strings
199 * @ct: Another string
201 * returns 0 if @cs and @ct are equal,
202 * < 0 if @cs is less than @ct
203 * > 0 if @cs is greater than @ct
205 int strcmp(const char *cs, const char *ct)
207 register int r0 asm("0") = 0;
210 asm volatile ("0: clst %2,%3\n"
217 : "+d" (ret), "+d" (r0), "+a" (cs), "+a" (ct)
221 EXPORT_SYMBOL(strcmp);
224 * strrchr - Find the last occurrence of a character in a string
225 * @s: The string to be searched
226 * @c: The character to search for
228 char * strrchr(const char * s, int c)
230 size_t len = __strend(s) - s;
234 if (s[len] == (char) c)
235 return (char *) s + len;
239 EXPORT_SYMBOL(strrchr);
241 static inline int clcle(const char *s1, unsigned long l1,
242 const char *s2, unsigned long l2)
244 register unsigned long r2 asm("2") = (unsigned long) s1;
245 register unsigned long r3 asm("3") = (unsigned long) l1;
246 register unsigned long r4 asm("4") = (unsigned long) s2;
247 register unsigned long r5 asm("5") = (unsigned long) l2;
250 asm volatile ("0: clcle %1,%3,0\n"
254 : "=&d" (cc), "+a" (r2), "+a" (r3),
255 "+a" (r4), "+a" (r5) : : "cc", "memory");
260 * strstr - Find the first substring in a %NUL terminated string
261 * @s1: The string to be searched
262 * @s2: The string to search for
264 char * strstr(const char * s1,const char * s2)
268 l2 = __strend(s2) - s2;
271 l1 = __strend(s1) - s1;
275 cc = clcle(s1, l2, s2, l2);
282 EXPORT_SYMBOL(strstr);
285 * memchr - Find a character in an area of memory.
286 * @s: The memory area
287 * @c: The byte to search for
288 * @n: The size of the area.
290 * returns the address of the first occurrence of @c, or %NULL
293 void *memchr(const void *s, int c, size_t n)
295 register int r0 asm("0") = (char) c;
296 const void *ret = s + n;
298 asm volatile ("0: srst %0,%1\n"
303 : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
306 EXPORT_SYMBOL(memchr);
309 * memcmp - Compare two areas of memory
310 * @cs: One area of memory
311 * @ct: Another area of memory
312 * @count: The size of the area.
314 int memcmp(const void *cs, const void *ct, size_t n)
318 ret = clcle(cs, n, ct, n);
320 ret = ret == 1 ? -1 : 1;
323 EXPORT_SYMBOL(memcmp);
326 * memscan - Find a character in an area of memory.
327 * @s: The memory area
328 * @c: The byte to search for
329 * @n: The size of the area.
331 * returns the address of the first occurrence of @c, or 1 byte past
332 * the area if @c is not found
334 void *memscan(void *s, int c, size_t n)
336 register int r0 asm("0") = (char) c;
337 const void *ret = s + n;
339 asm volatile ("0: srst %0,%1\n"
341 : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
344 EXPORT_SYMBOL(memscan);