eeeb0f85bbb29b672bd05bce2f7b1031beacb501
[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 return (UINT16)(((UINT8*)Buffer)[0] | (((UINT8*)Buffer)[1] << 8));
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 ((UINT8*)Buffer)[0] = (UINT8)Value;
62 ((UINT8*)Buffer)[1] = (UINT8)(Value >> 8);
63 return Value;
64 }
65
66 /**
67 Reads a 24-bit value from memory that may be unaligned.
68
69 This function returns the 24-bit value pointed to by Buffer. The function
70 guarantees that the read operation does not produce an alignment fault.
71
72 If the Buffer is NULL, then ASSERT().
73
74 @param Buffer Pointer to a 24-bit value that may be unaligned.
75
76 @return The value read.
77
78 **/
79 UINT32
80 EFIAPI
81 ReadUnaligned24 (
82 IN CONST UINT32 *Buffer
83 )
84 {
85 return (UINT32)(
86 ReadUnaligned16 ((UINT16*)Buffer) |
87 (((UINT8*)Buffer)[2] << 16)
88 );
89 }
90
91 /**
92 Writes a 24-bit value to memory that may be unaligned.
93
94 This function writes the 24-bit value specified by Value to Buffer. Value is
95 returned. The function guarantees that the write operation does not produce
96 an alignment fault.
97
98 If the Buffer is NULL, then ASSERT().
99
100 @param Buffer Pointer to a 24-bit value that may be unaligned.
101 @param Value 24-bit value to write to Buffer.
102
103 @return The value written.
104
105 **/
106 UINT32
107 EFIAPI
108 WriteUnaligned24 (
109 OUT UINT32 *Buffer,
110 IN UINT32 Value
111 )
112 {
113 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value);
114 *(UINT8*)((UINT16*)Buffer + 1) = (UINT8)(Value >> 16);
115 return Value;
116 }
117
118 /**
119 Reads a 32-bit value from memory that may be unaligned.
120
121 This function returns the 32-bit value pointed to by Buffer. The function
122 guarantees that the read operation does not produce an alignment fault.
123
124 If the Buffer is NULL, then ASSERT().
125
126 @param Buffer Pointer to a 32-bit value that may be unaligned.
127
128 @return *Uint32
129
130 **/
131 UINT32
132 EFIAPI
133 ReadUnaligned32 (
134 IN CONST UINT32 *Buffer
135 )
136 {
137 return (UINT32)(
138 ReadUnaligned16 ((UINT16*)Buffer) |
139 (ReadUnaligned16 ((UINT16*)Buffer + 1) << 16)
140 );
141 }
142
143 /**
144 Writes a 32-bit value to memory that may be unaligned.
145
146 This function writes the 32-bit value specified by Value to Buffer. Value is
147 returned. The function guarantees that the write operation does not produce
148 an alignment fault.
149
150 If the Buffer is NULL, then ASSERT().
151
152 @param Buffer Pointer to a 32-bit value that may be unaligned.
153 @param Value 32-bit value to write to Buffer.
154
155 @return Value
156
157 **/
158 UINT32
159 EFIAPI
160 WriteUnaligned32 (
161 OUT UINT32 *Buffer,
162 IN UINT32 Value
163 )
164 {
165 WriteUnaligned16 ((UINT16*)Buffer, (UINT16)Value);
166 WriteUnaligned16 ((UINT16*)Buffer + 1, (UINT16)(Value >> 16));
167 return Value;
168 }
169
170 /**
171 Reads a 64-bit value from memory that may be unaligned.
172
173 This function returns the 64-bit value pointed to by Buffer. The function
174 guarantees that the read operation does not produce an alignment fault.
175
176 If the Buffer is NULL, then ASSERT().
177
178 @param Buffer Pointer to a 64-bit value that may be unaligned.
179
180 @return *Uint64
181
182 **/
183 UINT64
184 EFIAPI
185 ReadUnaligned64 (
186 IN CONST UINT64 *Buffer
187 )
188 {
189 return (UINT64)(
190 ReadUnaligned32 ((UINT32*)Buffer) |
191 LShiftU64 (ReadUnaligned32 ((UINT32*)Buffer + 1), 32)
192 );
193 }
194
195 /**
196 Writes a 64-bit value to memory that may be unaligned.
197
198 This function writes the 64-bit value specified by Value to Buffer. Value is
199 returned. The function guarantees that the write operation does not produce
200 an alignment fault.
201
202 If the Buffer is NULL, then ASSERT().
203
204 @param Buffer Pointer to a 64-bit value that may be unaligned.
205 @param Value 64-bit value to write to Buffer.
206
207 @return Value
208
209 **/
210 UINT64
211 EFIAPI
212 WriteUnaligned64 (
213 OUT UINT64 *Buffer,
214 IN UINT64 Value
215 )
216 {
217 WriteUnaligned32 ((UINT32*)Buffer, (UINT32)Value);
218 WriteUnaligned32 ((UINT32*)Buffer + 1, (UINT32)RShiftU64 (Value, 32));
219 return Value;
220 }