OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libc / string / x86_64 / strspn.S
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
11    The GNU C Library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2.1 of the License, or (at your option) any later version.
15
16    The GNU C Library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with the GNU C Library; if not, see
23    <http://www.gnu.org/licenses/>.  */
24
25 #include "_glibc_inc.h"
26
27         .text
28 ENTRY (strspn)
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         movl $32,  %ecx                 /* 32*8 bytes = 256 bytes.  */
40         movq %rsp, %rdi
41         xorl %eax, %eax                 /* We store 0s.  */
42         cld
43         rep
44         stosq
45
46         movq %rsi, %rax                 /* Setup stopset.  */
47
48 /* For understanding the following code remember that %rcx == 0 now.
49    Although all the following instruction only modify %cl we always
50    have a correct zero-extended 64-bit value in %rcx.  */
51
52         /* Next 3 insns are 6 bytes total, make sure we decode them in one go */
53         .p2align 3,,6
54 L(2):
55         movb (%rax), %cl        /* get byte from stopset */
56         testb %cl, %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 1(%rax), %cl       /* get byte from stopset */
61         testb %cl, %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 2(%rax), %cl       /* get byte from stopset */
66         testb %cl, %cl          /* is NUL char? */
67         jz L(1)                 /* yes => start compare loop */
68         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
69
70         movb 3(%rax), %cl       /* get byte from stopset */
71         addq $4, %rax           /* increment stopset pointer */
72         movb %cl, (%rsp,%rcx)   /* set corresponding byte in stopset table */
73         testb %cl, %cl          /* is NUL char? */
74         jnz L(2)                /* no => process next dword from stopset */
75
76 L(1):   leaq -4(%rdx), %rax     /* prepare loop */
77
78         /* We use a neat trick for the following loop.  Normally we would
79            have to test for two termination conditions
80            1. a character in the stopset was found
81            and
82            2. the end of the string was found
83            But as a sign that the character is in the stopset we store its
84            value in the table.  But the value of NUL is NUL so the loop
85            terminates for NUL in every case.  */
86
87         /* Next 3 insns are 9 bytes total. */
88         /* .p2align 4,,9 would make sure we decode them in one go, */
89         /* but it will also align entire function to 16 bytes, */
90         /* potentially creating largish padding at link time. */
91         /* We are aligning to 8 bytes instead: */
92         .p2align 3,,8
93 L(3):
94         addq $4, %rax           /* adjust pointer for full loop round */
95
96         movb (%rax), %cl        /* get byte from string */
97         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
98         jz L(4)                 /* no => return */
99
100         movb 1(%rax), %cl       /* get byte from string */
101         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
102         jz L(5)                 /* no => return */
103
104         movb 2(%rax), %cl       /* get byte from string */
105         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
106         jz L(6)                 /* no => return */
107
108         movb 3(%rax), %cl       /* get byte from string */
109         testb %cl, (%rsp,%rcx)  /* is it contained in skipset? */
110         jnz L(3)                /* yes => start loop again */
111
112         incq %rax               /* adjust pointer */
113 L(6):   incq %rax
114 L(5):   incq %rax
115
116 L(4):   addq $256, %rsp         /* remove stopset */
117         subq %rdx, %rax         /* we have to return the number of valid
118                                    characters, so compute distance to first
119                                    non-valid character */
120         ret
121 END (strspn)
122
123 libc_hidden_def(strspn)