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
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 #ifdef __HAVE_ARCH_STRLEN
47 size_t strlen(const char *s
)
49 return __strend(s
) - s
;
51 EXPORT_SYMBOL(strlen
);
55 * strnlen - Find the length of a length-limited string
56 * @s: The string to be sized
57 * @n: The maximum number of bytes to search
59 * returns the minimum of the length of @s and @n
61 #ifdef __HAVE_ARCH_STRNLEN
62 size_t strnlen(const char *s
, size_t n
)
64 return __strnend(s
, n
) - s
;
66 EXPORT_SYMBOL(strnlen
);
70 * strcpy - Copy a %NUL terminated string
71 * @dest: Where to copy the string to
72 * @src: Where to copy the string from
74 * returns a pointer to @dest
76 #ifdef __HAVE_ARCH_STRCPY
77 char *strcpy(char *dest
, const char *src
)
79 register int r0
asm("0") = 0;
82 asm volatile ("0: mvst %0,%1\n"
84 : "+&a" (dest
), "+&a" (src
) : "d" (r0
)
88 EXPORT_SYMBOL(strcpy
);
92 * strlcpy - Copy a %NUL terminated string into a sized buffer
93 * @dest: Where to copy the string to
94 * @src: Where to copy the string from
95 * @size: size of destination buffer
97 * Compatible with *BSD: the result is always a valid
98 * NUL-terminated string that fits in the buffer (unless,
99 * of course, the buffer size is zero). It does not pad
100 * out the result like strncpy() does.
102 #ifdef __HAVE_ARCH_STRLCPY
103 size_t strlcpy(char *dest
, const char *src
, size_t size
)
105 size_t ret
= __strend(src
) - src
;
108 size_t len
= (ret
>= size
) ? size
-1 : ret
;
110 memcpy(dest
, src
, len
);
114 EXPORT_SYMBOL(strlcpy
);
118 * strncpy - Copy a length-limited, %NUL-terminated string
119 * @dest: Where to copy the string to
120 * @src: Where to copy the string from
121 * @n: The maximum number of bytes to copy
123 * The result is not %NUL-terminated if the source exceeds
126 #ifdef __HAVE_ARCH_STRNCPY
127 char *strncpy(char *dest
, const char *src
, size_t n
)
129 size_t len
= __strnend(src
, n
) - src
;
130 memset(dest
+ len
, 0, n
- len
);
131 memcpy(dest
, src
, len
);
134 EXPORT_SYMBOL(strncpy
);
138 * strcat - Append one %NUL-terminated string to another
139 * @dest: The string to be appended to
140 * @src: The string to append to it
142 * returns a pointer to @dest
144 #ifdef __HAVE_ARCH_STRCAT
145 char *strcat(char *dest
, const char *src
)
147 register int r0
asm("0") = 0;
151 asm volatile ("0: srst %0,%1\n"
155 : "=&a" (dummy
), "+a" (dest
), "+a" (src
)
156 : "d" (r0
), "0" (0UL) : "cc", "memory" );
159 EXPORT_SYMBOL(strcat
);
163 * strlcat - Append a length-limited, %NUL-terminated string to another
164 * @dest: The string to be appended to
165 * @src: The string to append to it
166 * @n: The size of the destination buffer.
168 #ifdef __HAVE_ARCH_STRLCAT
169 size_t strlcat(char *dest
, const char *src
, size_t n
)
171 size_t dsize
= __strend(dest
) - dest
;
172 size_t len
= __strend(src
) - src
;
173 size_t res
= dsize
+ len
;
181 memcpy(dest
, src
, len
);
185 EXPORT_SYMBOL(strlcat
);
189 * strncat - Append a length-limited, %NUL-terminated string to another
190 * @dest: The string to be appended to
191 * @src: The string to append to it
192 * @n: The maximum numbers of bytes to copy
194 * returns a pointer to @dest
196 * Note that in contrast to strncpy, strncat ensures the result is
199 #ifdef __HAVE_ARCH_STRNCAT
200 char *strncat(char *dest
, const char *src
, size_t n
)
202 size_t len
= __strnend(src
, n
) - src
;
203 char *p
= __strend(dest
);
209 EXPORT_SYMBOL(strncat
);
213 * strcmp - Compare two strings
215 * @s2: Another string
217 * returns 0 if @s1 and @s2 are equal,
218 * < 0 if @s1 is less than @s2
219 * > 0 if @s1 is greater than @s2
221 #ifdef __HAVE_ARCH_STRCMP
222 int strcmp(const char *s1
, const char *s2
)
224 register int r0
asm("0") = 0;
227 asm volatile ("0: clst %2,%3\n"
234 : "+d" (ret
), "+d" (r0
), "+a" (s1
), "+a" (s2
)
238 EXPORT_SYMBOL(strcmp
);
242 * strrchr - Find the last occurrence of a character in a string
243 * @s: The string to be searched
244 * @c: The character to search for
246 #ifdef __HAVE_ARCH_STRRCHR
247 char *strrchr(const char *s
, int c
)
249 size_t len
= __strend(s
) - s
;
253 if (s
[len
] == (char) c
)
254 return (char *) s
+ len
;
258 EXPORT_SYMBOL(strrchr
);
261 static inline int clcle(const char *s1
, unsigned long l1
,
262 const char *s2
, unsigned long l2
)
264 register unsigned long r2
asm("2") = (unsigned long) s1
;
265 register unsigned long r3
asm("3") = (unsigned long) l1
;
266 register unsigned long r4
asm("4") = (unsigned long) s2
;
267 register unsigned long r5
asm("5") = (unsigned long) l2
;
270 asm volatile ("0: clcle %1,%3,0\n"
274 : "=&d" (cc
), "+a" (r2
), "+a" (r3
),
275 "+a" (r4
), "+a" (r5
) : : "cc", "memory");
280 * strstr - Find the first substring in a %NUL terminated string
281 * @s1: The string to be searched
282 * @s2: The string to search for
284 #ifdef __HAVE_ARCH_STRSTR
285 char *strstr(const char *s1
, const char *s2
)
289 l2
= __strend(s2
) - s2
;
292 l1
= __strend(s1
) - s1
;
296 cc
= clcle(s1
, l2
, s2
, l2
);
303 EXPORT_SYMBOL(strstr
);
307 * memchr - Find a character in an area of memory.
308 * @s: The memory area
309 * @c: The byte to search for
310 * @n: The size of the area.
312 * returns the address of the first occurrence of @c, or %NULL
315 #ifdef __HAVE_ARCH_MEMCHR
316 void *memchr(const void *s
, int c
, size_t n
)
318 register int r0
asm("0") = (char) c
;
319 const void *ret
= s
+ n
;
321 asm volatile ("0: srst %0,%1\n"
326 : "+a" (ret
), "+&a" (s
) : "d" (r0
) : "cc", "memory");
329 EXPORT_SYMBOL(memchr
);
333 * memcmp - Compare two areas of memory
334 * @s1: One area of memory
335 * @s2: Another area of memory
336 * @n: The size of the area.
338 #ifdef __HAVE_ARCH_MEMCMP
339 int memcmp(const void *s1
, const void *s2
, size_t n
)
343 ret
= clcle(s1
, n
, s2
, n
);
345 ret
= ret
== 1 ? -1 : 1;
348 EXPORT_SYMBOL(memcmp
);
352 * memscan - Find a character in an area of memory.
353 * @s: The memory area
354 * @c: The byte to search for
355 * @n: The size of the area.
357 * returns the address of the first occurrence of @c, or 1 byte past
358 * the area if @c is not found
360 #ifdef __HAVE_ARCH_MEMSCAN
361 void *memscan(void *s
, int c
, size_t n
)
363 register int r0
asm("0") = (char) c
;
364 const void *ret
= s
+ n
;
366 asm volatile ("0: srst %0,%1\n"
368 : "+a" (ret
), "+&a" (s
) : "d" (r0
) : "cc", "memory");
371 EXPORT_SYMBOL(memscan
);