Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/include/i386/sstring.h
blob: bd2dd2d5134716b5b027fd4dd8bceb2f8ee75cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#if 0
#ifndef _I386_STRING_I486_H_
#define _I386_STRING_I486_H_
#if defined(__OPTIMIZE__) && defined(__GNUC__) && defined(__i386__)
/*
 * This string-include defines all string functions as inline
 * functions. Use gcc. It also assumes ds=es=data space, this should be
 * normal. Most of the string-functions are rather heavily hand-optimized,
 * see especially strtok,strstr,str[c]spn. They should work, but are not
 * very easy to understand. Everything is done entirely within the register
 * set, making the functions fast and clean. 
 *
 *		Copyright (C) 1991, 1992 Linus Torvalds
 *		Revised and optimized for i486/pentium
 *		1994/03/15 by Alberto Vignani/Davide Parodi @crf.it
 *
 *	Split into 2 CPU specific files by Alan Cox to keep #ifdef noise down.
 *
 *	Revised and optimized again by Jan Hubicka  (1997/11/16)
 *	(please report bugs to hubicka@paru.cas.cz)
 *
 *	memset and memcpy routines seems to be always faster at 486 and
 *	pentium but at pentium MMX they are sometimes bit slower (5-10%)..
 *	because of less strict register allocation they produces better code.
 */


#define __HAVE_ARCH_MEMCPY
#define memcpy(d,s,count) \
(__builtin_constant_p(count) ? \
 __memcpy_c((d),(s),(count)) : \
 __memcpy_g((d),(s),(count)))

/*
 *	These ought to get tweaked to do some cache priming.
 */


/* This implementation of the memcpy is designed for moveoldpoints from
 * mkrealloctables. It is expected to work well for both small and large
 * sizes.
 *
 * Small (1-10) and meduim (300) sizes seems to be important for XaoS. 
 * So implementation is not super fast for large sizes, but my experiemnts
 * don't show large improvements in speed anyway.
 *
 * We use rep movsX operations (they works well on PPro and don't seems to be
 * so bad on Pentium) and expect cld operation to be set. Hope that it will
 * not make problems.
 *
 * My attempt was to use c code where possible to let GCC do the
 */
extern inline void *__memcpy_g(void *to, const register void *from,
			       register size_t n);
extern inline void *__memcpy_g(void *to, const register void *from,
			       register size_t n)
{
    register void *tmp = (void *) to;
    if (n >= 7) {
	register int c = (-(int) to) & 3;
	n -= c;
	__asm__ __volatile__(	/*Align the destination */
				"rep\n\tmovsb":"=c"(c), "=D"(tmp),
				"=S"(from):"c"(c), "D"((long) tmp),
				"S"((long) from):"memory");
	c = n >> 2;
	__asm__ __volatile__(	/*Copy the main body */
				"rep\n\tmovsl":"=c"(c), "=D"(tmp),
				"=S"(from):"c"(c), "D"((long) tmp),
				"S"((long) from):"memory");
	n &= 3;
    }
    __asm__ __volatile__("rep\n\tmovsb":"=c"(n), "=D"(tmp),
			 "=S"(from):"c"(n), "D"((long) tmp),
			 "S"((long) from):"memory");
    return (to);
}

/*
 * This looks horribly ugly, but the compiler can optimize it totally,
 * as the count is constant.
 */

#define COMMON(x) \
__asm__ __volatile__ ( \
	"\n.align 4\n" \
	"1:\tmovl (%2),%0\n\t" \
	"addl $4,%2\n\t" \
	"movl %0,(%1)\n\t" \
	"addl $4,%1\n\t" \
	"decl %3\n\t" \
	"jnz 1b\n" \
	x \
	:"=r" (dummy1), "=r" (tmp), "=r" (from), "=r" (dummy2)  \
	:"1" (tmp), "2" (from), "3" (n/4) \
	:"memory"); \
return (to); \

extern inline void *__memcpy_c(void *to, const void *from, size_t n);
extern inline void *__memcpy_c(void *to, const void *from, size_t n)
{
    if (n < 24) {
	if (n >= 4)
	    ((unsigned long *) to)[0] = ((const unsigned long *) from)[0];
	if (n >= 8)
	    ((unsigned long *) to)[1] = ((const unsigned long *) from)[1];
	if (n >= 12)
	    ((unsigned long *) to)[2] = ((const unsigned long *) from)[2];
	if (n >= 16)
	    ((unsigned long *) to)[3] = ((const unsigned long *) from)[3];
	if (n >= 20)
	    ((unsigned long *) to)[4] = ((const unsigned long *) from)[4];
	switch ((unsigned int) (n % 4)) {
	case 3:
	    ((unsigned short *) to)[n / 2 - 1] =
		((const unsigned short *) from)[n / 2 - 1];
	    ((unsigned char *) to)[n - 1] =
		((const unsigned char *) from)[n - 1];
	    return to;
	case 2:
	    ((unsigned short *) to)[n / 2 - 1] =
		((const unsigned short *) from)[n / 2 - 1];
	    return to;
	case 1:
	    ((unsigned char *) to)[n - 1] =
		((const unsigned char *) from)[n - 1];
	case 0:
	    return to;
	}
    }
    {
	register void *tmp = (void *) to;
	register int dummy1, dummy2;
	switch ((unsigned int) (n % 4)) {
	case 0:
	    COMMON("");
	case 1:
	    COMMON("movb (%2),%b0 ; movb %b0,(%1)");
	case 2:
	    COMMON("movw (%2),%w0 ; movw %w0,(%1)");
	case 3:
	    COMMON("movw (%2),%w0 ; movw %w0,(%1)\n\t"
		   "movb 2(%2),%b0 ; movb %b0,2(%1)");
	}
    }
    return to;
}

