strchr.S revision 1.5 1 1.5 christos /*
2 1.5 christos * Written by J.T. Conklin <jtc (at) acorntoolworks.com>
3 1.5 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.5 christos RCSID("$NetBSD: strchr.S,v 1.5 2009/07/19 23:45:29 christos Exp $")
10 1.1 christos #endif
11 1.1 christos
12 1.5 christos ENTRY(strchr)
13 1.5 christos movzbq %sil,%rcx
14 1.5 christos
15 1.5 christos /*
16 1.5 christos * Align to word boundary.
17 1.5 christos * Consider unrolling loop?
18 1.5 christos */
19 1.5 christos .Lalign:
20 1.5 christos testb $7,%dil
21 1.5 christos je .Lword_aligned
22 1.5 christos movb (%rdi),%dl
23 1.5 christos cmpb %cl,%dl
24 1.5 christos je .Ldone
25 1.5 christos incq %rdi
26 1.5 christos testb %dl,%dl
27 1.5 christos jne .Lalign
28 1.5 christos jmp .Lzero
29 1.5 christos
30 1.5 christos .Lword_aligned:
31 1.5 christos /* copy char to all bytes in word */
32 1.5 christos movb %cl,%ch
33 1.5 christos movq %rcx,%rdx
34 1.5 christos salq $16,%rcx
35 1.5 christos orq %rdx,%rcx
36 1.5 christos movq %rcx,%rdx
37 1.5 christos salq $32,%rcx
38 1.5 christos orq %rdx,%rcx
39 1.3 dsl
40 1.3 dsl movabsq $0x0101010101010101,%r8
41 1.5 christos movabsq $0x8080808080808080,%r9
42 1.1 christos
43 1.5 christos /* Check whether any byte in the word is equal to ch or 0. */
44 1.5 christos _ALIGN_TEXT
45 1.5 christos .Lloop:
46 1.5 christos movq (%rdi),%rdx
47 1.3 dsl addq $8,%rdi
48 1.5 christos movq %rdx,%rsi
49 1.5 christos subq %r8,%rdx
50 1.5 christos xorq %rcx,%rsi
51 1.5 christos subq %r8,%rsi
52 1.5 christos orq %rsi,%rdx
53 1.5 christos testq %r9,%rdx
54 1.5 christos je .Lloop
55 1.5 christos
56 1.5 christos /*
57 1.5 christos * In rare cases, the above loop may exit prematurely. We must
58 1.5 christos * return to the loop if none of the bytes in the word match
59 1.5 christos * ch or are equal to 0.
60 1.5 christos */
61 1.5 christos
62 1.5 christos movb -8(%rdi),%dl
63 1.5 christos cmpb %cl,%dl /* 1st byte == ch? */
64 1.5 christos jne 1f
65 1.5 christos subq $8,%rdi
66 1.5 christos jmp .Ldone
67 1.5 christos 1: testb %dl,%dl /* 1st byte == 0? */
68 1.5 christos je .Lzero
69 1.5 christos
70 1.5 christos movb -7(%rdi),%dl
71 1.5 christos cmpb %cl,%dl /* 2nd byte == ch? */
72 1.5 christos jne 1f
73 1.5 christos subq $7,%rdi
74 1.5 christos jmp .Ldone
75 1.5 christos 1: testb %dl,%dl /* 2nd byte == 0? */
76 1.5 christos je .Lzero
77 1.5 christos
78 1.5 christos movb -6(%rdi),%dl
79 1.5 christos cmpb %cl,%dl /* 3rd byte == ch? */
80 1.5 christos jne 1f
81 1.5 christos subq $6,%rdi
82 1.5 christos jmp .Ldone
83 1.5 christos 1: testb %dl,%dl /* 3rd byte == 0? */
84 1.5 christos je .Lzero
85 1.5 christos
86 1.5 christos movb -5(%rdi),%dl
87 1.5 christos cmpb %cl,%dl /* 4th byte == ch? */
88 1.5 christos jne 1f
89 1.5 christos subq $5,%rdi
90 1.5 christos jmp .Ldone
91 1.5 christos 1: testb %dl,%dl /* 4th byte == 0? */
92 1.5 christos je .Lzero
93 1.5 christos
94 1.5 christos movb -4(%rdi),%dl
95 1.5 christos cmpb %cl,%dl /* 5th byte == ch? */
96 1.5 christos jne 1f
97 1.5 christos subq $4,%rdi
98 1.5 christos jmp .Ldone
99 1.5 christos 1: testb %dl,%dl /* 5th byte == 0? */
100 1.5 christos je .Lzero
101 1.5 christos
102 1.5 christos movb -3(%rdi),%dl
103 1.5 christos cmpb %cl,%dl /* 6th byte == ch? */
104 1.5 christos jne 1f
105 1.5 christos subq $3,%rdi
106 1.5 christos jmp .Ldone
107 1.5 christos 1: testb %dl,%dl /* 6th byte == 0? */
108 1.5 christos je .Lzero
109 1.5 christos
110 1.5 christos movb -2(%rdi),%dl
111 1.5 christos cmpb %cl,%dl /* 7th byte == ch? */
112 1.5 christos jne 1f
113 1.5 christos subq $2,%rdi
114 1.5 christos jmp .Ldone
115 1.5 christos 1: testb %dl,%dl /* 7th byte == 0? */
116 1.5 christos je .Lzero
117 1.5 christos
118 1.5 christos movb -1(%rdi),%dl
119 1.5 christos cmpb %cl,%dl /* 8th byte == ch? */
120 1.5 christos jne 1f
121 1.5 christos subq $1,%rdi
122 1.5 christos jmp .Ldone
123 1.5 christos 1: testb %dl,%dl /* 8th byte == 0? */
124 1.5 christos jne .Lloop
125 1.5 christos
126 1.5 christos .Lzero:
127 1.5 christos /* If a ch wasn't found, return 0. */
128 1.5 christos xorq %rdi,%rdi
129 1.1 christos
130 1.5 christos .Ldone:
131 1.5 christos movq %rdi,%rax
132 1.3 dsl ret
133 1.2 dsl STRONG_ALIAS(index,strchr)
134