patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-m68k / string.h
1 #ifndef _M68K_STRING_H_
2 #define _M68K_STRING_H_
3
4 #include <asm/setup.h>
5 #include <asm/page.h>
6
7 #define __HAVE_ARCH_STRCPY
8 static inline char * strcpy(char * dest,const char *src)
9 {
10   char *xdest = dest;
11
12   __asm__ __volatile__
13        ("1:\tmoveb %1@+,%0@+\n\t"
14         "jne 1b"
15         : "=a" (dest), "=a" (src)
16         : "0" (dest), "1" (src) : "memory");
17   return xdest;
18 }
19
20 #define __HAVE_ARCH_STRNCPY
21 static inline char * strncpy(char *dest, const char *src, size_t n)
22 {
23   char *xdest = dest;
24
25   if (n == 0)
26     return xdest;
27
28   __asm__ __volatile__
29        ("1:\tmoveb %1@+,%0@+\n\t"
30         "jeq 2f\n\t"
31         "subql #1,%2\n\t"
32         "jne 1b\n\t"
33         "2:"
34         : "=a" (dest), "=a" (src), "=d" (n)
35         : "0" (dest), "1" (src), "2" (n)
36         : "memory");
37   return xdest;
38 }
39
40 #define __HAVE_ARCH_STRCAT
41 static inline char * strcat(char * dest, const char * src)
42 {
43         char *tmp = dest;
44
45         while (*dest)
46                 dest++;
47         while ((*dest++ = *src++))
48                 ;
49
50         return tmp;
51 }
52
53 #define __HAVE_ARCH_STRNCAT
54 static inline char * strncat(char *dest, const char *src, size_t count)
55 {
56         char *tmp = dest;
57
58         if (count) {
59                 while (*dest)
60                         dest++;
61                 while ((*dest++ = *src++)) {
62                         if (--count == 0) {
63                                 *dest++='\0';
64                                 break;
65                         }
66                 }
67         }
68
69         return tmp;
70 }
71
72 #define __HAVE_ARCH_STRCHR
73 static inline char * strchr(const char * s, int c)
74 {
75   const char ch = c;
76
77   for(; *s != ch; ++s)
78     if (*s == '\0')
79       return( NULL );
80   return( (char *) s);
81 }
82
83 #if 0
84 #define __HAVE_ARCH_STRPBRK
85 static inline char *strpbrk(const char *cs,const char *ct)
86 {
87   const char *sc1,*sc2;
88
89   for( sc1 = cs; *sc1 != '\0'; ++sc1)
90     for( sc2 = ct; *sc2 != '\0'; ++sc2)
91       if (*sc1 == *sc2)
92         return((char *) sc1);
93   return( NULL );
94 }
95 #endif
96
97 #if 0
98 #define __HAVE_ARCH_STRSPN
99 static inline size_t strspn(const char *s, const char *accept)
100 {
101   const char *p;
102   const char *a;
103   size_t count = 0;
104
105   for (p = s; *p != '\0'; ++p)
106     {
107       for (a = accept; *a != '\0'; ++a)
108         if (*p == *a)
109           break;
110       if (*a == '\0')
111         return count;
112       else
113         ++count;
114     }
115
116   return count;
117 }
118 #endif
119
120 /* strstr !! */
121
122 #define __HAVE_ARCH_STRLEN
123 static inline size_t strlen(const char * s)
124 {
125   const char *sc;
126   for (sc = s; *sc != '\0'; ++sc) ;
127   return(sc - s);
128 }
129
130 /* strnlen !! */
131
132 #define __HAVE_ARCH_STRCMP
133 static inline int strcmp(const char * cs,const char * ct)
134 {
135   char __res;
136
137   __asm__
138        ("1:\tmoveb %0@+,%2\n\t" /* get *cs */
139         "cmpb %1@+,%2\n\t"      /* compare a byte */
140         "jne  2f\n\t"           /* not equal, break out */
141         "tstb %2\n\t"           /* at end of cs? */
142         "jne  1b\n\t"           /* no, keep going */
143         "jra  3f\n\t"           /* strings are equal */
144         "2:\tsubb %1@-,%2\n\t"  /* *cs - *ct */
145         "3:"
146         : "=a" (cs), "=a" (ct), "=d" (__res)
147         : "0" (cs), "1" (ct));
148   return __res;
149 }
150
151 #define __HAVE_ARCH_STRNCMP
152 static inline int strncmp(const char * cs,const char * ct,size_t count)
153 {
154   char __res;
155
156   if (!count)
157     return 0;
158   __asm__
159        ("1:\tmovb %0@+,%3\n\t"          /* get *cs */
160         "cmpb   %1@+,%3\n\t"            /* compare a byte */
161         "jne    3f\n\t"                 /* not equal, break out */
162         "tstb   %3\n\t"                 /* at end of cs? */
163         "jeq    4f\n\t"                 /* yes, all done */
164         "subql  #1,%2\n\t"              /* no, adjust count */
165         "jne    1b\n\t"                 /* more to do, keep going */
166         "2:\tmoveq #0,%3\n\t"           /* strings are equal */
167         "jra    4f\n\t"
168         "3:\tsubb %1@-,%3\n\t"          /* *cs - *ct */
169         "4:"
170         : "=a" (cs), "=a" (ct), "=d" (count), "=d" (__res)
171         : "0" (cs), "1" (ct), "2" (count));
172   return __res;
173 }
174
175 #define __HAVE_ARCH_MEMSET
176 /*
177  * This is really ugly, but its highly optimizatiable by the
178  * compiler and is meant as compensation for gcc's missing
179  * __builtin_memset(). For the 680[23]0 it might be worth considering
180  * the optimal number of misaligned writes compared to the number of
181  * tests'n'branches needed to align the destination address. The
182  * 680[46]0 doesn't really care due to their copy-back caches.
183  *                                              10/09/96 - Jes Sorensen
184  */
185 static inline void * __memset_g(void * s, int c, size_t count)
186 {
187   void *xs = s;
188   size_t temp;
189
190   if (!count)
191     return xs;
192
193   c &= 0xff;
194   c |= c << 8;
195   c |= c << 16;
196
197   if (count < 36){
198           long *ls = s;
199
200           switch(count){
201           case 32: case 33: case 34: case 35:
202                   *ls++ = c;
203           case 28: case 29: case 30: case 31:
204                   *ls++ = c;
205           case 24: case 25: case 26: case 27:
206                   *ls++ = c;
207           case 20: case 21: case 22: case 23:
208                   *ls++ = c;
209           case 16: case 17: case 18: case 19:
210                   *ls++ = c;
211           case 12: case 13: case 14: case 15:
212                   *ls++ = c;
213           case 8: case 9: case 10: case 11:
214                   *ls++ = c;
215           case 4: case 5: case 6: case 7:
216                   *ls++ = c;
217                   break;
218           default:
219                   break;
220           }
221           s = ls;
222           if (count & 0x02){
223                   short *ss = s;
224                   *ss++ = c;
225                   s = ss;
226           }
227           if (count & 0x01){
228                   char *cs = s;
229                   *cs++ = c;
230                   s = cs;
231           }
232           return xs;
233   }
234
235   if ((long) s & 1)
236     {
237       char *cs = s;
238       *cs++ = c;
239       s = cs;
240       count--;
241     }
242   if (count > 2 && (long) s & 2)
243     {
244       short *ss = s;
245       *ss++ = c;
246       s = ss;
247       count -= 2;
248     }
249   temp = count >> 2;
250   if (temp)
251     {
252       long *ls = s;
253       temp--;
254       do
255         *ls++ = c;
256       while (temp--);
257       s = ls;
258     }
259   if (count & 2)
260     {
261       short *ss = s;
262       *ss++ = c;
263       s = ss;
264     }
265   if (count & 1)
266     {
267       char *cs = s;
268       *cs = c;
269     }
270   return xs;
271 }
272
273 /*
274  * __memset_page assumes that data is longword aligned. Most, if not
275  * all, of these page sized memsets are performed on page aligned
276  * areas, thus we do not need to check if the destination is longword
277  * aligned. Of course we suffer a serious performance loss if this is
278  * not the case but I think the risk of this ever happening is
279  * extremely small. We spend a lot of time clearing pages in
280  * get_empty_page() so I think it is worth it anyway. Besides, the
281  * 680[46]0 do not really care about misaligned writes due to their
282  * copy-back cache.
283  *
284  * The optimized case for the 680[46]0 is implemented using the move16
285  * instruction. My tests showed that this implementation is 35-45%
286  * faster than the original implementation using movel, the only
287  * caveat is that the destination address must be 16-byte aligned.
288  *                                            01/09/96 - Jes Sorensen
289  */
290 static inline void * __memset_page(void * s,int c,size_t count)
291 {
292   unsigned long data, tmp;
293   void *xs, *sp;
294
295   xs = sp = s;
296
297   c = c & 255;
298   data = c | (c << 8);
299   data |= data << 16;
300
301 #ifdef CPU_M68040_OR_M68060_ONLY
302
303   if (((unsigned long) s) & 0x0f)
304           __memset_g(s, c, count);
305   else{
306           *((unsigned long *)(s))++ = data;
307           *((unsigned long *)(s))++ = data;
308           *((unsigned long *)(s))++ = data;
309           *((unsigned long *)(s))++ = data;
310
311           __asm__ __volatile__("1:\t"
312                                ".chip 68040\n\t"
313                                "move16 %2@+,%0@+\n\t"
314                                ".chip 68k\n\t"
315                                "subqw  #8,%2\n\t"
316                                "subqw  #8,%2\n\t"
317                                "dbra   %1,1b\n\t"
318                                : "=a" (s), "=d" (tmp)
319                                : "a" (sp), "0" (s), "1" ((count - 16) / 16 - 1)
320                                );
321   }
322
323 #else
324   __asm__ __volatile__("1:\t"
325                        "movel %2,%0@+\n\t"
326                        "movel %2,%0@+\n\t"
327                        "movel %2,%0@+\n\t"
328                        "movel %2,%0@+\n\t"
329                        "movel %2,%0@+\n\t"
330                        "movel %2,%0@+\n\t"
331                        "movel %2,%0@+\n\t"
332                        "movel %2,%0@+\n\t"
333                        "dbra  %1,1b\n\t"
334                        : "=a" (s), "=d" (tmp)
335                        : "d" (data), "0" (s), "1" (count / 32 - 1)
336                        );
337 #endif
338
339   return xs;
340 }
341
342 extern void *memset(void *,int,__kernel_size_t);
343
344 #define __memset_const(s,c,count) \
345 ((count==PAGE_SIZE) ? \
346   __memset_page((s),(c),(count)) : \
347   __memset_g((s),(c),(count)))
348
349 #define memset(s, c, count) \
350 (__builtin_constant_p(count) ? \
351  __memset_const((s),(c),(count)) : \
352  __memset_g((s),(c),(count)))
353
354 #define __HAVE_ARCH_MEMCPY
355 extern void * memcpy(void *, const void *, size_t );
356 /*
357  * __builtin_memcpy() does not handle page-sized memcpys very well,
358  * thus following the same assumptions as for page-sized memsets, this
359  * function copies page-sized areas using an unrolled loop, without
360  * considering alignment.
361  *
362  * For the 680[46]0 only kernels we use the move16 instruction instead
363  * as it writes through the data-cache, invalidating the cache-lines
364  * touched. In this way we do not use up the entire data-cache (well,
365  * half of it on the 68060) by copying a page. An unrolled loop of two
366  * move16 instructions seem to the fastest. The only caveat is that
367  * both source and destination must be 16-byte aligned, if not we fall
368  * back to the generic memcpy function.  - Jes
369  */
370 static inline void * __memcpy_page(void * to, const void * from, size_t count)
371 {
372   unsigned long tmp;
373   void *xto = to;
374
375 #ifdef CPU_M68040_OR_M68060_ONLY
376
377   if (((unsigned long) to | (unsigned long) from) & 0x0f)
378           return memcpy(to, from, count);
379
380   __asm__ __volatile__("1:\t"
381                        ".chip 68040\n\t"
382                        "move16 %1@+,%0@+\n\t"
383                        "move16 %1@+,%0@+\n\t"
384                        ".chip 68k\n\t"
385                        "dbra  %2,1b\n\t"
386                        : "=a" (to), "=a" (from), "=d" (tmp)
387                        : "0" (to), "1" (from) , "2" (count / 32 - 1)
388                        );
389 #else
390   __asm__ __volatile__("1:\t"
391                        "movel %1@+,%0@+\n\t"
392                        "movel %1@+,%0@+\n\t"
393                        "movel %1@+,%0@+\n\t"
394                        "movel %1@+,%0@+\n\t"
395                        "movel %1@+,%0@+\n\t"
396                        "movel %1@+,%0@+\n\t"
397                        "movel %1@+,%0@+\n\t"
398                        "movel %1@+,%0@+\n\t"
399                        "dbra  %2,1b\n\t"
400                        : "=a" (to), "=a" (from), "=d" (tmp)
401                        : "0" (to), "1" (from) , "2" (count / 32 - 1)
402                        );
403 #endif
404   return xto;
405 }
406
407 #define __memcpy_const(to, from, n) \
408 ((n==PAGE_SIZE) ? \
409   __memcpy_page((to),(from),(n)) : \
410   __builtin_memcpy((to),(from),(n)))
411
412 #define memcpy(to, from, n) \
413 (__builtin_constant_p(n) ? \
414  __memcpy_const((to),(from),(n)) : \
415  memcpy((to),(from),(n)))
416
417 #define __HAVE_ARCH_MEMMOVE
418 static inline void * memmove(void * dest,const void * src, size_t n)
419 {
420   void *xdest = dest;
421   size_t temp;
422
423   if (!n)
424     return xdest;
425
426   if (dest < src)
427     {
428       if ((long) dest & 1)
429         {
430           char *cdest = dest;
431           const char *csrc = src;
432           *cdest++ = *csrc++;
433           dest = cdest;
434           src = csrc;
435           n--;
436         }
437       if (n > 2 && (long) dest & 2)
438         {
439           short *sdest = dest;
440           const short *ssrc = src;
441           *sdest++ = *ssrc++;
442           dest = sdest;
443           src = ssrc;
444           n -= 2;
445         }
446       temp = n >> 2;
447       if (temp)
448         {
449           long *ldest = dest;
450           const long *lsrc = src;
451           temp--;
452           do
453             *ldest++ = *lsrc++;
454           while (temp--);
455           dest = ldest;
456           src = lsrc;
457         }
458       if (n & 2)
459         {
460           short *sdest = dest;
461           const short *ssrc = src;
462           *sdest++ = *ssrc++;
463           dest = sdest;
464           src = ssrc;
465         }
466       if (n & 1)
467         {
468           char *cdest = dest;
469           const char *csrc = src;
470           *cdest = *csrc;
471         }
472     }
473   else
474     {
475       dest = (char *) dest + n;
476       src = (const char *) src + n;
477       if ((long) dest & 1)
478         {
479           char *cdest = dest;
480           const char *csrc = src;
481           *--cdest = *--csrc;
482           dest = cdest;
483           src = csrc;
484           n--;
485         }
486       if (n > 2 && (long) dest & 2)
487         {
488           short *sdest = dest;
489           const short *ssrc = src;
490           *--sdest = *--ssrc;
491           dest = sdest;
492           src = ssrc;
493           n -= 2;
494         }
495       temp = n >> 2;
496       if (temp)
497         {
498           long *ldest = dest;
499           const long *lsrc = src;
500           temp--;
501           do
502             *--ldest = *--lsrc;
503           while (temp--);
504           dest = ldest;
505           src = lsrc;
506         }
507       if (n & 2)
508         {
509           short *sdest = dest;
510           const short *ssrc = src;
511           *--sdest = *--ssrc;
512           dest = sdest;
513           src = ssrc;
514         }
515       if (n & 1)
516         {
517           char *cdest = dest;
518           const char *csrc = src;
519           *--cdest = *--csrc;
520         }
521     }
522   return xdest;
523 }
524
525 #define __HAVE_ARCH_MEMCMP
526 extern int memcmp(const void * ,const void * ,size_t );
527 #define memcmp(cs, ct, n) \
528 (__builtin_constant_p(n) ? \
529  __builtin_memcmp((cs),(ct),(n)) : \
530  memcmp((cs),(ct),(n)))
531
532 #define __HAVE_ARCH_MEMCHR
533 static inline void *memchr(const void *cs, int c, size_t count)
534 {
535         /* Someone else can optimize this, I don't care - tonym@mac.linux-m68k.org */
536         unsigned char *ret = (unsigned char *)cs;
537         for(;count>0;count--,ret++)
538                 if(*ret == c) return ret;
539
540         return NULL;
541 }
542
543 #endif /* _M68K_STRING_H_ */