]>
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);
100 Rotates a 64-bit integer left between 0 and 63 bits, filling
101 the low bits with the high bits that were rotated.
103 This function rotates the 64-bit value Operand to the left by Count bits. The
104 low Count bits are fill with the high Count bits of Operand. The rotated
107 @param Operand The 64-bit operand to rotate left.
108 @param Count The number of bits to rotate left.
110 @return Operand <<< Count.
115 InternalMathLRotU64 (
120 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
124 Rotates a 64-bit integer right between 0 and 63 bits, filling
125 the high bits with the high low bits that were rotated.
127 This function rotates the 64-bit value Operand to the right by Count bits.
128 The high Count bits are fill with the low Count bits of Operand. The rotated
131 @param Operand The 64-bit operand to rotate right.
132 @param Count The number of bits to rotate right.
134 @return Operand >>> Count.
139 InternalMathRRotU64 (
144 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
148 Switches the endianess of a 64-bit integer.
150 This function swaps the bytes in a 64-bit unsigned value to switch the value
151 from little endian to big endian or vice versa. The byte swapped value is
154 @param Operand A 64-bit unsigned value.
156 @return The byte swapped Operand.
161 InternalMathSwapBytes64 (
168 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
169 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
171 return (LowerBytes
<< 32 | HigherBytes
);
175 Multiplies a 64-bit unsigned integer by a 32-bit unsigned integer
176 and generates a 64-bit unsigned result.
178 This function multiplies the 64-bit unsigned value Multiplicand by the 32-bit
179 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
180 bit unsigned result is returned.
182 @param Multiplicand A 64-bit unsigned value.
183 @param Multiplier A 32-bit unsigned value.
185 @return Multiplicand * Multiplier
190 InternalMathMultU64x32 (
191 IN UINT64 Multiplicand
,
195 return Multiplicand
* Multiplier
;
200 Multiplies a 64-bit unsigned integer by a 64-bit unsigned integer
201 and generates a 64-bit unsigned result.
203 This function multiplies the 64-bit unsigned value Multiplicand by the 64-bit
204 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
205 bit unsigned result is returned.
207 @param Multiplicand A 64-bit unsigned value.
208 @param Multiplier A 64-bit unsigned value.
210 @return Multiplicand * Multiplier.
215 InternalMathMultU64x64 (
216 IN UINT64 Multiplicand
,
220 return Multiplicand
* Multiplier
;
224 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
225 generates a 64-bit unsigned result.
227 This function divides the 64-bit unsigned value Dividend by the 32-bit
228 unsigned value Divisor and generates a 64-bit unsigned quotient. This
229 function returns the 64-bit unsigned quotient.
231 @param Dividend A 64-bit unsigned value.
232 @param Divisor A 32-bit unsigned value.
234 @return Dividend / Divisor.
239 InternalMathDivU64x32 (
244 return Dividend
/ Divisor
;
248 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
249 generates a 32-bit unsigned remainder.
251 This function divides the 64-bit unsigned value Dividend by the 32-bit
252 unsigned value Divisor and generates a 32-bit remainder. This function
253 returns the 32-bit unsigned remainder.
255 @param Dividend A 64-bit unsigned value.
256 @param Divisor A 32-bit unsigned value.
258 @return Dividend % Divisor.
263 InternalMathModU64x32 (
268 return (UINT32
)(Dividend
% Divisor
);
272 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
273 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
275 This function divides the 64-bit unsigned value Dividend by the 32-bit
276 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
277 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
278 This function returns the 64-bit unsigned quotient.
280 @param Dividend A 64-bit unsigned value.
281 @param Divisor A 32-bit unsigned value.
282 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
283 optional and may be NULL.
285 @return Dividend / Divisor.
290 InternalMathDivRemU64x32 (
293 OUT UINT32
*Remainder OPTIONAL
296 if (Remainder
!= NULL
) {
297 *Remainder
= (UINT32
)(Dividend
% Divisor
);
299 return Dividend
/ Divisor
;
303 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
304 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
306 This function divides the 64-bit unsigned value Dividend by the 64-bit
307 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
308 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
309 This function returns the 64-bit unsigned quotient.
311 @param Dividend A 64-bit unsigned value.
312 @param Divisor A 64-bit unsigned value.
313 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
314 optional and may be NULL.
316 @return Dividend / Divisor
321 InternalMathDivRemU64x64 (
324 OUT UINT64
*Remainder OPTIONAL
327 if (Remainder
!= NULL
) {
328 *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
;
361 return Dividend
/ Divisor
;