]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Arm/Unaligned.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseLib / Arm / Unaligned.c
CommitLineData
ebd04fc2 1/** @file\r
2 Unaligned access functions of BaseLib for ARM.\r
9095d37b 3\r
ebd04fc2 4 volatile was added to work around optimization issues.\r
5\r
9095d37b 6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
bb817c56 7 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
9344f092 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
ebd04fc2 9\r
10**/\r
11\r
12#include "BaseLibInternals.h"\r
13\r
14/**\r
15 Reads a 16-bit value from memory that may be unaligned.\r
16\r
17 This function returns the 16-bit value pointed to by Buffer. The function\r
18 guarantees that the read operation does not produce an alignment fault.\r
19\r
20 If the Buffer is NULL, then ASSERT().\r
21\r
35a17154 22 @param Buffer The pointer to a 16-bit value that may be unaligned.\r
ebd04fc2 23\r
24 @return The 16-bit value read from Buffer.\r
25\r
26**/\r
27UINT16\r
28EFIAPI\r
29ReadUnaligned16 (\r
2f88bd3a 30 IN CONST UINT16 *Buffer\r
ebd04fc2 31 )\r
32{\r
2f88bd3a
MK
33 volatile UINT8 LowerByte;\r
34 volatile UINT8 HigherByte;\r
ebd04fc2 35\r
36 ASSERT (Buffer != NULL);\r
37\r
2f88bd3a
MK
38 LowerByte = ((UINT8 *)Buffer)[0];\r
39 HigherByte = ((UINT8 *)Buffer)[1];\r
ebd04fc2 40\r
41 return (UINT16)(LowerByte | (HigherByte << 8));\r
42}\r
43\r
44/**\r
45 Writes a 16-bit value to memory that may be unaligned.\r
46\r
47 This function writes the 16-bit value specified by Value to Buffer. Value is\r
48 returned. The function guarantees that the write operation does not produce\r
49 an alignment fault.\r
50\r
51 If the Buffer is NULL, then ASSERT().\r
52\r
35a17154 53 @param Buffer The pointer to a 16-bit value that may be unaligned.\r
ebd04fc2 54 @param Value 16-bit value to write to Buffer.\r
55\r
56 @return The 16-bit value to write to Buffer.\r
57\r
58**/\r
59UINT16\r
60EFIAPI\r
61WriteUnaligned16 (\r
2f88bd3a
MK
62 OUT UINT16 *Buffer,\r
63 IN UINT16 Value\r
ebd04fc2 64 )\r
65{\r
66 ASSERT (Buffer != NULL);\r
67\r
2f88bd3a
MK
68 ((volatile UINT8 *)Buffer)[0] = (UINT8)Value;\r
69 ((volatile UINT8 *)Buffer)[1] = (UINT8)(Value >> 8);\r
ebd04fc2 70\r
71 return Value;\r
72}\r
73\r
74/**\r
75 Reads a 24-bit value from memory that may be unaligned.\r
76\r
77 This function returns the 24-bit value pointed to by Buffer. The function\r
78 guarantees that the read operation does not produce an alignment fault.\r
79\r
80 If the Buffer is NULL, then ASSERT().\r
81\r
35a17154 82 @param Buffer The pointer to a 24-bit value that may be unaligned.\r
ebd04fc2 83\r
84 @return The 24-bit value read from Buffer.\r
85\r
86**/\r
87UINT32\r
88EFIAPI\r
89ReadUnaligned24 (\r
2f88bd3a 90 IN CONST UINT32 *Buffer\r
ebd04fc2 91 )\r
92{\r
93 ASSERT (Buffer != NULL);\r
94\r
95 return (UINT32)(\r
2f88bd3a
MK
96 ReadUnaligned16 ((UINT16 *)Buffer) |\r
97 (((UINT8 *)Buffer)[2] << 16)\r
98 );\r
ebd04fc2 99}\r
100\r
101/**\r
102 Writes a 24-bit value to memory that may be unaligned.\r
103\r
104 This function writes the 24-bit value specified by Value to Buffer. Value is\r
105 returned. The function guarantees that the write operation does not produce\r
106 an alignment fault.\r
107\r
108 If the Buffer is NULL, then ASSERT().\r
109\r
35a17154 110 @param Buffer The pointer to a 24-bit value that may be unaligned.\r
ebd04fc2 111 @param Value 24-bit value to write to Buffer.\r
112\r
113 @return The 24-bit value to write to Buffer.\r
114\r
115**/\r
116UINT32\r
117EFIAPI\r
118WriteUnaligned24 (\r
2f88bd3a
MK
119 OUT UINT32 *Buffer,\r
120 IN UINT32 Value\r
ebd04fc2 121 )\r
122{\r
123 ASSERT (Buffer != NULL);\r
124\r
2f88bd3a
MK
125 WriteUnaligned16 ((UINT16 *)Buffer, (UINT16)Value);\r
126 *(UINT8 *)((UINT16 *)Buffer + 1) = (UINT8)(Value >> 16);\r
ebd04fc2 127 return Value;\r
128}\r
129\r
130/**\r
131 Reads a 32-bit value from memory that may be unaligned.\r
132\r
133 This function returns the 32-bit value pointed to by Buffer. The function\r
134 guarantees that the read operation does not produce an alignment fault.\r
135\r
136 If the Buffer is NULL, then ASSERT().\r
137\r
35a17154 138 @param Buffer The pointer to a 32-bit value that may be unaligned.\r
ebd04fc2 139\r
140 @return The 32-bit value read from Buffer.\r
141\r
142**/\r
143UINT32\r
144EFIAPI\r
145ReadUnaligned32 (\r
2f88bd3a 146 IN CONST UINT32 *Buffer\r
ebd04fc2 147 )\r
148{\r
149 UINT16 LowerBytes;\r
150 UINT16 HigherBytes;\r
151\r
152 ASSERT (Buffer != NULL);\r
153\r
2f88bd3a
MK
154 LowerBytes = ReadUnaligned16 ((UINT16 *)Buffer);\r
155 HigherBytes = ReadUnaligned16 ((UINT16 *)Buffer + 1);\r
ebd04fc2 156\r
2f88bd3a 157 return (UINT32)(LowerBytes | (HigherBytes << 16));\r
ebd04fc2 158}\r
159\r
160/**\r
161 Writes a 32-bit value to memory that may be unaligned.\r
162\r
163 This function writes the 32-bit value specified by Value to Buffer. Value is\r
164 returned. The function guarantees that the write operation does not produce\r
165 an alignment fault.\r
166\r
167 If the Buffer is NULL, then ASSERT().\r
168\r
2fc59a00 169 @param Buffer The pointer to a 32-bit value that may be unaligned.\r
ebd04fc2 170 @param Value 32-bit value to write to Buffer.\r
171\r
172 @return The 32-bit value to write to Buffer.\r
173\r
174**/\r
175UINT32\r
176EFIAPI\r
177WriteUnaligned32 (\r
2f88bd3a
MK
178 OUT UINT32 *Buffer,\r
179 IN UINT32 Value\r
ebd04fc2 180 )\r
181{\r
182 ASSERT (Buffer != NULL);\r
183\r
2f88bd3a
MK
184 WriteUnaligned16 ((UINT16 *)Buffer, (UINT16)Value);\r
185 WriteUnaligned16 ((UINT16 *)Buffer + 1, (UINT16)(Value >> 16));\r
ebd04fc2 186 return Value;\r
187}\r
188\r
189/**\r
190 Reads a 64-bit value from memory that may be unaligned.\r
191\r
192 This function returns the 64-bit value pointed to by Buffer. The function\r
193 guarantees that the read operation does not produce an alignment fault.\r
194\r
195 If the Buffer is NULL, then ASSERT().\r
196\r
35a17154 197 @param Buffer The pointer to a 64-bit value that may be unaligned.\r
ebd04fc2 198\r
199 @return The 64-bit value read from Buffer.\r
200\r
201**/\r
202UINT64\r
203EFIAPI\r
204ReadUnaligned64 (\r
2f88bd3a 205 IN CONST UINT64 *Buffer\r
ebd04fc2 206 )\r
207{\r
208 UINT32 LowerBytes;\r
209 UINT32 HigherBytes;\r
210\r
211 ASSERT (Buffer != NULL);\r
212\r
2f88bd3a
MK
213 LowerBytes = ReadUnaligned32 ((UINT32 *)Buffer);\r
214 HigherBytes = ReadUnaligned32 ((UINT32 *)Buffer + 1);\r
ebd04fc2 215\r
2f88bd3a 216 return (UINT64)(LowerBytes | LShiftU64 (HigherBytes, 32));\r
ebd04fc2 217}\r
218\r
219/**\r
220 Writes a 64-bit value to memory that may be unaligned.\r
221\r
222 This function writes the 64-bit value specified by Value to Buffer. Value is\r
223 returned. The function guarantees that the write operation does not produce\r
224 an alignment fault.\r
225\r
226 If the Buffer is NULL, then ASSERT().\r
227\r
35a17154 228 @param Buffer The pointer to a 64-bit value that may be unaligned.\r
ebd04fc2 229 @param Value 64-bit value to write to Buffer.\r
230\r
231 @return The 64-bit value to write to Buffer.\r
232\r
233**/\r
234UINT64\r
235EFIAPI\r
236WriteUnaligned64 (\r
2f88bd3a
MK
237 OUT UINT64 *Buffer,\r
238 IN UINT64 Value\r
ebd04fc2 239 )\r
240{\r
241 ASSERT (Buffer != NULL);\r
242\r
2f88bd3a
MK
243 WriteUnaligned32 ((UINT32 *)Buffer, (UINT32)Value);\r
244 WriteUnaligned32 ((UINT32 *)Buffer + 1, (UINT32)RShiftU64 (Value, 32));\r
ebd04fc2 245 return Value;\r
246}\r