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.2 jakllsch RCSID("$NetBSD: memchr.S,v 1.2 2014/03/22 19:38:46 jakllsch Exp $") 10 1.1 christos #endif 11 1.1 christos 12 1.1 christos ENTRY(memchr) 13 1.1 christos pushl %esi 14 1.1 christos movl 8(%esp),%eax 15 1.1 christos movzbl 12(%esp),%ecx 16 1.1 christos movl 16(%esp),%esi 17 1.1 christos 18 1.1 christos /* 19 1.2 jakllsch * Align to word boundary. 20 1.1 christos * Consider unrolling loop? 21 1.1 christos */ 22 1.1 christos testl %esi,%esi /* nbytes == 0? */ 23 1.1 christos je .Lzero 24 1.1 christos .Lalign: 25 1.1 christos testb $3,%al 26 1.1 christos je .Lword_aligned 27 1.1 christos cmpb (%eax),%cl 28 1.1 christos je .Ldone 29 1.1 christos incl %eax 30 1.1 christos decl %esi 31 1.1 christos jnz .Lalign 32 1.1 christos jmp .Lzero 33 1.1 christos 34 1.1 christos .Lword_aligned: 35 1.1 christos /* copy char to all bytes in word */ 36 1.1 christos movb %cl,%ch 37 1.1 christos movl %ecx,%edx 38 1.1 christos sall $16,%ecx 39 1.1 christos orl %edx,%ecx 40 1.1 christos 41 1.1 christos _ALIGN_TEXT 42 1.1 christos .Lloop: 43 1.1 christos cmpl $3,%esi /* nbytes > 4 */ 44 1.1 christos jbe .Lbyte 45 1.1 christos movl (%eax),%edx 46 1.1 christos addl $4,%eax 47 1.1 christos xorl %ecx,%edx 48 1.1 christos subl $4,%esi 49 1.1 christos subl $0x01010101,%edx 50 1.1 christos testl $0x80808080,%edx 51 1.1 christos je .Lloop 52 1.1 christos 53 1.1 christos /* 54 1.1 christos * In rare cases, the above loop may exit prematurely. We must 55 1.1 christos * return to the loop if none of the bytes in the word are 56 1.1 christos * equal to ch. 57 1.1 christos */ 58 1.1 christos 59 1.1 christos /* 60 1.1 christos * High load-use latency on the Athlon leads to significant 61 1.1 christos * stalls, so we preload the next char as soon as possible 62 1.1 christos * instead of using cmp mem8, reg8. 63 1.1 christos * 64 1.1 christos * Alignment here avoids a stall on the Athlon, even though 65 1.1 christos * it's not a branch target. 66 1.1 christos */ 67 1.1 christos _ALIGN_TEXT 68 1.1 christos cmpb -4(%eax),%cl /* 1st byte == ch? */ 69 1.1 christos movb -3(%eax),%dl 70 1.1 christos jne 1f 71 1.1 christos subl $4,%eax 72 1.1 christos jmp .Ldone 73 1.1 christos 74 1.1 christos _ALIGN_TEXT 75 1.1 christos 1: cmpb %dl,%cl /* 2nd byte == ch? */ 76 1.1 christos movb -2(%eax),%dl 77 1.1 christos jne 1f 78 1.1 christos subl $3,%eax 79 1.1 christos jmp .Ldone 80 1.1 christos 81 1.1 christos _ALIGN_TEXT 82 1.1 christos 1: cmpb %dl,%cl /* 3rd byte == ch? */ 83 1.1 christos movb -1(%eax),%dl 84 1.1 christos jne 1f 85 1.1 christos subl $2,%eax 86 1.1 christos jmp .Ldone 87 1.1 christos 88 1.1 christos _ALIGN_TEXT 89 1.1 christos 1: cmpb %dl,%cl /* 4th byte == ch? */ 90 1.1 christos jne .Lloop 91 1.1 christos decl %eax 92 1.1 christos jmp .Ldone 93 1.1 christos 94 1.1 christos .Lbyte: 95 1.1 christos testl %esi,%esi 96 1.1 christos je .Lzero 97 1.1 christos .Lbyte_loop: 98 1.1 christos cmpb (%eax),%cl 99 1.1 christos je .Ldone 100 1.1 christos incl %eax 101 1.1 christos decl %esi 102 1.1 christos jnz .Lbyte_loop 103 1.1 christos 104 1.1 christos .Lzero: 105 1.1 christos xorl %eax,%eax 106 1.1 christos 107 1.1 christos .Ldone: 108 1.1 christos popl %esi 109 1.1 christos ret 110 1.2 jakllsch END(memchr) 111