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
15 #include <linux/string.h>
16 #include <linux/export.h>
18 #ifdef __HAVE_ARCH_STRCPY
19 char *strcpy(char *dest, const char *src)
22 asm volatile("1:\tlodsb\n\t"
26 : "=&S" (d0), "=&D" (d1), "=&a" (d2)
27 : "0" (src), "1" (dest) : "memory");
30 EXPORT_SYMBOL(strcpy);
33 #ifdef __HAVE_ARCH_STRNCPY
34 char *strncpy(char *dest, const char *src, size_t count)
37 asm volatile("1:\tdecl %2\n\t"
46 : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
47 : "0" (src), "1" (dest), "2" (count) : "memory");
50 EXPORT_SYMBOL(strncpy);
53 #ifdef __HAVE_ARCH_STRCAT
54 char *strcat(char *dest, const char *src)
57 asm volatile("repne\n\t"
64 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
65 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu) : "memory");
68 EXPORT_SYMBOL(strcat);
71 #ifdef __HAVE_ARCH_STRNCAT
72 char *strncat(char *dest, const char *src, size_t count)
75 asm volatile("repne\n\t"
87 : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
88 : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu), "g" (count)
92 EXPORT_SYMBOL(strncat);
95 #ifdef __HAVE_ARCH_STRCMP
96 int strcmp(const char *cs, const char *ct)
100 asm volatile("1:\tlodsb\n\t"
103 "testb %%al,%%al\n\t"
105 "xorl %%eax,%%eax\n\t"
107 "2:\tsbbl %%eax,%%eax\n\t"
110 : "=a" (res), "=&S" (d0), "=&D" (d1)
115 EXPORT_SYMBOL(strcmp);
118 #ifdef __HAVE_ARCH_STRNCMP
119 int strncmp(const char *cs, const char *ct, size_t count)
123 asm volatile("1:\tdecl %3\n\t"
128 "testb %%al,%%al\n\t"
130 "2:\txorl %%eax,%%eax\n\t"
132 "3:\tsbbl %%eax,%%eax\n\t"
135 : "=a" (res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
136 : "1" (cs), "2" (ct), "3" (count)
140 EXPORT_SYMBOL(strncmp);
143 #ifdef __HAVE_ARCH_STRCHR
144 char *strchr(const char *s, int c)
148 asm volatile("movb %%al,%%ah\n"
152 "testb %%al,%%al\n\t"
157 : "=a" (res), "=&S" (d0)
162 EXPORT_SYMBOL(strchr);
165 #ifdef __HAVE_ARCH_STRLEN
166 size_t strlen(const char *s)
170 asm volatile("repne\n\t"
172 : "=c" (res), "=&D" (d0)
173 : "1" (s), "a" (0), "0" (0xffffffffu)
177 EXPORT_SYMBOL(strlen);
180 #ifdef __HAVE_ARCH_MEMCHR
181 void *memchr(const void *cs, int c, size_t count)
187 asm volatile("repne\n\t"
192 : "=D" (res), "=&c" (d0)
193 : "a" (c), "0" (cs), "1" (count)
197 EXPORT_SYMBOL(memchr);
200 #ifdef __HAVE_ARCH_MEMSCAN
201 void *memscan(void *addr, int c, size_t size)
205 asm volatile("repnz; scasb\n\t"
209 : "=D" (addr), "=c" (size)
210 : "0" (addr), "1" (size), "a" (c)
214 EXPORT_SYMBOL(memscan);
217 #ifdef __HAVE_ARCH_STRNLEN
218 size_t strnlen(const char *s, size_t count)
222 asm volatile("movl %2,%0\n\t"
224 "1:\tcmpb $0,(%0)\n\t"
231 : "=a" (res), "=&d" (d0)
232 : "c" (s), "1" (count)
236 EXPORT_SYMBOL(strnlen);