1 // SPDX-License-Identifier: GPL-2.0
3 * Optimized string functions
6 * Copyright IBM Corp. 2004
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #define IN_ARCH_STRING_C 1
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/export.h>
20 * Helper functions to find the end of a string
22 static inline char *__strend(const char *s)
30 : [e] "+&a" (e), [s] "+&a" (s)
32 : "cc", "memory", "0");
36 static inline char *__strnend(const char *s, size_t n)
38 const char *p = s + n;
44 : [p] "+&d" (p), [s] "+&a" (s)
46 : "cc", "memory", "0");
51 * strlen - Find the length of a string
52 * @s: The string to be sized
54 * returns the length of @s
56 #ifdef __HAVE_ARCH_STRLEN
57 size_t strlen(const char *s)
59 return __strend(s) - s;
61 EXPORT_SYMBOL(strlen);
65 * strnlen - Find the length of a length-limited string
66 * @s: The string to be sized
67 * @n: The maximum number of bytes to search
69 * returns the minimum of the length of @s and @n
71 #ifdef __HAVE_ARCH_STRNLEN
72 size_t strnlen(const char *s, size_t n)
74 return __strnend(s, n) - s;
76 EXPORT_SYMBOL(strnlen);
80 * strcpy - Copy a %NUL terminated string
81 * @dest: Where to copy the string to
82 * @src: Where to copy the string from
84 * returns a pointer to @dest
86 #ifdef __HAVE_ARCH_STRCPY
87 char *strcpy(char *dest, const char *src)
93 "0: mvst %[dest],%[src]\n"
95 : [dest] "+&a" (dest), [src] "+&a" (src)
97 : "cc", "memory", "0");
100 EXPORT_SYMBOL(strcpy);
104 * strncpy - Copy a length-limited, %NUL-terminated string
105 * @dest: Where to copy the string to
106 * @src: Where to copy the string from
107 * @n: The maximum number of bytes to copy
109 * The result is not %NUL-terminated if the source exceeds
112 #ifdef __HAVE_ARCH_STRNCPY
113 char *strncpy(char *dest, const char *src, size_t n)
115 size_t len = __strnend(src, n) - src;
116 memset(dest + len, 0, n - len);
117 memcpy(dest, src, len);
120 EXPORT_SYMBOL(strncpy);
124 * strcat - Append one %NUL-terminated string to another
125 * @dest: The string to be appended to
126 * @src: The string to append to it
128 * returns a pointer to @dest
130 #ifdef __HAVE_ARCH_STRCAT
131 char *strcat(char *dest, const char *src)
133 unsigned long dummy = 0;
138 "0: srst %[dummy],%[dest]\n"
140 "1: mvst %[dummy],%[src]\n"
142 : [dummy] "+&a" (dummy), [dest] "+&a" (dest), [src] "+&a" (src)
144 : "cc", "memory", "0");
147 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 #ifdef __HAVE_ARCH_STRLCAT
157 size_t strlcat(char *dest, const char *src, size_t n)
159 size_t dsize = __strend(dest) - dest;
160 size_t len = __strend(src) - src;
161 size_t res = dsize + len;
169 memcpy(dest, src, len);
173 EXPORT_SYMBOL(strlcat);
177 * strncat - Append a length-limited, %NUL-terminated string to another
178 * @dest: The string to be appended to
179 * @src: The string to append to it
180 * @n: The maximum numbers of bytes to copy
182 * returns a pointer to @dest
184 * Note that in contrast to strncpy, strncat ensures the result is
187 #ifdef __HAVE_ARCH_STRNCAT
188 char *strncat(char *dest, const char *src, size_t n)
190 size_t len = __strnend(src, n) - src;
191 char *p = __strend(dest);
197 EXPORT_SYMBOL(strncat);
201 * strcmp - Compare two strings
203 * @s2: Another string
205 * returns 0 if @s1 and @s2 are equal,
206 * < 0 if @s1 is less than @s2
207 * > 0 if @s1 is greater than @s2
209 #ifdef __HAVE_ARCH_STRCMP
210 int strcmp(const char *s1, const char *s2)
216 "0: clst %[s1],%[s2]\n"
219 " ic %[ret],0(%[s1])\n"
223 : [ret] "+&d" (ret), [s1] "+&a" (s1), [s2] "+&a" (s2)
225 : "cc", "memory", "0");
228 EXPORT_SYMBOL(strcmp);
231 static inline int clcle(const char *s1, unsigned long l1,
232 const char *s2, unsigned long l2)
234 union register_pair r1 = { .even = (unsigned long)s1, .odd = l1, };
235 union register_pair r3 = { .even = (unsigned long)s2, .odd = l2, };
239 "0: clcle %[r1],%[r3],0\n"
243 : [cc] "=&d" (cc), [r1] "+&d" (r1.pair), [r3] "+&d" (r3.pair)
250 * strstr - Find the first substring in a %NUL terminated string
251 * @s1: The string to be searched
252 * @s2: The string to search for
254 #ifdef __HAVE_ARCH_STRSTR
255 char *strstr(const char *s1, const char *s2)
259 l2 = __strend(s2) - s2;
262 l1 = __strend(s1) - s1;
266 cc = clcle(s1, l2, s2, l2);
273 EXPORT_SYMBOL(strstr);
277 * memchr - Find a character in an area of memory.
278 * @s: The memory area
279 * @c: The byte to search for
280 * @n: The size of the area.
282 * returns the address of the first occurrence of @c, or %NULL
285 #ifdef __HAVE_ARCH_MEMCHR
286 void *memchr(const void *s, int c, size_t n)
288 const void *ret = s + n;
292 "0: srst %[ret],%[s]\n"
297 : [ret] "+&a" (ret), [s] "+&a" (s)
299 : "cc", "memory", "0");
302 EXPORT_SYMBOL(memchr);
306 * memcmp - Compare two areas of memory
307 * @s1: One area of memory
308 * @s2: Another area of memory
309 * @n: The size of the area.
311 #ifdef __HAVE_ARCH_MEMCMP
312 int memcmp(const void *s1, const void *s2, size_t n)
316 ret = clcle(s1, n, s2, n);
318 ret = ret == 1 ? -1 : 1;
321 EXPORT_SYMBOL(memcmp);
325 * memscan - Find a character in an area of memory.
326 * @s: The memory area
327 * @c: The byte to search for
328 * @n: The size of the area.
330 * returns the address of the first occurrence of @c, or 1 byte past
331 * the area if @c is not found
333 #ifdef __HAVE_ARCH_MEMSCAN
334 void *memscan(void *s, int c, size_t n)
336 const void *ret = s + n;
340 "0: srst %[ret],%[s]\n"
342 : [ret] "+&a" (ret), [s] "+&a" (s)
344 : "cc", "memory", "0");
347 EXPORT_SYMBOL(memscan);