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: strcat.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(strcat) 13 1.1 christos pushl %ebx 14 1.1 christos movl 8(%esp),%ecx 15 1.1 christos movl 12(%esp),%eax 16 1.1 christos 17 1.1 christos /* 18 1.1 christos * Align destination to word boundary. 19 1.1 christos * Consider unrolling loop? 20 1.1 christos */ 21 1.1 christos .Lscan: 22 1.1 christos .Lscan_align: 23 1.1 christos testb $3,%cl 24 1.1 christos je .Lscan_aligned 25 1.1 christos cmpb $0,(%ecx) 26 1.1 christos je .Lcopy 27 1.1 christos incl %ecx 28 1.1 christos jmp .Lscan_align 29 1.1 christos 30 1.1 christos _ALIGN_TEXT 31 1.1 christos .Lscan_aligned: 32 1.1 christos .Lscan_loop: 33 1.1 christos movl (%ecx),%ebx 34 1.1 christos addl $4,%ecx 35 1.1 christos leal -0x01010101(%ebx),%edx 36 1.1 christos testl $0x80808080,%edx 37 1.1 christos je .Lscan_loop 38 1.1 christos 39 1.1 christos /* 40 1.1 christos * In rare cases, the above loop may exit prematurely. We must 41 1.1 christos * return to the loop if none of the bytes in the word equal 0. 42 1.1 christos */ 43 1.1 christos 44 1.1 christos /* 45 1.1 christos * The optimal code for determining whether each byte is zero 46 1.1 christos * differs by processor. This space-optimized code should be 47 1.1 christos * acceptable on all, especially since we don't expect it to 48 1.1 christos * be run frequently, 49 1.1 christos */ 50 1.1 christos 51 1.1 christos testb %bl,%bl /* 1st byte == 0? */ 52 1.1 christos jne 1f 53 1.1 christos subl $4,%ecx 54 1.1 christos jmp .Lcopy 55 1.1 christos 56 1.1 christos 1: testb %bh,%bh /* 2nd byte == 0? */ 57 1.1 christos jne 1f 58 1.1 christos subl $3,%ecx 59 1.1 christos jmp .Lcopy 60 1.1 christos 61 1.1 christos 1: shrl $16,%ebx 62 1.1 christos testb %bl,%bl /* 3rd byte == 0? */ 63 1.1 christos jne 1f 64 1.1 christos subl $2,%ecx 65 1.1 christos jmp .Lcopy 66 1.1 christos 67 1.1 christos 1: testb %bh,%bh /* 4th byte == 0? */ 68 1.1 christos jne .Lscan_loop 69 1.1 christos subl $1,%ecx 70 1.1 christos 71 1.1 christos /* 72 1.1 christos * Align source to a word boundary. 73 1.1 christos * Consider unrolling loop? 74 1.1 christos */ 75 1.1 christos .Lcopy: 76 1.1 christos .Lcopy_align: 77 1.1 christos testl $3,%eax 78 1.1 christos je .Lcopy_aligned 79 1.1 christos movb (%eax),%bl 80 1.1 christos incl %eax 81 1.1 christos movb %bl,(%ecx) 82 1.1 christos incl %ecx 83 1.1 christos testb %bl,%bl 84 1.1 christos jne .Lcopy_align 85 1.1 christos jmp .Ldone 86 1.1 christos 87 1.1 christos _ALIGN_TEXT 88 1.1 christos .Lcopy_loop: 89 1.1 christos movl %ebx,(%ecx) 90 1.1 christos addl $4,%ecx 91 1.1 christos .Lcopy_aligned: 92 1.1 christos movl (%eax),%ebx 93 1.1 christos addl $4,%eax 94 1.1 christos leal -0x01010101(%ebx),%edx 95 1.1 christos testl $0x80808080,%edx 96 1.1 christos je .Lcopy_loop 97 1.1 christos 98 1.1 christos /* 99 1.1 christos * In rare cases, the above loop may exit prematurely. We must 100 1.1 christos * return to the loop if none of the bytes in the word equal 0. 101 1.1 christos */ 102 1.1 christos 103 1.1 christos movb %bl,(%ecx) 104 1.1 christos incl %ecx 105 1.1 christos testb %bl,%bl 106 1.1 christos je .Ldone 107 1.1 christos 108 1.1 christos movb %bh,(%ecx) 109 1.1 christos incl %ecx 110 1.1 christos testb %bh,%bh 111 1.1 christos je .Ldone 112 1.1 christos 113 1.1 christos shrl $16,%ebx 114 1.1 christos movb %bl,(%ecx) 115 1.1 christos incl %ecx 116 1.1 christos testb %bl,%bl 117 1.1 christos je .Ldone 118 1.1 christos 119 1.1 christos movb %bh,(%ecx) 120 1.1 christos incl %ecx 121 1.1 christos testb %bh,%bh 122 1.1 christos jne .Lcopy_aligned 123 1.1 christos 124 1.1 christos .Ldone: 125 1.1 christos movl 8(%esp),%eax 126 1.1 christos popl %ebx 127 1.1 christos ret 128 1.2 jakllsch END(strcat) 129