string.c 8.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5
/*
 *    Optimized string functions
 *
 *  S390 version
6
 *    Copyright IBM Corp. 2004
L
Linus Torvalds 已提交
7 8 9 10 11 12
 *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 */

#define IN_ARCH_STRING_C 1

#include <linux/types.h>
13 14
#include <linux/string.h>
#include <linux/export.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24

/*
 * Helper functions to find the end of a string
 */
static inline char *__strend(const char *s)
{
	register unsigned long r0 asm("0") = 0;

	asm volatile ("0: srst  %0,%1\n"
		      "   jo    0b"
25
		      : "+d" (r0), "+a" (s) :  : "cc", "memory");
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35
	return (char *) r0;
}

static inline char *__strnend(const char *s, size_t n)
{
	register unsigned long r0 asm("0") = 0;
	const char *p = s + n;

	asm volatile ("0: srst  %0,%1\n"
		      "   jo    0b"
36
		      : "+d" (p), "+a" (s) : "d" (r0) : "cc", "memory");
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45
	return (char *) p;
}

/**
 * strlen - Find the length of a string
 * @s: The string to be sized
 *
 * returns the length of @s
 */
46
#ifdef __HAVE_ARCH_STRLEN
L
Linus Torvalds 已提交
47 48 49 50 51
size_t strlen(const char *s)
{
	return __strend(s) - s;
}
EXPORT_SYMBOL(strlen);
52
#endif
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60

/**
 * strnlen - Find the length of a length-limited string
 * @s: The string to be sized
 * @n: The maximum number of bytes to search
 *
 * 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)
L
Linus Torvalds 已提交
63 64 65 66
{
	return __strnend(s, n) - s;
}
EXPORT_SYMBOL(strnlen);
67
#endif
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75

/**
 * strcpy - Copy a %NUL terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 *
 * returns a pointer to @dest
 */
76
#ifdef __HAVE_ARCH_STRCPY
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88
char *strcpy(char *dest, const char *src)
{
	register int r0 asm("0") = 0;
	char *ret = dest;

	asm volatile ("0: mvst  %0,%1\n"
		      "   jo    0b"
		      : "+&a" (dest), "+&a" (src) : "d" (r0)
		      : "cc", "memory" );
	return ret;
}
EXPORT_SYMBOL(strcpy);
89
#endif
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98 99 100 101

/**
 * strlcpy - Copy a %NUL terminated string into a sized buffer
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @size: size of destination buffer
 *
 * Compatible with *BSD: the result is always a valid
 * NUL-terminated string that fits in the buffer (unless,
 * of course, the buffer size is zero). It does not pad
 * out the result like strncpy() does.
 */
102
#ifdef __HAVE_ARCH_STRLCPY
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
size_t strlcpy(char *dest, const char *src, size_t size)
{
	size_t ret = __strend(src) - src;

	if (size) {
		size_t len = (ret >= size) ? size-1 : ret;
		dest[len] = '\0';
110
		memcpy(dest, src, len);
L
Linus Torvalds 已提交
111 112 113 114
	}
	return ret;
}
EXPORT_SYMBOL(strlcpy);
115
#endif
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125

/**
 * strncpy - Copy a length-limited, %NUL-terminated string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @n: The maximum number of bytes to copy
 *
 * The result is not %NUL-terminated if the source exceeds
 * @n bytes.
 */
126
#ifdef __HAVE_ARCH_STRNCPY
L
Linus Torvalds 已提交
127 128 129
char *strncpy(char *dest, const char *src, size_t n)
{
	size_t len = __strnend(src, n) - src;
130 131
	memset(dest + len, 0, n - len);
	memcpy(dest, src, len);
L
Linus Torvalds 已提交
132 133 134
	return dest;
}
EXPORT_SYMBOL(strncpy);
135
#endif
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143

/**
 * strcat - Append one %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 *
 * returns a pointer to @dest
 */