#undef COMMON


#define __HAVE_ARCH_MEMMOVE
extern inline void *memmove(void *dest, const void *src, size_t n);
extern inline void *memmove(void *dest, const void *src, size_t n)
{
    register void *tmp = (void *) dest;
    if (dest < src)
	__asm__ __volatile__("cld\n\t" "rep\n\t" "movsb":	/* no output */
			     :"c"(n), "S"(src), "D"(tmp):"cx", "si", "di",
			     "memory");
    else
  __asm__ __volatile__("std\n\t" "rep\n\t" "movsb\n\t" "cld":	/* no output */
  : "c"(n), "S"(n - 1 + (const char *) src), "D"(n - 1 + (char *) tmp):"cx", "si", "di", "memory");
    return dest;
}

#define memcmp __builtin_memcmp

#define __HAVE_ARCH_MEMCHR
extern inline void *memchr(const void *cs, int c, size_t count);
extern inline void *memchr(const void *cs, int c, size_t count)
{
    register void *__res;
    if (!count)
	return NULL;
    __asm__ __volatile__("cld\n\t"
			 "repne\n\t"
			 "scasb\n\t"
			 "je 1f\n\t"
			 "movl $1,%0\n"
			 "1:\tdecl %0":"=D"(__res):"a"(c), "D"(cs),
			 "c"(count):"cx");
    return __res;
}



#define __HAVE_ARCH_MEMSET
#define memset(s,c,count) \
(__builtin_constant_p(c) ? \
 (__builtin_constant_p(count) ? \
  __memset_cc((s),(c),(count)) : \
  __memset_cg((s),(c),(count))) : \
 (__builtin_constant_p(count) ? \
  __memset_gc((s),(c),(count)) : \
  __memset_gg((s),(c),(count))))




extern inline void *__memset_cg(void *s, char c, size_t count);
extern inline void *__memset_cg(void *s, char c, size_t count)
{
    int tmp2;
    register void *tmp = (void *) s;
    __asm__ __volatile__("shrl $1,%%ecx\n\t"
			 "rep\n\t"
			 "stosw\n\t"
			 "jnc 1f\n\t"
			 "movb %%al,(%%edi)\n"
			 "1:":"=c"(tmp2), "=D"(tmp):"c"(count), "D"(tmp),
			 "a"(0x0101U * (unsigned char) c):"memory");
    return s;
}

extern inline void *__memset_gg(void *s, char c, size_t count);
extern inline void *__memset_gg(void *s, char c, size_t count)
{
    register void *tmp = (void *) s;
    int tmp2;
    __asm__ __volatile__("movb %%al,%%ah\n\t"
			 "shrl $1,%%ecx\n\t"
			 "rep\n\t"
			 "stosw\n\t"
			 "jnc 1f\n\t"
			 "movb %%al,(%%edi)\n"
			 "1:":"=c"(tmp2), "=D"(tmp):"c"(count), "D"(tmp),
			 "a"(c):"memory");
    return s;
}

/*
 * This non-rep routines are not much faster (slower for small strings)
 * but they allows better register allocation
 */
#define COMMON(x) \
__asm__ __volatile__ ( \
	"\n.align 4\n" \
	"1:\tmovl %k2,(%k0)\n\t" \
	"addl $4,%k0\n\t" \
	"decl %k1\n\t" \
	"jnz 1b\n" \
	x \
	:"=r" (tmp), "=r" (dummy) \
	:"q" ((unsigned) pattern),  "0"  (tmp), "1" (count/4) \
	:"memory"); \
return s;

extern inline void *__memset_cc(void *s, unsigned long pattern,
				size_t count);
