OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [linux/] [uClibc/] [libc/] [string/] [i386/] [string.c] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * This string-include defines all string functions as inline
3
 * functions. Use gcc. It also assumes ds=es=data space, this should be
4
 * normal. Most of the string-functions are rather heavily hand-optimized,
5
 * see especially strtok,strstr,str[c]spn. They should work, but are not
6
 * very easy to understand. Everything is done entirely within the register
7
 * set, making the functions fast and clean. String instructions have been
8
 * used through-out, making for "slightly" unclear code :-)
9
 *
10
 *              NO Copyright (C) 1991, 1992 Linus Torvalds,
11
 *              consider these trivial functions to be PD.
12
 *
13
 * Modified for uClibc by Erik Andersen <andersen@codepoet.org>
14
 * These make no attempt to use nifty things like mmx/3dnow/etc.
15
 * These are not inline, and will therefore not be as fast as
16
 * modifying the headers to use inlines (and cannot therefore
17
 * do tricky things when dealing with const memory).  But they
18
 * should (I hope!) be faster than their generic equivalents....
19
 *
20
 * More importantly, these should provide a good example for
21
 * others to follow when adding arch specific optimizations.
22
 *  -Erik
23
 */
24
 
25
#define _STDIO_UTILITY
26
#define _GNU_SOURCE
27
#include <string.h>
28
#include <locale.h> /* for __LOCALE_C_ONLY */
29
 
30
#ifdef L_strcpy
31
char * strcpy(char * dest, const char * src)
32
{
33
    int d0, d1, d2;
34
    __asm__ __volatile__(
35
            "1:\tlodsb\n\t"
36
            "stosb\n\t"
37
            "testb %%al,%%al\n\t"
38
            "jne 1b"
39
            : "=&S" (d0), "=&D" (d1), "=&a" (d2)
40
            :"0" (src),"1" (dest) : "memory");
41
    return dest;
42
}
43
#endif
44
 
45
 
46
#ifdef L_strncpy
47
char * strncpy(char * dest, const char * src, size_t count)
48
{
49
    int d0, d1, d2, d3;
50
    __asm__ __volatile__(
51
            "1:\tdecl %2\n\t"
52
            "js 2f\n\t"
53
            "lodsb\n\t"
54
            "stosb\n\t"
55
            "testb %%al,%%al\n\t"
56
            "jne 1b\n\t"
57
            "rep\n\t"
58
            "stosb\n"
59
            "2:"
60
            : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
61
            :"0" (src),"1" (dest),"2" (count) : "memory");
62
    return dest;
63
}
64
#endif
65
 
66
 
67
#ifdef L_strcat
68
char *strcat(char * dest, const char * src)
69
{
70
    int d0, d1, d2, d3;
71
    __asm__ __volatile__(
72
            "repne\n\t"
73
            "scasb\n\t"
74
            "decl %1\n"
75
            "1:\tlodsb\n\t"
76
            "stosb\n\t"
77
            "testb %%al,%%al\n\t"
78
            "jne 1b"
79
            : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
80
            : "0" (src), "1" (dest), "2" (0), "3" (0xffffffff):"memory");
81
    return dest;
82
}
83
#endif
84
 
85
 
86
#ifdef L_strncat
87
char *strncat(char * dest,
88
        const char * src, size_t count)
89
{
90
    int d0, d1, d2, d3;
91
    __asm__ __volatile__(
92
            "repne\n\t"
93
            "scasb\n\t"
94
            "decl %1\n\t"
95
            "movl %8,%3\n"
96
            "1:\tdecl %3\n\t"
97
            "js 2f\n\t"
98
            "lodsb\n\t"
99
            "stosb\n\t"
100
            "testb %%al,%%al\n\t"
101
            "jne 1b\n"
102
            "2:\txorl %2,%2\n\t"
103
            "stosb"
104
            : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
105
            : "0" (src),"1" (dest),"2" (0),"3" (0xffffffff), "g" (count)
106
            : "memory");
107
    return dest;
108
}
109
#endif
110
 
111
 
112
#ifdef L_strcmp
113
int strcmp(const char *cs, const char *ct)
114
{
115
    int d0, d1;
116
    register int __res;
117
    __asm__ __volatile__(
118
            "1:\tlodsb\n\t"
119
            "scasb\n\t"
120
            "jne 2f\n\t"
121
            "testb %%al,%%al\n\t"
122
            "jne 1b\n\t"
123
            "xorl %%eax,%%eax\n\t"
124
            "jmp 3f\n"
125
            "2:\tsbbl %%eax,%%eax\n\t"
126
            "orb $1,%%al\n"
127
            "3:"
128
            :"=a" (__res), "=&S" (d0), "=&D" (d1)
129
            :"1" (cs),"2" (ct));
130
    return __res;
131
}
132
#ifdef __LOCALE_C_ONLY
133
weak_alias(strcmp,strcoll);
134
#endif /* __LOCALE_C_ONLY */
135
#endif
136
 
137
 
138
#ifdef L_strncmp
139
int strncmp(const char *cs, const char *ct, size_t count)
140
{
141
    register int __res;
142
    int d0, d1, d2;
143
    __asm__ __volatile__(
144
            "1:\tdecl %3\n\t"
145
            "js 2f\n\t"
146
            "lodsb\n\t"
147
            "scasb\n\t"
148
            "jne 3f\n\t"
149
            "testb %%al,%%al\n\t"
150
            "jne 1b\n"
151
            "2:\txorl %%eax,%%eax\n\t"
152
            "jmp 4f\n"
153
            "3:\tsbbl %%eax,%%eax\n\t"
154
            "orb $1,%%al\n"
155
            "4:"
156
            :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
157
            :"1" (cs),"2" (ct),"3" (count));
158
    return __res;
159
}
160
#endif
161
 
