strcspn.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* strcspn (str, ss) -- Return the length of the initial segment of STR
  2. which contains no characters from SS.
  3. For AMD x86-64.
  4. Copyright (C) 1994-1997, 2000, 2002, 2003, 2004, 2005
  5. Free Software Foundation, Inc.
  6. This file is part of the GNU C Library.
  7. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>.
  8. Bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>.
  9. Adopted for x86-64 by Andreas Jaeger <aj@suse.de>.
  10. The GNU C Library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2.1 of the License, or (at your option) any later version.
  14. The GNU C Library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with the GNU C Library; if not, write to the Free
  20. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. 02111-1307 USA. */
  22. #include "_glibc_inc.h"
  23. /* Seems to be unrolled too much */
  24. /* BEWARE: `#ifdef strcspn' means that strcspn is redefined as `strpbrk' */
  25. #define STRPBRK_P (defined strcspn)
  26. .text
  27. ENTRY (strcspn)
  28. movq %rdi, %rdx /* Save SRC. */
  29. /* First we create a table with flags for all possible characters.
  30. For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
  31. supported by the C string functions we have 256 characters.
  32. Before inserting marks for the stop characters we clear the whole
  33. table. */
  34. movq %rdi, %r8 /* Save value. */
  35. subq $256, %rsp /* Make space for 256 bytes. */
  36. movl $32, %ecx /* 32*8 bytes = 256 bytes. */
  37. movq %rsp, %rdi
  38. xorl %eax, %eax /* We store 0s. */
  39. cld
  40. rep
  41. stosq
  42. movq %rsi, %rax /* Setup skipset. */
  43. /* For understanding the following code remember that %rcx == 0 now.
  44. Although all the following instruction only modify %cl we always
  45. have a correct zero-extended 64-bit value in %rcx. */
  46. /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
  47. .p2align 3,,6
  48. L(2): movb (%rax), %cl /* get byte from skipset */
  49. testb %cl, %cl /* is NUL char? */
  50. jz L(1) /* yes => start compare loop */
  51. movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
  52. movb 1(%rax), %cl /* get byte from skipset */
  53. testb %cl, %cl /* is NUL char? */
  54. jz L(1) /* yes => start compare loop */
  55. movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
  56. movb 2(%rax), %cl /* get byte from skipset */
  57. testb %cl, %cl /* is NUL char? */
  58. jz L(1) /* yes => start compare loop */
  59. movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
  60. movb 3(%rax), %cl /* get byte from skipset */
  61. addq $4, %rax /* increment skipset pointer */
  62. movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */
  63. testb %cl, %cl /* is NUL char? */
  64. jnz L(2) /* no => process next dword from skipset */
  65. L(1): leaq -4(%rdx), %rax /* prepare loop */
  66. /* We use a neat trick for the following loop. Normally we would
  67. have to test for two termination conditions
  68. 1. a character in the skipset was found
  69. and
  70. 2. the end of the string was found
  71. But as a sign that the character is in the skipset we store its
  72. value in the table. But the value of NUL is NUL so the loop
  73. terminates for NUL in every case. */
  74. /* Next 3 insns are 9 bytes total. */
  75. /* .p2align 4,,9 would make sure we decode them in one go, */
  76. /* but it will also align entire function to 16 bytes, */
  77. /* potentially creating largish padding at link time. */
  78. /* We are aligning to 8 bytes instead: */
  79. .p2align 3,,8
  80. L(3): addq $4, %rax /* adjust pointer for full loop round */
  81. movb (%rax), %cl /* get byte from string */
  82. cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  83. je L(4) /* yes => return */
  84. movb 1(%rax), %cl /* get byte from string */
  85. cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  86. je L(5) /* yes => return */
  87. movb 2(%rax), %cl /* get byte from string */
  88. cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  89. jz L(6) /* yes => return */
  90. movb 3(%rax), %cl /* get byte from string */
  91. cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  92. jne L(3) /* no => start loop again */
  93. incq %rax /* adjust pointer */
  94. L(6): incq %rax
  95. L(5): incq %rax
  96. L(4): addq $256, %rsp /* remove skipset */
  97. #if STRPBRK_P
  98. xorl %edx,%edx
  99. orb %cl, %cl /* was last character NUL? */
  100. cmovzq %rdx, %rax /* Yes: return NULL */
  101. #else
  102. subq %rdx, %rax /* we have to return the number of valid
  103. characters, so compute distance to first
  104. non-valid character */
  105. #endif
  106. ret
  107. END (strcspn)
  108. #if !STRPBRK_P
  109. libc_hidden_def(strcspn)
  110. #endif