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