]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/BaseMemoryLibStm/Arm/CopyMem.S
f90589c225940f081e6588c23e596fabc608733e
[mirror_edk2.git] / ArmPkg / Library / BaseMemoryLibStm / Arm / CopyMem.S
1 #------------------------------------------------------------------------------
2 #
3 # CopyMem() worker for ARM
4 #
5 # This file started out as C code that did 64 bit moves if the buffer was
6 # 32-bit aligned, else it does a byte copy. It also does a byte copy for
7 # any trailing bytes. It was updated to do 32-byte copies using stm/ldm.
8 #
9 # Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
10 # This program and the accompanying materials
11 # are licensed and made available under the terms and conditions of the BSD License
12 # which accompanies this distribution. The full text of the license may be found at
13 # http://opensource.org/licenses/bsd-license.php
14 #
15 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #
18 #------------------------------------------------------------------------------
19
20 #include <AsmMacroIoLib.h>
21
22 /**
23 Copy Length bytes from Source to Destination. Overlap is OK.
24
25 This implementation
26
27 @param Destination Target of copy
28 @param Source Place to copy from
29 @param Length Number of bytes to copy
30
31 @return Destination
32
33
34 VOID *
35 EFIAPI
36 InternalMemCopyMem (
37 OUT VOID *DestinationBuffer,
38 IN CONST VOID *SourceBuffer,
39 IN UINTN Length
40 )
41 **/
42 ASM_FUNC(InternalMemCopyMem)
43 stmfd sp!, {r4-r11, lr}
44 // Save the input parameters in extra registers (r11 = destination, r14 = source, r12 = length)
45 mov r11, r0
46 mov r10, r0
47 mov r12, r2
48 mov r14, r1
49
50 memcopy_check_overlapped:
51 cmp r11, r1
52 // If (dest < source)
53 bcc memcopy_check_optim_default
54 // If (dest <= source). But with the previous condition -> If (dest == source)
55 bls memcopy_end
56
57 // If (source + length < dest)
58 rsb r3, r1, r11
59 cmp r12, r3
60 bcc memcopy_check_optim_default
61
62 // If (length == 0)
63 cmp r12, #0
64 beq memcopy_end
65
66 b memcopy_check_optim_overlap
67
68 memcopy_check_optim_default:
69 // Check if we can use an optimized path ((length >= 32) && destination word-aligned && source word-aligned) for the memcopy (optimized path if r0 == 1)
70 tst r0, #0xF
71 movne r0, #0
72 bne memcopy_default
73 tst r1, #0xF
74 movne r3, #0
75 moveq r3, #1
76 cmp r2, #31
77 movls r0, #0
78 andhi r0, r3, #1
79 b memcopy_default
80
81 memcopy_check_optim_overlap:
82 // r10 = dest_end, r14 = source_end
83 add r10, r11, r12
84 add r14, r12, r1
85
86 // Are we in the optimized case ((length >= 32) && dest_end word-aligned && source_end word-aligned)
87 cmp r2, #31
88 movls r0, #0
89 movhi r0, #1
90 tst r10, #0xF
91 movne r0, #0
92 tst r14, #0xF
93 movne r0, #0
94 b memcopy_overlapped
95
96 memcopy_overlapped_non_optim:
97 // We read 1 byte from the end of the source buffer
98 sub r3, r14, #1
99 sub r12, r12, #1
100 ldrb r3, [r3, #0]
101 sub r2, r10, #1
102 cmp r12, #0
103 // We write 1 byte at the end of the dest buffer
104 sub r10, r10, #1
105 sub r14, r14, #1
106 strb r3, [r2, #0]
107 bne memcopy_overlapped_non_optim
108 b memcopy_end
109
110 // r10 = dest_end, r14 = source_end
111 memcopy_overlapped:
112 // Are we in the optimized case ?
113 cmp r0, #0
114 beq memcopy_overlapped_non_optim
115
116 // Optimized Overlapped - Read 32 bytes
117 sub r14, r14, #32
118 sub r12, r12, #32
119 cmp r12, #31
120 ldmia r14, {r2-r9}
121
122 // If length is less than 32 then disable optim
123 movls r0, #0
124
125 cmp r12, #0
126
127 // Optimized Overlapped - Write 32 bytes
128 sub r10, r10, #32
129 stmia r10, {r2-r9}
130
131 // while (length != 0)
132 bne memcopy_overlapped
133 b memcopy_end
134
135 memcopy_default_non_optim:
136 // Byte copy
137 ldrb r3, [r14], #1
138 sub r12, r12, #1
139 strb r3, [r10], #1
140
141 memcopy_default:
142 cmp r12, #0
143 beq memcopy_end
144
145 // r10 = dest, r14 = source
146 memcopy_default_loop:
147 cmp r0, #0
148 beq memcopy_default_non_optim
149
150 // Optimized memcopy - Read 32 Bytes
151 sub r12, r12, #32
152 cmp r12, #31
153 ldmia r14!, {r2-r9}
154
155 // If length is less than 32 then disable optim
156 movls r0, #0
157
158 cmp r12, #0
159
160 // Optimized memcopy - Write 32 Bytes
161 stmia r10!, {r2-r9}
162
163 // while (length != 0)
164 bne memcopy_default_loop
165
166 memcopy_end:
167 mov r0, r11
168 ldmfd sp!, {r4-r11, pc}
169