added ASSERT()
[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 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 ASSERT (Buffer != NULL);
37
38 return (UINT16)(((UINT8*)Buffer)[0] | (((UINT8*)Buffer)[1] << 8));
39 }
40
41 /**
42 Writes a 16-bit value to memory that may be unaligned.
43
44 This function writes the 16-bit value specified by Value to Buffer. Value is
45 returned. The function guarantees that the write operation does not produce
46 an alignment fault.
47
48 If the Buffer is NULL, then ASSERT().
49
50 @param Buffer Pointer to a 16-bit value that may be unaligned.
51 @param Value 16-bit value to write to Buffer.
52
53 @return Value
54
55 **/
56 UINT16
57 EFIAPI
58 WriteUnaligned16 (
59 OUT UINT16 *Buffer,
60 IN UINT16 Value
61 )
62 {
63 ASSERT (Buffer != NULL);
64
65 ((UINT8*)Buffer)[0] = (UINT8)Value;
66 ((UINT8*)Buffer)[1] = (UINT8)(Value >> 8);
67
68 return Value;
69 }
70
71 /**
72 Reads a 24-bit value from memory that may be unaligned.
73
74 This function returns the 24-bit value pointed to by Buffer. The function
75 guarantees that the read operation does not produce an alignment fault.
76
77 If the Buffer is NULL, then ASSERT().
78
79 @param Buffer Pointer to a 24-bit value that may be unaligned.
80
81 @return The value read.
82
83 **/
84 UINT32
85 EFIAPI
86 ReadUnaligned24 (
87 IN CONST UINT32 *Buffer
88 )
89 {
90 ASSERT (Buffer != NULL);
91
92 return (UINT32)(
93 ReadUnaligned16 ((UINT16*)Buffer) |
94 (((UINT8*)Buffer)[2] << 16)
95 );
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 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value);
123 *(UINT8*)((UINT16*)Buffer + 1) = (UINT8)(Value >> 16);
124 return Value;
125 }
126
127 /**
128 Reads a 32-bit value from memory that may be unaligned.
129
130 This function returns the 32-bit value pointed to by Buffer. The function
131 guarantees that the read operation does not produce an alignment fault.
132
133 If the Buffer is NULL, then ASSERT().
134
135 @param Buffer Pointer to a 32-bit value that may be unaligned.
136
137 @return *Uint32
138
139 **/
140 UINT32
141 EFIAPI
142 ReadUnaligned32 (
143 IN CONST UINT32 *Buffer
144 )
145 {
146 ASSERT (Buffer != NULL);
147
148 return (UINT32)(
149 ReadUnaligned16 ((UINT16*)Buffer) |
150 (ReadUnaligned16 ((UINT16*)Buffer + 1) << 16)
151 );
152 }
153
154 /**
155 Writes a 32-bit value to memory that may be unaligned.
156
157 This function writes the 32-bit value specified by Value to Buffer. Value is
158 returned. The function guarantees that the write operation does not produce
159 an alignment fault.
160
161 If the Buffer is NULL, then ASSERT().
162
163 @param Buffer Pointer to a 32-bit value that may be unaligned.
164 @param Value 32-bit value to write to Buffer.
165
166 @return Value
167
168 **/
169 UINT32
170 EFIAPI
171 WriteUnaligned32 (
172 OUT UINT32 *Buffer,
173 IN UINT32 Value
174 )
175 {
176 ASSERT (Buffer != NULL);
177
178 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value);
179 WriteUnaligned16 ((UINT16*)Buffer + 1, (UINT16)(Value >> 16));
180 return Value;
181 }
182
183 /**
184 Reads a 64-bit value from memory that may be unaligned.
185
186 This function returns the 64-bit value pointed to by Buffer. The function
187 guarantees that the read operation does not produce an alignment fault.
188
189 If the Buffer is NULL, then ASSERT().
190
191 @param Buffer Pointer to a 64-bit value that may be unaligned.
192
193 @return *Uint64
194
195 **/
196 UINT64
197 EFIAPI
198 ReadUnaligned64 (
199 IN CONST UINT64 *Buffer
200 )
201 {
202 ASSERT (Buffer != NULL);
203
204 return (UINT64)(
205 ReadUnaligned32 ((UINT32*)Buffer) |
206 LShiftU64 (ReadUnaligned32 ((UINT32*)Buffer + 1), 32)
207 );
208 }
209
210 /**
211 Writes a 64-bit value to memory that may be unaligned.
212
213 This function writes the 64-bit value specified by Value to Buffer. Value is
214 returned. The function guarantees that the write operation does not produce
215 an alignment fault.
216
217 If the Buffer is NULL, then ASSERT().
218
219 @param Buffer Pointer to a 64-bit value that may be unaligned.
220 @param Value 64-bit value to write to Buffer.
221
222 @return Value
223
224 **/
225 UINT64
226 EFIAPI
227 WriteUnaligned64 (
228 OUT UINT64 *Buffer,
229 IN UINT64 Value
230 )
231 {
232 ASSERT (Buffer != NULL);
233
234 WriteUnaligned32 ((UINT32*)Buffer, (UINT32)Value);
235 WriteUnaligned32 ((UINT32*)Buffer + 1, (UINT32)RShiftU64 (Value, 32));
236 return Value;
237 }