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