144
#ifdef __HAVE_ARCH_STRCAT
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
char *strcat(char *dest, const char *src)
{
	register int r0 asm("0") = 0;
	unsigned long dummy;
	char *ret = dest;

	asm volatile ("0: srst  %0,%1\n"
		      "   jo    0b\n"
		      "1: mvst  %0,%2\n"
		      "   jo    1b"
		      : "=&a" (dummy), "+a" (dest), "+a" (src)
		      : "d" (r0), "0" (0UL) : "cc", "memory" );
	return ret;
}
EXPORT_SYMBOL(strcat);
160
#endif
L
Linus Torvalds 已提交
161 162 163 164 165 166 167

/**
 * strlcat - Append a length-limited, %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 * @n: The size of the destination buffer.
 */
168
#ifdef __HAVE_ARCH_STRLCAT
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180
size_t strlcat(char *dest, const char *src, size_t n)
{
	size_t dsize = __strend(dest) - dest;
	size_t len = __strend(src) - src;
	size_t res = dsize + len;

	if (dsize < n) {
		dest += dsize;
		n -= dsize;
		if (len >= n)
			len = n - 1;
		dest[len] = '\0';
181
		memcpy(dest, src, len);
L
Linus Torvalds 已提交
182 183 184 185
	}
	return res;
}
EXPORT_SYMBOL(strlcat);
186
#endif
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195 196 197 198

/**
 * strncat - Append a length-limited, %NUL-terminated string to another
 * @dest: The string to be appended to
 * @src: The string to append to it
 * @n: The maximum numbers of bytes to copy
 *
 * returns a pointer to @dest
 *
 * Note that in contrast to strncpy, strncat ensures the result is
 * terminated.
 */
199
#ifdef __HAVE_ARCH_STRNCAT
L
Linus Torvalds 已提交
200 201 202 203 204 205
char *strncat(char *dest, const char *src, size_t n)
{
	size_t len = __strnend(src, n) - src;
	char *p = __strend(dest);

	p[len] = '\0';
206
	memcpy(p, src, len);
L
Linus Torvalds 已提交
207 208 209
	return dest;
}
EXPORT_SYMBOL(strncat);
210
#endif
L
Linus Torvalds 已提交
211 212 213

/**
 * strcmp - Compare two strings
214 215
 * @s1: One string
 * @s2: Another string
L
Linus Torvalds 已提交
216
 *
217 218 219
 * returns   0 if @s1 and @s2 are equal,
 *	   < 0 if @s1 is less than @s2
 *	   > 0 if @s1 is greater than @s2
L
Linus Torvalds 已提交
220
 */
221
#ifdef __HAVE_ARCH_STRCMP
222
int strcmp(const char *s1, const char *s2)
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231 232 233
{
	register int r0 asm("0") = 0;
	int ret = 0;

	asm volatile ("0: clst %2,%3\n"
		      "   jo   0b\n"
		      "   je   1f\n"
		      "   ic   %0,0(%2)\n"
		      "   ic   %1,0(%3)\n"
		      "   sr   %0,%1\n"
		      "1:"
234
		      : "+d" (ret), "+d" (r0), "+a" (s1), "+a" (s2)
235
		      : : "cc", "memory");
L
Linus Torvalds 已提交
236 237 238
	return ret;
}
EXPORT_SYMBOL(strcmp);
239
#endif
L
Linus Torvalds 已提交
240 241 242 243 244 245

/**
 * strrchr - Find the last occurrence of a character in a string
 * @s: The string to be searched
 * @c: The character to search for
 */
246
#ifdef __HAVE_ARCH_STRRCHR
247
char *strrchr(const char *s, int c)
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255
{
       size_t len = __strend(s) - s;

       if (len)
	       do {
		       if (s[len] == (char) c)
			       return (char *) s + len;
	       } while (--len > 0);
H
Heiko Carstens 已提交
256
       return NULL;
L
Linus Torvalds 已提交
257 258
}
EXPORT_SYMBOL(strrchr);
259
#endif
L
Linus Torvalds 已提交
260

261
static inline int clcle(const char *s1, unsigned long l1,
262
			const char *s2, unsigned long l2)
