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-2021 Free Software Foundation, Inc. 5 This file is part of the GNU C Library. 6 7 The GNU C Library is free software; you can redistribute it and/or 8 modify it under the terms of the GNU Lesser General Public 9 License as published by the Free Software Foundation; either 10 version 2.1 of the License, or (at your option) any later version. 11 12 The GNU C Library is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 Lesser General Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public 18 License along with the GNU C Library; if not, see 19 <https://www.gnu.org/licenses/>. */ 20 21#include <sysdep.h> 22 23 .text 24ENTRY (strspn) 25 26 movq %rdi, %rdx /* Save SRC. */ 27 28 /* First we create a table with flags for all possible characters. 29 For the ASCII (7bit/8bit) or ISO-8859-X character sets which are 30 supported by the C string functions we have 256 characters. 31 Before inserting marks for the stop characters we clear the whole 32 table. */ 33 movq %rdi, %r8 /* Save value. */ 34 subq $256, %rsp /* Make space for 256 bytes. */ 35 cfi_adjust_cfa_offset(256) 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 43 movq %rsi, %rax /* Setup stopset. */ 44 45/* For understanding the following code remember that %rcx == 0 now. 46 Although all the following instruction only modify %cl we always 47 have a correct zero-extended 64-bit value in %rcx. */ 48 49 .p2align 4 50L(2): movb (%rax), %cl /* get byte from stopset */ 51 testb %cl, %cl /* is NUL char? */ 52 jz L(1) /* yes => start compare loop */ 53 movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */ 54 55 movb 1(%rax), %cl /* get byte from stopset */ 56 testb $0xff, %cl /* is NUL char? */ 57 jz L(1) /* yes => start compare loop */ 58 movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */ 59 60 movb 2(%rax), %cl /* get byte from stopset */ 61 testb $0xff, %cl /* is NUL char? */ 62 jz L(1) /* yes => start compare loop */ 63 movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */ 64 65 movb 3(%rax), %cl /* get byte from stopset */ 66 addq $4, %rax /* increment stopset pointer */ 67 movb %cl, (%rsp,%rcx) /* set corresponding byte in stopset table */ 68 testb $0xff, %cl /* is NUL char? */ 69 jnz L(2) /* no => process next dword from stopset */ 70 71L(1): leaq -4(%rdx), %rax /* prepare loop */ 72 73 /* We use a neat trick for the following loop. Normally we would 74 have to test for two termination conditions 75 1. a character in the stopset was found 76 and 77 2. the end of the string was found 78 But as a sign that the character is in the stopset we store its 79 value in the table. But the value of NUL is NUL so the loop 80 terminates for NUL in every case. */ 81 82 .p2align 4 83L(3): addq $4, %rax /* adjust pointer for full loop round */ 84 85 movb (%rax), %cl /* get byte from string */ 86 testb %cl, (%rsp,%rcx) /* is it contained in skipset? */ 87 jz L(4) /* no => return */ 88 89 movb 1(%rax), %cl /* get byte from string */ 90 testb %cl, (%rsp,%rcx) /* is it contained in skipset? */ 91 jz L(5) /* no => return */ 92 93 movb 2(%rax), %cl /* get byte from string */ 94 testb %cl, (%rsp,%rcx) /* is it contained in skipset? */ 95 jz L(6) /* no => return */ 96 97 movb 3(%rax), %cl /* get byte from string */ 98 testb %cl, (%rsp,%rcx) /* is it contained in skipset? */ 99 jnz L(3) /* yes => start loop again */ 100 101 incq %rax /* adjust pointer */ 102L(6): incq %rax 103L(5): incq %rax 104 105L(4): addq $256, %rsp /* remove stopset */ 106 cfi_adjust_cfa_offset(-256) 107 subq %rdx, %rax /* we have to return the number of valid 108 characters, so compute distance to first 109 non-valid character */ 110 ret 111END (strspn) 112libc_hidden_builtin_def (strspn) 113