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