x86_64: Fix svml_s_exp10f4_core_sse4.S code formatting
[glibc.git] / sysdeps / x86_64 / strspn.S
blob61b76ee0a1d7036f3e199188c1cd73accd74bd54
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-2022 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
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.
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.
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/>.  */
21 #include <sysdep.h>
23         .text
24 ENTRY (strspn)
26         movq %rdi, %rdx         /* Save SRC.  */
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
43         movq %rsi, %rax                 /* Setup stopset.  */
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.  */
49         .p2align 4
50 L(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 */
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 */
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 */
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 */
71 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
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.  */
82         .p2align 4
83 L(3):   addq $4, %rax           /* adjust pointer for full loop round */
85         movb (%rax), %cl        /* get byte from string */
86         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
87         jz L(4)                 /* no => return */
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 */
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 */
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 */
101         incq %rax               /* adjust pointer */
102 L(6):   incq %rax
103 L(5):   incq %rax
105 L(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
111 END (strspn)
112 libc_hidden_builtin_def (strspn)