2 * arch/s390/lib/string.c
3 * Optimized string functions
6 * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
10 #define IN_ARCH_STRING_C 1
12 #include <linux/types.h>
13 #include <linux/module.h>
16 * Helper functions to find the end of a string
18 static inline char *__strend(const char *s
)
20 register unsigned long r0
asm("0") = 0;
22 asm volatile ("0: srst %0,%1\n"
24 : "+d" (r0
), "+a" (s
) : : "cc" );
28 static inline char *__strnend(const char *s
, size_t n
)
30 register unsigned long r0
asm("0") = 0;
31 const char *p
= s
+ n
;
33 asm volatile ("0: srst %0,%1\n"
35 : "+d" (p
), "+a" (s
) : "d" (r0
) : "cc" );
40 * strlen - Find the length of a string
41 * @s: The string to be sized
43 * returns the length of @s
45 size_t strlen(const char *s
)
48 return __strend(s
) - s
;
50 return __builtin_strlen(s
);
53 EXPORT_SYMBOL(strlen
);
56 * strnlen - Find the length of a length-limited string
57 * @s: The string to be sized
58 * @n: The maximum number of bytes to search
60 * returns the minimum of the length of @s and @n
62 size_t strnlen(const char * s
, size_t n
)
64 return __strnend(s
, n
) - s
;
66 EXPORT_SYMBOL(strnlen
);
69 * strcpy - Copy a %NUL terminated string
70 * @dest: Where to copy the string to
71 * @src: Where to copy the string from
73 * returns a pointer to @dest
75 char *strcpy(char *dest
, const char *src
)
78 register int r0
asm("0") = 0;
81 asm volatile ("0: mvst %0,%1\n"
83 : "+&a" (dest
), "+&a" (src
) : "d" (r0
)
87 return __builtin_strcpy(dest
, src
);
90 EXPORT_SYMBOL(strcpy
);
93 * strlcpy - Copy a %NUL terminated string into a sized buffer
94 * @dest: Where to copy the string to
95 * @src: Where to copy the string from
96 * @size: size of destination buffer
98 * Compatible with *BSD: the result is always a valid
99 * NUL-terminated string that fits in the buffer (unless,
100 * of course, the buffer size is zero). It does not pad
101 * out the result like strncpy() does.
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 __builtin_memcpy(dest
, src
, len
);
114 EXPORT_SYMBOL(strlcpy
);
117 * strncpy - Copy a length-limited, %NUL-terminated string
118 * @dest: Where to copy the string to
119 * @src: Where to copy the string from
120 * @n: The maximum number of bytes to copy
122 * The result is not %NUL-terminated if the source exceeds
125 char *strncpy(char *dest
, const char *src
, size_t n
)
127 size_t len
= __strnend(src
, n
) - src
;
128 __builtin_memset(dest
+ len
, 0, n
- len
);
129 __builtin_memcpy(dest
, src
, len
);
132 EXPORT_SYMBOL(strncpy
);
135 * strcat - Append one %NUL-terminated string to another
136 * @dest: The string to be appended to
137 * @src: The string to append to it
139 * returns a pointer to @dest
141 char *strcat(char *dest
, const char *src
)
143 register int r0
asm("0") = 0;
147 asm volatile ("0: srst %0,%1\n"
151 : "=&a" (dummy
), "+a" (dest
), "+a" (src
)
152 : "d" (r0
), "0" (0UL) : "cc", "memory" );
155 EXPORT_SYMBOL(strcat
);
158 * strlcat - Append a length-limited, %NUL-terminated string to another
159 * @dest: The string to be appended to
160 * @src: The string to append to it
161 * @n: The size of the destination buffer.
163 size_t strlcat(char *dest
, const char *src
, size_t n
)
165 size_t dsize
= __strend(dest
) - dest
;
166 size_t len
= __strend(src
) - src
;
167 size_t res
= dsize
+ len
;
175 __builtin_memcpy(dest
, src
, len
);
179 EXPORT_SYMBOL(strlcat
);
182 * strncat - Append a length-limited, %NUL-terminated string to another
183 * @dest: The string to be appended to
184 * @src: The string to append to it
185 * @n: The maximum numbers of bytes to copy
187 * returns a pointer to @dest
189 * Note that in contrast to strncpy, strncat ensures the result is
192 char *strncat(char *dest
, const char *src
, size_t n
)
194 size_t len
= __strnend(src
, n
) - src
;
195 char *p
= __strend(dest
);
198 __builtin_memcpy(p
, src
, len
);
201 EXPORT_SYMBOL(strncat
);
204 * strcmp - Compare two strings
206 * @ct: Another string
208 * returns 0 if @cs and @ct are equal,
209 * < 0 if @cs is less than @ct
210 * > 0 if @cs is greater than @ct
212 int strcmp(const char *cs
, const char *ct
)
214 register int r0
asm("0") = 0;
217 asm volatile ("0: clst %2,%3\n"
224 : "+d" (ret
), "+d" (r0
), "+a" (cs
), "+a" (ct
)
228 EXPORT_SYMBOL(strcmp
);
231 * strrchr - Find the last occurrence of a character in a string
232 * @s: The string to be searched
233 * @c: The character to search for
235 char * strrchr(const char * s
, int c
)
237 size_t len
= __strend(s
) - s
;
241 if (s
[len
] == (char) c
)
242 return (char *) s
+ len
;
246 EXPORT_SYMBOL(strrchr
);
249 * strstr - Find the first substring in a %NUL terminated string
250 * @s1: The string to be searched
251 * @s2: The string to search for
253 char * strstr(const char * s1
,const char * s2
)
257 l2
= __strend(s2
) - s2
;
260 l1
= __strend(s1
) - s1
;
262 register unsigned long r2
asm("2") = (unsigned long) s1
;
263 register unsigned long r3
asm("3") = (unsigned long) l2
;
264 register unsigned long r4
asm("4") = (unsigned long) s2
;
265 register unsigned long r5
asm("5") = (unsigned long) l2
;
268 asm volatile ("0: clcle %1,%3,0\n"
272 : "=&d" (cc
), "+a" (r2
), "+a" (r3
),
273 "+a" (r4
), "+a" (r5
) : : "cc" );
280 EXPORT_SYMBOL(strstr
);
283 * memchr - Find a character in an area of memory.
284 * @s: The memory area
285 * @c: The byte to search for
286 * @n: The size of the area.
288 * returns the address of the first occurrence of @c, or %NULL
291 void *memchr(const void *s
, int c
, size_t n
)
293 register int r0
asm("0") = (char) c
;
294 const void *ret
= s
+ n
;
296 asm volatile ("0: srst %0,%1\n"
301 : "+a" (ret
), "+&a" (s
) : "d" (r0
) : "cc" );
304 EXPORT_SYMBOL(memchr
);
307 * memcmp - Compare two areas of memory
308 * @cs: One area of memory
309 * @ct: Another area of memory
310 * @count: The size of the area.
312 int memcmp(const void *cs
, const void *ct
, size_t n
)
314 register unsigned long r2
asm("2") = (unsigned long) cs
;
315 register unsigned long r3
asm("3") = (unsigned long) n
;
316 register unsigned long r4
asm("4") = (unsigned long) ct
;
317 register unsigned long r5
asm("5") = (unsigned long) n
;
320 asm volatile ("0: clcle %1,%3,0\n"
324 : "=&d" (ret
), "+a" (r2
), "+a" (r3
), "+a" (r4
), "+a" (r5
)
327 ret
= *(char *) r2
- *(char *) r4
;
330 EXPORT_SYMBOL(memcmp
);
333 * memscan - Find a character in an area of memory.
334 * @s: The memory area
335 * @c: The byte to search for
336 * @n: The size of the area.
338 * returns the address of the first occurrence of @c, or 1 byte past
339 * the area if @c is not found
341 void *memscan(void *s
, int c
, size_t n
)
343 register int r0
asm("0") = (char) c
;
344 const void *ret
= s
+ n
;
346 asm volatile ("0: srst %0,%1\n"
348 : "+a" (ret
), "+&a" (s
) : "d" (r0
) : "cc" );
351 EXPORT_SYMBOL(memscan
);
354 * memcpy - Copy one area of memory to another
355 * @dest: Where to copy to
356 * @src: Where to copy from
357 * @n: The size of the area.
359 * returns a pointer to @dest
361 void *memcpy(void *dest
, const void *src
, size_t n
)
363 return __builtin_memcpy(dest
, src
, n
);
365 EXPORT_SYMBOL(memcpy
);
368 * memset - Fill a region of memory with the given value
369 * @s: Pointer to the start of the area.
370 * @c: The byte to fill the area with
371 * @n: The size of the area.
373 * returns a pointer to @s
375 void *memset(void *s
, int c
, size_t n
)
380 return __builtin_memset(s
, 0, n
);
389 EXPORT_SYMBOL(memset
);
This page took 0.037288 seconds and 5 git commands to generate.