]>
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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "BaseLibInternals.h"
19 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
20 are filled with zeros. The shifted value is returned.
22 This function shifts the 64-bit value Operand to the left by Count bits. The
23 low Count bits are set to zero. The shifted value is returned.
25 @param Operand The 64-bit operand to shift left.
26 @param Count The number of bits to shift left.
28 @return Operand << Count.
33 InternalMathLShiftU64 (
38 return Operand
<< Count
;
42 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
43 are filled with zeros. The shifted value is returned.
45 This function shifts the 64-bit value Operand to the right by Count bits. The
46 high Count bits are set to zero. The shifted value is returned.
48 @param Operand The 64-bit operand to shift right.
49 @param Count The number of bits to shift right.
51 @return Operand >> Count.
56 InternalMathRShiftU64 (
61 return Operand
>> Count
;
65 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
66 are filled with original integer's bit 63. The shifted value is returned.
68 This function shifts the 64-bit value Operand to the right by Count bits. The
69 high Count bits are set to bit 63 of Operand. The shifted value is returned.
71 @param Operand The 64-bit operand to shift right.
72 @param Count The number of bits to shift right.
74 @return Operand arithmetically shifted right by Count.
79 InternalMathARShiftU64 (
87 // Test if this compiler supports arithmetic shift
89 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
90 if (TestValue
== -1) {
92 // Arithmetic shift is supported
94 return (UINT64
)((INT64
)Operand
>> Count
);
98 // Arithmetic is not supported
100 return (Operand
>> Count
) |
101 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
106 Rotates a 64-bit integer left between 0 and 63 bits, filling
107 the low bits with the high bits that were rotated.
109 This function rotates the 64-bit value Operand to the left by Count bits. The
110 low Count bits are fill with the high Count bits of Operand. The rotated
113 @param Operand The 64-bit operand to rotate left.
114 @param Count The number of bits to rotate left.
116 @return Operand <<< Count.
121 InternalMathLRotU64 (
126 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
130 Rotates a 64-bit integer right between 0 and 63 bits, filling
131 the high bits with the high low bits that were rotated.
133 This function rotates the 64-bit value Operand to the right by Count bits.
134 The high Count bits are fill with the low Count bits of Operand. The rotated
137 @param Operand The 64-bit operand to rotate right.
138 @param Count The number of bits to rotate right.
140 @return Operand >>> Count.
145 InternalMathRRotU64 (
150 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
154 Switches the endianess of a 64-bit integer.
156 This function swaps the bytes in a 64-bit unsigned value to switch the value
157 from little endian to big endian or vice versa. The byte swapped value is
160 @param Operand A 64-bit unsigned value.
162 @return The byte swapped Operand.
167 InternalMathSwapBytes64 (
174 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
175 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
177 return (LowerBytes
<< 32 | HigherBytes
);
181 Multiplies a 64-bit unsigned integer by a 32-bit unsigned integer
182 and generates a 64-bit unsigned result.
184 This function multiplies the 64-bit unsigned value Multiplicand by the 32-bit
185 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
186 bit unsigned result is returned.
188 @param Multiplicand A 64-bit unsigned value.
189 @param Multiplier A 32-bit unsigned value.
191 @return Multiplicand * Multiplier
196 InternalMathMultU64x32 (
197 IN UINT64 Multiplicand
,
201 return Multiplicand
* Multiplier
;
206 Multiplies a 64-bit unsigned integer by a 64-bit unsigned integer
207 and generates a 64-bit unsigned result.
209 This function multiplies the 64-bit unsigned value Multiplicand by the 64-bit
210 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
211 bit unsigned result is returned.
213 @param Multiplicand A 64-bit unsigned value.
214 @param Multiplier A 64-bit unsigned value.
216 @return Multiplicand * Multiplier.
221 InternalMathMultU64x64 (
222 IN UINT64 Multiplicand
,
226 return Multiplicand
* Multiplier
;
230 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
231 generates a 64-bit unsigned result.
233 This function divides the 64-bit unsigned value Dividend by the 32-bit
234 unsigned value Divisor and generates a 64-bit unsigned quotient. This
235 function returns the 64-bit unsigned quotient.
237 @param Dividend A 64-bit unsigned value.
238 @param Divisor A 32-bit unsigned value.
240 @return Dividend / Divisor.
245 InternalMathDivU64x32 (
250 return Dividend
/ Divisor
;
254 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
255 generates a 32-bit unsigned remainder.
257 This function divides the 64-bit unsigned value Dividend by the 32-bit
258 unsigned value Divisor and generates a 32-bit remainder. This function
259 returns the 32-bit unsigned remainder.
261 @param Dividend A 64-bit unsigned value.
262 @param Divisor A 32-bit unsigned value.
264 @return Dividend % Divisor.
269 InternalMathModU64x32 (
274 return (UINT32
)(Dividend
% Divisor
);
278 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
279 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
281 This function divides the 64-bit unsigned value Dividend by the 32-bit
282 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
283 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
284 This function returns the 64-bit unsigned quotient.
286 @param Dividend A 64-bit unsigned value.
287 @param Divisor A 32-bit unsigned value.
288 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
289 optional and may be NULL.
291 @return Dividend / Divisor.
296 InternalMathDivRemU64x32 (
299 OUT UINT32
*Remainder OPTIONAL
302 if (Remainder
!= NULL
) {
303 *Remainder
= (UINT32
)(Dividend
% Divisor
);
305 return Dividend
/ Divisor
;
309 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
310 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
312 This function divides the 64-bit unsigned value Dividend by the 64-bit
313 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
314 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
315 This function returns the 64-bit unsigned quotient.
317 @param Dividend A 64-bit unsigned value.
318 @param Divisor A 64-bit unsigned value.
319 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
320 optional and may be NULL.
322 @return Dividend / Divisor
327 InternalMathDivRemU64x64 (
330 OUT UINT64
*Remainder OPTIONAL
333 if (Remainder
!= NULL
) {
334 *Remainder
= Dividend
% Divisor
;
336 return Dividend
/ Divisor
;
340 Divides a 64-bit signed integer by a 64-bit signed integer and
341 generates a 64-bit signed result and an optional 64-bit signed remainder.
343 This function divides the 64-bit signed value Dividend by the 64-bit
344 signed value Divisor and generates a 64-bit signed quotient. If Remainder
345 is not NULL, then the 64-bit signed remainder is returned in Remainder.
346 This function returns the 64-bit signed quotient.
348 @param Dividend A 64-bit signed value.
349 @param Divisor A 64-bit signed value.
350 @param Remainder A pointer to a 64-bit signed value. This parameter is
351 optional and may be NULL.
353 @return Dividend / Divisor.
358 InternalMathDivRemS64x64 (
361 OUT INT64
*Remainder OPTIONAL
364 if (Remainder
!= NULL
) {
365 *Remainder
= Dividend
% Divisor
;
367 return Dividend
/ Divisor
;