strspn.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* strspn (str, ss) -- Return the length of the initial segment of STR
  2. which contains only 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, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "_glibc_inc.h"
  22. .text
  23. ENTRY (strspn)
  24. movq %rdi, %rdx /* Save SRC. */
  25. /* First we create a table with flags for all possible characters.
  26. For the ASCII (7bit/8bit) or ISO-8859-X character sets which are
  27. supported by the C string functions we have 256 characters.
  28. Before inserting marks for the stop characters we clear the whole
  29. table. */
  30. movq %rdi, %r8 /* Save value. */
  31. subq $256, %rsp /* Make space for 256 bytes. */
  32. movl $32, %ecx /* 32*8 bytes = 256 bytes. */
  33. movq %rsp, %rdi
  34. xorl %eax, %eax /* We store 0s. */
  35. cld
  36. rep
  37. stosq
  38. movq %rsi, %rax /* Setup stopset. */
  39. /* For understanding the following code remember that %rcx == 0 now.
  40. Although all the following instruction only modify %cl we always
  41. have a correct zero-extended 64-bit value in %rcx. */
  42. /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
  43. .p2align 3,,6
  44. L(2):
  45. movb (%rax), %cl /* get byte from stopset */
  46. testb %cl, %cl /* is NUL char? */
  47. jz L(1) /* yes => start compare loop */
  48. movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */
  49. movb 1(%rax), %cl /* get byte from stopset */
  50. testb %cl, %cl /* is NUL char? */
  51. jz L(1) /* yes => start compare loop */
  52. movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */
  53. movb 2(%rax), %cl /* get byte from stopset */
  54. testb %cl, %cl /* is NUL char? */
  55. jz L(1) /* yes => start compare loop */
  56. movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */
  57. movb 3(%rax), %cl /* get byte from stopset */
  58. addq $4, %rax /* increment stopset pointer */
  59. movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */
  60. testb %cl, %cl /* is NUL char? */
  61. jnz L(2) /* no => process next dword from stopset */
  62. L(1): leaq -4(%rdx), %rax /* prepare loop */
  63. /* We use a neat trick for the following loop. Normally we would
  64. have to test for two termination conditions
  65. 1. a character in the stopset was found
  66. and
  67. 2. the end of the string was found
  68. But as a sign that the character is in the stopset we store its
  69. value in the table. But the value of NUL is NUL so the loop
  70. terminates for NUL in every case. */
  71. /* Next 3 insns are 9 bytes total. */
  72. /* .p2align 4,,9 would make sure we decode them in one go, */
  73. /* but it will also align entire function to 16 bytes, */
  74. /* potentially creating largish padding at link time. */
  75. /* We are aligning to 8 bytes instead: */
  76. .p2align 3,,8
  77. L(3):
  78. addq $4, %rax /* adjust pointer for full loop round */
  79. movb (%rax), %cl /* get byte from string */
  80. testb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  81. jz L(4) /* no => return */
  82. movb 1(%rax), %cl /* get byte from string */
  83. testb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  84. jz L(5) /* no => return */
  85. movb 2(%rax), %cl /* get byte from string */
  86. testb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  87. jz L(6) /* no => return */
  88. movb 3(%rax), %cl /* get byte from string */
  89. testb %cl, (%rsp,%rcx) /* is it contained in skipset? */
  90. jnz L(3) /* yes => start loop again */
  91. incq %rax /* adjust pointer */
  92. L(6): incq %rax
  93. L(5): incq %rax
  94. L(4): addq $256, %rsp /* remove stopset */
  95. subq %rdx, %rax /* we have to return the number of valid
  96. characters, so compute distance to first
  97. non-valid character */
  98. ret
  99. END (strspn)
  100. libc_hidden_def(strspn)