memcpy.S revision 1.1 1 1.1 matt /* $NetBSD: memcpy.S,v 1.1 2014/08/10 05:47:35 matt Exp $ */
2 1.1 matt
3 1.1 matt /*-
4 1.1 matt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 matt * All rights reserved.
6 1.1 matt *
7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
8 1.1 matt * by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * Redistribution and use in source and binary forms, with or without
11 1.1 matt * modification, are permitted provided that the following conditions
12 1.1 matt * are met:
13 1.1 matt * 1. Redistributions of source code must retain the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer.
15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 matt * notice, this list of conditions and the following disclaimer in the
17 1.1 matt * documentation and/or other materials provided with the distribution.
18 1.1 matt *
19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
30 1.1 matt */
31 1.1 matt
32 1.1 matt #include <machine/asm.h>
33 1.1 matt
34 1.1 matt RCSID("$NetBSD: memcpy.S,v 1.1 2014/08/10 05:47:35 matt Exp $")
35 1.1 matt
36 1.1 matt /* LINTSTUB: void *memcpy(void * restrict, const void * restrict, size_t); */
37 1.1 matt
38 1.1 matt ENTRY(memcpy)
39 1.1 matt mov x10, x0
40 1.1 matt mov x11, x1
41 1.1 matt cbz x2, .Lmemcpy_ret
42 1.1 matt
43 1.1 matt cmp x2, #7
44 1.1 matt b.ls .Lmemcpy_last_dword
45 1.1 matt
46 1.1 matt ands x3, x10, #7
47 1.1 matt b.eq .Lmemcpy_dword_aligned
48 1.1 matt
49 1.1 matt /*
50 1.1 matt * The dst address doesn't have dword alignment. The src address may or may
51 1.1 matt * not have the same alignment. Make dst dword aligned. Hope src will be
52 1.1 matt * dword aligned but if it isn't, take advantage of unaligned access.
53 1.1 matt */
54 1.1 matt add x2, x2, x3 /* add unalignment to length */
55 1.1 matt sub x2, x2, #8 /* now subtract a dword */
56 1.1 matt
57 1.1 matt tbz x10, #0, .Lmemcpy_hword_aligned
58 1.1 matt ldrb w4, [x11], #1
59 1.1 matt strb w4, [x10], #1
60 1.1 matt .Lmemcpy_hword_aligned:
61 1.1 matt tbz x10, #1, .Lmemcpy_word_aligned
62 1.1 matt ldrh w4, [x11], #2
63 1.1 matt strh w4, [x10], #2
64 1.1 matt .Lmemcpy_word_aligned:
65 1.1 matt tbz x10, #2, .Lmemcpy_dword_aligned
66 1.1 matt ldr w4, [x11], #4
67 1.1 matt str w4, [x10], #4
68 1.1 matt .Lmemcpy_dword_aligned:
69 1.1 matt /*
70 1.1 matt * destination is now dword aligned.
71 1.1 matt */
72 1.1 matt subs x2, x2, #32
73 1.1 matt b.mi .Lmemcpy_last_oword
74 1.1 matt
75 1.1 matt .Lmemcpy_oword_loop:
76 1.1 matt ldp x4, x5, [x11], #16
77 1.1 matt ldp x6, x7, [x11], #16
78 1.1 matt stp x4, x5, [x10], #16
79 1.1 matt stp x6, x7, [x10], #16
80 1.1 matt cbz x2, .Lmemcpy_ret
81 1.1 matt subs x2, x2, #32
82 1.1 matt b.pl .Lmemcpy_oword_loop
83 1.1 matt
84 1.1 matt .Lmemcpy_last_oword:
85 1.1 matt /*
86 1.1 matt * We have 31 bytes or less to copy. First see if we can write a qword
87 1.1 matt */
88 1.1 matt tbz x2, #4, .Lmemcpy_last_qword
89 1.1 matt ldp x4, x5, [x11], #16 /* read word */
90 1.1 matt stp x4, x5, [x10], #16 /* write word */
91 1.1 matt
92 1.1 matt .Lmemcpy_last_qword:
93 1.1 matt /*
94 1.1 matt * We have 15 bytes or less to copy. First see if we can write a dword
95 1.1 matt */
96 1.1 matt tbz x2, #3, .Lmemcpy_last_dword
97 1.1 matt ldr x4, [x11], #8 /* read word */
98 1.1 matt str x4, [x10], #8 /* write word */
99 1.1 matt
100 1.1 matt .Lmemcpy_last_dword:
101 1.1 matt /*
102 1.1 matt * We have 7 bytes or less to copy. First see if we can write a word
103 1.1 matt */
104 1.1 matt tbz x2, #2, .Lmemcpy_last_word
105 1.1 matt ldr w4, [x11], #4 /* read word */
106 1.1 matt str w4, [x10], #4 /* write word */
107 1.1 matt
108 1.1 matt .Lmemcpy_last_word:
109 1.1 matt /*
110 1.1 matt * We have 3 bytes or less to copy. First see if we can write a hword
111 1.1 matt */
112 1.1 matt tbz x2, #1, .Lmemcpy_last_hword
113 1.1 matt ldrh w4, [x11], #2
114 1.1 matt strh w4, [x10], #2
115 1.1 matt
116 1.1 matt .Lmemcpy_last_hword:
117 1.1 matt /*
118 1.1 matt * We have 1 or none bytes to copy.
119 1.1 matt */
120 1.1 matt tbz x2, #0, .Lmemcpy_ret
121 1.1 matt ldrb w4, [x11]
122 1.1 matt strb w4, [x10]
123 1.1 matt
124 1.1 matt .Lmemcpy_ret:
125 1.1 matt ret
126 1.1 matt END(memcpy)
127