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