strrchr.S revision 1.1 1 1.1 christos /*
2 1.1 christos * Written by J.T. Conklin <jtc (at) acorntoolworks.com>
3 1.1 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.1 christos RCSID("$NetBSD: strrchr.S,v 1.1 2005/12/20 19:28:49 christos Exp $")
10 1.1 christos #endif
11 1.1 christos
12 1.1 christos #ifdef RINDEX
13 1.1 christos ENTRY(rindex)
14 1.1 christos #else
15 1.1 christos ENTRY(strrchr)
16 1.1 christos #endif
17 1.1 christos pushl %esi
18 1.1 christos pushl %edi
19 1.1 christos pushl %ebx
20 1.1 christos movl 16(%esp),%edx
21 1.1 christos movzbl 20(%esp),%ecx
22 1.1 christos
23 1.1 christos /* zero return value */
24 1.1 christos xorl %eax,%eax
25 1.1 christos
26 1.1 christos /*
27 1.1 christos * Align to word boundary.
28 1.1 christos * Consider unrolling loop?
29 1.1 christos */
30 1.1 christos .Lalign:
31 1.1 christos testb $3,%dl
32 1.1 christos je .Lword_aligned
33 1.1 christos movb (%edx),%bl
34 1.1 christos cmpb %cl,%bl
35 1.1 christos jne 1f
36 1.1 christos movl %edx,%eax
37 1.1 christos 1: testb %bl,%bl
38 1.1 christos je .Ldone
39 1.1 christos incl %edx
40 1.1 christos jmp .Lalign
41 1.1 christos
42 1.1 christos .Lword_aligned:
43 1.1 christos /* copy char to all bytes in word */
44 1.1 christos movb %cl,%ch
45 1.1 christos movl %ecx,%edi
46 1.1 christos sall $16,%ecx
47 1.1 christos orl %edi,%ecx
48 1.1 christos
49 1.1 christos /* Check whether any byte in the word is equal to ch or 0. */
50 1.1 christos _ALIGN_TEXT
51 1.1 christos .Lloop:
52 1.1 christos movl (%edx),%ebx
53 1.1 christos addl $4,%edx
54 1.1 christos movl %ebx,%esi
55 1.1 christos leal -0x01010101(%ebx),%edi
56 1.1 christos xorl %ecx,%esi
57 1.1 christos subl $0x01010101,%esi
58 1.1 christos orl %esi,%edi
59 1.1 christos testl $0x80808080,%edi
60 1.1 christos je .Lloop
61 1.1 christos
62 1.1 christos /*
63 1.1 christos * In rare cases, the above loop may exit prematurely. We must
64 1.1 christos * return to the loop if none of the bytes in the word match
65 1.1 christos * ch or are equal to 0.
66 1.1 christos */
67 1.1 christos
68 1.1 christos _ALIGN_TEXT
69 1.1 christos cmpb %cl,%bl /* 1st byte == ch? */
70 1.1 christos jne 1f
71 1.1 christos leal -4(%edx),%eax
72 1.1 christos 1: testb %bl,%bl /* 1st byte == 0? */
73 1.1 christos je .Ldone
74 1.1 christos
75 1.1 christos cmpb %cl,%bh /* 2nd byte == ch? */
76 1.1 christos jne 1f
77 1.1 christos leal -3(%edx),%eax
78 1.1 christos 1: testb %bh,%bh /* 2nd byte == 0? */
79 1.1 christos je .Ldone
80 1.1 christos
81 1.1 christos shrl $16,%ebx
82 1.1 christos cmpb %cl,%bl /* 3rd byte == ch? */
83 1.1 christos jne 1f
84 1.1 christos leal -2(%edx),%eax
85 1.1 christos 1: testb %bl,%bl /* 3rd byte == 0? */
86 1.1 christos je .Ldone
87 1.1 christos
88 1.1 christos cmpb %cl,%bh /* 4th byte == ch? */
89 1.1 christos jne 1f
90 1.1 christos leal -1(%edx),%eax
91 1.1 christos 1: testb %bh,%bh /* 4th byte == 0? */
92 1.1 christos jne .Lloop
93 1.1 christos
94 1.1 christos .Ldone:
95 1.1 christos popl %ebx
96 1.1 christos popl %edi
97 1.1 christos popl %esi
98 1.1 christos ret
99