263 264
{
	register unsigned long r2 asm("2") = (unsigned long) s1;
265
	register unsigned long r3 asm("3") = (unsigned long) l1;
266 267 268 269 270 271 272 273 274
	register unsigned long r4 asm("4") = (unsigned long) s2;
	register unsigned long r5 asm("5") = (unsigned long) l2;
	int cc;

	asm volatile ("0: clcle %1,%3,0\n"
		      "   jo    0b\n"
		      "   ipm   %0\n"
		      "   srl   %0,28"
		      : "=&d" (cc), "+a" (r2), "+a" (r3),
275
			"+a" (r4), "+a" (r5) : : "cc", "memory");
276 277 278
	return cc;
}

L
Linus Torvalds 已提交
279 280 281 282 283
/**
 * strstr - Find the first substring in a %NUL terminated string
 * @s1: The string to be searched
 * @s2: The string to search for
 */
284
#ifdef __HAVE_ARCH_STRSTR
285
char *strstr(const char *s1, const char *s2)
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293
{
	int l1, l2;

	l2 = __strend(s2) - s2;
	if (!l2)
		return (char *) s1;
	l1 = __strend(s1) - s1;
	while (l1-- >= l2) {
294
		int cc;
295

296
		cc = clcle(s1, l2, s2, l2);
L
Linus Torvalds 已提交
297 298 299 300
		if (!cc)
			return (char *) s1;
		s1++;
	}
H
Heiko Carstens 已提交
301
	return NULL;
L
Linus Torvalds 已提交
302 303
}
EXPORT_SYMBOL(strstr);
304
#endif
L
Linus Torvalds 已提交
305 306 307 308 309 310 311 312 313 314

/**
 * memchr - Find a character in an area of memory.
 * @s: The memory area
 * @c: The byte to search for
 * @n: The size of the area.
 *
 * returns the address of the first occurrence of @c, or %NULL
 * if @c is not found
 */
315
#ifdef __HAVE_ARCH_MEMCHR
L
Linus Torvalds 已提交
316 317 318 319 320 321 322 323 324 325
void *memchr(const void *s, int c, size_t n)
{
	register int r0 asm("0") = (char) c;
	const void *ret = s + n;

	asm volatile ("0: srst  %0,%1\n"
		      "   jo    0b\n"
		      "   jl	1f\n"
		      "   la    %0,0\n"
		      "1:"
326
		      : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
L
Linus Torvalds 已提交
327 328 329
	return (void *) ret;
}
EXPORT_SYMBOL(memchr);
330
#endif
L
Linus Torvalds 已提交
331 332 333

/**
 * memcmp - Compare two areas of memory
334 335
 * @s1: One area of memory
 * @s2: Another area of memory
L
Linus Torvalds 已提交
336 337
 * @count: The size of the area.
 */
338
#ifdef __HAVE_ARCH_MEMCMP
339
int memcmp(const void *s1, const void *s2, size_t n)
L
Linus Torvalds 已提交
340
{
341
	int ret;
L
Linus Torvalds 已提交
342

343
	ret = clcle(s1, n, s2, n);
L
Linus Torvalds 已提交
344
	if (ret)
345
		ret = ret == 1 ? -1 : 1;
L
Linus Torvalds 已提交
346 347 348
	return ret;
}
EXPORT_SYMBOL(memcmp);
349
#endif
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357 358 359

/**
 * memscan - Find a character in an area of memory.
 * @s: The memory area
 * @c: The byte to search for
 * @n: The size of the area.
 *
 * returns the address of the first occurrence of @c, or 1 byte past
 * the area if @c is not found
 */
360
#ifdef __HAVE_ARCH_MEMSCAN
L
Linus Torvalds 已提交
361 362 363 364 365 366 367
void *memscan(void *s, int c, size_t n)
{
	register int r0 asm("0") = (char) c;
	const void *ret = s + n;

	asm volatile ("0: srst  %0,%1\n"
		      "   jo    0b\n"
368
		      : "+a" (ret), "+&a" (s) : "d" (r0) : "cc", "memory");
L
Linus Torvalds 已提交
369 370 371
	return (void *) ret;
}
EXPORT_SYMBOL(memscan);
372
#endif