blob: c86f452256de935492dae82c0256f8040791f9c8 [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_STRING_32_H
2#define _ASM_X86_STRING_32_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4#ifdef __KERNEL__
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Joe Perches06b0f572008-03-23 01:03:33 -07006/* Let gcc decide whether to inline or use the out of line functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
8#define __HAVE_ARCH_STRCPY
Andi Kleenb520b852007-07-21 17:09:59 +02009extern char *strcpy(char *dest, const char *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#define __HAVE_ARCH_STRNCPY
Andi Kleenb520b852007-07-21 17:09:59 +020012extern char *strncpy(char *dest, const char *src, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define __HAVE_ARCH_STRCAT
Andi Kleenb520b852007-07-21 17:09:59 +020015extern char *strcat(char *dest, const char *src);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define __HAVE_ARCH_STRNCAT
Andi Kleenb520b852007-07-21 17:09:59 +020018extern char *strncat(char *dest, const char *src, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#define __HAVE_ARCH_STRCMP
Andi Kleenb520b852007-07-21 17:09:59 +020021extern int strcmp(const char *cs, const char *ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define __HAVE_ARCH_STRNCMP
Andi Kleenb520b852007-07-21 17:09:59 +020024extern int strncmp(const char *cs, const char *ct, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define __HAVE_ARCH_STRCHR
Andi Kleenb520b852007-07-21 17:09:59 +020027extern char *strchr(const char *s, int c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define __HAVE_ARCH_STRLEN
Andi Kleenb520b852007-07-21 17:09:59 +020030extern size_t strlen(const char *s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Joe Perches78d64fc2008-05-12 15:44:39 +020032static __always_inline void *__memcpy(void *to, const void *from, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Joe Perches78d64fc2008-05-12 15:44:39 +020034 int d0, d1, d2;
35 asm volatile("rep ; movsl\n\t"
36 "movl %4,%%ecx\n\t"
37 "andl $3,%%ecx\n\t"
38 "jz 1f\n\t"
39 "rep ; movsb\n\t"
40 "1:"
41 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
42 : "0" (n / 4), "g" (n), "1" ((long)to), "2" ((long)from)
43 : "memory");
44 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47/*
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070048 * This looks ugly, but the compiler can optimize it totally,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * as the count is constant.
50 */
Joe Perches78d64fc2008-05-12 15:44:39 +020051static __always_inline void *__constant_memcpy(void *to, const void *from,
52 size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070054 long esi, edi;
Joe Perches78d64fc2008-05-12 15:44:39 +020055 if (!n)
56 return to;
57
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070058 switch (n) {
Joe Perches78d64fc2008-05-12 15:44:39 +020059 case 1:
60 *(char *)to = *(char *)from;
61 return to;
62 case 2:
63 *(short *)to = *(short *)from;
64 return to;
65 case 4:
66 *(int *)to = *(int *)from;
67 return to;
68
69 case 3:
70 *(short *)to = *(short *)from;
71 *((char *)to + 2) = *((char *)from + 2);
72 return to;
73 case 5:
74 *(int *)to = *(int *)from;
75 *((char *)to + 4) = *((char *)from + 4);
76 return to;
77 case 6:
78 *(int *)to = *(int *)from;
79 *((short *)to + 2) = *((short *)from + 2);
80 return to;
81 case 8:
82 *(int *)to = *(int *)from;
83 *((int *)to + 1) = *((int *)from + 1);
84 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Joe Perches78d64fc2008-05-12 15:44:39 +020086
87 esi = (long)from;
88 edi = (long)to;
89 if (n >= 5 * 4) {
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070090 /* large block: use rep prefix */
91 int ecx;
Joe Perches78d64fc2008-05-12 15:44:39 +020092 asm volatile("rep ; movsl"
93 : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
94 : "0" (n / 4), "1" (edi), "2" (esi)
95 : "memory"
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070096 );
97 } else {
98 /* small block: don't clobber ecx + smaller code */
Joe Perches78d64fc2008-05-12 15:44:39 +020099 if (n >= 4 * 4)
100 asm volatile("movsl"
101 : "=&D"(edi), "=&S"(esi)
102 : "0"(edi), "1"(esi)
103 : "memory");
104 if (n >= 3 * 4)
105 asm volatile("movsl"
106 : "=&D"(edi), "=&S"(esi)
107 : "0"(edi), "1"(esi)
108 : "memory");
109 if (n >= 2 * 4)
110 asm volatile("movsl"
111 : "=&D"(edi), "=&S"(esi)
112 : "0"(edi), "1"(esi)
113 : "memory");
114 if (n >= 1 * 4)
115 asm volatile("movsl"
116 : "=&D"(edi), "=&S"(esi)
117 : "0"(edi), "1"(esi)
118 : "memory");
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700119 }
120 switch (n % 4) {
121 /* tail */
Joe Perches78d64fc2008-05-12 15:44:39 +0200122 case 0:
123 return to;
124 case 1:
125 asm volatile("movsb"
126 : "=&D"(edi), "=&S"(esi)
127 : "0"(edi), "1"(esi)
128 : "memory");
129 return to;
130 case 2:
131 asm volatile("movsw"
132 : "=&D"(edi), "=&S"(esi)
133 : "0"(edi), "1"(esi)
134 : "memory");
135 return to;
136 default:
137 asm volatile("movsw\n\tmovsb"
138 : "=&D"(edi), "=&S"(esi)
139 : "0"(edi), "1"(esi)
140 : "memory");
141 return to;
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145#define __HAVE_ARCH_MEMCPY
146
147#ifdef CONFIG_X86_USE_3DNOW
148
149#include <asm/mmx.h>
150
151/*
152 * This CPU favours 3DNow strongly (eg AMD Athlon)
153 */
154
Joe Perches78d64fc2008-05-12 15:44:39 +0200155static inline void *__constant_memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 if (len < 512)
158 return __constant_memcpy(to, from, len);
159 return _mmx_memcpy(to, from, len);
160}
161
Joe Perches78d64fc2008-05-12 15:44:39 +0200162static inline void *__memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 if (len < 512)
165 return __memcpy(to, from, len);
166 return _mmx_memcpy(to, from, len);
167}
168
Joe Perches78d64fc2008-05-12 15:44:39 +0200169#define memcpy(t, f, n) \
170 (__builtin_constant_p((n)) \
171 ? __constant_memcpy3d((t), (f), (n)) \
172 : __memcpy3d((t), (f), (n)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174#else
175
176/*
177 * No 3D Now!
178 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200179
Vegard Nossumf8561292008-04-04 00:53:23 +0200180#ifndef CONFIG_KMEMCHECK
Joe Perches78d64fc2008-05-12 15:44:39 +0200181#define memcpy(t, f, n) \
182 (__builtin_constant_p((n)) \
183 ? __constant_memcpy((t), (f), (n)) \
184 : __memcpy((t), (f), (n)))
Vegard Nossumf8561292008-04-04 00:53:23 +0200185#else
186/*
187 * kmemcheck becomes very happy if we use the REP instructions unconditionally,
188 * because it means that we know both memory operands in advance.
189 */
190#define memcpy(t, f, n) __memcpy((t), (f), (n))
191#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193#endif
194
195#define __HAVE_ARCH_MEMMOVE
Joe Perches78d64fc2008-05-12 15:44:39 +0200196void *memmove(void *dest, const void *src, size_t n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198#define memcmp __builtin_memcmp
199
200#define __HAVE_ARCH_MEMCHR
Joe Perches78d64fc2008-05-12 15:44:39 +0200201extern void *memchr(const void *cs, int c, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Joe Perches78d64fc2008-05-12 15:44:39 +0200203static inline void *__memset_generic(void *s, char c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Joe Perches78d64fc2008-05-12 15:44:39 +0200205 int d0, d1;
206 asm volatile("rep\n\t"
207 "stosb"
208 : "=&c" (d0), "=&D" (d1)
209 : "a" (c), "1" (s), "0" (count)
210 : "memory");
211 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/* we might want to write optimized versions of these later */
Joe Perches78d64fc2008-05-12 15:44:39 +0200215#define __constant_count_memset(s, c, count) __memset_generic((s), (c), (count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/*
Joe Perches78d64fc2008-05-12 15:44:39 +0200218 * memset(x, 0, y) is a reasonably common thing to do, so we want to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * things 32 bits at a time even when we don't know the size of the
220 * area at compile-time..
221 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200222static __always_inline
223void *__constant_c_memset(void *s, unsigned long c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Joe Perches78d64fc2008-05-12 15:44:39 +0200225 int d0, d1;
226 asm volatile("rep ; stosl\n\t"
227 "testb $2,%b3\n\t"
228 "je 1f\n\t"
229 "stosw\n"
230 "1:\ttestb $1,%b3\n\t"
231 "je 2f\n\t"
232 "stosb\n"
233 "2:"
234 : "=&c" (d0), "=&D" (d1)
235 : "a" (c), "q" (count), "0" (count/4), "1" ((long)s)
236 : "memory");
237 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/* Added by Gertjan van Wingerde to make minix and sysv module work */
241#define __HAVE_ARCH_STRNLEN
Joe Perches78d64fc2008-05-12 15:44:39 +0200242extern size_t strnlen(const char *s, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* end of additional stuff */
244
245#define __HAVE_ARCH_STRSTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246extern char *strstr(const char *cs, const char *ct);
247
248/*
249 * This looks horribly ugly, but the compiler can optimize it totally,
250 * as we by now know that both pattern and count is constant..
251 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200252static __always_inline
253void *__constant_c_and_count_memset(void *s, unsigned long pattern,
254 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 switch (count) {
Joe Perches78d64fc2008-05-12 15:44:39 +0200257 case 0:
258 return s;
259 case 1:
260 *(unsigned char *)s = pattern & 0xff;
261 return s;
262 case 2:
263 *(unsigned short *)s = pattern & 0xffff;
264 return s;
265 case 3:
266 *(unsigned short *)s = pattern & 0xffff;
267 *((unsigned char *)s + 2) = pattern & 0xff;
268 return s;
269 case 4:
270 *(unsigned long *)s = pattern;
271 return s;
272 }
273
274#define COMMON(x) \
275 asm volatile("rep ; stosl" \
276 x \
277 : "=&c" (d0), "=&D" (d1) \
H. Peter Anvin1a20d3e2008-05-26 13:36:53 -0700278 : "a" (eax), "0" (count/4), "1" ((long)s) \
Joe Perches78d64fc2008-05-12 15:44:39 +0200279 : "memory")
280
281 {
282 int d0, d1;
H. Peter Anvin1a20d3e2008-05-26 13:36:53 -0700283#if __GNUC__ == 4 && __GNUC_MINOR__ == 0
284 /* Workaround for broken gcc 4.0 */
285 register unsigned long eax asm("%eax") = pattern;
286#else
287 unsigned long eax = pattern;
288#endif
289
Joe Perches78d64fc2008-05-12 15:44:39 +0200290 switch (count % 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 case 0:
Joe Perches78d64fc2008-05-12 15:44:39 +0200292 COMMON("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return s;
294 case 1:
Joe Perches78d64fc2008-05-12 15:44:39 +0200295 COMMON("\n\tstosb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return s;
297 case 2:
Joe Perches78d64fc2008-05-12 15:44:39 +0200298 COMMON("\n\tstosw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return s;
Joe Perches78d64fc2008-05-12 15:44:39 +0200300 default:
301 COMMON("\n\tstosw\n\tstosb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return s;
Joe Perches78d64fc2008-05-12 15:44:39 +0200303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Joe Perches78d64fc2008-05-12 15:44:39 +0200305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#undef COMMON
307}
308
Joe Perches78d64fc2008-05-12 15:44:39 +0200309#define __constant_c_x_memset(s, c, count) \
310 (__builtin_constant_p(count) \
311 ? __constant_c_and_count_memset((s), (c), (count)) \
312 : __constant_c_memset((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Joe Perches78d64fc2008-05-12 15:44:39 +0200314#define __memset(s, c, count) \
315 (__builtin_constant_p(count) \
316 ? __constant_count_memset((s), (c), (count)) \
317 : __memset_generic((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319#define __HAVE_ARCH_MEMSET
Joe Perches78d64fc2008-05-12 15:44:39 +0200320#define memset(s, c, count) \
321 (__builtin_constant_p(c) \
322 ? __constant_c_x_memset((s), (0x01010101UL * (unsigned char)(c)), \
323 (count)) \
324 : __memset((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326/*
327 * find the first occurrence of byte 'c', or 1 past the area if none
328 */
329#define __HAVE_ARCH_MEMSCAN
Joe Perches78d64fc2008-05-12 15:44:39 +0200330extern void *memscan(void *addr, int c, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332#endif /* __KERNEL__ */
333
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700334#endif /* _ASM_X86_STRING_32_H */