string.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /* Optimized, inlined string functions. i386 version.
  2. Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #ifndef _STRING_H
  17. # error "Never use <bits/string.h> directly; include <string.h> instead."
  18. #endif
  19. /* The ix86 processors can access unaligned multi-byte variables. */
  20. #define _STRING_ARCH_unaligned 1
  21. /* We only provide optimizations if the user selects them and if
  22. GNU CC is used. */
  23. #if !defined __NO_STRING_INLINES && defined __USE_STRING_INLINES \
  24. && defined __GNUC__ && __GNUC__ >= 2
  25. #ifndef __STRING_INLINE
  26. # ifdef __cplusplus
  27. # define __STRING_INLINE inline
  28. # else
  29. # define __STRING_INLINE extern __inline
  30. # endif
  31. #endif
  32. /* Copy N bytes of SRC to DEST. */
  33. #define _HAVE_STRING_ARCH_memcpy 1
  34. #define memcpy(dest, src, n) \
  35. (__extension__ (__builtin_constant_p (n) \
  36. ? __memcpy_c (dest, src, n) \
  37. : memcpy (dest, src, n)))
  38. /* This looks horribly ugly, but the compiler can optimize it totally,
  39. as the count is constant. */
  40. __STRING_INLINE void *__memcpy_c (void *__dest, __const void *__src,
  41. size_t __n);
  42. __STRING_INLINE void *
  43. __memcpy_c (void *__dest, __const void *__src, size_t __n)
  44. {
  45. register unsigned long int __d0, __d1, __d2;
  46. union {
  47. unsigned int __ui;
  48. unsigned short int __usi;
  49. unsigned char __uc;
  50. } *__u = __dest;
  51. switch (__n)
  52. {
  53. case 0:
  54. return __dest;
  55. case 1:
  56. __u->__uc = *(const unsigned char *) __src;
  57. return __dest;
  58. case 2:
  59. __u->__usi = *(const unsigned short int *) __src;
  60. return __dest;
  61. case 3:
  62. __u->__usi = *(const unsigned short int *) __src;
  63. __u = (void *) __u + 2;
  64. __u->__uc = *(2 + (const unsigned char *) __src);
  65. return __dest;
  66. case 4:
  67. __u->__ui = *(const unsigned int *) __src;
  68. return __dest;
  69. case 6:
  70. __u->__ui = *(const unsigned int *) __src;
  71. __u = (void *) __u + 4;
  72. __u->__usi = *(2 + (const unsigned short int *) __src);
  73. return __dest;
  74. case 8:
  75. __u->__ui = *(const unsigned int *) __src;
  76. __u = (void *) __u + 4;
  77. __u->__ui = *(1 + (const unsigned int *) __src);
  78. return __dest;
  79. case 12:
  80. __u->__ui = *(const unsigned int *) __src;
  81. __u = (void *) __u + 4;
  82. __u->__ui = *(1 + (const unsigned int *) __src);
  83. __u = (void *) __u + 4;
  84. __u->__ui = *(2 + (const unsigned int *) __src);
  85. return __dest;
  86. case 16:
  87. __u->__ui = *(const unsigned int *) __src;
  88. __u = (void *) __u + 4;
  89. __u->__ui = *(1 + (const unsigned int *) __src);
  90. __u = (void *) __u + 4;
  91. __u->__ui = *(2 + (const unsigned int *) __src);
  92. __u = (void *) __u + 4;
  93. __u->__ui = *(3 + (const unsigned int *) __src);
  94. return __dest;
  95. case 20:
  96. __u->__ui = *(const unsigned int *) __src;
  97. __u = (void *) __u + 4;
  98. __u->__ui = *(1 + (const unsigned int *) __src);
  99. __u = (void *) __u + 4;
  100. __u->__ui = *(2 + (const unsigned int *) __src);
  101. __u = (void *) __u + 4;
  102. __u->__ui = *(3 + (const unsigned int *) __src);
  103. __u = (void *) __u + 4;
  104. __u->__ui = *(4 + (const unsigned int *) __src);
  105. return __dest;
  106. }
  107. #define __COMMON_CODE(x) \
  108. __asm__ __volatile__ \
  109. ("cld\n\t" \
  110. "rep; movsl" \
  111. x \
  112. : "=&c" (__d0), "=&D" (__d1), "=&S" (__d2) \
  113. : "0" (__n / 4), "1" (&__u->__uc), "2" (__src) \
  114. : "memory");
  115. switch (__n % 4)
  116. {
  117. case 0:
  118. __COMMON_CODE ("");
  119. break;
  120. case 1:
  121. __COMMON_CODE ("\n\tmovsb");
  122. break;
  123. case 2:
  124. __COMMON_CODE ("\n\tmovsw");
  125. break;
  126. case 3:
  127. __COMMON_CODE ("\n\tmovsw\n\tmovsb");
  128. break;
  129. }
  130. return __dest;
  131. #undef __COMMON_CODE
  132. }
  133. /* Copy N bytes of SRC to DEST, guaranteeing
  134. correct behavior for overlapping strings. */
  135. #define _HAVE_STRING_ARCH_memmove 1
  136. #ifndef _FORCE_INLINES
  137. __STRING_INLINE void *
  138. memmove (void *__dest, __const void *__src, size_t __n)
  139. {
  140. register unsigned long int __d0, __d1, __d2;
  141. if (__dest < __src)
  142. __asm__ __volatile__
  143. ("cld\n\t"
  144. "rep\n\t"
  145. "movsb"
  146. : "=&c" (__d0), "=&S" (__d1), "=&D" (__d2)
  147. : "0" (__n), "1" (__src), "2" (__dest)
  148. : "memory");
  149. else
  150. __asm__ __volatile__
  151. ("std\n\t"
  152. "rep\n\t"
  153. "movsb\n\t"
  154. "cld"
  155. : "=&c" (__d0), "=&S" (__d1), "=&D" (__d2)
  156. : "0" (__n), "1" (__n - 1 + (const char *) __src),
  157. "2" (__n - 1 + (char *) __dest)
  158. : "memory");
  159. return __dest;
  160. }
  161. #endif
  162. /* Set N bytes of S to C. */
  163. #define _HAVE_STRING_ARCH_memset 1
  164. #define memset(s, c, n) \
  165. (__extension__ (__builtin_constant_p (c) \
  166. ? (__builtin_constant_p (n) \
  167. ? __memset_cc (s, 0x01010101UL * (unsigned char) (c), n) \
  168. : __memset_cg (s, 0x01010101UL * (unsigned char) (c), n))\
  169. : __memset_gg (s, c, n)))
  170. __STRING_INLINE void *__memset_cc (void *__s, unsigned long int __pattern,
  171. size_t __n);
  172. __STRING_INLINE void *
  173. __memset_cc (void *__s, unsigned long int __pattern, size_t __n)
  174. {
  175. register unsigned long int __d0, __d1;
  176. union {
  177. unsigned int __ui;
  178. unsigned short int __usi;
  179. unsigned char __uc;
  180. } *__u = __s;
  181. switch (__n)
  182. {
  183. case 0:
  184. return __s;
  185. case 1:
  186. __u->__uc = __pattern;
  187. return __s;
  188. case 2:
  189. __u->__usi = __pattern;
  190. return __s;
  191. case 3:
  192. __u->__usi = __pattern;
  193. __u = __extension__ ((void *) __u + 2);
  194. __u->__uc = __pattern;
  195. return __s;
  196. case 4:
  197. __u->__ui = __pattern;
  198. return __s;
  199. }
  200. #define __COMMON_CODE(x) \
  201. __asm__ __volatile__ \
  202. ("cld\n\t" \
  203. "rep; stosl" \
  204. x \
  205. : "=&c" (__d0), "=&D" (__d1) \
  206. : "a" (__pattern), "0" (__n / 4), "1" (&__u->__uc) \
  207. : "memory")
  208. switch (__n % 4)
  209. {
  210. case 0:
  211. __COMMON_CODE ("");
  212. break;
  213. case 1:
  214. __COMMON_CODE ("\n\tstosb");
  215. break;
  216. case 2:
  217. __COMMON_CODE ("\n\tstosw");
  218. break;
  219. case 3:
  220. __COMMON_CODE ("\n\tstosw\n\tstosb");
  221. break;
  222. }
  223. return __s;
  224. #undef __COMMON_CODE
  225. }
  226. __STRING_INLINE void *__memset_cg (void *__s, unsigned long __c, size_t __n);
  227. __STRING_INLINE void *
  228. __memset_cg (void *__s, unsigned long __c, size_t __n)
  229. {
  230. register unsigned long int __d0, __d1;
  231. __asm__ __volatile__
  232. ("cld\n\t"
  233. "rep; stosl\n\t"
  234. "testb $2,%b3\n\t"
  235. "je 1f\n\t"
  236. "stosw\n"
  237. "1:\n\t"
  238. "testb $1,%b3\n\t"
  239. "je 2f\n\t"
  240. "stosb\n"
  241. "2:"
  242. : "=&c" (__d0), "=&D" (__d1)
  243. : "a" (__c), "q" (__n), "0" (__n / 4), "1" (__s)
  244. : "memory");
  245. return __s;
  246. }
  247. __STRING_INLINE void *__memset_gg (void *__s, char __c, size_t __n);
  248. __STRING_INLINE void *
  249. __memset_gg (void *__s, char __c, size_t __n)
  250. {
  251. register unsigned long int __d0, __d1;
  252. __asm__ __volatile__
  253. ("cld\n\t"
  254. "rep; stosb"
  255. : "=&D" (__d0), "=&c" (__d1)
  256. : "a" (__c), "0" (__s), "1" (__n)
  257. : "memory");
  258. return __s;
  259. }
  260. /* Search N bytes of S for C. */
  261. #define _HAVE_STRING_ARCH_memchr 1
  262. #ifndef _FORCE_INLINES
  263. __STRING_INLINE void *
  264. memchr (__const void *__s, int __c, size_t __n)
  265. {
  266. register unsigned long int __d0;
  267. register void *__res;
  268. if (__n == 0)
  269. return NULL;
  270. __asm__ __volatile__
  271. ("cld\n\t"
  272. "repne; scasb\n\t"
  273. "je 1f\n\t"
  274. "movl $1,%0\n"
  275. "1:"
  276. : "=D" (__res), "=&c" (__d0)
  277. : "a" (__c), "0" (__s), "1" (__n));
  278. return __res - 1;
  279. }
  280. #endif
  281. /* Return the length of S. */
  282. #define _HAVE_STRING_ARCH_strlen 1
  283. #ifndef _FORCE_INLINES
  284. __STRING_INLINE size_t
  285. strlen (__const char *__str)
  286. {
  287. register unsigned long int __d0;
  288. register size_t __res;
  289. __asm__ __volatile__
  290. ("cld\n\t"
  291. "repne; scasb\n\t"
  292. "notl %0"
  293. : "=c" (__res), "=&D" (__d0)
  294. : "1" (__str), "a" (0), "0" (0xffffffff)
  295. : "cc");
  296. return __res - 1;
  297. }
  298. #endif
  299. /* Copy SRC to DEST. */
  300. #define _HAVE_STRING_ARCH_strcpy 1
  301. #ifndef _FORCE_INLINES
  302. __STRING_INLINE char *
  303. strcpy (char *__dest, __const char *__src)
  304. {
  305. register unsigned long int __d0, __d1;
  306. __asm__ __volatile__
  307. ("cld\n"
  308. "1:\n\t"
  309. "lodsb\n\t"
  310. "stosb\n\t"
  311. "testb %%al,%%al\n\t"
  312. "jne 1b"
  313. : "=&S" (__d0), "=&D" (__d1)
  314. : "0" (__src), "1" (__dest)
  315. : "ax", "memory", "cc");
  316. return __dest;
  317. }
  318. #endif
  319. /* Copy no more than N characters of SRC to DEST. */
  320. #define _HAVE_STRING_ARCH_strncpy 1
  321. #ifndef _FORCE_INLINES
  322. __STRING_INLINE char *
  323. strncpy (char *__dest, __const char *__src, size_t __n)
  324. {
  325. register unsigned long int __d0, __d1, __d2;
  326. __asm__ __volatile__
  327. ("cld\n"
  328. "1:\n\t"
  329. "decl %2\n\t"
  330. "js 2f\n\t"
  331. "lodsb\n\t"
  332. "stosb\n\t"
  333. "testb %%al,%%al\n\t"
  334. "jne 1b\n\t"
  335. "rep; stosb\n"
  336. "2:"
  337. : "=&S" (__d0), "=&D" (__d1), "=&c" (__d2)
  338. : "0" (__src), "1" (__dest), "2" (__n)
  339. : "ax", "memory", "cc");
  340. return __dest;
  341. }
  342. #endif
  343. /* Append SRC onto DEST. */
  344. #define _HAVE_STRING_ARCH_strcat 1
  345. #ifndef _FORCE_INLINES
  346. __STRING_INLINE char *
  347. strcat (char *__dest, __const char *__src)
  348. {
  349. register unsigned long int __d0, __d1, __d2, __d3;
  350. __asm__ __volatile__
  351. ("cld\n\t"
  352. "repne; scasb\n\t"
  353. "decl %1\n"
  354. "1:\n\t"
  355. "lodsb\n\t"
  356. "stosb\n\t"
  357. "testb %%al,%%al\n\t"
  358. "jne 1b"
  359. : "=&S" (__d0), "=&D" (__d1), "=&c" (__d2), "=&a" (__d3)
  360. : "0" (__src), "1" (__dest), "2" (0xffffffff), "3" (0)
  361. : "memory", "cc");
  362. return __dest;
  363. }
  364. #endif
  365. /* Append no more than N characters from SRC onto DEST. */
  366. #define _HAVE_STRING_ARCH_strncat 1
  367. #ifndef _FORCE_INLINES
  368. __STRING_INLINE char *
  369. strncat (char *__dest, __const char *__src, size_t __n)
  370. {
  371. register unsigned long int __d0, __d1, __d2, __d3;
  372. __asm__ __volatile__
  373. ("cld\n\t"
  374. "repne; scasb\n\t"
  375. "decl %1\n\t"
  376. "movl %4,%2\n"
  377. "1:\n\t"
  378. "decl %2\n\t"
  379. "js 2f\n\t"
  380. "lodsb\n\t"
  381. "stosb\n\t"
  382. "testb %%al,%%al\n\t"
  383. "jne 1b\n\t"
  384. "jmp 3f\n"
  385. "2:\n\t"
  386. "xorl %3,%3\n\t"
  387. "stosb\n"
  388. "3:"
  389. : "=&S" (__d0), "=&D" (__d1), "=&c" (__d2), "=&a" (__d3)
  390. : "g" (__n), "0" (__src), "1" (__dest), "2" (0xffffffff), "3" (0)
  391. : "memory", "cc");
  392. return __dest;
  393. }
  394. #endif
  395. /* Compare S1 and S2. */
  396. #define _HAVE_STRING_ARCH_strcmp 1
  397. #ifndef _FORCE_INLINES
  398. __STRING_INLINE int
  399. strcmp (__const char *__s1, __const char *__s2)
  400. {
  401. register unsigned long int __d0, __d1;
  402. register int __res;
  403. __asm__ __volatile__
  404. ("cld\n"
  405. "1:\n\t"
  406. "lodsb\n\t"
  407. "scasb\n\t"
  408. "jne 2f\n\t"
  409. "testb %%al,%%al\n\t"
  410. "jne 1b\n\t"
  411. "xorl %%eax,%%eax\n\t"
  412. "jmp 3f\n"
  413. "2:\n\t"
  414. "sbbl %%eax,%%eax\n\t"
  415. "orb $1,%%eax\n"
  416. "3:"
  417. : "=a" (__res), "=&S" (__d0), "=&D" (__d1)
  418. : "1" (__s1), "2" (__s2)
  419. : "cc");
  420. return __res;
  421. }
  422. #endif
  423. /* Compare N characters of S1 and S2. */
  424. #define _HAVE_STRING_ARCH_strncmp 1
  425. #ifndef _FORCE_INLINES
  426. __STRING_INLINE int
  427. strncmp (__const char *__s1, __const char *__s2, size_t __n)
  428. {
  429. register unsigned long int __d0, __d1, __d2;
  430. register int __res;
  431. __asm__ __volatile__
  432. ("cld\n"
  433. "1:\n\t"
  434. "decl %3\n\t"
  435. "js 2f\n\t"
  436. "lodsb\n\t"
  437. "scasb\n\t"
  438. "jne 3f\n\t"
  439. "testb %%al,%%al\n\t"
  440. "jne 1b\n"
  441. "2:\n\t"
  442. "xorl %%eax,%%eax\n\t"
  443. "jmp 4f\n"
  444. "3:\n\t"
  445. "sbbl %%eax,%%eax\n\t"
  446. "orb $1,%%al\n"
  447. "4:"
  448. : "=a" (__res), "=&S" (__d0), "=&D" (__d1), "=&c" (__d2)
  449. : "1" (__s1), "2" (__s2), "3" (__n)
  450. : "cc");
  451. return __res;
  452. }
  453. #endif
  454. /* Find the first occurrence of C in S. */
  455. #define _HAVE_STRING_ARCH_strchr 1
  456. #define strchr(s, c) \
  457. (__extension__ (__builtin_constant_p (c) \
  458. ? __strchr_c (s, ((c) & 0xff) << 8) \
  459. : __strchr_g (s, c)))
  460. __STRING_INLINE char *__strchr_g (__const char *__s, int __c);
  461. __STRING_INLINE char *
  462. __strchr_g (__const char *__s, int __c)
  463. {
  464. register unsigned long int __d0;
  465. register char *__res;
  466. __asm__ __volatile__
  467. ("cld\n\t"
  468. "movb %%al,%%ah\n"
  469. "1:\n\t"
  470. "lodsb\n\t"
  471. "cmpb %%ah,%%al\n\t"
  472. "je 2f\n\t"
  473. "testb %%al,%%al\n\t"
  474. "jne 1b\n\t"
  475. "movl $1,%1\n"
  476. "2:\n\t"
  477. "movl %1,%0"
  478. : "=a" (__res), "=&S" (__d0)
  479. : "0" (__c), "1" (__s)
  480. : "cc");
  481. return __res - 1;
  482. }
  483. __STRING_INLINE char *__strchr_c (__const char *__s, int __c);
  484. __STRING_INLINE char *
  485. __strchr_c (__const char *__s, int __c)
  486. {
  487. register unsigned long int __d0;
  488. register char *__res;
  489. __asm__ __volatile__
  490. ("cld\n\t"
  491. "1:\n\t"
  492. "lodsb\n\t"
  493. "cmpb %%ah,%%al\n\t"
  494. "je 2f\n\t"
  495. "testb %%al,%%al\n\t"
  496. "jne 1b\n\t"
  497. "movl $1,%1\n"
  498. "2:\n\t"
  499. "movl %1,%0"
  500. : "=a" (__res), "=&S" (__d0)
  501. : "0" (__c), "1" (__s)
  502. : "cc");
  503. return __res - 1;
  504. }
  505. /* Find the first occurrence of C in S or the final NUL byte. */
  506. #define _HAVE_STRING_ARCH_strchrnul 1
  507. #define __strchrnul(s, c) \
  508. (__extension__ (__builtin_constant_p (c) \
  509. ? ((c) == '\0' \
  510. ? (char *) __rawmemchr (s, c) \
  511. : __strchrnul_c (s, ((c) & 0xff) << 8)) \
  512. : __strchrnul_g (s, c)))
  513. __STRING_INLINE char *__strchrnul_g (__const char *__s, int __c);
  514. __STRING_INLINE char *
  515. __strchrnul_g (__const char *__s, int __c)
  516. {
  517. register unsigned long int __d0;
  518. register char *__res;
  519. __asm__ __volatile__
  520. ("cld\n\t"
  521. "movb %%al,%%ah\n"
  522. "1:\n\t"
  523. "lodsb\n\t"
  524. "cmpb %%ah,%%al\n\t"
  525. "je 2f\n\t"
  526. "testb %%al,%%al\n\t"
  527. "jne 1b\n\t"
  528. "2:\n\t"
  529. "movl %1,%0"
  530. : "=a" (__res), "=&S" (__d0)
  531. : "0" (__c), "1" (__s)
  532. : "cc");
  533. return __res - 1;
  534. }
  535. __STRING_INLINE char *__strchrnul_c (__const char *__s, int __c);
  536. __STRING_INLINE char *
  537. __strchrnul_c (__const char *__s, int __c)
  538. {
  539. register unsigned long int __d0;
  540. register char *__res;
  541. __asm__ __volatile__
  542. ("cld\n\t"
  543. "1:\n\t"
  544. "lodsb\n\t"
  545. "cmpb %%ah,%%al\n\t"
  546. "je 2f\n\t"
  547. "testb %%al,%%al\n\t"
  548. "jne 1b\n\t"
  549. "2:\n\t"
  550. "movl %1,%0"
  551. : "=a" (__res), "=&S" (__d0)
  552. : "0" (__c), "1" (__s)
  553. : "cc");
  554. return __res - 1;
  555. }
  556. #ifdef __USE_GNU
  557. # define strchrnul(s, c) __strchrnul (s, c)
  558. #endif
  559. /* Return the length of the initial segment of S which
  560. consists entirely of characters not in REJECT. */
  561. #define _HAVE_STRING_ARCH_strcspn 1
  562. #ifndef _FORCE_INLINES
  563. # ifdef __PIC__
  564. __STRING_INLINE size_t
  565. strcspn (__const char *__s, __const char *__reject)
  566. {
  567. register unsigned long int __d0, __d1, __d2;
  568. register char *__res;
  569. __asm__ __volatile__
  570. ("pushl %%ebx\n\t"
  571. "cld\n\t"
  572. "movl %4,%%edi\n\t"
  573. "repne; scasb\n\t"
  574. "notl %%ecx\n\t"
  575. "decl %%ecx\n\t"
  576. "movl %%ecx,%%ebx\n"
  577. "1:\n\t"
  578. "lodsb\n\t"
  579. "testb %%al,%%al\n\t"
  580. "je 2f\n\t"
  581. "movl %4,%%edi\n\t"
  582. "movl %%ebx,%%ecx\n\t"
  583. "repne; scasb\n\t"
  584. "jne 1b\n"
  585. "2:\n\t"
  586. "popl %%ebx"
  587. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&D" (__d2)
  588. : "d" (__reject), "0" (__s), "1" (0), "2" (0xffffffff)
  589. : "cc");
  590. return (__res - 1) - __s;
  591. }
  592. # else
  593. __STRING_INLINE size_t
  594. strcspn (__const char *__s, __const char *__reject)
  595. {
  596. register unsigned long int __d0, __d1, __d2, __d3;
  597. register char *__res;
  598. __asm__ __volatile__
  599. ("cld\n\t"
  600. "movl %5,%%edi\n\t"
  601. "repne; scasb\n\t"
  602. "notl %%ecx\n\t"
  603. "decl %%ecx\n\t"
  604. "movl %%ecx,%%edx\n"
  605. "1:\n\t"
  606. "lodsb\n\t"
  607. "testb %%al,%%al\n\t"
  608. "je 2f\n\t"
  609. "movl %5,%%edi\n\t"
  610. "movl %%edx,%%ecx\n\t"
  611. "repne; scasb\n\t"
  612. "jne 1b\n"
  613. "2:"
  614. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&d" (__d2), "=&D" (__d3)
  615. : "g" (__reject), "0" (__s), "1" (0), "2" (0xffffffff)
  616. : "cc");
  617. return (__res - 1) - __s;
  618. }
  619. # endif
  620. #endif
  621. /* Return the length of the initial segment of S which
  622. consists entirely of characters in ACCEPT. */
  623. #define _HAVE_STRING_ARCH_strspn 1
  624. #ifndef _FORCE_INLINES
  625. # ifdef __PIC__
  626. __STRING_INLINE size_t
  627. strspn (__const char *__s, __const char *__accept)
  628. {
  629. register unsigned long int __d0, __d1, __d2;
  630. register char *__res;
  631. __asm__ __volatile__
  632. ("pushl %%ebx\n\t"
  633. "cld\n\t"
  634. "movl %4,%%edi\n\t"
  635. "repne; scasb\n\t"
  636. "notl %%ecx\n\t"
  637. "decl %%ecx\n\t"
  638. "movl %%ecx,%%ebx\n"
  639. "1:\n\t"
  640. "lodsb\n\t"
  641. "testb %%al,%%al\n\t"
  642. "je 2f\n\t"
  643. "movl %4,%%edi\n\t"
  644. "movl %%ebx,%%ecx\n\t"
  645. "repne; scasb\n\t"
  646. "je 1b\n"
  647. "2:\n\t"
  648. "popl %%ebx"
  649. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&D" (__d2)
  650. : "r" (__accept), "0" (__s), "1" (0), "2" (0xffffffff)
  651. : "cc");
  652. return (__res - 1) - __s;
  653. }
  654. # else
  655. __STRING_INLINE size_t
  656. strspn (__const char *__s, __const char *__accept)
  657. {
  658. register unsigned long int __d0, __d1, __d2, __d3;
  659. register char *__res;
  660. __asm__ __volatile__
  661. ("cld\n\t"
  662. "movl %5,%%edi\n\t"
  663. "repne; scasb\n\t"
  664. "notl %%ecx\n\t"
  665. "decl %%ecx\n\t"
  666. "movl %%ecx,%%edx\n"
  667. "1:\n\t"
  668. "lodsb\n\t"
  669. "testb %%al,%%al\n\t"
  670. "je 2f\n\t"
  671. "movl %5,%%edi\n\t"
  672. "movl %%edx,%%ecx\n\t"
  673. "repne; scasb\n\t"
  674. "je 1b\n"
  675. "2:"
  676. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&d" (__d2), "=&D" (__d3)
  677. : "g" (__accept), "0" (__s), "1" (0), "2" (0xffffffff)
  678. : "cc");
  679. return (__res - 1) - __s;
  680. }
  681. # endif
  682. #endif
  683. /* Find the first occurrence in S of any character in ACCEPT. */
  684. #define _HAVE_STRING_ARCH_strpbrk 1
  685. #ifndef _FORCE_INLINES
  686. # ifdef __PIC__
  687. __STRING_INLINE char *
  688. strpbrk (__const char *__s, __const char *__accept)
  689. {
  690. unsigned long int __d0, __d1, __d2;
  691. register char *__res;
  692. __asm__ __volatile__
  693. ("pushl %%ebx\n\t"
  694. "cld\n\t"
  695. "movl %4,%%edi\n\t"
  696. "repne; scasb\n\t"
  697. "notl %%ecx\n\t"
  698. "decl %%ecx\n\t"
  699. "movl %%ecx,%%ebx\n"
  700. "1:\n\t"
  701. "lodsb\n\t"
  702. "testb %%al,%%al\n\t"
  703. "je 2f\n\t"
  704. "movl %4,%%edi\n\t"
  705. "movl %%ebx,%%ecx\n\t"
  706. "repne; scasb\n\t"
  707. "jne 1b\n\t"
  708. "decl %0\n\t"
  709. "jmp 3f\n"
  710. "2:\n\t"
  711. "xorl %0,%0\n"
  712. "3:\n\t"
  713. "popl %%ebx"
  714. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&D" (__d2)
  715. : "r" (__accept), "0" (__s), "1" (0), "2" (0xffffffff)
  716. : "cc");
  717. return __res;
  718. }
  719. # else
  720. __STRING_INLINE char *
  721. strpbrk (__const char *__s, __const char *__accept)
  722. {
  723. register unsigned long int __d0, __d1, __d2, __d3;
  724. register char *__res;
  725. __asm__ __volatile__
  726. ("cld\n\t"
  727. "movl %5,%%edi\n\t"
  728. "repne; scasb\n\t"
  729. "notl %%ecx\n\t"
  730. "decl %%ecx\n\t"
  731. "movl %%ecx,%%edx\n"
  732. "1:\n\t"
  733. "lodsb\n\t"
  734. "testb %%al,%%al\n\t"
  735. "je 2f\n\t"
  736. "movl %5,%%edi\n\t"
  737. "movl %%edx,%%ecx\n\t"
  738. "repne; scasb\n\t"
  739. "jne 1b\n\t"
  740. "decl %0\n\t"
  741. "jmp 3f\n"
  742. "2:\n\t"
  743. "xorl %0,%0\n"
  744. "3:"
  745. : "=&S" (__res), "=&a" (__d0), "=&c" (__d1), "=&d" (__d2), "=&D" (__d3)
  746. : "g" (__accept), "0" (__s), "1" (0), "2" (0xffffffff)
  747. : "cc");
  748. return __res;
  749. }
  750. # endif
  751. #endif
  752. /* Find the first occurrence of NEEDLE in HAYSTACK. */
  753. #define _HAVE_STRING_ARCH_strstr 1
  754. #ifndef _FORCE_INLINES
  755. # ifdef __PIC__
  756. __STRING_INLINE char *
  757. strstr (__const char *__haystack, __const char *__needle)
  758. {
  759. register unsigned long int __d0, __d1, __d2;
  760. register char *__res;
  761. __asm__ __volatile__
  762. ("pushl %%ebx\n\t"
  763. "cld\n\t" \
  764. "movl %4,%%edi\n\t"
  765. "repne; scasb\n\t"
  766. "notl %%ecx\n\t"
  767. "decl %%ecx\n\t" /* NOTE! This also sets Z if searchstring='' */
  768. "movl %%ecx,%%ebx\n"
  769. "1:\n\t"
  770. "movl %4,%%edi\n\t"
  771. "movl %%esi,%%eax\n\t"
  772. "movl %%ebx,%%ecx\n\t"
  773. "repe; cmpsb\n\t"
  774. "je 2f\n\t" /* also works for empty string, see above */
  775. "xchgl %%eax,%%esi\n\t"
  776. "incl %%esi\n\t"
  777. "cmpb $0,-1(%%eax)\n\t"
  778. "jne 1b\n\t"
  779. "xorl %%eax,%%eax\n\t"
  780. "2:\n\t"
  781. "popl %%ebx"
  782. : "=&a" (__res), "=&c" (__d0), "=&S" (__d1), "=&D" (__d2)
  783. : "r" (__needle), "0" (0), "1" (0xffffffff), "2" (__haystack)
  784. : "cc");
  785. return __res;
  786. }
  787. # else
  788. __STRING_INLINE char *
  789. strstr (__const char *__haystack, __const char *__needle)
  790. {
  791. register unsigned long int __d0, __d1, __d2, __d3;
  792. register char *__res;
  793. __asm__ __volatile__
  794. ("cld\n\t" \
  795. "movl %5,%%edi\n\t"
  796. "repne; scasb\n\t"
  797. "notl %%ecx\n\t"
  798. "decl %%ecx\n\t" /* NOTE! This also sets Z if searchstring='' */
  799. "movl %%ecx,%%edx\n"
  800. "1:\n\t"
  801. "movl %5,%%edi\n\t"
  802. "movl %%esi,%%eax\n\t"
  803. "movl %%edx,%%ecx\n\t"
  804. "repe; cmpsb\n\t"
  805. "je 2f\n\t" /* also works for empty string, see above */
  806. "xchgl %%eax,%%esi\n\t"
  807. "incl %%esi\n\t"
  808. "cmpb $0,-1(%%eax)\n\t"
  809. "jne 1b\n\t"
  810. "xorl %%eax,%%eax\n\t"
  811. "2:"
  812. : "=&a" (__res), "=&c" (__d0), "=&S" (__d1), "=&d" (__d2), "=&D" (__d3)
  813. : "g" (__needle), "0" (0), "1" (0xffffffff), "2" (__haystack)
  814. : "cc");
  815. return __res;
  816. }
  817. # endif
  818. #endif
  819. #ifndef _FORCE_INLINES
  820. # undef __STRING_INLINE
  821. #endif
  822. #endif /* use string inlines && GNU CC */