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