added ASSERT()
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ipf / Unaligned.c
CommitLineData
878ddf1f 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 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 *Uint16\r
28\r
29**/\r
30UINT16\r
31EFIAPI\r
32ReadUnaligned16 (\r
33 IN CONST UINT16 *Buffer\r
34 )\r
35{\r
1342c9cf 36 ASSERT (Buffer != NULL);\r
37\r
878ddf1f 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 Value\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
1342c9cf 63 ASSERT (Buffer != NULL);\r
64\r
878ddf1f 65 ((UINT8*)Buffer)[0] = (UINT8)Value;\r
66 ((UINT8*)Buffer)[1] = (UINT8)(Value >> 8);\r
1342c9cf 67\r
878ddf1f 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 value read.\r
82\r
83**/\r
84UINT32\r
85EFIAPI\r
86ReadUnaligned24 (\r
87 IN CONST UINT32 *Buffer\r
88 )\r
89{\r
1342c9cf 90 ASSERT (Buffer != NULL);\r
91\r
878ddf1f 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 value written.\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
1342c9cf 120 ASSERT (Buffer != NULL);\r
121\r
878ddf1f 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 *Uint32\r
138\r
139**/\r
140UINT32\r
141EFIAPI\r
142ReadUnaligned32 (\r
143 IN CONST UINT32 *Buffer\r
144 )\r
145{\r
1342c9cf 146 ASSERT (Buffer != NULL);\r
147\r
878ddf1f 148 return (UINT32)(\r
149 ReadUnaligned16 ((UINT16*)Buffer) |\r
150 (ReadUnaligned16 ((UINT16*)Buffer + 1) << 16)\r
151 );\r
152}\r
153\r
154/**\r
155 Writes a 32-bit value to memory that may be unaligned.\r
156\r
157 This function writes the 32-bit value specified by Value to Buffer. Value is\r
158 returned. The function guarantees that the write operation does not produce\r
159 an alignment fault.\r
160\r
161 If the Buffer is NULL, then ASSERT().\r
162\r
163 @param Buffer Pointer to a 32-bit value that may be unaligned.\r
164 @param Value 32-bit value to write to Buffer.\r
165\r
166 @return Value\r
167\r
168**/\r
169UINT32\r
170EFIAPI\r
171WriteUnaligned32 (\r
172 OUT UINT32 *Buffer,\r
173 IN UINT32 Value\r
174 )\r
175{\r
1342c9cf 176 ASSERT (Buffer != NULL);\r
177\r
878ddf1f 178 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value);\r
179 WriteUnaligned16 ((UINT16*)Buffer + 1, (UINT16)(Value >> 16));\r
180 return Value;\r
181}\r
182\r
183/**\r
184 Reads a 64-bit value from memory that may be unaligned.\r
185\r
186 This function returns the 64-bit value pointed to by Buffer. The function\r
187 guarantees that the read operation does not produce an alignment fault.\r
188\r
189 If the Buffer is NULL, then ASSERT().\r
190\r
191 @param Buffer Pointer to a 64-bit value that may be unaligned.\r
192\r
193 @return *Uint64\r
194\r
195**/\r
196UINT64\r
197EFIAPI\r
198ReadUnaligned64 (\r
199 IN CONST UINT64 *Buffer\r
200 )\r
201{\r
1342c9cf 202 ASSERT (Buffer != NULL);\r
203\r
878ddf1f 204 return (UINT64)(\r
205 ReadUnaligned32 ((UINT32*)Buffer) |\r
206 LShiftU64 (ReadUnaligned32 ((UINT32*)Buffer + 1), 32)\r
207 );\r
208}\r
209\r
210/**\r
211 Writes a 64-bit value to memory that may be unaligned.\r
212\r
213 This function writes the 64-bit value specified by Value to Buffer. Value is\r
214 returned. The function guarantees that the write operation does not produce\r
215 an alignment fault.\r
216\r
217 If the Buffer is NULL, then ASSERT().\r
218\r
219 @param Buffer Pointer to a 64-bit value that may be unaligned.\r
220 @param Value 64-bit value to write to Buffer.\r
221\r
222 @return Value\r
223\r
224**/\r
225UINT64\r
226EFIAPI\r
227WriteUnaligned64 (\r
228 OUT UINT64 *Buffer,\r
229 IN UINT64 Value\r
230 )\r
231{\r
1342c9cf 232 ASSERT (Buffer != NULL);\r
233\r
878ddf1f 234 WriteUnaligned32 ((UINT32*)Buffer, (UINT32)Value);\r
235 WriteUnaligned32 ((UINT32*)Buffer + 1, (UINT32)RShiftU64 (Value, 32));\r
236 return Value;\r
237}\r