1 // SPDX-License-Identifier: GPL-2.0
3 * Most of the string-functions are rather heavily hand-optimized,
4 * see especially strsep,strstr,str[c]spn. They should work, but are not
5 * very easy to understand. Everything is done entirely within the register
6 * set, making the functions fast and clean. String instructions have been
7 * used through-out, making for "slightly" unclear code :-)
9 * AK: On P4 and K7 using non string instruction implementations might be faster
10 * for large memory blocks. But most of them are unlikely to be used on large
14 #include <linux/string.h>
15 #include <linux/export.h>
17 #ifdef __HAVE_ARCH_STRCPY
18 char *strcpy(char *dest, const char *src)
21 asm volatile("1:\tlodsb\n\t"
25 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
26 : "0" (src), "1" (dest) : "memory");
29 EXPORT_SYMBOL(strcpy);
32 #ifdef __HAVE_ARCH_STRNCPY
33 char *strncpy(char *dest, const char *src, size_t count)
36 asm volatile("1:\tdecl %2\n\t"
45 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
46 : "0" (src), "1" (dest), "2" (count) : "memory");
49 EXPORT_SYMBOL(strncpy);
52 #ifdef __HAVE_ARCH_STRCAT
53 char *strcat(char *dest, const char *src)
56 asm volatile("repne\n\t"
63 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
64 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu) : "memory");
67 EXPORT_SYMBOL(strcat);
70 #ifdef __HAVE_ARCH_STRNCAT
71 char *strncat(char *dest, const char *src, size_t count)
74 asm volatile("repne\n\t"
86 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
87 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu), "g" (count)
91 EXPORT_SYMBOL(strncat);
94 #ifdef __HAVE_ARCH_STRCMP
95 int strcmp(const char *cs, const char *ct)
99 asm volatile("1:\tlodsb\n\t"
102 "testb %%al,%%al\n\t"
104 "xorl %%eax,%%eax\n\t"
106 "2:\tsbbl %%eax,%%eax\n\t"
109 : "=a" (res), "=&S" (d0), "=&D" (d1)
114 EXPORT_SYMBOL(strcmp);
117 #ifdef __HAVE_ARCH_STRNCMP
118 int strncmp(const char *cs, const char *ct, size_t count)
122 asm volatile("1:\tdecl %3\n\t"
127 "testb %%al,%%al\n\t"
129 "2:\txorl %%eax,%%eax\n\t"
131 "3:\tsbbl %%eax,%%eax\n\t"
134 : "=a" (res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
135 : "1" (cs), "2" (ct), "3" (count)
139 EXPORT_SYMBOL(strncmp);
142 #ifdef __HAVE_ARCH_STRCHR
143 char *strchr(const char *s, int c)
147 asm volatile("movb %%al,%%ah\n"
151 "testb %%al,%%al\n\t"
156 : "=a" (res), "=&S" (d0)
161 EXPORT_SYMBOL(strchr);
164 #ifdef __HAVE_ARCH_STRLEN
165 size_t strlen(const char *s)
169 asm volatile("repne\n\t"
171 : "=c" (res), "=&D" (d0)
172 : "1" (s), "a" (0), "0" (0xffffffffu)
176 EXPORT_SYMBOL(strlen);
179 #ifdef __HAVE_ARCH_MEMCHR
180 void *memchr(const void *cs, int c, size_t count)
186 asm volatile("repne\n\t"
191 : "=D" (res), "=&c" (d0)
192 : "a" (c), "0" (cs), "1" (count)
196 EXPORT_SYMBOL(memchr);
199 #ifdef __HAVE_ARCH_MEMSCAN
200 void *memscan(void *addr, int c, size_t size)
204 asm volatile("repnz; scasb\n\t"
208 : "=D" (addr), "=c" (size)
209 : "0" (addr), "1" (size), "a" (c)
213 EXPORT_SYMBOL(memscan);
216 #ifdef __HAVE_ARCH_STRNLEN
217 size_t strnlen(const char *s, size_t count)
221 asm volatile("movl %2,%0\n\t"
223 "1:\tcmpb $0,(%0)\n\t"
230 : "=a" (res), "=&d" (d0)
231 : "c" (s), "1" (count)
235 EXPORT_SYMBOL(strnlen);