]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
2 Leaf math worker functions that require 64-bit arithmetic support from the
5 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "BaseLibInternals.h"
13 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
14 are filled with zeros. The shifted value is returned.
16 This function shifts the 64-bit value Operand to the left by Count bits. The
17 low Count bits are set to zero. The shifted value is returned.
19 @param Operand The 64-bit operand to shift left.
20 @param Count The number of bits to shift left.
22 @return Operand << Count.
27 InternalMathLShiftU64 (
32 return Operand
<< Count
;
36 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
37 are filled with zeros. The shifted value is returned.
39 This function shifts the 64-bit value Operand to the right by Count bits. The
40 high Count bits are set to zero. The shifted value is returned.
42 @param Operand The 64-bit operand to shift right.
43 @param Count The number of bits to shift right.
45 @return Operand >> Count.
50 InternalMathRShiftU64 (
55 return Operand
>> Count
;
59 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
60 are filled with original integer's bit 63. The shifted value is returned.
62 This function shifts the 64-bit value Operand to the right by Count bits. The
63 high Count bits are set to bit 63 of Operand. The shifted value is returned.
65 @param Operand The 64-bit operand to shift right.
66 @param Count The number of bits to shift right.
68 @return Operand arithmetically shifted right by Count.
73 InternalMathARShiftU64 (
81 // Test if this compiler supports arithmetic shift
83 TestValue
= (INTN
)((INT64
)(1ULL << 63) >> 63);
84 if (TestValue
== -1) {
86 // Arithmetic shift is supported
88 return (UINT64
)((INT64
)Operand
>> Count
);
92 // Arithmetic is not supported
94 return (Operand
>> Count
) |
95 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
99 Rotates a 64-bit integer left between 0 and 63 bits, filling
100 the low bits with the high bits that were rotated.
102 This function rotates the 64-bit value Operand to the left by Count bits. The
103 low Count bits are fill with the high Count bits of Operand. The rotated
106 @param Operand The 64-bit operand to rotate left.
107 @param Count The number of bits to rotate left.
109 @return Operand <<< Count.
114 InternalMathLRotU64 (
119 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
123 Rotates a 64-bit integer right between 0 and 63 bits, filling
124 the high bits with the high low bits that were rotated.
126 This function rotates the 64-bit value Operand to the right by Count bits.
127 The high Count bits are fill with the low Count bits of Operand. The rotated
130 @param Operand The 64-bit operand to rotate right.
131 @param Count The number of bits to rotate right.
133 @return Operand >>> Count.
138 InternalMathRRotU64 (
143 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
147 Switches the endianess of a 64-bit integer.
149 This function swaps the bytes in a 64-bit unsigned value to switch the value
150 from little endian to big endian or vice versa. The byte swapped value is
153 @param Operand A 64-bit unsigned value.
155 @return The byte swapped Operand.
160 InternalMathSwapBytes64 (
167 LowerBytes
= (UINT64
)SwapBytes32 ((UINT32
)Operand
);
168 HigherBytes
= (UINT64
)SwapBytes32 ((UINT32
)(Operand
>> 32));
170 return (LowerBytes
<< 32 | HigherBytes
);
174 Multiplies a 64-bit unsigned integer by a 32-bit unsigned integer
175 and generates a 64-bit unsigned result.
177 This function multiplies the 64-bit unsigned value Multiplicand by the 32-bit
178 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
179 bit unsigned result is returned.
181 @param Multiplicand A 64-bit unsigned value.
182 @param Multiplier A 32-bit unsigned value.
184 @return Multiplicand * Multiplier
189 InternalMathMultU64x32 (
190 IN UINT64 Multiplicand
,
194 return Multiplicand
* Multiplier
;
198 Multiplies a 64-bit unsigned integer by a 64-bit unsigned integer
199 and generates a 64-bit unsigned result.
201 This function multiplies the 64-bit unsigned value Multiplicand by the 64-bit
202 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
203 bit unsigned result is returned.
205 @param Multiplicand A 64-bit unsigned value.
206 @param Multiplier A 64-bit unsigned value.
208 @return Multiplicand * Multiplier.
213 InternalMathMultU64x64 (
214 IN UINT64 Multiplicand
,
218 return Multiplicand
* Multiplier
;
222 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
223 generates a 64-bit unsigned result.
225 This function divides the 64-bit unsigned value Dividend by the 32-bit
226 unsigned value Divisor and generates a 64-bit unsigned quotient. This
227 function returns the 64-bit unsigned quotient.
229 @param Dividend A 64-bit unsigned value.
230 @param Divisor A 32-bit unsigned value.
232 @return Dividend / Divisor.
237 InternalMathDivU64x32 (
242 return Dividend
/ Divisor
;
246 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
247 generates a 32-bit unsigned remainder.
249 This function divides the 64-bit unsigned value Dividend by the 32-bit
250 unsigned value Divisor and generates a 32-bit remainder. This function
251 returns the 32-bit unsigned remainder.
253 @param Dividend A 64-bit unsigned value.
254 @param Divisor A 32-bit unsigned value.
256 @return Dividend % Divisor.
261 InternalMathModU64x32 (
266 return (UINT32
)(Dividend
% Divisor
);
270 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
271 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
273 This function divides the 64-bit unsigned value Dividend by the 32-bit
274 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
275 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
276 This function returns the 64-bit unsigned quotient.
278 @param Dividend A 64-bit unsigned value.
279 @param Divisor A 32-bit unsigned value.
280 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
281 optional and may be NULL.
283 @return Dividend / Divisor.
288 InternalMathDivRemU64x32 (
291 OUT UINT32
*Remainder OPTIONAL
294 if (Remainder
!= NULL
) {
295 *Remainder
= (UINT32
)(Dividend
% Divisor
);
298 return Dividend
/ Divisor
;
302 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
303 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
305 This function divides the 64-bit unsigned value Dividend by the 64-bit
306 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
307 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
308 This function returns the 64-bit unsigned quotient.
310 @param Dividend A 64-bit unsigned value.
311 @param Divisor A 64-bit unsigned value.
312 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
313 optional and may be NULL.
315 @return Dividend / Divisor
320 InternalMathDivRemU64x64 (
323 OUT UINT64
*Remainder OPTIONAL
326 if (Remainder
!= NULL
) {
327 *Remainder
= Dividend
% Divisor
;
330 return Dividend
/ Divisor
;
334 Divides a 64-bit signed integer by a 64-bit signed integer and
335 generates a 64-bit signed result and an optional 64-bit signed remainder.
337 This function divides the 64-bit signed value Dividend by the 64-bit
338 signed value Divisor and generates a 64-bit signed quotient. If Remainder
339 is not NULL, then the 64-bit signed remainder is returned in Remainder.
340 This function returns the 64-bit signed quotient.
342 @param Dividend A 64-bit signed value.
343 @param Divisor A 64-bit signed value.
344 @param Remainder A pointer to a 64-bit signed value. This parameter is
345 optional and may be NULL.
347 @return Dividend / Divisor.
352 InternalMathDivRemS64x64 (
355 OUT INT64
*Remainder OPTIONAL
358 if (Remainder
!= NULL
) {
359 *Remainder
= Dividend
% Divisor
;
362 return Dividend
/ Divisor
;