string.c 8.7 KB

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