string.c 8.6 KB

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