extern inline void *__memset_cc(void *s, unsigned long pattern,
				size_t count)
{
    pattern = ((unsigned char) pattern) * 0x01010101UL;
    if (count < 24) {
	/*Handle small values manualy since they are incredibly slow */

	if (count >= 4)
	    *(unsigned long *) s = pattern;
	if (count >= 8)
	    ((unsigned long *) s)[1] = pattern;
	if (count >= 12)
	    ((unsigned long *) s)[2] = pattern;
	if (count >= 16)
	    ((unsigned long *) s)[3] = pattern;
	if (count >= 20)
	    ((unsigned long *) s)[4] = pattern;
	switch ((unsigned int) (count % 4)) {
	case 3:
	    ((unsigned short *) s)[count / 2 - 1] = pattern;
	    ((unsigned char *) s)[count - 1] = pattern;
	    return s;
	case 2:
	    ((unsigned short *) s)[count / 2 - 1] = pattern;
	    return s;
	case 1:
	    ((unsigned char *) s)[count - 1] = pattern;
	case 0:
	    return s;
	}
    } else {
	register void *tmp = (void *) s;
	register int dummy;
	switch ((unsigned int) (count % 4)) {
	case 0:
	    COMMON("");
	case 1:
	    COMMON("movb %b2,(%0)");
	case 2:
	    COMMON("movw %w2,(%0)");
	case 3:
	    COMMON("movw %w2,(%0)\n\tmovb %b2,2(%0)");
	}
    }
    return s;
}

extern inline void *__memset_gc(void *s, unsigned long pattern,
				size_t count);
extern inline void *__memset_gc(void *s, unsigned long pattern,
				size_t count)
{
    if (count < 4) {
	if (count > 1)
	  __asm__("movb %b0,%h0\n\t": "=q"(pattern):"0"((unsigned)
		pattern));
	switch ((unsigned int) (count)) {
	case 3:
	    ((unsigned short *) s)[0] = pattern;
	    ((unsigned char *) s)[2] = pattern;
	    return s;
	case 2:
	    *((unsigned short *) s) = pattern;
	    return s;
	case 1:
	    *(unsigned char *) s = pattern;
	case 0:
	    return s;
	}
    }

  __asm__("movb %b0,%h0\n\t" "pushw %w0\n\t" "shll $16,%k0\n\t" "popw %w0\n": "=q"(pattern):"0"((unsigned)
	pattern));

    if (count < 24) {
	/*Handle small values manualy since they are incredibly slow */

	*(unsigned long *) s = pattern;
	if (count >= 8)
	    ((unsigned long *) s)[1] = pattern;
	if (count >= 12)
	    ((unsigned long *) s)[2] = pattern;
	if (count >= 16)
	    ((unsigned long *) s)[3] = pattern;
	if (count >= 20)
	    ((unsigned long *) s)[4] = pattern;
	switch ((unsigned int) (count % 4)) {
	case 3:
	    ((unsigned short *) s)[count / 2 - 1] = pattern;
	    ((unsigned char *) s)[count - 1] = pattern;
	    return s;
	case 2:
	    ((unsigned short *) s)[count / 2 - 1] = pattern;
	    return s;
	case 1:
	    ((unsigned char *) s)[count - 1] = pattern;
	case 0:
	    return s;
	}
    } else {
	register void *tmp = (void *) s;
	register int dummy;
	switch ((unsigned int) (count % 4)) {
	case 0:
	    COMMON("");
	case 1:
	    COMMON("movb %b2,(%0)");
	case 2:
	    COMMON("movw %w2,(%0)");
	case 3:
	    COMMON("movw %w2,(%0)\n\tmovb %b2,2(%0)");
	}
    }
    return s;
}

#undef COMMON


/*
 * find the first occurrence of byte 'c', or 1 past the area if none
 */
#define __HAVE_ARCH_MEMSCAN
extern inline void *memscan(void *addr, int c, size_t size);
extern inline void *memscan(void *addr, int c, size_t size)
{
    if (!size)
	return addr;
    __asm__ __volatile__("cld\n\t"
			 "repnz; scasb\n\t"
			 "jnz 1f\n\t"
			 "dec %%edi\n\t"
			 "1:":"=D"(addr), "=c"(size):"0"(addr), "1"(size),
			 "a"(c));
    return addr;
}

#define memset_long(x,y,z) __memset_long(x,y,z)

extern inline void *__memset_long(void *s, char c, size_t count);
extern inline void *__memset_long(void *s, char c, size_t count)
{
    register unsigned int fill = c;
    register void *tmp = (void *) s;
    if (count >= 7) {
	register int c = (-(int) s) & 3;
/*__asm__ __volatile__ ("movb %b0,%h0":"=r"(fill):"r"(fill));*/
	fill |= fill << 8;
	count -= c;
	fill |= fill << 16;
	__asm__ __volatile__("rep\n\tstosb":"=c"(c), "=D"(tmp):"c"(c),
			     "D"(tmp), "a"(fill):"memory");
	c = count >> 2;
	__asm__ __volatile__("rep\n\tstosl":"=c"(c), "=D"(tmp):"c"(c),
			     "D"(tmp), "a"(fill):"memory");
	count &= 3;
    }
    __asm__ __volatile__("rep\n\tstosb":"=c"(count), "=D"(tmp):"c"(count),
			 "D"(tmp), "a"((char) fill):"memory");
    return s;
}
#endif
#endif
#endif