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