]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Unaligned.c
935e1693002660de31e464892f99a07650dbccc4
[mirror_edk2.git] / MdePkg / Library / BaseLib / Unaligned.c
1 /** @file
2 Unaligned access functions of BaseLib.
3
4 Copyright (c) 2006, Intel Corporation
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 /**
24 Reads a 16-bit value from memory that may be unaligned.
25
26 This function returns the 16-bit value pointed to by Buffer. The function
27 guarantees that the read operation does not produce an alignment fault.
28
29 If the Buffer is NULL, then ASSERT().
30
31 @param Buffer Pointer to a 16-bit value that may be unaligned.
32
33 @return *Uint16
34
35 **/
36 UINT16
37 EFIAPI
38 ReadUnaligned16 (
39 IN CONST UINT16 *Buffer
40 )
41 {
42 ASSERT (Buffer != NULL);
43
44 return *Buffer;
45 }
46
47 /**
48 Writes a 16-bit value to memory that may be unaligned.
49
50 This function writes the 16-bit value specified by Value to Buffer. Value is
51 returned. The function guarantees that the write operation does not produce
52 an alignment fault.
53
54 If the Buffer is NULL, then ASSERT().
55
56 @param Buffer Pointer to a 16-bit value that may be unaligned.
57 @param Value 16-bit value to write to Buffer.
58
59 @return Value
60
61 **/
62 UINT16
63 EFIAPI
64 WriteUnaligned16 (
65 OUT UINT16 *Buffer,
66 IN UINT16 Value
67 )
68 {
69 ASSERT (Buffer != NULL);
70
71 return *Buffer = 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 *Buffer & 0xffffff;
96 }
97
98 /**
99 Writes a 24-bit value to memory that may be unaligned.
100
101 This function writes the 24-bit value specified by Value to Buffer. Value is
102 returned. The function guarantees that the write operation does not produce
103 an alignment fault.
104
105 If the Buffer is NULL, then ASSERT().
106
107 @param Buffer Pointer to a 24-bit value that may be unaligned.
108 @param Value 24-bit value to write to Buffer.
109
110 @return The value written.
111
112 **/
113 UINT32
114 EFIAPI
115 WriteUnaligned24 (
116 OUT UINT32 *Buffer,
117 IN UINT32 Value
118 )
119 {
120 ASSERT (Buffer != NULL);
121
122 *Buffer = BitFieldWrite32 (*Buffer, 0, 23, Value);
123 return Value;
124 }
125
126 /**
127 Reads a 32-bit value from memory that may be unaligned.
128
129 This function returns the 32-bit value pointed to by Buffer. The function
130 guarantees that the read operation does not produce an alignment fault.
131
132 If the Buffer is NULL, then ASSERT().
133
134 @param Buffer Pointer to a 32-bit value that may be unaligned.
135
136 @return *Uint32
137
138 **/
139 UINT32
140 EFIAPI
141 ReadUnaligned32 (
142 IN CONST UINT32 *Buffer
143 )
144 {
145 ASSERT (Buffer != NULL);
146
147 return *Buffer;
148 }
149
150 /**
151 Writes a 32-bit value to memory that may be unaligned.
152
153 This function writes the 32-bit value specified by Value to Buffer. Value is
154 returned. The function guarantees that the write operation does not produce
155 an alignment fault.
156
157 If the Buffer is NULL, then ASSERT().
158
159 @param Buffer Pointer to a 32-bit value that may be unaligned.
160 @param Value 32-bit value to write to Buffer.
161
162 @return Value
163
164 **/
165 UINT32
166 EFIAPI
167 WriteUnaligned32 (
168 OUT UINT32 *Buffer,
169 IN UINT32 Value
170 )
171 {
172 ASSERT (Buffer != NULL);
173
174 return *Buffer = Value;
175 }
176
177 /**
178 Reads a 64-bit value from memory that may be unaligned.
179
180 This function returns the 64-bit value pointed to by Buffer. The function
181 guarantees that the read operation does not produce an alignment fault.
182
183 If the Buffer is NULL, then ASSERT().
184
185 @param Buffer Pointer to a 64-bit value that may be unaligned.
186
187 @return *Uint64
188
189 **/
190 UINT64
191 EFIAPI
192 ReadUnaligned64 (
193 IN CONST UINT64 *Buffer
194 )
195 {
196 ASSERT (Buffer != NULL);
197
198 return *Buffer;
199 }
200
201 /**
202 Writes a 64-bit value to memory that may be unaligned.
203
204 This function writes the 64-bit value specified by Value to Buffer. Value is
205 returned. The function guarantees that the write operation does not produce
206 an alignment fault.
207
208 If the Buffer is NULL, then ASSERT().
209
210 @param Buffer Pointer to a 64-bit value that may be unaligned.
211 @param Value 64-bit value to write to Buffer.
212
213 @return Value
214
215 **/
216 UINT64
217 EFIAPI
218 WriteUnaligned64 (
219 OUT UINT64 *Buffer,
220 IN UINT64 Value
221 )
222 {
223 ASSERT (Buffer != NULL);
224
225 return *Buffer = Value;
226 }