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