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