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