]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/s390/lib/string.c
846ec64ab2c9f3a9d36fb5d70478b6acffaeface
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
)
47 return __strend(s
) - s
;
49 return __builtin_strlen(s
);
52 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 size_t strnlen(const char * s
, size_t n
)
63 return __strnend(s
, n
) - s
;
65 EXPORT_SYMBOL(strnlen
);
68 * strcpy - Copy a %NUL terminated string
69 * @dest: Where to copy the string to
70 * @src: Where to copy the string from
72 * returns a pointer to @dest
74 char *strcpy(char *dest
, const char *src
)
77 register int r0
asm("0") = 0;
80 asm volatile ("0: mvst %0,%1\n"
82 : "+&a" (dest
), "+&a" (src
) : "d" (r0
)
86 return __builtin_strcpy(dest
, src
);
89 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 size_t strlcpy(char *dest
, const char *src
, size_t size
)
104 size_t ret
= __strend(src
) - src
;
107 size_t len
= (ret
>= size
) ? size
-1 : ret
;
109 __builtin_memcpy(dest
, src
, len
);
113 EXPORT_SYMBOL(strlcpy
);
116 * strncpy - Copy a length-limited, %NUL-terminated string
117 * @dest: Where to copy the string to
118 * @src: Where to copy the string from
119 * @n: The maximum number of bytes to copy
121 * The result is not %NUL-terminated if the source exceeds
124 char *strncpy(char *dest
, const char *src
, size_t n
)
126 size_t len
= __strnend(src
, n
) - src
;
127 __builtin_memset(dest
+ len
, 0, n
- len
);
128 __builtin_memcpy(dest
, src
, len
);
131 EXPORT_SYMBOL(strncpy
);
134 * strcat - Append one %NUL-terminated string to another
135 * @dest: The string to be appended to
136 * @src: The string to append to it
138 * returns a pointer to @dest
140 char *strcat(char *dest
, const char *src
)
142 register int r0
asm("0") = 0;
146 asm volatile ("0: srst %0,%1\n"
150 : "=&a" (dummy
), "+a" (dest
), "+a" (src
)
151 : "d" (r0
), "0" (0UL) : "cc", "memory" );
154 EXPORT_SYMBOL(strcat
);
157 * strlcat - Append a length-limited, %NUL-terminated string to another
158 * @dest: The string to be appended to
159 * @src: The string to append to it
160 * @n: The size of the destination buffer.
162 size_t strlcat(char *dest
, const char *src
, size_t n
)
164 size_t dsize
= __strend(dest
) - dest
;
165 size_t len
= __strend(src
) - src
;
166 size_t res
= dsize
+ len
;
174 __builtin_memcpy(dest
, src
, len
);
178 EXPORT_SYMBOL(strlcat
);
181 * strncat - Append a length-limited, %NUL-terminated string to another
182 * @dest: The string to be appended to
183 * @src: The string to append to it
184 * @n: The maximum numbers of bytes to copy
186 * returns a pointer to @dest
188 * Note that in contrast to strncpy, strncat ensures the result is
191 char *strncat(char *dest
, const char *src
, size_t n
)
193 size_t len
= __strnend(src
, n
) - src
;
194 char *p
= __strend(dest
);
197 __builtin_memcpy(p
, src
, len
);
200 EXPORT_SYMBOL(strncat
);
203 * strcmp - Compare two strings
205 * @ct: Another string
207 * returns 0 if @cs and @ct are equal,
208 * < 0 if @cs is less than @ct
209 * > 0 if @cs is greater than @ct
211 int strcmp(const char *cs
, const char *ct
)
213 register int r0
asm("0") = 0;
216 asm volatile ("0: clst %2,%3\n"
223 : "+d" (ret
), "+d" (r0
), "+a" (cs
), "+a" (ct
)
227 EXPORT_SYMBOL(strcmp
);
230 * strrchr - Find the last occurrence of a character in a string
231 * @s: The string to be searched
232 * @c: The character to search for
234 char * strrchr(const char * s
, int c
)
236 size_t len
= __strend(s
) - s
;
240 if (s
[len
] == (char) c
)
241 return (char *) s
+ len
;
245 EXPORT_SYMBOL(strrchr
);
248 * strstr - Find the first substring in a %NUL terminated string
249 * @s1: The string to be searched
250 * @s2: The string to search for
252 char * strstr(const char * s1
,const char * s2
)
256 l2
= __strend(s2
) - s2
;
259 l1
= __strend(s1
) - s1
;
261 register unsigned long r2
asm("2") = (unsigned long) s1
;
262 register unsigned long r3
asm("3") = (unsigned long) l2
;
263 register unsigned long r4
asm("4") = (unsigned long) s2
;
264 register unsigned long r5
asm("5") = (unsigned long) l2
;
267 asm volatile ("0: clcle %1,%3,0\n"
271 : "=&d" (cc
), "+a" (r2
), "+a" (r3
),
272 "+a" (r4
), "+a" (r5
) : : "cc" );
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
)
313 register unsigned long r2
asm("2") = (unsigned long) cs
;
314 register unsigned long r3
asm("3") = (unsigned long) n
;
315 register unsigned long r4
asm("4") = (unsigned long) ct
;
316 register unsigned long r5
asm("5") = (unsigned long) n
;
319 asm volatile ("0: clcle %1,%3,0\n"
323 : "=&d" (ret
), "+a" (r2
), "+a" (r3
), "+a" (r4
), "+a" (r5
)
326 ret
= *(char *) r2
- *(char *) r4
;
329 EXPORT_SYMBOL(memcmp
);
332 * memscan - Find a character in an area of memory.
333 * @s: The memory area
334 * @c: The byte to search for
335 * @n: The size of the area.
337 * returns the address of the first occurrence of @c, or 1 byte past
338 * the area if @c is not found
340 void *memscan(void *s
, int c
, size_t n
)
342 register int r0
asm("0") = (char) c
;
343 const void *ret
= s
+ n
;
345 asm volatile ("0: srst %0,%1\n"
347 : "+a" (ret
), "+&a" (s
) : "d" (r0
) : "cc" );
350 EXPORT_SYMBOL(memscan
);
353 * memcpy - Copy one area of memory to another
354 * @dest: Where to copy to
355 * @src: Where to copy from
356 * @n: The size of the area.
358 * returns a pointer to @dest
360 void *memcpy(void *dest
, const void *src
, size_t n
)
362 return __builtin_memcpy(dest
, src
, n
);
364 EXPORT_SYMBOL(memcpy
);
367 * memset - Fill a region of memory with the given value
368 * @s: Pointer to the start of the area.
369 * @c: The byte to fill the area with
370 * @n: The size of the area.
372 * returns a pointer to @s
374 void *memset(void *s
, int c
, size_t n
)
379 return __builtin_memset(s
, 0, n
);
388 EXPORT_SYMBOL(memset
);