]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/Math64.c
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. 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
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.
19 Leaf math worker functions that require 64-bit arithmetic support from the
24 #include "BaseLibInternals.h"
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.
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.
33 @param Operand The 64-bit operand to shift left.
34 @param Count The number of bits to shift left.
36 @return Operand << Count
41 InternalMathLShiftU64 (
46 return Operand
<< Count
;
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.
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.
56 @param Operand The 64-bit operand to shift right.
57 @param Count The number of bits to shift right.
59 @return Operand >> Count
64 InternalMathRShiftU64 (
69 return Operand
>> Count
;
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.
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.
79 If Count is greater than 63, then ASSERT().
81 @param Operand The 64-bit operand to shift right.
82 @param Count The number of bits to shift right.
84 @return Operand arithmetically shifted right by Count
89 InternalMathARShiftU64 (
97 // Test if this compiler supports arithmetic shift
99 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
100 if (TestValue
== -1) {
102 // Arithmetic shift is supported
104 return (UINT64
)((INT64
)Operand
>> Count
);
108 // Arithmetic is not supported
110 return (Operand
>> Count
) |
111 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
116 Rotates a 64-bit integer left between 0 and 63 bits, filling
117 the low bits with the high bits that were rotated.
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
123 @param Operand The 64-bit operand to rotate left.
124 @param Count The number of bits to rotate left.
126 @return Operand <<< Count
131 InternalMathLRotU64 (
136 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
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.
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
147 @param Operand The 64-bit operand to rotate right.
148 @param Count The number of bits to rotate right.
150 @return Operand >>> Count
155 InternalMathRRotU64 (
160 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
164 Switches the endianess of a 64-bit integer.
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
170 @param Operand A 64-bit unsigned value.
172 @return The byte swaped Operand.
177 InternalMathSwapBytes64 (
184 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
185 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
187 return (LowerBytes
<< 32 | HigherBytes
);
191 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
192 and generates a 64-bit unsigned result.
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.
198 @param Multiplicand A 64-bit unsigned value.
199 @param Multiplier A 32-bit unsigned value.
201 @return Multiplicand * Multiplier
206 InternalMathMultU64x32 (
207 IN UINT64 Multiplicand
,
211 return Multiplicand
* Multiplier
;
216 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
217 and generates a 64-bit unsigned result.
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.
223 @param Multiplicand A 64-bit unsigned value.
224 @param Multiplier A 64-bit unsigned value.
226 @return Multiplicand * Multiplier
231 InternalMathMultU64x64 (
232 IN UINT64 Multiplicand
,
236 return Multiplicand
* Multiplier
;
240 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
241 generates a 64-bit unsigned result.
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.
247 @param Dividend A 64-bit unsigned value.
248 @param Divisor A 32-bit unsigned value.
250 @return Dividend / Divisor
255 InternalMathDivU64x32 (
260 return Dividend
/ Divisor
;
264 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
265 and generates a 32-bit unsigned remainder.
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.
271 @param Dividend A 64-bit unsigned value.
272 @param Divisor A 32-bit unsigned value.
274 @return Dividend % Divisor
279 InternalMathModU64x32 (
284 return (UINT32
)(Dividend
% Divisor
);
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.
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.
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.
301 @return Dividend / Divisor
306 InternalMathDivRemU64x32 (
309 OUT UINT32
*Remainder OPTIONAL
312 if (Remainder
!= NULL
) {
313 *Remainder
= (UINT32
)(Dividend
% Divisor
);
315 return Dividend
/ Divisor
;
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.
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.
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.
332 @return Dividend / Divisor
337 InternalMathDivRemU64x64 (
340 OUT UINT64
*Remainder OPTIONAL
343 if (Remainder
!= NULL
) {
344 *Remainder
= Dividend
% Divisor
;
346 return Dividend
/ Divisor
;
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.
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.
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.
363 @return Dividend / Divisor
367 InternalMathDivRemS64x64 (
370 OUT INT64
*Remainder OPTIONAL
373 if (Remainder
!= NULL
) {
374 *Remainder
= Dividend
% Divisor
;
376 return Dividend
/ Divisor
;