|
Packit Service |
82fcde |
/* strchrnul (str, chr) -- Return pointer to first occurrence of CHR in STR
|
|
Packit Service |
82fcde |
or the final NUL byte.
|
|
Packit Service |
82fcde |
For Intel 80x86, x>=3.
|
|
Packit Service |
82fcde |
Copyright (C) 1994-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Ulrich Drepper <drepper@gnu.org>
|
|
Packit Service |
82fcde |
Some optimisations by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <sysdep.h>
|
|
Packit Service |
82fcde |
#include "asm-syntax.h"
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define PARMS 4+4 /* space for 1 saved reg */
|
|
Packit Service |
82fcde |
#define RTN PARMS
|
|
Packit Service |
82fcde |
#define STR RTN
|
|
Packit Service |
82fcde |
#define CHR STR+4
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
.text
|
|
Packit Service |
82fcde |
ENTRY (__strchrnul)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
pushl %edi /* Save callee-safe registers used here. */
|
|
Packit Service |
82fcde |
cfi_adjust_cfa_offset (4)
|
|
Packit Service |
82fcde |
cfi_rel_offset (edi, 0)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movl STR(%esp), %eax
|
|
Packit Service |
82fcde |
movl CHR(%esp), %edx
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* At the moment %edx contains CHR. What we need for the
|
|
Packit Service |
82fcde |
algorithm is CHR in all bytes of the dword. Avoid
|
|
Packit Service |
82fcde |
operations on 16 bit words because these require an
|
|
Packit Service |
82fcde |
prefix byte (and one more cycle). */
|
|
Packit Service |
82fcde |
movb %dl, %dh /* now it is 0|0|c|c */
|
|
Packit Service |
82fcde |
movl %edx, %ecx
|
|
Packit Service |
82fcde |
shll $16, %edx /* now it is c|c|0|0 */
|
|
Packit Service |
82fcde |
movw %cx, %dx /* and finally c|c|c|c */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Before we start with the main loop we process single bytes
|
|
Packit Service |
82fcde |
until the source pointer is aligned. This has two reasons:
|
|
Packit Service |
82fcde |
1. aligned 32-bit memory access is faster
|
|
Packit Service |
82fcde |
and (more important)
|
|
Packit Service |
82fcde |
2. we process in the main loop 32 bit in one step although
|
|
Packit Service |
82fcde |
we don't know the end of the string. But accessing at
|
|
Packit Service |
82fcde |
4-byte alignment guarantees that we never access illegal
|
|
Packit Service |
82fcde |
memory if this would not also be done by the trivial
|
|
Packit Service |
82fcde |
implementation (this is because all processor inherent
|
|
Packit Service |
82fcde |
boundaries are multiples of 4. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
testb $3, %al /* correctly aligned ? */
|
|
Packit Service |
82fcde |
jz L(11) /* yes => begin loop */
|
|
Packit Service |
82fcde |
movb (%eax), %cl /* load byte in question (we need it twice) */
|
|
Packit Service |
82fcde |
cmpb %cl, %dl /* compare byte */
|
|
Packit Service |
82fcde |
je L(6) /* target found => return */
|
|
Packit Service |
82fcde |
testb %cl, %cl /* is NUL? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return NULL */
|
|
Packit Service |
82fcde |
incl %eax /* increment pointer */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
testb $3, %al /* correctly aligned ? */
|
|
Packit Service |
82fcde |
jz L(11) /* yes => begin loop */
|
|
Packit Service |
82fcde |
movb (%eax), %cl /* load byte in question (we need it twice) */
|
|
Packit Service |
82fcde |
cmpb %cl, %dl /* compare byte */
|
|
Packit Service |
82fcde |
je L(6) /* target found => return */
|
|
Packit Service |
82fcde |
testb %cl, %cl /* is NUL? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return NULL */
|
|
Packit Service |
82fcde |
incl %eax /* increment pointer */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
testb $3, %al /* correctly aligned ? */
|
|
Packit Service |
82fcde |
jz L(11) /* yes => begin loop */
|
|
Packit Service |
82fcde |
movb (%eax), %cl /* load byte in question (we need it twice) */
|
|
Packit Service |
82fcde |
cmpb %cl, %dl /* compare byte */
|
|
Packit Service |
82fcde |
je L(6) /* target found => return */
|
|
Packit Service |
82fcde |
testb %cl, %cl /* is NUL? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return NULL */
|
|
Packit Service |
82fcde |
incl %eax /* increment pointer */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* No we have reached alignment. */
|
|
Packit Service |
82fcde |
jmp L(11) /* begin loop */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* We exit the loop if adding MAGIC_BITS to LONGWORD fails to
|
|
Packit Service |
82fcde |
change any of the hole bits of LONGWORD.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
1) Is this safe? Will it catch all the zero bytes?
|
|
Packit Service |
82fcde |
Suppose there is a byte with all zeros. Any carry bits
|
|
Packit Service |
82fcde |
propagating from its left will fall into the hole at its
|
|
Packit Service |
82fcde |
least significant bit and stop. Since there will be no
|
|
Packit Service |
82fcde |
carry from its most significant bit, the LSB of the
|
|
Packit Service |
82fcde |
byte to the left will be unchanged, and the zero will be
|
|
Packit Service |
82fcde |
detected.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
2) Is this worthwhile? Will it ignore everything except
|
|
Packit Service |
82fcde |
zero bytes? Suppose every byte of LONGWORD has a bit set
|
|
Packit Service |
82fcde |
somewhere. There will be a carry into bit 8. If bit 8
|
|
Packit Service |
82fcde |
is set, this will carry into bit 16. If bit 8 is clear,
|
|
Packit Service |
82fcde |
one of bits 9-15 must be set, so there will be a carry
|
|
Packit Service |
82fcde |
into bit 16. Similarly, there will be a carry into bit
|
|
Packit Service |
82fcde |
24. If one of bits 24-31 is set, there will be a carry
|
|
Packit Service |
82fcde |
into bit 32 (=carry flag), so all of the hole bits will
|
|
Packit Service |
82fcde |
be changed.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
3) But wait! Aren't we looking for CHR, not zero?
|
|
Packit Service |
82fcde |
Good point. So what we do is XOR LONGWORD with a longword,
|
|
Packit Service |
82fcde |
each of whose bytes is CHR. This turns each byte that is CHR
|
|
Packit Service |
82fcde |
into a zero. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Each round the main loop processes 16 bytes. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
ALIGN(4)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(1): addl $16, %eax /* adjust pointer for whole round */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(11): movl (%eax), %ecx /* get word (= 4 bytes) in question */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
|
|
Packit Service |
82fcde |
are now 0 */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* CHR */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* According to the algorithm we had to reverse the effect of the
|
|
Packit Service |
82fcde |
XOR first and then test the overflow bits. But because the
|
|
Packit Service |
82fcde |
following XOR would destroy the carry flag and it would (in a
|
|
Packit Service |
82fcde |
representation with more than 32 bits) not alter then last
|
|
Packit Service |
82fcde |
overflow, we can now test this condition. If no carry is signaled
|
|
Packit Service |
82fcde |
no overflow must have occurred in the last byte => it was 0. */
|
|
Packit Service |
82fcde |
jnc L(7)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* We are only interested in carry bits that change due to the
|
|
Packit Service |
82fcde |
previous add, so remove original bits */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Now test for the other three overflow bits. */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* If at least one byte of the word is CHR we don't get 0 in %edi. */
|
|
Packit Service |
82fcde |
jnz L(7) /* found it => return pointer */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Now we made sure the dword does not contain the character we are
|
|
Packit Service |
82fcde |
looking for. But because we deal with strings we have to check
|
|
Packit Service |
82fcde |
for the end of string before testing the next dword. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* restore original dword without reload */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* 0 */
|
|
Packit Service |
82fcde |
jnc L(7) /* highest byte is NUL => return NULL */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* (word+magic)^word */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(7) /* found NUL => return NULL */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movl 4(%eax), %ecx /* get word (= 4 bytes) in question */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
|
|
Packit Service |
82fcde |
are now 0 */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* CHR */
|
|
Packit Service |
82fcde |
jnc L(71) /* highest byte is CHR => return pointer */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(71) /* found it => return pointer */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* restore original dword without reload */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* 0 */
|
|
Packit Service |
82fcde |
jnc L(71) /* highest byte is NUL => return NULL */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* (word+magic)^word */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(71) /* found NUL => return NULL */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movl 8(%eax), %ecx /* get word (= 4 bytes) in question */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
|
|
Packit Service |
82fcde |
are now 0 */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* CHR */
|
|
Packit Service |
82fcde |
jnc L(72) /* highest byte is CHR => return pointer */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(72) /* found it => return pointer */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* restore original dword without reload */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* 0 */
|
|
Packit Service |
82fcde |
jnc L(72) /* highest byte is NUL => return NULL */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* (word+magic)^word */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(72) /* found NUL => return NULL */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
movl 12(%eax), %ecx /* get word (= 4 bytes) in question */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* XOR with word c|c|c|c => bytes of str == c
|
|
Packit Service |
82fcde |
are now 0 */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* CHR */
|
|
Packit Service |
82fcde |
jnc L(73) /* highest byte is CHR => return pointer */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* ((word^charmask)+magic)^(word^charmask) */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jnz L(73) /* found it => return pointer */
|
|
Packit Service |
82fcde |
xorl %edx, %ecx /* restore original dword without reload */
|
|
Packit Service |
82fcde |
movl $0xfefefeff, %edi /* magic value */
|
|
Packit Service |
82fcde |
addl %ecx, %edi /* add the magic value to the word. We get
|
|
Packit Service |
82fcde |
carry bits reported for each byte which
|
|
Packit Service |
82fcde |
is *not* 0 */
|
|
Packit Service |
82fcde |
jnc L(73) /* highest byte is NUL => return NULL */
|
|
Packit Service |
82fcde |
xorl %ecx, %edi /* (word+magic)^word */
|
|
Packit Service |
82fcde |
orl $0xfefefeff, %edi /* set all non-carry bits */
|
|
Packit Service |
82fcde |
incl %edi /* add 1: if one carry bit was *not* set
|
|
Packit Service |
82fcde |
the addition will not result in 0. */
|
|
Packit Service |
82fcde |
jz L(1) /* no NUL found => restart loop */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(73): addl $4, %eax /* adjust pointer */
|
|
Packit Service |
82fcde |
L(72): addl $4, %eax
|
|
Packit Service |
82fcde |
L(71): addl $4, %eax
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* We now scan for the byte in which the character was matched.
|
|
Packit Service |
82fcde |
But we have to take care of the case that a NUL char is
|
|
Packit Service |
82fcde |
found before this in the dword. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(7): testb %cl, %cl /* is first byte CHR? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return pointer */
|
|
Packit Service |
82fcde |
cmpb %dl, %cl /* is first byte NUL? */
|
|
Packit Service |
82fcde |
je L(6) /* yes => return NULL */
|
|
Packit Service |
82fcde |
incl %eax /* it's not in the first byte */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
testb %ch, %ch /* is second byte CHR? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return pointer */
|
|
Packit Service |
82fcde |
cmpb %dl, %ch /* is second byte NUL? */
|
|
Packit Service |
82fcde |
je L(6) /* yes => return NULL? */
|
|
Packit Service |
82fcde |
incl %eax /* it's not in the second byte */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
shrl $16, %ecx /* make upper byte accessible */
|
|
Packit Service |
82fcde |
testb %cl, %cl /* is third byte CHR? */
|
|
Packit Service |
82fcde |
jz L(6) /* yes => return pointer */
|
|
Packit Service |
82fcde |
cmpb %dl, %cl /* is third byte NUL? */
|
|
Packit Service |
82fcde |
je L(6) /* yes => return NULL */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* It must be in the fourth byte and it cannot be NUL. */
|
|
Packit Service |
82fcde |
incl %eax
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
L(6): popl %edi /* restore saved register content */
|
|
Packit Service |
82fcde |
cfi_adjust_cfa_offset (-4)
|
|
Packit Service |
82fcde |
cfi_restore (edi)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
ret
|
|
Packit Service |
82fcde |
END (__strchrnul)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
weak_alias (__strchrnul, strchrnul)
|