string.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Optimized string functions
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 2004
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  7. */
  8. #define IN_ARCH_STRING_C 1
  9. #include <linux/types.h>
  10. #include <linux/module.h>
  11. /*
  12. * Helper functions to find the end of a string
  13. */
  14. static inline char *__strend(const char *s)
  15. {
  16. register unsigned long r0 asm("0") = 0;
  17. asm volatile ("0: srst %0,%1\n"
  18. " jo 0b"
  19. : "+d" (r0), "+a" (s) : : "cc" );
  20. return (char *) r0;
  21. }
  22. static inline char *__strnend(const char *s, size_t n)
  23. {
  24. register unsigned long r0 asm("0") = 0;
  25. const char *p = s + n;
  26. asm volatile ("0: srst %0,%1\n"
  27. " jo 0b"
  28. : "+d" (p), "+a" (s) : "d" (r0) : "cc" );
  29. return (char *) p;
  30. }
  31. /**
  32. * strlen - Find the length of a string
  33. * @s: The string to be sized
  34. *
  35. * returns the length of @s
  36. */
  37. size_t strlen(const char *s)
  38. {
  39. return __strend(s) - s;
  40. }
  41. EXPORT_SYMBOL(strlen);
  42. /**
  43. * strnlen - Find the length of a length-limited string
  44. * @s: The string to be sized
  45. * @n: The maximum number of bytes to search
  46. *
  47. * returns the minimum of the length of @s and @n
  48. */
  49. size_t strnlen(const char * s, size_t n)
  50. {
  51. return __strnend(s, n) - s;
  52. }
  53. EXPORT_SYMBOL(strnlen);
  54. /**
  55. * strcpy - Copy a %NUL terminated string
  56. * @dest: Where to copy the string to
  57. * @src: Where to copy the string from
  58. *
  59. * returns a pointer to @dest
  60. */
  61. char *strcpy(char *dest, const char *src)
  62. {
  63. register int r0 asm("0") = 0;
  64. char *ret = dest;
  65. asm volatile ("0: mvst %0,%1\n"
  66. " jo 0b"
  67. : "+&a" (dest), "+&a" (src) : "d" (r0)
  68. : "cc", "memory" );
  69. return ret;
  70. }
  71. EXPORT_SYMBOL(strcpy);
  72. /**
  73. * strlcpy - Copy a %NUL terminated string into a sized buffer
  74. * @dest: Where to copy the string to
  75. * @src: Where to copy the string from
  76. * @size: size of destination buffer
  77. *
  78. * Compatible with *BSD: the result is always a valid
  79. * NUL-terminated string that fits in the buffer (unless,
  80. * of course, the buffer size is zero). It does not pad
  81. * out the result like strncpy() does.
  82. */
  83. size_t strlcpy(char *dest, const char *src, size_t size)
  84. {
  85. size_t ret = __strend(src) - src;
  86. if (size) {
  87. size_t len = (ret >= size) ? size-1 : ret;
  88. dest[len] = '\0';
  89. memcpy(dest, src, len);
  90. }
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(strlcpy);
  94. /**
  95. * strncpy - Copy a length-limited, %NUL-terminated string
  96. * @dest: Where to copy the string to
  97. * @src: Where to copy the string from
  98. * @n: The maximum number of bytes to copy
  99. *
  100. * The result is not %NUL-terminated if the source exceeds
  101. * @n bytes.
  102. */
  103. char *strncpy(char *dest, const char *src, size_t n)
  104. {
  105. size_t len = __strnend(src, n) - src;
  106. memset(dest + len, 0, n - len);
  107. memcpy(dest, src, len);
  108. return dest;
  109. }
  110. EXPORT_SYMBOL(strncpy);
  111. /**
  112. * strcat - Append one %NUL-terminated string to another
  113. * @dest: The string to be appended to
  114. * @src: The string to append to it
  115. *
  116. * returns a pointer to @dest
  117. */
  118. char *strcat(char *dest, const char *src)
  119. {
  120. register int r0 asm("0") = 0;
  121. unsigned long dummy;
  122. char *ret = dest;
  123. asm volatile ("0: srst %0,%1\n"
  124. " jo 0b\n"
  125. "1: mvst %0,%2\n"
  126. " jo 1b"
  127. : "=&a" (dummy), "+a" (dest), "+a" (src)
  128. : "d" (r0), "0" (0UL) : "cc", "memory" );
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(strcat);
  132. /**
  133. * strlcat - Append a length-limited, %NUL-terminated string to another
  134. * @dest: The string to be appended to
  135. * @src: The string to append to it
  136. * @n: The size of the destination buffer.
  137. */
  138. size_t strlcat(char *dest, const char *src, size_t n)
  139. {
  140. size_t dsize = __strend(dest) - dest;
  141. size_t len = __strend(src) - src;
  142. size_t res = dsize + len;
  143. if (dsize < n) {
  144. dest += dsize;
  145. n -= dsize;
  146. if (len >= n)
  147. len = n - 1;
  148. dest[len] = '\0';
  149. memcpy(dest, src, len);
  150. }
  151. return res;
  152. }
  153. EXPORT_SYMBOL(strlcat);
  154. /**
  155. * strncat - Append a length-limited, %NUL-terminated string to another
  156. * @dest: The string to be appended to
  157. * @src: The string to append to it
  158. * @n: The maximum numbers of bytes to copy
  159. *
  160. * returns a pointer to @dest
  161. *
  162. * Note that in contrast to strncpy, strncat ensures the result is
  163. * terminated.
  164. */
  165. char *strncat(char *dest, const char *src, size_t n)
  166. {
  167. size_t len = __strnend(src, n) - src;
  168. char *p = __strend(dest);
  169. p[len] = '\0';
  170. memcpy(p, src, len);
  171. return dest;
  172. }
  173. EXPORT_SYMBOL(strncat);
  174. /**
  175. * strcmp - Compare two strings
  176. * @cs: One string
  177. * @ct: Another string
  178. *
  179. * returns 0 if @cs and @ct are equal,
  180. * < 0 if @cs is less than @ct
  181. * > 0 if @cs is greater than @ct
  182. */
  183. int strcmp(const char *cs, const char *ct)
  184. {
  185. register int r0 asm("0") = 0;
  186. int ret = 0;
  187. asm volatile ("0: clst %2,%3\n"
  188. " jo 0b\n"
  189. " je 1f\n"
  190. " ic %0,0(%2)\n"
  191. " ic %1,0(%3)\n"
  192. " sr %0,%1\n"
  193. "1:"
  194. : "+d" (ret), "+d" (r0), "+a" (cs), "+a" (ct)
  195. : : "cc" );
  196. return ret;
  197. }
  198. EXPORT_SYMBOL(strcmp);
  199. /**
  200. * strrchr - Find the last occurrence of a character in a string
  201. * @s: The string to be searched
  202. * @c: The character to search for
  203. */
  204. char * strrchr(const char * s, int c)
  205. {
  206. size_t len = __strend(s) - s;
  207. if (len)
  208. do {
  209. if (s[len] == (char) c)
  210. return (char *) s + len;
  211. } while (--len > 0);
  212. return NULL;
  213. }
  214. EXPORT_SYMBOL(strrchr);
  215. static inline int clcle(const char *s1, unsigned long l1,
  216. const char *s2, unsigned long l2)
  217. {
  218. register unsigned long r2 asm("2") = (unsigned long) s1;
  219. register unsigned long r3 asm("3") = (unsigned long) l1;
  220. register unsigned long r4 asm("4") = (unsigned long) s2;
  221. register unsigned long r5 asm("5") = (unsigned long) l2;
  222. int cc;
  223. asm volatile ("0: clcle %1,%3,0\n"
  224. " jo 0b\n"
  225. " ipm %0\n"
  226. " srl %0,28"
  227. : "=&d" (cc), "+a" (r2), "+a" (r3),
  228. "+a" (r4), "+a" (r5) : : "cc");
  229. return cc;
  230. }
  231. /**
  232. * strstr - Find the first substring in a %NUL terminated string
  233. * @s1: The string to be searched
  234. * @s2: The string to search for
  235. */
  236. char * strstr(const char * s1,const char * s2)
  237. {
  238. int l1, l2;
  239. l2 = __strend(s2) - s2;
  240. if (!l2)
  241. return (char *) s1;
  242. l1 = __strend(s1) - s1;
  243. while (l1-- >= l2) {
  244. int cc;
  245. cc = clcle(s1, l2, s2, l2);
  246. if (!cc)
  247. return (char *) s1;
  248. s1++;
  249. }
  250. return NULL;
  251. }
  252. EXPORT_SYMBOL(strstr);
  253. /**
  254. * memchr - Find a character in an area of memory.
  255. * @s: The memory area
  256. * @c: The byte to search for
  257. * @n: The size of the area.
  258. *
  259. * returns the address of the first occurrence of @c, or %NULL
  260. * if @c is not found
  261. */
  262. void *memchr(const void *s, int c, size_t n)
  263. {
  264. register int r0 asm("0") = (char) c;
  265. const void *ret = s + n;
  266. asm volatile ("0: srst %0,%1\n"
  267. " jo 0b\n"
  268. " jl 1f\n"
  269. " la %0,0\n"
  270. "1:"
  271. : "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );
  272. return (void *) ret;
  273. }
  274. EXPORT_SYMBOL(memchr);
  275. /**
  276. * memcmp - Compare two areas of memory
  277. * @cs: One area of memory
  278. * @ct: Another area of memory
  279. * @count: The size of the area.
  280. */
  281. int memcmp(const void *cs, const void *ct, size_t n)
  282. {
  283. int ret;
  284. ret = clcle(cs, n, ct, n);
  285. if (ret)
  286. ret = ret == 1 ? -1 : 1;
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(memcmp);
  290. /**
  291. * memscan - Find a character in an area of memory.
  292. * @s: The memory area
  293. * @c: The byte to search for
  294. * @n: The size of the area.
  295. *
  296. * returns the address of the first occurrence of @c, or 1 byte past
  297. * the area if @c is not found
  298. */
  299. void *memscan(void *s, int c, size_t n)
  300. {
  301. register int r0 asm("0") = (char) c;
  302. const void *ret = s + n;
  303. asm volatile ("0: srst %0,%1\n"
  304. " jo 0b\n"
  305. : "+a" (ret), "+&a" (s) : "d" (r0) : "cc" );
  306. return (void *) ret;
  307. }
  308. EXPORT_SYMBOL(memscan);