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