]>
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, Intel Corporation<BR>
6 All rights reserved. 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.
19 // Include common header file for this module.
23 #include "BaseLibInternals.h"
26 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
27 are filled with zeros. The shifted value is returned.
29 This function shifts the 64-bit value Operand to the left by Count bits. The
30 low Count bits are set to zero. The shifted value is returned.
32 @param Operand The 64-bit operand to shift left.
33 @param Count The number of bits to shift left.
35 @return Operand << Count
40 InternalMathLShiftU64 (
45 return Operand
<< Count
;
49 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
50 are filled with zeros. The shifted value is returned.
52 This function shifts the 64-bit value Operand to the right by Count bits. The
53 high Count bits are set to zero. The shifted value is returned.
55 @param Operand The 64-bit operand to shift right.
56 @param Count The number of bits to shift right.
58 @return Operand >> Count
63 InternalMathRShiftU64 (
68 return Operand
>> Count
;
72 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
73 are filled with original integer's bit 63. The shifted value is returned.
75 This function shifts the 64-bit value Operand to the right by Count bits. The
76 high Count bits are set to bit 63 of Operand. The shifted value is returned.
78 If Count is greater than 63, then ASSERT().
80 @param Operand The 64-bit operand to shift right.
81 @param Count The number of bits to shift right.
83 @return Operand arithmetically shifted right by Count
88 InternalMathARShiftU64 (
96 // Test if this compiler supports arithmetic shift
98 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
99 if (TestValue
== -1) {
101 // Arithmetic shift is supported
103 return (UINT64
)((INT64
)Operand
>> Count
);
107 // Arithmetic is not supported
109 return (Operand
>> Count
) |
110 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
115 Rotates a 64-bit integer left between 0 and 63 bits, filling
116 the low bits with the high bits that were rotated.
118 This function rotates the 64-bit value Operand to the left by Count bits. The
119 low Count bits are fill with the high Count bits of Operand. The rotated
122 @param Operand The 64-bit operand to rotate left.
123 @param Count The number of bits to rotate left.
125 @return Operand <<< Count
130 InternalMathLRotU64 (
135 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
139 Rotates a 64-bit integer right between 0 and 63 bits, filling
140 the high bits with the high low bits that were rotated.
142 This function rotates the 64-bit value Operand to the right by Count bits.
143 The high Count bits are fill with the low Count bits of Operand. The rotated
146 @param Operand The 64-bit operand to rotate right.
147 @param Count The number of bits to rotate right.
149 @return Operand >>> Count
154 InternalMathRRotU64 (
159 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
163 Switches the endianess of a 64-bit integer.
165 This function swaps the bytes in a 64-bit unsigned value to switch the value
166 from little endian to big endian or vice versa. The byte swapped value is
169 @param Operand A 64-bit unsigned value.
171 @return The byte swaped Operand.
176 InternalMathSwapBytes64 (
183 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
184 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
186 return (LowerBytes
<< 32 | HigherBytes
);
190 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
191 and generates a 64-bit unsigned result.
193 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
194 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
195 bit unsigned result is returned.
197 @param Multiplicand A 64-bit unsigned value.
198 @param Multiplier A 32-bit unsigned value.
200 @return Multiplicand * Multiplier
205 InternalMathMultU64x32 (
206 IN UINT64 Multiplicand
,
210 return Multiplicand
* Multiplier
;
215 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
216 and generates a 64-bit unsigned result.
218 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
219 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
220 bit unsigned result is returned.
222 @param Multiplicand A 64-bit unsigned value.
223 @param Multiplier A 64-bit unsigned value.
225 @return Multiplicand * Multiplier
230 InternalMathMultU64x64 (
231 IN UINT64 Multiplicand
,
235 return Multiplicand
* Multiplier
;
239 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
240 generates a 64-bit unsigned result.
242 This function divides the 64-bit unsigned value Dividend by the 32-bit
243 unsigned value Divisor and generates a 64-bit unsigned quotient. This
244 function returns the 64-bit unsigned quotient.
246 @param Dividend A 64-bit unsigned value.
247 @param Divisor A 32-bit unsigned value.
249 @return Dividend / Divisor
254 InternalMathDivU64x32 (
259 return Dividend
/ Divisor
;
263 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
264 and generates a 32-bit unsigned remainder.
266 This function divides the 64-bit unsigned value Dividend by the 32-bit
267 unsigned value Divisor and generates a 32-bit remainder. This function
268 returns the 32-bit unsigned remainder.
270 @param Dividend A 64-bit unsigned value.
271 @param Divisor A 32-bit unsigned value.
273 @return Dividend % Divisor
278 InternalMathModU64x32 (
283 return (UINT32
)(Dividend
% Divisor
);
287 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
288 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
290 This function divides the 64-bit unsigned value Dividend by the 32-bit
291 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
292 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
293 This function returns the 64-bit unsigned quotient.
295 @param Dividend A 64-bit unsigned value.
296 @param Divisor A 32-bit unsigned value.
297 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
298 optional and may be NULL.
300 @return Dividend / Divisor
305 InternalMathDivRemU64x32 (
308 OUT UINT32
*Remainder OPTIONAL
311 if (Remainder
!= NULL
) {
312 *Remainder
= (UINT32
)(Dividend
% Divisor
);
314 return Dividend
/ Divisor
;
318 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
319 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
321 This function divides the 64-bit unsigned value Dividend by the 64-bit
322 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
323 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
324 This function returns the 64-bit unsigned quotient.
326 @param Dividend A 64-bit unsigned value.
327 @param Divisor A 64-bit unsigned value.
328 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
329 optional and may be NULL.
331 @return Dividend / Divisor
336 InternalMathDivRemU64x64 (
339 OUT UINT64
*Remainder OPTIONAL
342 if (Remainder
!= NULL
) {
343 *Remainder
= Dividend
% Divisor
;
345 return Dividend
/ Divisor
;
349 Divides a 64-bit signed integer by a 64-bit signed integer and
350 generates a 64-bit signed result and a optional 64-bit signed remainder.
352 This function divides the 64-bit unsigned value Dividend by the 64-bit
353 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
354 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
355 This function returns the 64-bit unsigned quotient.
357 @param Dividend A 64-bit signed value.
358 @param Divisor A 64-bit signed value.
359 @param Remainder A pointer to a 64-bit signed value. This parameter is
360 optional and may be NULL.
362 @return Dividend / Divisor
366 InternalMathDivRemS64x64 (
369 OUT INT64
*Remainder OPTIONAL
372 if (Remainder
!= NULL
) {
373 *Remainder
= Dividend
% Divisor
;
375 return Dividend
/ Divisor
;