]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/Math64.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / BaseLib / Math64.c
1 /*++
2
3 Copyright (c) 2004 - 2006, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 Match64.c
16
17 Abstract:
18
19 Leaf math worker functions that require 64-bit arithmetic support from the
20 compiler.
21
22 --*/
23
24 #include "BaseLibInternals.h"
25
26 /**
27 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
28 are filled with zeros. The shifted value is returned.
29
30 This function shifts the 64-bit value Operand to the left by Count bits. The
31 low Count bits are set to zero. The shifted value is returned.
32
33 @param Operand The 64-bit operand to shift left.
34 @param Count The number of bits to shift left.
35
36 @return Operand << Count
37
38 **/
39 UINT64
40 EFIAPI
41 InternalMathLShiftU64 (
42 IN UINT64 Operand,
43 IN UINTN Count
44 )
45 {
46 return Operand << Count;
47 }
48
49 /**
50 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
51 are filled with zeros. The shifted value is returned.
52
53 This function shifts the 64-bit value Operand to the right by Count bits. The
54 high Count bits are set to zero. The shifted value is returned.
55
56 @param Operand The 64-bit operand to shift right.
57 @param Count The number of bits to shift right.
58
59 @return Operand >> Count
60
61 **/
62 UINT64
63 EFIAPI
64 InternalMathRShiftU64 (
65 IN UINT64 Operand,
66 IN UINTN Count
67 )
68 {
69 return Operand >> Count;
70 }
71
72 /**
73 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
74 are filled with original integer's bit 63. The shifted value is returned.
75
76 This function shifts the 64-bit value Operand to the right by Count bits. The
77 high Count bits are set to bit 63 of Operand. The shifted value is returned.
78
79 If Count is greater than 63, then ASSERT().
80
81 @param Operand The 64-bit operand to shift right.
82 @param Count The number of bits to shift right.
83
84 @return Operand arithmetically shifted right by Count
85
86 **/
87 UINT64
88 EFIAPI
89 InternalMathARShiftU64 (
90 IN UINT64 Operand,
91 IN UINTN Count
92 )
93 {
94 INTN TestValue;
95
96 //
97 // Test if this compiler supports arithmetic shift
98 //
99 TestValue = (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
100 if (TestValue == -1) {
101 //
102 // Arithmetic shift is supported
103 //
104 return (UINT64)((INT64)Operand >> Count);
105 }
106
107 //
108 // Arithmetic is not supported
109 //
110 return (Operand >> Count) |
111 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
112 }
113
114
115 /**
116 Rotates a 64-bit integer left between 0 and 63 bits, filling
117 the low bits with the high bits that were rotated.
118
119 This function rotates the 64-bit value Operand to the left by Count bits. The
120 low Count bits are fill with the high Count bits of Operand. The rotated
121 value is returned.
122
123 @param Operand The 64-bit operand to rotate left.
124 @param Count The number of bits to rotate left.
125
126 @return Operand <<< Count
127
128 **/
129 UINT64
130 EFIAPI
131 InternalMathLRotU64 (
132 IN UINT64 Operand,
133 IN UINTN Count
134 )
135 {
136 return (Operand << Count) | (Operand >> (64 - Count));
137 }
138
139 /**
140 Rotates a 64-bit integer right between 0 and 63 bits, filling
141 the high bits with the high low bits that were rotated.
142
143 This function rotates the 64-bit value Operand to the right by Count bits.
144 The high Count bits are fill with the low Count bits of Operand. The rotated
145 value is returned.
146
147 @param Operand The 64-bit operand to rotate right.
148 @param Count The number of bits to rotate right.
149
150 @return Operand >>> Count
151
152 **/
153 UINT64
154 EFIAPI
155 InternalMathRRotU64 (
156 IN UINT64 Operand,
157 IN UINTN Count
158 )
159 {
160 return (Operand >> Count) | (Operand << (64 - Count));
161 }
162
163 /**
164 Switches the endianess of a 64-bit integer.
165
166 This function swaps the bytes in a 64-bit unsigned value to switch the value
167 from little endian to big endian or vice versa. The byte swapped value is
168 returned.
169
170 @param Operand A 64-bit unsigned value.
171
172 @return The byte swaped Operand.
173
174 **/
175 UINT64
176 EFIAPI
177 InternalMathSwapBytes64 (
178 IN UINT64 Operand
179 )
180 {
181 UINT64 LowerBytes;
182 UINT64 HigherBytes;
183
184 LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);
185 HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));
186
187 return (LowerBytes << 32 | HigherBytes);
188 }
189
190 /**
191 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
192 and generates a 64-bit unsigned result.
193
194 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
195 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
196 bit unsigned result is returned.
197
198 @param Multiplicand A 64-bit unsigned value.
199 @param Multiplier A 32-bit unsigned value.
200
201 @return Multiplicand * Multiplier
202
203 **/
204 UINT64
205 EFIAPI
206 InternalMathMultU64x32 (
207 IN UINT64 Multiplicand,
208 IN UINT32 Multiplier
209 )
210 {
211 return Multiplicand * Multiplier;
212 }
213
214
215 /**
216 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
217 and generates a 64-bit unsigned result.
218
219 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
220 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
221 bit unsigned result is returned.
222
223 @param Multiplicand A 64-bit unsigned value.
224 @param Multiplier A 64-bit unsigned value.
225
226 @return Multiplicand * Multiplier
227
228 **/
229 UINT64
230 EFIAPI
231 InternalMathMultU64x64 (
232 IN UINT64 Multiplicand,
233 IN UINT64 Multiplier
234 )
235 {
236 return Multiplicand * Multiplier;
237 }
238
239 /**
240 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
241 generates a 64-bit unsigned result.
242
243 This function divides the 64-bit unsigned value Dividend by the 32-bit
244 unsigned value Divisor and generates a 64-bit unsigned quotient. This
245 function returns the 64-bit unsigned quotient.
246
247 @param Dividend A 64-bit unsigned value.
248 @param Divisor A 32-bit unsigned value.
249
250 @return Dividend / Divisor
251
252 **/
253 UINT64
254 EFIAPI
255 InternalMathDivU64x32 (
256 IN UINT64 Dividend,
257 IN UINT32 Divisor
258 )
259 {
260 return Dividend / Divisor;
261 }
262
263 /**
264 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
265 and generates a 32-bit unsigned remainder.
266
267 This function divides the 64-bit unsigned value Dividend by the 32-bit
268 unsigned value Divisor and generates a 32-bit remainder. This function
269 returns the 32-bit unsigned remainder.
270
271 @param Dividend A 64-bit unsigned value.
272 @param Divisor A 32-bit unsigned value.
273
274 @return Dividend % Divisor
275
276 **/
277 UINT32
278 EFIAPI
279 InternalMathModU64x32 (
280 IN UINT64 Dividend,
281 IN UINT32 Divisor
282 )
283 {
284 return (UINT32)(Dividend % Divisor);
285 }
286
287 /**
288 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
289 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
290
291 This function divides the 64-bit unsigned value Dividend by the 32-bit
292 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
293 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
294 This function returns the 64-bit unsigned quotient.
295
296 @param Dividend A 64-bit unsigned value.
297 @param Divisor A 32-bit unsigned value.
298 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
299 optional and may be NULL.
300
301 @return Dividend / Divisor
302
303 **/
304 UINT64
305 EFIAPI
306 InternalMathDivRemU64x32 (
307 IN UINT64 Dividend,
308 IN UINT32 Divisor,
309 OUT UINT32 *Remainder OPTIONAL
310 )
311 {
312 if (Remainder != NULL) {
313 *Remainder = (UINT32)(Dividend % Divisor);
314 }
315 return Dividend / Divisor;
316 }
317
318 /**
319 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
320 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
321
322 This function divides the 64-bit unsigned value Dividend by the 64-bit
323 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
324 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
325 This function returns the 64-bit unsigned quotient.
326
327 @param Dividend A 64-bit unsigned value.
328 @param Divisor A 64-bit unsigned value.
329 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
330 optional and may be NULL.
331
332 @return Dividend / Divisor
333
334 **/
335 UINT64
336 EFIAPI
337 InternalMathDivRemU64x64 (
338 IN UINT64 Dividend,
339 IN UINT64 Divisor,
340 OUT UINT64 *Remainder OPTIONAL
341 )
342 {
343 if (Remainder != NULL) {
344 *Remainder = Dividend % Divisor;
345 }
346 return Dividend / Divisor;
347 }
348
349 /**
350 Divides a 64-bit signed integer by a 64-bit signed integer and
351 generates a 64-bit signed result and a optional 64-bit signed remainder.
352
353 This function divides the 64-bit unsigned value Dividend by the 64-bit
354 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
355 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
356 This function returns the 64-bit unsigned quotient.
357
358 @param Dividend A 64-bit signed value.
359 @param Divisor A 64-bit signed value.
360 @param Remainder A pointer to a 64-bit signed value. This parameter is
361 optional and may be NULL.
362
363 @return Dividend / Divisor
364
365 **/
366 INT64
367 InternalMathDivRemS64x64 (
368 IN INT64 Dividend,
369 IN INT64 Divisor,
370 OUT INT64 *Remainder OPTIONAL
371 )
372 {
373 if (Remainder != NULL) {
374 *Remainder = Dividend % Divisor;
375 }
376 return Dividend / Divisor;
377 }