blob: 3d3e8353ee5c09db9b83000efde37a9e34365690 [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;
Joe Perches78d64fc2008-05-12 15:44:39 +020068 case 3:
69 *(short *)to = *(short *)from;
70 *((char *)to + 2) = *((char *)from + 2);
71 return to;
72 case 5:
73 *(int *)to = *(int *)from;
74 *((char *)to + 4) = *((char *)from + 4);
75 return to;
76 case 6:
77 *(int *)to = *(int *)from;
78 *((short *)to + 2) = *((short *)from + 2);
79 return to;
80 case 8:
81 *(int *)to = *(int *)from;
82 *((int *)to + 1) = *((int *)from + 1);
83 return to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Joe Perches78d64fc2008-05-12 15:44:39 +020085
86 esi = (long)from;
87 edi = (long)to;
88 if (n >= 5 * 4) {
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070089 /* large block: use rep prefix */
90 int ecx;
Joe Perches78d64fc2008-05-12 15:44:39 +020091 asm volatile("rep ; movsl"
92 : "=&c" (ecx), "=&D" (edi), "=&S" (esi)
93 : "0" (n / 4), "1" (edi), "2" (esi)
94 : "memory"
Denis Vlasenkod5b63d72005-05-01 08:58:48 -070095 );
96 } else {
97 /* small block: don't clobber ecx + smaller code */
Joe Perches78d64fc2008-05-12 15:44:39 +020098 if (n >= 4 * 4)
99 asm volatile("movsl"
100 : "=&D"(edi), "=&S"(esi)
101 : "0"(edi), "1"(esi)
102 : "memory");
103 if (n >= 3 * 4)
104 asm volatile("movsl"
105 : "=&D"(edi), "=&S"(esi)
106 : "0"(edi), "1"(esi)
107 : "memory");
108 if (n >= 2 * 4)
109 asm volatile("movsl"
110 : "=&D"(edi), "=&S"(esi)
111 : "0"(edi), "1"(esi)
112 : "memory");
113 if (n >= 1 * 4)
114 asm volatile("movsl"
115 : "=&D"(edi), "=&S"(esi)
116 : "0"(edi), "1"(esi)
117 : "memory");
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700118 }
119 switch (n % 4) {
120 /* tail */
Joe Perches78d64fc2008-05-12 15:44:39 +0200121 case 0:
122 return to;
123 case 1:
124 asm volatile("movsb"
125 : "=&D"(edi), "=&S"(esi)
126 : "0"(edi), "1"(esi)
127 : "memory");
128 return to;
129 case 2:
130 asm volatile("movsw"
131 : "=&D"(edi), "=&S"(esi)
132 : "0"(edi), "1"(esi)
133 : "memory");
134 return to;
135 default:
136 asm volatile("movsw\n\tmovsb"
137 : "=&D"(edi), "=&S"(esi)
138 : "0"(edi), "1"(esi)
139 : "memory");
140 return to;
Denis Vlasenkod5b63d72005-05-01 08:58:48 -0700141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144#define __HAVE_ARCH_MEMCPY
145
146#ifdef CONFIG_X86_USE_3DNOW
147
148#include <asm/mmx.h>
149
150/*
151 * This CPU favours 3DNow strongly (eg AMD Athlon)
152 */
153
Joe Perches78d64fc2008-05-12 15:44:39 +0200154static inline void *__constant_memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 if (len < 512)
157 return __constant_memcpy(to, from, len);
158 return _mmx_memcpy(to, from, len);
159}
160
Joe Perches78d64fc2008-05-12 15:44:39 +0200161static inline void *__memcpy3d(void *to, const void *from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 if (len < 512)
164 return __memcpy(to, from, len);
165 return _mmx_memcpy(to, from, len);
166}
167
Joe Perches78d64fc2008-05-12 15:44:39 +0200168#define memcpy(t, f, n) \
169 (__builtin_constant_p((n)) \
170 ? __constant_memcpy3d((t), (f), (n)) \
171 : __memcpy3d((t), (f), (n)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173#else
174
175/*
176 * No 3D Now!
177 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200178
Vegard Nossumf8561292008-04-04 00:53:23 +0200179#ifndef CONFIG_KMEMCHECK
Arjan van de Venff60fab2009-09-28 14:21:22 +0200180
181#if (__GNUC__ >= 4)
182#define memcpy(t, f, n) __builtin_memcpy(t, f, n)
183#else
Joe Perches78d64fc2008-05-12 15:44:39 +0200184#define memcpy(t, f, n) \
185 (__builtin_constant_p((n)) \
186 ? __constant_memcpy((t), (f), (n)) \
187 : __memcpy((t), (f), (n)))
Arjan van de Venff60fab2009-09-28 14:21:22 +0200188#endif
Vegard Nossumf8561292008-04-04 00:53:23 +0200189#else
190/*
191 * kmemcheck becomes very happy if we use the REP instructions unconditionally,
192 * because it means that we know both memory operands in advance.
193 */
194#define memcpy(t, f, n) __memcpy((t), (f), (n))
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#endif
198
199#define __HAVE_ARCH_MEMMOVE
Joe Perches78d64fc2008-05-12 15:44:39 +0200200void *memmove(void *dest, const void *src, size_t n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202#define memcmp __builtin_memcmp
203
204#define __HAVE_ARCH_MEMCHR
Joe Perches78d64fc2008-05-12 15:44:39 +0200205extern void *memchr(const void *cs, int c, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Joe Perches78d64fc2008-05-12 15:44:39 +0200207static inline void *__memset_generic(void *s, char c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Joe Perches78d64fc2008-05-12 15:44:39 +0200209 int d0, d1;
210 asm volatile("rep\n\t"
211 "stosb"
212 : "=&c" (d0), "=&D" (d1)
213 : "a" (c), "1" (s), "0" (count)
214 : "memory");
215 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218/* we might want to write optimized versions of these later */
Joe Perches78d64fc2008-05-12 15:44:39 +0200219#define __constant_count_memset(s, c, count) __memset_generic((s), (c), (count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
Joe Perches78d64fc2008-05-12 15:44:39 +0200222 * memset(x, 0, y) is a reasonably common thing to do, so we want to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * things 32 bits at a time even when we don't know the size of the
224 * area at compile-time..
225 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200226static __always_inline
227void *__constant_c_memset(void *s, unsigned long c, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Joe Perches78d64fc2008-05-12 15:44:39 +0200229 int d0, d1;
230 asm volatile("rep ; stosl\n\t"
231 "testb $2,%b3\n\t"
232 "je 1f\n\t"
233 "stosw\n"
234 "1:\ttestb $1,%b3\n\t"
235 "je 2f\n\t"
236 "stosb\n"
237 "2:"
238 : "=&c" (d0), "=&D" (d1)
239 : "a" (c), "q" (count), "0" (count/4), "1" ((long)s)
240 : "memory");
241 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/* Added by Gertjan van Wingerde to make minix and sysv module work */
245#define __HAVE_ARCH_STRNLEN
Joe Perches78d64fc2008-05-12 15:44:39 +0200246extern size_t strnlen(const char *s, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* end of additional stuff */
248
249#define __HAVE_ARCH_STRSTR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250extern char *strstr(const char *cs, const char *ct);
251
252/*
253 * This looks horribly ugly, but the compiler can optimize it totally,
254 * as we by now know that both pattern and count is constant..
255 */
Joe Perches78d64fc2008-05-12 15:44:39 +0200256static __always_inline
257void *__constant_c_and_count_memset(void *s, unsigned long pattern,
258 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 switch (count) {
Joe Perches78d64fc2008-05-12 15:44:39 +0200261 case 0:
262 return s;
263 case 1:
264 *(unsigned char *)s = pattern & 0xff;
265 return s;
266 case 2:
267 *(unsigned short *)s = pattern & 0xffff;
268 return s;
269 case 3:
270 *(unsigned short *)s = pattern & 0xffff;
271 *((unsigned char *)s + 2) = pattern & 0xff;
272 return s;
273 case 4:
274 *(unsigned long *)s = pattern;
275 return s;
276 }
277
278#define COMMON(x) \
279 asm volatile("rep ; stosl" \
280 x \
281 : "=&c" (d0), "=&D" (d1) \
H. Peter Anvin1a20d3e2008-05-26 13:36:53 -0700282 : "a" (eax), "0" (count/4), "1" ((long)s) \
Joe Perches78d64fc2008-05-12 15:44:39 +0200283 : "memory")
284
285 {
286 int d0, d1;
H. Peter Anvin1a20d3e2008-05-26 13:36:53 -0700287#if __GNUC__ == 4 && __GNUC_MINOR__ == 0
288 /* Workaround for broken gcc 4.0 */
289 register unsigned long eax asm("%eax") = pattern;
290#else
291 unsigned long eax = pattern;
292#endif
293
Joe Perches78d64fc2008-05-12 15:44:39 +0200294 switch (count % 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case 0:
Joe Perches78d64fc2008-05-12 15:44:39 +0200296 COMMON("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return s;
298 case 1:
Joe Perches78d64fc2008-05-12 15:44:39 +0200299 COMMON("\n\tstosb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return s;
301 case 2:
Joe Perches78d64fc2008-05-12 15:44:39 +0200302 COMMON("\n\tstosw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return s;
Joe Perches78d64fc2008-05-12 15:44:39 +0200304 default:
305 COMMON("\n\tstosw\n\tstosb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return s;
Joe Perches78d64fc2008-05-12 15:44:39 +0200307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Joe Perches78d64fc2008-05-12 15:44:39 +0200309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#undef COMMON
311}
312
Joe Perches78d64fc2008-05-12 15:44:39 +0200313#define __constant_c_x_memset(s, c, count) \
314 (__builtin_constant_p(count) \
315 ? __constant_c_and_count_memset((s), (c), (count)) \
316 : __constant_c_memset((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Joe Perches78d64fc2008-05-12 15:44:39 +0200318#define __memset(s, c, count) \
319 (__builtin_constant_p(count) \
320 ? __constant_count_memset((s), (c), (count)) \
321 : __memset_generic((s), (c), (count)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323#define __HAVE_ARCH_MEMSET
Arjan van de Venff60fab2009-09-28 14:21:22 +0200324#if (__GNUC__ >= 4)
325#define memset(s, c, count) __builtin_memset(s, c, count)
326#else
Joe Perches78d64fc2008-05-12 15:44:39 +0200327#define memset(s, c, count) \
328 (__builtin_constant_p(c) \
329 ? __constant_c_x_memset((s), (0x01010101UL * (unsigned char)(c)), \
330 (count)) \
331 : __memset((s), (c), (count)))
Arjan van de Venff60fab2009-09-28 14:21:22 +0200332#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/*
335 * find the first occurrence of byte 'c', or 1 past the area if none
336 */
337#define __HAVE_ARCH_MEMSCAN
Joe Perches78d64fc2008-05-12 15:44:39 +0200338extern void *memscan(void *addr, int c, size_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#endif /* __KERNEL__ */
341
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700342#endif /* _ASM_X86_STRING_32_H */