/*1* arch/s390/lib/string.c2* Optimized string functions3*4* S390 version5* Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation6* Author(s): Martin Schwidefsky ([email protected])7*/89#define IN_ARCH_STRING_C 11011#include <linux/types.h>12#include <linux/module.h>1314/*15* Helper functions to find the end of a string16*/17static inline char *__strend(const char *s)18{19register unsigned long r0 asm("0") = 0;2021asm volatile ("0: srst %0,%1\n"22" jo 0b"23: "+d" (r0), "+a" (s) : : "cc" );24return (char *) r0;25}2627static inline char *__strnend(const char *s, size_t n)28{29register unsigned long r0 asm("0") = 0;30const char *p = s + n;3132asm volatile ("0: srst %0,%1\n"33" jo 0b"34: "+d" (p), "+a" (s) : "d" (r0) : "cc" );35return (char *) p;36}3738/**39* strlen - Find the length of a string40* @s: The string to be sized41*42* returns the length of @s43*/44size_t strlen(const char *s)45{46#if __GNUC__ < 447return __strend(s) - s;48#else49return __builtin_strlen(s);50#endif51}52EXPORT_SYMBOL(strlen);5354/**55* strnlen - Find the length of a length-limited string56* @s: The string to be sized57* @n: The maximum number of bytes to search58*59* returns the minimum of the length of @s and @n60*/61size_t strnlen(const char * s, size_t n)62{63return __strnend(s, n) - s;64}65EXPORT_SYMBOL(strnlen);6667/**68* strcpy - Copy a %NUL terminated string69* @dest: Where to copy the string to70* @src: Where to copy the string from71*72* returns a pointer to @dest73*/74char *strcpy(char *dest, const char *src)75{76#if __GNUC__ < 477register int r0 asm("0") = 0;78char *ret = dest;7980asm volatile ("0: mvst %0,%1\n"81" jo 0b"82: "+&a" (dest), "+&a" (src) : "d" (r0)83: "cc", "memory" );84return ret;85#else86return __builtin_strcpy(dest, src);87#endif88}89EXPORT_SYMBOL(strcpy);9091/**92* strlcpy - Copy a %NUL terminated string into a sized buffer93* @dest: Where to copy the string to94* @src: Where to copy the string from95* @size: size of destination buffer96*97* Compatible with *BSD: the result is always a valid98* NUL-terminated string that fits in the buffer (unless,99* of course, the buffer size is zero). It does not pad100* out the result like strncpy() does.101*/102size_t strlcpy(char *dest, const char *src, size_t size)103{104size_t ret = __strend(src) - src;105106if (size) {107size_t len = (ret >= size) ? size-1 : ret;108dest[len] = '\0';109__builtin_memcpy(dest, src, len);110}111return ret;112}113EXPORT_SYMBOL(strlcpy);114115/**116* strncpy - Copy a length-limited, %NUL-terminated string117* @dest: Where to copy the string to118* @src: Where to copy the string from119* @n: The maximum number of bytes to copy120*121* The result is not %NUL-terminated if the source exceeds122* @n bytes.123*/124char *strncpy(char *dest, const char *src, size_t n)125{126size_t len = __strnend(src, n) - src;127__builtin_memset(dest + len, 0, n - len);128__builtin_memcpy(dest, src, len);129return dest;130}131EXPORT_SYMBOL(strncpy);132133/**134* strcat - Append one %NUL-terminated string to another135* @dest: The string to be appended to136* @src: The string to append to it137*138* returns a pointer to @dest139*/140char *strcat(char *dest, const char *src)141{142register int r0 asm("0") = 0;143unsigned long dummy;144char *ret = dest;145146asm volatile ("0: srst %0,%1\n"147" jo 0b\n"148"1: mvst %0,%2\n"149" jo 1b"150: "=&a" (dummy), "+a" (dest), "+a" (src)151: "d" (r0), "0" (0UL) : "cc", "memory" );152return ret;153}154EXPORT_SYMBOL(strcat);155156/**157* strlcat - Append a length-limited, %NUL-terminated string to another158* @dest: The string to be appended to159* @src: The string to append to it160* @n: The size of the destination buffer.161*/162size_t strlcat(char *dest, const char *src, size_t n)163{164size_t dsize = __strend(dest) - dest;165size_t len = __strend(src) - src;166size_t res = dsize + len;167168if (dsize < n) {169dest += dsize;170n -= dsize;171if (len >= n)172len = n - 1;173dest[len] = '\0';174__builtin_memcpy(dest, src, len);175}176return res;177}178EXPORT_SYMBOL(strlcat);179180/**181* strncat - Append a length-limited, %NUL-terminated string to another182* @dest: The string to be appended to183* @src: The string to append to it184* @n: The maximum numbers of bytes to copy185*186* returns a pointer to @dest187*188* Note that in contrast to strncpy, strncat ensures the result is189* terminated.190*/191char *strncat(char *dest, const char *src, size_t n)192{193size_t len = __strnend(src, n) - src;194char *p = __strend(dest);195196p[len] = '\0';197__builtin_memcpy(p, src, len);198return dest;199}200EXPORT_SYMBOL(strncat);201202/**203* strcmp - Compare two strings204* @cs: One string205* @ct: Another string206*207* returns 0 if @cs and @ct are equal,208* < 0 if @cs is less than @ct209* > 0 if @cs is greater than @ct210*/211int strcmp(const char *cs, const char *ct)212{213register int r0 asm("0") = 0;214int ret = 0;215216asm volatile ("0: clst %2,%3\n"217" jo 0b\n"218" je 1f\n"219" ic %0,0(%2)\n"220" ic %1,0(%3)\n"221" sr %0,%1\n"222"1:"223: "+d" (ret), "+d" (r0), "+a" (cs), "+a" (ct)224: : "cc" );225return ret;226}227EXPORT_SYMBOL(strcmp);228229/**230* strrchr - Find the last occurrence of a character in a string231* @s: The string to be searched232* @c: The character to search for233*/234char * strrchr(const char * s, int c)235{236size_t len = __strend(s) - s;237238if (len)239do {240if (s[len] == (char) c)241return (char *) s + len;242} while (--len > 0);243return NULL;244}245EXPORT_SYMBOL(strrchr);246247/**248* strstr - Find the first substring in a %NUL terminated string249* @s1: The string to be searched250* @s2: The string to search for251*/252char * strstr(const char * s1,const char * s2)253{254int l1, l2;255256l2 = __strend(s2) - s2;257if (!l2)258return (char *) s1;259l1 = __strend(s1) - s1;260while (l1-- >= l2) {261register unsigned long r2 asm("2") = (unsigned long) s1;262register unsigned long r3 asm("3") = (unsigned long) l2;263register unsigned long r4 asm("4") = (unsigned long) s2;264register unsigned long r5 asm("5") = (unsigned long) l2;265int cc;266267asm volatile ("0: clcle %1,%3,0\n"268" jo 0b\n"269" ipm %0\n"270" srl %0,28"271: "=&d" (cc), "+a" (r2), "+a" (r3),272"+a" (r4), "+a" (r5) : : "cc" );273if (!cc)274return (char *) s1;275s1++;276}277return NULL;278}279EXPORT_SYMBOL(strstr);280281/**282* memchr - Find a character in an area of memory.283* @s: The memory area284* @c: The byte to search for285* @n: The size of the area.286*287* returns the address of the first occurrence of @c, or %NULL288* if @c is not found289*/290void *memchr(const void *s, int c, size_t n)291{292register int r0 asm("0") = (char) c;293const void *ret = s + n;294295asm volatile ("0: srst %0,%1\n"296" jo 0b\n"297" jl 1f\n"298" la %0,0\n"299"1:"300: "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );301return (void *) ret;302}303EXPORT_SYMBOL(memchr);304305/**306* memcmp - Compare two areas of memory307* @cs: One area of memory308* @ct: Another area of memory309* @count: The size of the area.310*/311int memcmp(const void *cs, const void *ct, size_t n)312{313register unsigned long r2 asm("2") = (unsigned long) cs;314register unsigned long r3 asm("3") = (unsigned long) n;315register unsigned long r4 asm("4") = (unsigned long) ct;316register unsigned long r5 asm("5") = (unsigned long) n;317int ret;318319asm volatile ("0: clcle %1,%3,0\n"320" jo 0b\n"321" ipm %0\n"322" srl %0,28"323: "=&d" (ret), "+a" (r2), "+a" (r3), "+a" (r4), "+a" (r5)324: : "cc" );325if (ret)326ret = *(char *) r2 - *(char *) r4;327return ret;328}329EXPORT_SYMBOL(memcmp);330331/**332* memscan - Find a character in an area of memory.333* @s: The memory area334* @c: The byte to search for335* @n: The size of the area.336*337* returns the address of the first occurrence of @c, or 1 byte past338* the area if @c is not found339*/340void *memscan(void *s, int c, size_t n)341{342register int r0 asm("0") = (char) c;343const void *ret = s + n;344345asm volatile ("0: srst %0,%1\n"346" jo 0b\n"347: "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );348return (void *) ret;349}350EXPORT_SYMBOL(memscan);351352/**353* memcpy - Copy one area of memory to another354* @dest: Where to copy to355* @src: Where to copy from356* @n: The size of the area.357*358* returns a pointer to @dest359*/360void *memcpy(void *dest, const void *src, size_t n)361{362return __builtin_memcpy(dest, src, n);363}364EXPORT_SYMBOL(memcpy);365366/**367* memset - Fill a region of memory with the given value368* @s: Pointer to the start of the area.369* @c: The byte to fill the area with370* @n: The size of the area.371*372* returns a pointer to @s373*/374void *memset(void *s, int c, size_t n)375{376char *xs;377378if (c == 0)379return __builtin_memset(s, 0, n);380381xs = (char *) s;382if (n > 0)383do {384*xs++ = c;385} while (--n > 0);386return s;387}388EXPORT_SYMBOL(memset);389390391