string_mm.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _M68K_STRING_H_
  2. #define _M68K_STRING_H_
  3. #include <linux/types.h>
  4. #include <linux/compiler.h>
  5. static inline size_t __kernel_strlen(const char *s)
  6. {
  7. const char *sc;
  8. for (sc = s; *sc++; )
  9. ;
  10. return sc - s - 1;
  11. }
  12. static inline char *__kernel_strcpy(char *dest, const char *src)
  13. {
  14. char *xdest = dest;
  15. asm volatile ("\n"
  16. "1: move.b (%1)+,(%0)+\n"
  17. " jne 1b"
  18. : "+a" (dest), "+a" (src)
  19. : : "memory");
  20. return xdest;
  21. }
  22. #ifndef __IN_STRING_C
  23. #define __HAVE_ARCH_STRLEN
  24. #define strlen(s) (__builtin_constant_p(s) ? \
  25. __builtin_strlen(s) : \
  26. __kernel_strlen(s))
  27. #define __HAVE_ARCH_STRNLEN
  28. static inline size_t strnlen(const char *s, size_t count)
  29. {
  30. const char *sc = s;
  31. asm volatile ("\n"
  32. "1: subq.l #1,%1\n"
  33. " jcs 2f\n"
  34. " tst.b (%0)+\n"
  35. " jne 1b\n"
  36. " subq.l #1,%0\n"
  37. "2:"
  38. : "+a" (sc), "+d" (count));
  39. return sc - s;
  40. }
  41. #define __HAVE_ARCH_STRCPY
  42. #if __GNUC__ >= 4
  43. #define strcpy(d, s) (__builtin_constant_p(s) && \
  44. __builtin_strlen(s) <= 32 ? \
  45. __builtin_strcpy(d, s) : \
  46. __kernel_strcpy(d, s))
  47. #else
  48. #define strcpy(d, s) __kernel_strcpy(d, s)
  49. #endif
  50. #define __HAVE_ARCH_STRNCPY
  51. static inline char *strncpy(char *dest, const char *src, size_t n)
  52. {
  53. char *xdest = dest;
  54. asm volatile ("\n"
  55. " jra 2f\n"
  56. "1: move.b (%1),(%0)+\n"
  57. " jeq 2f\n"
  58. " addq.l #1,%1\n"
  59. "2: subq.l #1,%2\n"
  60. " jcc 1b\n"
  61. : "+a" (dest), "+a" (src), "+d" (n)
  62. : : "memory");
  63. return xdest;
  64. }
  65. #define __HAVE_ARCH_STRCAT
  66. #define strcat(d, s) ({ \
  67. char *__d = (d); \
  68. strcpy(__d + strlen(__d), (s)); \
  69. })
  70. #define __HAVE_ARCH_STRCHR
  71. static inline char *strchr(const char *s, int c)
  72. {
  73. char sc, ch = c;
  74. for (; (sc = *s++) != ch; ) {
  75. if (!sc)
  76. return NULL;
  77. }
  78. return (char *)s - 1;
  79. }
  80. #define __HAVE_ARCH_STRCMP
  81. static inline int strcmp(const char *cs, const char *ct)
  82. {
  83. char res;
  84. asm ("\n"
  85. "1: move.b (%0)+,%2\n" /* get *cs */
  86. " cmp.b (%1)+,%2\n" /* compare a byte */
  87. " jne 2f\n" /* not equal, break out */
  88. " tst.b %2\n" /* at end of cs? */
  89. " jne 1b\n" /* no, keep going */
  90. " jra 3f\n" /* strings are equal */
  91. "2: sub.b -(%1),%2\n" /* *cs - *ct */
  92. "3:"
  93. : "+a" (cs), "+a" (ct), "=d" (res));
  94. return res;
  95. }
  96. #define __HAVE_ARCH_MEMSET
  97. extern void *memset(void *, int, __kernel_size_t);
  98. #define memset(d, c, n) __builtin_memset(d, c, n)
  99. #define __HAVE_ARCH_MEMCPY
  100. extern void *memcpy(void *, const void *, __kernel_size_t);
  101. #define memcpy(d, s, n) __builtin_memcpy(d, s, n)
  102. #define __HAVE_ARCH_MEMMOVE
  103. extern void *memmove(void *, const void *, __kernel_size_t);
  104. #define __HAVE_ARCH_MEMCMP
  105. extern int memcmp(const void *, const void *, __kernel_size_t);
  106. #define memcmp(d, s, n) __builtin_memcmp(d, s, n)
  107. #endif
  108. #endif /* _M68K_STRING_H_ */