summaryrefslogtreecommitdiff
path: root/libc/string/i386/string.h
blob: cf4333dec0a6ef02bd82c615c352f031bf505f00 (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
/*
 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball
 */

#if !defined _STRING_H
#error "Never use <libc-string_i386.h> directly; include <string.h> instead"
#endif

#ifndef _LIBC_STRING_i386_H
#define _LIBC_STRING_i386_H 1

static __always_inline
void *inlined_memset_const_c_count4(void *s, unsigned eax, unsigned count)
{
	int ecx, edi;

	if (count == 0)
		return s;

	/* Very small (2 stores or less) are best done with direct
	 * mov <const>,<mem> instructions (they do not clobber registers) */
	if (count == 1) {
		*(char *)(s + 0) = eax;
		return s;
	}

	/* You wonder why & 0xff is needed? Try memset(p, '\xff', size).
	 * If char is signed, '\xff' == -1! */
	eax = (eax & 0xff) * 0x01010101; /* done at compile time */

	if (count == 2) {
		*(short *)(s + 0) = eax;
		return s;
	}
	if (count == 3) {
		*(short *)(s + 0) = eax;
		*(char *) (s + 2) = eax;
		return s;
	}
	if (count == 1*4 + 0) {
		*(int *)(s + 0) = eax;
		return s;
	}
	if (count == 1*4 + 1) {
		*(int *) (s + 0) = eax;
		*(char *)(s + 4) = eax;
		return s;
	}
	if (count == 1*4 + 2) {
		*(int *)  (s + 0) = eax;
		*(short *)(s + 4) = eax;
		return s;
	}

	/* Small string stores: don't clobber ecx
	 * (clobbers only eax and edi) */
#define small_store(arg) { \
	__asm__ __volatile__( \
		arg \
		: "=&D" (edi) \
		: "a" (eax), "0" (s) \
		: "memory" \
	); \
	return s; \
}
	if (count == 1*4 + 3) small_store("stosl; stosw; stosb");
	if (count == 2*4 + 0) {
		((int *)s)[0] = eax;
		((int *)s)[1] = eax;
		return s;
	}
	if (count == 2*4 + 1) small_store("stosl; stosl; stosb");
	if (count == 2*4 + 2) small_store("stosl; stosl; stosw");
	if (count == 2*4 + 3) small_store("stosl; stosl; stosw; stosb");
	if (count == 3*4 + 0) small_store("stosl; stosl; stosl");
	if (count == 3*4 + 1) small_store("stosl; stosl; stosl; stosb");
	if (count == 3*4 + 2) small_store("stosl; stosl; stosl; stosw");
	if (count == 3*4 + 3) small_store("stosl; stosl; stosl; stosw; stosb");
	if (count == 4*4 + 0) small_store("stosl; stosl; stosl; stosl");
	if (count == 4*4 + 1) small_store("stosl; stosl; stosl; stosl; stosb");
	/* going over 7 bytes is suboptimal */
	/* stosw is 2-byte insn, so this one takes 6 bytes: */
	if (count == 4*4 + 2) small_store("stosl; stosl; stosl; stosl; stosw");
	/* 7 bytes */
	if (count == 4*4 + 3) small_store("stosl; stosl; stosl; stosl; stosw; stosb");
	/* 5 bytes */
	if (count == 5*4 + 0) small_store("stosl; stosl; stosl; stosl; stosl");
	/* 6 bytes */
	if (count == 5*4 + 1) small_store("stosl; stosl; stosl; stosl; stosl; stosb");
	/* 7 bytes */
	if (count == 5*4 + 2) small_store("stosl; stosl; stosl; stosl; stosl; stosw");
	/* 8 bytes, but oh well... */
	if (count == 5*4 + 3) small_store("stosl; stosl; stosl; stosl; stosl; stosw; stosb");
	/* 6 bytes */
	if (count == 6*4 + 0) small_store("stosl; stosl; stosl; stosl; stosl; stosl");
	/* the rest would be 7+ bytes and is handled below instead */
#undef small_store

	/* Not small, but multiple-of-4 store.
	 * "mov <const>,%ecx; rep; stosl" sequence is 7 bytes */
	__asm__ __volatile__(
		"	rep; stosl\n"
		: "=&c" (ecx), "=&D" (edi)
		: "a" (eax), "0" (count / 4), "1" (s)
		: "memory"
	);
	return s;
}
#if 1 /* -51 bytes on shared i386 build with gcc 4.3.0 */
#define memset(s, c, count) ( \
	( !(__builtin_constant_p(c) && __builtin_constant_p(count)) \
	  || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
	) \
	? memset((s), (c), (count)) \
	: inlined_memset_const_c_count4((s), (c), (count)) \
	)