162
 
163
#ifdef L_strchr
164
char * strchr(const char *s, int c)
165
{
166
    int d0;
167
    register char * __res;
168
    __asm__ __volatile__(
169
            "movb %%al,%%ah\n"
170
            "1:\tlodsb\n\t"
171
            "cmpb %%ah,%%al\n\t"
172
            "je 2f\n\t"
173
            "testb %%al,%%al\n\t"
174
            "jne 1b\n\t"
175
            "movl $1,%1\n"
176
            "2:\tmovl %1,%0\n\t"
177
            "decl %0"
178
            :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
179
    return __res;
180
}
181
weak_alias(strchr,index);
182
#endif
183
 
184
 
185
#ifdef L_strrchr
186
char *strrchr(const char *s, int c)
187
{
188
    int d0, d1;
189
    register char * __res;
190
    __asm__ __volatile__(
191
            "movb %%al,%%ah\n"
192
            "1:\tlodsb\n\t"
193
            "cmpb %%ah,%%al\n\t"
194
            "jne 2f\n\t"
195
            "leal -1(%%esi),%0\n"
196
            "2:\ttestb %%al,%%al\n\t"
197
            "jne 1b"
198
            :"=g" (__res), "=&S" (d0), "=&a" (d1) :"0" (0),"1" (s),"2" (c));
199
    return __res;
200
}
201
weak_alias(strrchr,rindex);
202
#endif
203
 
204
 
205
 
206
#ifdef L_strlen
207
size_t strlen(const char *s)
208
{
209
    int d0;
210
    register int __res;
211
    __asm__ __volatile__(
212
            "repne\n\t"
213
            "scasb\n\t"
214
            "notl %0\n\t"
215
            "decl %0"
216
            :"=c" (__res), "=&D" (d0) :"1" (s),"a" (0), "0" (0xffffffff));
217
    return __res;
218
}
219
#endif
220
 
221
 
222
#ifdef L_strnlen
223
size_t strnlen(const char *s, size_t count)
224
{
225
    int d0;
226
    register int __res;
227
    __asm__ __volatile__(
228
            "movl %2,%0\n\t"
229
            "jmp 2f\n"
230
            "1:\tcmpb $0,(%0)\n\t"
231
            "je 3f\n\t"
232
            "incl %0\n"
233
            "2:\tdecl %1\n\t"
234
            "cmpl $-1,%1\n\t"
235
            "jne 1b\n"
236
            "3:\tsubl %2,%0"
237
            :"=a" (__res), "=&d" (d0)
238
            :"c" (s),"1" (count));
239
    return __res;
240
}
241
#endif
242
 
243
 
244
#ifdef L_memcpy
245
void *memcpy(void * to, const void * from, size_t n)
246
{
247
    int d0, d1, d2;
248
    __asm__ __volatile__(
249
            "rep ; movsl\n\t"
250
            "testb $2,%b4\n\t"
251
            "je 1f\n\t"
252
            "movsw\n"
253
            "1:\ttestb $1,%b4\n\t"
254
            "je 2f\n\t"
255
            "movsb\n"
256
            "2:"
257
            : "=&c" (d0), "=&D" (d1), "=&S" (d2)
258
            :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
259
            : "memory");
260
    return (to);
261
}
262
#endif
263
 
264
 
265
#ifdef L_memmove
266
void *memmove(void *dest, const void *src, size_t n)
267
{
268
    int d0, d1, d2;
269
    if (dest<src)
270
        __asm__ __volatile__(
271
                "rep\n\t"
272
                "movsb"
273
                : "=&c" (d0), "=&S" (d1), "=&D" (d2)
274
                :"0" (n),"1" (src),"2" (dest)
275
                : "memory");
276
    else
277
        __asm__ __volatile__(
278
                "std\n\t"
279
                "rep\n\t"
280
                "movsb\n\t"
281
                "cld"
282
                : "=&c" (d0), "=&S" (d1), "=&D" (d2)
283
                :"0" (n),
284
                "1" (n-1+(const char *)src),
285
                "2" (n-1+(char *)dest)
286
                :"memory");
287
    return dest;
288
}
289
#endif
290
 
291
#ifdef L_memchr
292
void *memchr(const void *cs, int c, size_t count)
293
{
294
    int d0;
295
    register void * __res;
296
    if (!count)
297
        return NULL;
298
    __asm__ __volatile__(
299
            "repne\n\t"
300
            "scasb\n\t"
301
            "je 1f\n\t"
302
            "movl $1,%0\n"
303
            "1:\tdecl %0"
304
            :"=D" (__res), "=&c" (d0) : "a" (c),"0" (cs),"1" (count));
305
    return __res;
306
}
307
#endif
308
 
309
#ifdef L_memset
310
void *memset(void *s, int c, size_t count)
311
{
312
    int d0, d1;
313
    __asm__ __volatile__(
314
            "rep\n\t"
315
            "stosb"
316
            : "=&c" (d0), "=&D" (d1)
317
            :"a" (c),"1" (s),"0" (count)
318
            :"memory");
319
    return s;
320
}
321
#endif
322
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.