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