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