1 #ifndef _M68K_STRING_H_
2 #define _M68K_STRING_H_
7 #define __HAVE_ARCH_STRCPY
8 static inline char * strcpy(char * dest,const char *src)
13 ("1:\tmoveb %1@+,%0@+\n\t"
15 : "=a" (dest), "=a" (src)
16 : "0" (dest), "1" (src) : "memory");
20 #define __HAVE_ARCH_STRNCPY
21 static inline char * strncpy(char *dest, const char *src, size_t n)
29 ("1:\tmoveb %1@+,%0@+\n\t"
34 : "=a" (dest), "=a" (src), "=d" (n)
35 : "0" (dest), "1" (src), "2" (n)
40 #define __HAVE_ARCH_STRCAT
41 static inline char * strcat(char * dest, const char * src)
47 while ((*dest++ = *src++))
53 #define __HAVE_ARCH_STRNCAT
54 static inline char * strncat(char *dest, const char *src, size_t count)
61 while ((*dest++ = *src++)) {
72 #define __HAVE_ARCH_STRCHR
73 static inline char * strchr(const char * s, int c)
84 #define __HAVE_ARCH_STRPBRK
85 extern inline char * strpbrk(const char * cs,const char * ct)
89 for( sc1 = cs; *sc1 != '\0'; ++sc1)
90 for( sc2 = ct; *sc2 != '\0'; ++sc2)
98 #define __HAVE_ARCH_STRSPN
99 static inline size_t strspn(const char *s, const char *accept)
105 for (p = s; *p != '\0'; ++p)
107 for (a = accept; *a != '\0'; ++a)
121 #define __HAVE_ARCH_STRTOK
122 extern inline char * strtok(char * s,const char * ct)
126 sbegin = s ? s : ___strtok;
130 sbegin += strspn(sbegin,ct);
131 if (*sbegin == '\0') {
135 send = strpbrk( sbegin, ct);
136 if (send && *send != '\0')
145 #define __HAVE_ARCH_STRLEN
146 static inline size_t strlen(const char * s)
149 for (sc = s; *sc != '\0'; ++sc) ;
155 #define __HAVE_ARCH_STRCMP
156 static inline int strcmp(const char * cs,const char * ct)
161 ("1:\tmoveb %0@+,%2\n\t" /* get *cs */
162 "cmpb %1@+,%2\n\t" /* compare a byte */
163 "jne 2f\n\t" /* not equal, break out */
164 "tstb %2\n\t" /* at end of cs? */
165 "jne 1b\n\t" /* no, keep going */
166 "jra 3f\n\t" /* strings are equal */
167 "2:\tsubb %1@-,%2\n\t" /* *cs - *ct */
169 : "=a" (cs), "=a" (ct), "=d" (__res)
170 : "0" (cs), "1" (ct));
174 #define __HAVE_ARCH_STRNCMP
175 static inline int strncmp(const char * cs,const char * ct,size_t count)
182 ("1:\tmovb %0@+,%3\n\t" /* get *cs */
183 "cmpb %1@+,%3\n\t" /* compare a byte */
184 "jne 3f\n\t" /* not equal, break out */
185 "tstb %3\n\t" /* at end of cs? */
186 "jeq 4f\n\t" /* yes, all done */
187 "subql #1,%2\n\t" /* no, adjust count */
188 "jne 1b\n\t" /* more to do, keep going */
189 "2:\tmoveq #0,%3\n\t" /* strings are equal */
191 "3:\tsubb %1@-,%3\n\t" /* *cs - *ct */
193 : "=a" (cs), "=a" (ct), "=d" (count), "=d" (__res)
194 : "0" (cs), "1" (ct), "2" (count));
198 #define __HAVE_ARCH_MEMSET
200 * This is really ugly, but its highly optimizatiable by the
201 * compiler and is meant as compensation for gcc's missing
202 * __builtin_memset(). For the 680[23]0 it might be worth considering
203 * the optimal number of misaligned writes compared to the number of
204 * tests'n'branches needed to align the destination address. The
205 * 680[46]0 doesn't really care due to their copy-back caches.
206 * 10/09/96 - Jes Sorensen
208 static inline void * __memset_g(void * s, int c, size_t count)
224 case 32: case 33: case 34: case 35:
226 case 28: case 29: case 30: case 31:
228 case 24: case 25: case 26: case 27:
230 case 20: case 21: case 22: case 23:
232 case 16: case 17: case 18: case 19:
234 case 12: case 13: case 14: case 15:
236 case 8: case 9: case 10: case 11:
238 case 4: case 5: case 6: case 7:
265 if (count > 2 && (long) s & 2)
297 * __memset_page assumes that data is longword aligned. Most, if not
298 * all, of these page sized memsets are performed on page aligned
299 * areas, thus we do not need to check if the destination is longword
300 * aligned. Of course we suffer a serious performance loss if this is
301 * not the case but I think the risk of this ever happening is
302 * extremely small. We spend a lot of time clearing pages in
303 * get_empty_page() so I think it is worth it anyway. Besides, the
304 * 680[46]0 do not really care about misaligned writes due to their
307 * The optimized case for the 680[46]0 is implemented using the move16
308 * instruction. My tests showed that this implementation is 35-45%
309 * faster than the original implementation using movel, the only
310 * caveat is that the destination address must be 16-byte aligned.
311 * 01/09/96 - Jes Sorensen
313 static inline void * __memset_page(void * s,int c,size_t count)
315 unsigned long data, tmp;
324 #ifdef CPU_M68040_OR_M68060_ONLY
326 if (((unsigned long) s) & 0x0f)
327 __memset_g(s, c, count);
329 *((unsigned long *)(s))++ = data;
330 *((unsigned long *)(s))++ = data;
331 *((unsigned long *)(s))++ = data;
332 *((unsigned long *)(s))++ = data;
334 __asm__ __volatile__("1:\t"
336 "move16 %2@+,%0@+\n\t"
341 : "=a" (s), "=d" (tmp)
342 : "a" (sp), "0" (s), "1" ((count - 16) / 16 - 1)
347 __asm__ __volatile__("1:\t"
357 : "=a" (s), "=d" (tmp)
358 : "d" (data), "0" (s), "1" (count / 32 - 1)
365 extern void *memset(void *,int,__kernel_size_t);
367 #define __memset_const(s,c,count) \
368 ((count==PAGE_SIZE) ? \
369 __memset_page((s),(c),(count)) : \
370 __memset_g((s),(c),(count)))
372 #define memset(s, c, count) \
373 (__builtin_constant_p(count) ? \
374 __memset_const((s),(c),(count)) : \
375 __memset_g((s),(c),(count)))
377 #define __HAVE_ARCH_MEMCPY
378 extern void * memcpy(void *, const void *, size_t );
380 * __builtin_memcpy() does not handle page-sized memcpys very well,
381 * thus following the same assumptions as for page-sized memsets, this
382 * function copies page-sized areas using an unrolled loop, without
383 * considering alignment.
385 * For the 680[46]0 only kernels we use the move16 instruction instead
386 * as it writes through the data-cache, invalidating the cache-lines
387 * touched. In this way we do not use up the entire data-cache (well,
388 * half of it on the 68060) by copying a page. An unrolled loop of two
389 * move16 instructions seem to the fastest. The only caveat is that
390 * both source and destination must be 16-byte aligned, if not we fall
391 * back to the generic memcpy function. - Jes
393 static inline void * __memcpy_page(void * to, const void * from, size_t count)
398 #ifdef CPU_M68040_OR_M68060_ONLY
400 if (((unsigned long) to | (unsigned long) from) & 0x0f)
401 return memcpy(to, from, count);
403 __asm__ __volatile__("1:\t"
405 "move16 %1@+,%0@+\n\t"
406 "move16 %1@+,%0@+\n\t"
409 : "=a" (to), "=a" (from), "=d" (tmp)
410 : "0" (to), "1" (from) , "2" (count / 32 - 1)
413 __asm__ __volatile__("1:\t"
414 "movel %1@+,%0@+\n\t"
415 "movel %1@+,%0@+\n\t"
416 "movel %1@+,%0@+\n\t"
417 "movel %1@+,%0@+\n\t"
418 "movel %1@+,%0@+\n\t"
419 "movel %1@+,%0@+\n\t"
420 "movel %1@+,%0@+\n\t"
421 "movel %1@+,%0@+\n\t"
423 : "=a" (to), "=a" (from), "=d" (tmp)
424 : "0" (to), "1" (from) , "2" (count / 32 - 1)
430 #define __memcpy_const(to, from, n) \
432 __memcpy_page((to),(from),(n)) : \
433 __builtin_memcpy((to),(from),(n)))
435 #define memcpy(to, from, n) \
436 (__builtin_constant_p(n) ? \
437 __memcpy_const((to),(from),(n)) : \
438 memcpy((to),(from),(n)))
440 #define __HAVE_ARCH_MEMMOVE
441 static inline void * memmove(void * dest,const void * src, size_t n)
454 const char *csrc = src;
460 if (n > 2 && (long) dest & 2)
463 const short *ssrc = src;
473 const long *lsrc = src;
484 const short *ssrc = src;
492 const char *csrc = src;
498 dest = (char *) dest + n;
499 src = (const char *) src + n;
503 const char *csrc = src;
509 if (n > 2 && (long) dest & 2)
512 const short *ssrc = src;
522 const long *lsrc = src;
533 const short *ssrc = src;
541 const char *csrc = src;
548 #define __HAVE_ARCH_MEMCMP
549 extern int memcmp(const void * ,const void * ,size_t );
550 #define memcmp(cs, ct, n) \
551 (__builtin_constant_p(n) ? \
552 __builtin_memcmp((cs),(ct),(n)) : \
553 memcmp((cs),(ct),(n)))
555 #define __HAVE_ARCH_MEMCHR
556 extern inline void * memchr(const void * cs, int c, size_t count) {
557 /* Someone else can optimize this, I don't care - tonym@mac.linux-m68k.org */
558 unsigned char *ret = (unsigned char *)cs;
559 for(;count>0;count--,ret++)
560 if(*ret == c) return ret;
565 #endif /* _M68K_STRING_H_ */