#endif


static __always_inline
void *inlined_mempcpy_const_count4(void *d, const void *s, unsigned count)
{
	int ecx;
	char *esi, *edi;

	if (count == 0)
		return d;

	if (count == 1) {
		*(char *)d = *(char *)s;
		return d + 1;
	}
	if (count == 2) {
		*(short *)d = *(short *)s;
		return d + 2;
	}
	/* Small string moves: don't clobber ecx
	 * (clobbers only esi and edi) */
#define small_move(arg) { \
	__asm__ __volatile__( \
		arg \
		: "=&S" (esi), "=&D" (edi) \
		: "0" (s), "1" (d) \
		: "memory" \
	); \
	return edi; \
}
	if (count == 3) small_move("movsw; movsb");
	if (count == 1*4 + 0) {
		*(int *)d = *(int *)s;
		return d + 4;
	}
	if (count == 1*4 + 1) small_move("movsl; movsb");
	if (count == 1*4 + 2) small_move("movsl; movsw");
	if (count == 1*4 + 3) small_move("movsl; movsw; movsb");
	if (count == 2*4 + 0) small_move("movsl; movsl");
	if (count == 2*4 + 1) small_move("movsl; movsl; movsb");
	if (count == 2*4 + 2) small_move("movsl; movsl; movsw");
	if (count == 2*4 + 3) small_move("movsl; movsl; movsw; movsb");
	if (count == 3*4 + 0) small_move("movsl; movsl; movsl");
	if (count == 3*4 + 1) small_move("movsl; movsl; movsl; movsb");
	if (count == 3*4 + 2) small_move("movsl; movsl; movsl; movsw");
	if (count == 3*4 + 3) small_move("movsl; movsl; movsl; movsw; movsb");
	if (count == 4*4 + 0) small_move("movsl; movsl; movsl; movsl");
	if (count == 4*4 + 1) small_move("movsl; movsl; movsl; movsl; movsb");
	/* going over 7 bytes is suboptimal */
	/* movsw is 2-byte insn, so this one takes 6 bytes: */
	if (count == 4*4 + 2) small_move("movsl; movsl; movsl; movsl; movsw");
	/* 7 bytes */
	if (count == 4*4 + 3) small_move("movsl; movsl; movsl; movsl; movsw; movsb");
	/* 5 bytes */
	if (count == 5*4 + 0) small_move("movsl; movsl; movsl; movsl; movsl");
	/* 6 bytes */
	if (count == 5*4 + 1) small_move("movsl; movsl; movsl; movsl; movsl; movsb");
	/* 7 bytes */
	if (count == 5*4 + 2) small_move("movsl; movsl; movsl; movsl; movsl; movsw");
	/* 8 bytes, but oh well... */
	if (count == 5*4 + 3) small_move("movsl; movsl; movsl; movsl; movsl; movsw; movsb");
	/* 6 bytes */
	if (count == 6*4 + 0) small_move("movsl; movsl; movsl; movsl; movsl; movsl");
	/* the rest would be 7+ bytes and is handled below instead */
#undef small_move

	/* Not small, but multiple-of-4 move.
	 * "mov <const>,%ecx; rep; movsl" sequence is 7 bytes */
	__asm__ __volatile__(
		"	rep; movsl\n"
		: "=&c" (ecx), "=&S" (esi), "=&D" (edi)
		: "0" (count / 4), "1" (s), "2" (d)
		: "memory"
	);
	return edi;
}
static __always_inline
void *inlined_memcpy_const_count4(void *d, const void *s, unsigned count)
{
	inlined_mempcpy_const_count4(d, s, count);
	return d;
}
#if 1 /* +34 bytes on shared i386 build with gcc 4.3.0 */
#define mempcpy(d, s, count) ( \
	( !(__builtin_constant_p(count)) \
	  || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
	) \
	? mempcpy((d), (s), (count)) \
	: inlined_mempcpy_const_count4((d), (s), (count)) \
	)
