memchr.S revision 1.3 1 1.3 christos /*
2 1.3 christos * Written by J.T. Conklin <jtc (at) acorntoolworks.com>
3 1.3 christos * Public domain.
4 1.1 christos */
5 1.1 christos
6 1.1 christos #include <machine/asm.h>
7 1.1 christos
8 1.1 christos #if defined(LIBC_SCCS)
9 1.2 dsl RCSID("$NetBSD: memchr.S,v 1.3 2009/07/19 23:45:29 christos Exp $")
10 1.1 christos #endif
11 1.1 christos
12 1.3 christos ENTRY(memchr)
13 1.3 christos movzbq %sil,%rcx
14 1.3 christos
15 1.3 christos /*
16 1.3 christos * Align to word boundary.
17 1.3 christos * Consider unrolling loop?
18 1.3 christos */
19 1.3 christos testq %rdx,%rdx /* nbytes == 0? */
20 1.3 christos je .Lzero
21 1.3 christos .Lalign:
22 1.3 christos testb $7,%dil
23 1.3 christos je .Lword_aligned
24 1.3 christos movq %rdi,%rax
25 1.3 christos cmpb (%rdi),%cl
26 1.3 christos je .Ldone
27 1.3 christos incq %rdi
28 1.3 christos decq %rdx
29 1.3 christos jnz .Lalign
30 1.3 christos jmp .Lzero
31 1.3 christos
32 1.3 christos .Lword_aligned:
33 1.3 christos /* copy char to all bytes in word */
34 1.3 christos movb %cl,%ch
35 1.3 christos movq %rcx,%rsi
36 1.3 christos salq $16,%rcx
37 1.3 christos orq %rsi,%rcx
38 1.3 christos movq %rcx,%rsi
39 1.3 christos salq $32,%rcx
40 1.3 christos orq %rsi,%rcx
41 1.2 dsl
42 1.2 dsl movabsq $0x0101010101010101,%r8
43 1.3 christos movabsq $0x8080808080808080,%r9
44 1.1 christos
45 1.3 christos _ALIGN_TEXT
46 1.3 christos .Lloop:
47 1.3 christos cmpq $7,%rdx /* nbytes > 8 */
48 1.3 christos jbe .Lbyte
49 1.3 christos movq (%rdi),%rsi
50 1.3 christos addq $8,%rdi
51 1.3 christos xorq %rcx,%rsi
52 1.3 christos subq $8,%rdx
53 1.3 christos subq %r8,%rsi
54 1.3 christos testq %r9,%rsi
55 1.3 christos je .Lloop
56 1.3 christos
57 1.3 christos /*
58 1.3 christos * In rare cases, the above loop may exit prematurely. We must
59 1.3 christos * return to the loop if none of the bytes in the word are
60 1.3 christos * equal to ch.
61 1.3 christos */
62 1.3 christos
63 1.3 christos leaq -8(%rdi),%rax
64 1.3 christos cmpb -8(%rdi),%cl /* 1st byte == ch? */
65 1.3 christos je .Ldone
66 1.3 christos
67 1.3 christos leaq -7(%rdi),%rax
68 1.3 christos cmpb -7(%rdi),%cl /* 2nd byte == ch? */
69 1.3 christos je .Ldone
70 1.3 christos
71 1.3 christos leaq -6(%rdi),%rax
72 1.3 christos cmpb -6(%rdi),%cl /* 3rd byte == ch? */
73 1.3 christos je .Ldone
74 1.3 christos
75 1.3 christos leaq -5(%rdi),%rax
76 1.3 christos cmpb -5(%rdi),%cl /* 4th byte == ch? */
77 1.3 christos je .Ldone
78 1.3 christos
79 1.3 christos leaq -4(%rdi),%rax
80 1.3 christos cmpb -4(%rdi),%cl /* 5th byte == ch? */
81 1.3 christos je .Ldone
82 1.3 christos
83 1.3 christos leaq -3(%rdi),%rax
84 1.3 christos cmpb -3(%rdi),%cl /* 6th byte == ch? */
85 1.3 christos je .Ldone
86 1.3 christos
87 1.3 christos leaq -2(%rdi),%rax
88 1.3 christos cmpb -2(%rdi),%cl /* 7th byte == ch? */
89 1.3 christos je .Ldone
90 1.3 christos
91 1.3 christos leaq -1(%rdi),%rax
92 1.3 christos cmpb -1(%rdi),%cl /* 7th byte == ch? */
93 1.3 christos jne .Lloop
94 1.3 christos ret
95 1.1 christos
96 1.3 christos .Lbyte:
97 1.3 christos testq %rdx,%rdx
98 1.3 christos je .Lzero
99 1.3 christos .Lbyte_loop:
100 1.3 christos movq %rdi,%rax
101 1.3 christos cmpb (%rdi),%cl
102 1.3 christos je .Ldone
103 1.3 christos incq %rdi
104 1.3 christos decq %rdx
105 1.3 christos jnz .Lbyte_loop
106 1.3 christos
107 1.3 christos .Lzero:
108 1.3 christos xorq %rax,%rax
109 1.1 christos
110 1.3 christos .Ldone:
111 1.1 christos ret
112