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