#define memcpy(d, s, count) ( \
	( !(__builtin_constant_p(count)) \
	  || ((count) > (6*4 + 0) && ((count) % 4) != 0) \
	) \
	? memcpy((d), (s), (count)) \
	: inlined_memcpy_const_count4((d), (s), (count)) \
	)
#endif


static __always_inline
size_t inlined_strlen(const char *s)
{
	int edi;
	int ecx;
	__asm__ __volatile__(
		"	repne; scasb\n"
	/*	"	notl	%0\n" */
	/*	"	decl	%0\n" */
		: "=c" (ecx), "=&D" (edi)
		: "1" (s), "a" (0), "0" (0xffffffffu)
		/* : no clobbers */
	);
	return -ecx - 1;
}
#if 0 /* +1108 bytes on shared i386 build with gcc 4.3.0 */
#define strlen(s) inlined_strlen(s)
#endif


static __always_inline
char *inlined_stpcpy(char *dest, const char *src)
{
	char *esi, *edi;
	int eax;
	__asm__ __volatile__(
		"1:	lodsb\n"
		"	stosb\n"
		"	testb	%%al, %%al\n"
		"	jnz	1b\n"
		: "=&S" (esi), "=&D" (edi), "=&a" (eax)
		: "0" (src), "1" (dest)
		: "memory"
	);
	return edi - 1;
}
static __always_inline
char *inlined_strcpy(char *dest, const char *src)
{
	inlined_stpcpy(dest, src);
	return dest;
}
#if 0 /* +562 bytes on shared i386 build with gcc 4.3.0 */
#define stpcpy(dest, src) inlined_stpcpy(dest, src)
#define strcpy(dest, src) inlined_strcpy(dest, src)
#endif


static __always_inline
void *inlined_memchr(const void *s, int c, size_t count)
{
	void *edi;
	int ecx;
	/* Unfortunately, c gets loaded to %eax (wide insn), not %al */
	__asm__ __volatile__(
		"	jecxz	1f\n"
		"	repne; scasb\n"
		"	leal	-1(%%edi), %%edi\n"
		"	je	2f\n"
		"1:\n"
		"	xorl	%%edi, %%edi\n"
		"2:\n"
		: "=&D" (edi), "=&c" (ecx)
		: "a" (c), "0" (s), "1" (count)
		/* : no clobbers */
	);
	return edi;
}
static __always_inline
void *inlined_memchr_const_c(const void *s, int c, size_t count)
{
#if defined __OPTIMIZE__
	void *edi;
	int ecx, eax;
	__asm__ __volatile__(
		"	jecxz	1f\n"
		"	movb	%4, %%al\n" /* const c to %%al */
		"	repne; scasb\n"
		"	leal	-1(%%edi), %%edi\n"
		"	je	2f\n"
		"1:\n"
		"	xorl	%%edi, %%edi\n"
		"2:\n"
		: "=&D" (edi), "=&c" (ecx), "=&a" (eax)
		: "0" (s), "i" (c), "1" (count)
		/* : no clobbers */
	);
	return edi;
#else
	/* With -O0, gcc can't figure out how to encode CONST c
	 * as an immediate operand. Generating slightly bigger code
	 * (usually "movl CONST,%eax", 3 bytes bigger than needed):
	 */
	void *edi;
	int ecx, eax;
	__asm__ __volatile__(
		"	jecxz	1f\n"
		"	repne; scasb\n"
		"	leal	-1(%%edi), %%edi\n"
		"	je	2f\n"
		"1:\n"
		"	xorl	%%edi, %%edi\n"
		"2:\n"
		: "=&D" (edi), "=&c" (ecx), "=&a" (eax)
		: "0" (s), "2" (c), "1" (count)
		/* : no clobbers */
	);
	return edi;
#endif
}
#if 1 /* +2 bytes on shared i386 build with gcc 4.3.0 */
#define memchr(s, c, count) ( \
	__builtin_constant_p(c) \
	? inlined_memchr_const_c(s, (c) & 0xff, count) \
	: inlined_memchr(s, c, count) \
	)
#endif

#endif /* _LIBC_STRING_i386_H  */