* sysdeps/x86_64/memchr.S: Handle invalid buffer pointers when
[glibc.git] / sysdeps / x86_64 / memchr.S
blob54b7af534c73665921d66602ae6c786ba5b8ff02
1 /* Copyright (C) 2009 Free Software Foundation, Inc.
2    Contributed by Ulrich Drepper <drepper@redhat.com>.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #include <sysdep.h>
23         .text
24 ENTRY (memchr)
25         movd    %esi, %xmm1
26         movq    %rdi, %rcx
27         punpcklbw %xmm1, %xmm1
28         andq    $~15, %rdi
29         testq   %rdx, %rdx
30         punpcklbw %xmm1, %xmm1
31         jz      3f
32         orl     $0xffffffff, %esi
33         movdqa  (%rdi), %xmm0
34         pshufd  $0, %xmm1, %xmm1
35         subq    %rdi, %rcx
36         pcmpeqb %xmm1, %xmm0
37         addq    %rcx, %rdx
38         shl     %cl, %esi
39         pmovmskb %xmm0, %ecx
40         andl    %esi, %ecx
41         movl    $16, %esi
42         jnz     1f
43         cmpq    %rsi, %rdx
44         jle     3f
46 2:      movdqa  (%rdi,%rsi), %xmm0
47         leaq    16(%rsi), %rsi
48         pcmpeqb %xmm1, %xmm0
49         pmovmskb %xmm0, %ecx
50         testl   %ecx, %ecx
51         jnz     1f
52         cmpq    %rsi, %rdx
53         jg      2b
55 3:      xorl    %eax, %eax
56         ret
58 1:      leaq    -16(%rdi,%rsi), %rax
59         bsfl    %ecx, %ecx
60         addq    %rcx, %rax
61         leaq    -16(%rsi,%rcx), %rsi
62         cmpq    %rsi, %rdx
63         jle     3b
64         ret
65 END (memchr)
67 strong_alias (memchr, __memchr)
68 libc_hidden_builtin_def (memchr)