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: strcpy.S,v 1.2 2014/03/22 19:16:34 jakllsch Exp $") 10 1.1 christos #endif 11 1.1 christos 12 1.1 christos /* 13 1.1 christos * This strcpy implementation copies a byte at a time until the 14 1.1 christos * source pointer is aligned to a word boundary, it then copies by 15 1.1 christos * words until it finds a word containing a zero byte, and finally 16 1.1 christos * copies by bytes until the end of the string is reached. 17 1.1 christos * 18 1.1 christos * While this may result in unaligned stores if the source and 19 1.1 christos * destination pointers are unaligned with respect to each other, 20 1.1 christos * it is still faster than either byte copies or the overhead of 21 1.1 christos * an implementation suitable for machines with strict alignment 22 1.1 christos * requirements. 23 1.1 christos */ 24 1.1 christos 25 1.1 christos ENTRY(strcpy) 26 1.1 christos movq %rdi,%rax 27 1.1 christos movabsq $0x0101010101010101,%r8 28 1.1 christos movabsq $0x8080808080808080,%r9 29 1.1 christos 30 1.1 christos /* 31 1.1 christos * Align source to a word boundary. 32 1.1 christos * Consider unrolling loop? 33 1.1 christos */ 34 1.1 christos _ALIGN_TEXT 35 1.1 christos .Lalign: 36 1.1 christos testb $7,%sil 37 1.1 christos je .Lword_aligned 38 1.1 christos movb (%rsi),%dl 39 1.1 christos incq %rsi 40 1.1 christos movb %dl,(%rdi) 41 1.1 christos incq %rdi 42 1.1 christos testb %dl,%dl 43 1.1 christos jne .Lalign 44 1.1 christos ret 45 1.1 christos 46 1.1 christos _ALIGN_TEXT 47 1.1 christos .Lloop: 48 1.1 christos movq %rdx,(%rdi) 49 1.1 christos addq $8,%rdi 50 1.1 christos .Lword_aligned: 51 1.1 christos movq (%rsi),%rdx 52 1.1 christos movq %rdx,%rcx 53 1.1 christos addq $8,%rsi 54 1.1 christos subq %r8,%rcx 55 1.1 christos testq %r9,%rcx 56 1.1 christos je .Lloop 57 1.1 christos 58 1.1 christos /* 59 1.1 christos * In rare cases, the above loop may exit prematurely. We must 60 1.1 christos * return to the loop if none of the bytes in the word equal 0. 61 1.1 christos */ 62 1.1 christos 63 1.1 christos movb %dl,(%rdi) 64 1.1 christos incq %rdi 65 1.1 christos testb %dl,%dl /* 1st byte == 0? */ 66 1.1 christos je .Ldone 67 1.1 christos 68 1.1 christos shrq $8,%rdx 69 1.1 christos movb %dl,(%rdi) 70 1.1 christos incq %rdi 71 1.1 christos testb %dl,%dl /* 2nd byte == 0? */ 72 1.1 christos je .Ldone 73 1.1 christos 74 1.1 christos shrq $8,%rdx 75 1.1 christos movb %dl,(%rdi) 76 1.1 christos incq %rdi 77 1.1 christos testb %dl,%dl /* 3rd byte == 0? */ 78 1.1 christos je .Ldone 79 1.1 christos 80 1.1 christos shrq $8,%rdx 81 1.1 christos movb %dl,(%rdi) 82 1.1 christos incq %rdi 83 1.1 christos testb %dl,%dl /* 4th byte == 0? */ 84 1.1 christos je .Ldone 85 1.1 christos 86 1.1 christos shrq $8,%rdx 87 1.1 christos movb %dl,(%rdi) 88 1.1 christos incq %rdi 89 1.1 christos testb %dl,%dl /* 5th byte == 0? */ 90 1.1 christos je .Ldone 91 1.1 christos 92 1.1 christos shrq $8,%rdx 93 1.1 christos movb %dl,(%rdi) 94 1.1 christos incq %rdi 95 1.1 christos testb %dl,%dl /* 6th byte == 0? */ 96 1.1 christos je .Ldone 97 1.1 christos 98 1.1 christos shrq $8,%rdx 99 1.1 christos movb %dl,(%rdi) 100 1.1 christos incq %rdi 101 1.1 christos testb %dl,%dl /* 7th byte == 0? */ 102 1.1 christos je .Ldone 103 1.1 christos 104 1.1 christos shrq $8,%rdx 105 1.1 christos movb %dl,(%rdi) 106 1.1 christos incq %rdi 107 1.1 christos testb %dl,%dl /* 8th byte == 0? */ 108 1.1 christos jne .Lword_aligned 109 1.1 christos 110 1.1 christos .Ldone: 111 1.1 christos ret 112 1.2 jakllsch END(strcpy) 113