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