| 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-2020 Free Software Foundation, Inc. |
| 5 | This file is part of the GNU C Library. |
| 6 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>. |
| 7 | Bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>. |
| 8 | Adopted for x86-64 by Andreas Jaeger <aj@suse.de>. |
| 9 | |
| 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 | |
| 15 | The GNU C Library is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | Lesser General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU Lesser General Public |
| 21 | License along with the GNU C Library; if not, see |
| 22 | <https://www.gnu.org/licenses/>. */ |
| 23 | |
| 24 | #include <sysdep.h> |
| 25 | #include "asm-syntax.h" |
| 26 | |
| 27 | .text |
| 28 | ENTRY (strcspn) |
| 29 | |
| 30 | movq %rdi, %rdx /* Save SRC. */ |
| 31 | |
| 32 | /* First we create a table with flags for all possible characters. |
| 33 | For the ASCII (7bit/8bit) or ISO-8859-X character sets which are |
| 34 | supported by the C string functions we have 256 characters. |
| 35 | Before inserting marks for the stop characters we clear the whole |
| 36 | table. */ |
| 37 | movq %rdi, %r8 /* Save value. */ |
| 38 | subq $256, %rsp /* Make space for 256 bytes. */ |
| 39 | cfi_adjust_cfa_offset(256) |
| 40 | movl $32, %ecx /* 32*8 bytes = 256 bytes. */ |
| 41 | movq %rsp, %rdi |
| 42 | xorl %eax, %eax /* We store 0s. */ |
| 43 | cld |
| 44 | rep |
| 45 | stosq |
| 46 | |
| 47 | movq %rsi, %rax /* Setup skipset. */ |
| 48 | |
| 49 | /* For understanding the following code remember that %rcx == 0 now. |
| 50 | Although all the following instruction only modify %cl we always |
| 51 | have a correct zero-extended 64-bit value in %rcx. */ |
| 52 | |
| 53 | .p2align 4 |
| 54 | L(2): movb (%rax), %cl /* get byte from skipset */ |
| 55 | testb %cl, %cl /* is NUL char? */ |
| 56 | jz L(1) /* yes => start compare loop */ |
| 57 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
| 58 | |
| 59 | movb 1(%rax), %cl /* get byte from skipset */ |
| 60 | testb $0xff, %cl /* is NUL char? */ |
| 61 | jz L(1) /* yes => start compare loop */ |
| 62 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
| 63 | |
| 64 | movb 2(%rax), %cl /* get byte from skipset */ |
| 65 | testb $0xff, %cl /* is NUL char? */ |
| 66 | jz L(1) /* yes => start compare loop */ |
| 67 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
| 68 | |
| 69 | movb 3(%rax), %cl /* get byte from skipset */ |
| 70 | addq $4, %rax /* increment skipset pointer */ |
| 71 | movb %cl, (%rsp,%rcx) /* set corresponding byte in skipset table */ |
| 72 | testb $0xff, %cl /* is NUL char? */ |
| 73 | jnz L(2) /* no => process next dword from skipset */ |
| 74 | |
| 75 | L(1): leaq -4(%rdx), %rax /* prepare loop */ |
| 76 | |
| 77 | /* We use a neat trick for the following loop. Normally we would |
| 78 | have to test for two termination conditions |
| 79 | 1. a character in the skipset was found |
| 80 | and |
| 81 | 2. the end of the string was found |
| 82 | But as a sign that the character is in the skipset we store its |
| 83 | value in the table. But the value of NUL is NUL so the loop |
| 84 | terminates for NUL in every case. */ |
| 85 | |
| 86 | .p2align 4 |
| 87 | L(3): addq $4, %rax /* adjust pointer for full loop round */ |
| 88 | |
| 89 | movb (%rax), %cl /* get byte from string */ |
| 90 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
| 91 | je L(4) /* yes => return */ |
| 92 | |
| 93 | movb 1(%rax), %cl /* get byte from string */ |
| 94 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
| 95 | je L(5) /* yes => return */ |
| 96 | |
| 97 | movb 2(%rax), %cl /* get byte from string */ |
| 98 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
| 99 | jz L(6) /* yes => return */ |
| 100 | |
| 101 | movb 3(%rax), %cl /* get byte from string */ |
| 102 | cmpb %cl, (%rsp,%rcx) /* is it contained in skipset? */ |
| 103 | jne L(3) /* no => start loop again */ |
| 104 | |
| 105 | incq %rax /* adjust pointer */ |
| 106 | L(6): incq %rax |
| 107 | L(5): incq %rax |
| 108 | |
| 109 | L(4): addq $256, %rsp /* remove skipset */ |
| 110 | cfi_adjust_cfa_offset(-256) |
| 111 | #ifdef USE_AS_STRPBRK |
| 112 | xorl %edx,%edx |
| 113 | orb %cl, %cl /* was last character NUL? */ |
| 114 | cmovzq %rdx, %rax /* Yes: return NULL */ |
| 115 | #else |
| 116 | subq %rdx, %rax /* we have to return the number of valid |
| 117 | characters, so compute distance to first |
| 118 | non-valid character */ |
| 119 | #endif |
| 120 | ret |
| 121 | END (strcspn) |
| 122 | libc_hidden_builtin_def (strcspn) |
| 123 | |