string.c 8.6 KB

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