]>
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 - 2008, 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.
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 If Count is greater than 63, then ASSERT().
73 @param Operand The 64-bit operand to shift right.
74 @param Count The number of bits to shift right.
76 @return Operand arithmetically shifted right by Count
81 InternalMathARShiftU64 (
89 // Test if this compiler supports arithmetic shift
91 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
92 if (TestValue
== -1) {
94 // Arithmetic shift is supported
96 return (UINT64
)((INT64
)Operand
>> Count
);
100 // Arithmetic is not supported
102 return (Operand
>> Count
) |
103 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
108 Rotates a 64-bit integer left between 0 and 63 bits, filling
109 the low bits with the high bits that were rotated.
111 This function rotates the 64-bit value Operand to the left by Count bits. The
112 low Count bits are fill with the high Count bits of Operand. The rotated
115 @param Operand The 64-bit operand to rotate left.
116 @param Count The number of bits to rotate left.
118 @return Operand <<< Count
123 InternalMathLRotU64 (
128 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
132 Rotates a 64-bit integer right between 0 and 63 bits, filling
133 the high bits with the high low bits that were rotated.
135 This function rotates the 64-bit value Operand to the right by Count bits.
136 The high Count bits are fill with the low Count bits of Operand. The rotated
139 @param Operand The 64-bit operand to rotate right.
140 @param Count The number of bits to rotate right.
142 @return Operand >>> Count
147 InternalMathRRotU64 (
152 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
156 Switches the endianess of a 64-bit integer.
158 This function swaps the bytes in a 64-bit unsigned value to switch the value
159 from little endian to big endian or vice versa. The byte swapped value is
162 @param Operand A 64-bit unsigned value.
164 @return The byte swaped Operand.
169 InternalMathSwapBytes64 (
176 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
177 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
179 return (LowerBytes
<< 32 | HigherBytes
);
183 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
184 and generates a 64-bit unsigned result.
186 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
187 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
188 bit unsigned result is returned.
190 @param Multiplicand A 64-bit unsigned value.
191 @param Multiplier A 32-bit unsigned value.
193 @return Multiplicand * Multiplier
198 InternalMathMultU64x32 (
199 IN UINT64 Multiplicand
,
203 return Multiplicand
* Multiplier
;
208 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
209 and generates a 64-bit unsigned result.
211 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
212 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
213 bit unsigned result is returned.
215 @param Multiplicand A 64-bit unsigned value.
216 @param Multiplier A 64-bit unsigned value.
218 @return Multiplicand * Multiplier
223 InternalMathMultU64x64 (
224 IN UINT64 Multiplicand
,
228 return Multiplicand
* Multiplier
;
232 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
233 generates a 64-bit unsigned result.
235 This function divides the 64-bit unsigned value Dividend by the 32-bit
236 unsigned value Divisor and generates a 64-bit unsigned quotient. This
237 function returns the 64-bit unsigned quotient.
239 @param Dividend A 64-bit unsigned value.
240 @param Divisor A 32-bit unsigned value.
242 @return Dividend / Divisor
247 InternalMathDivU64x32 (
252 return Dividend
/ Divisor
;
256 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
257 and generates a 32-bit unsigned remainder.
259 This function divides the 64-bit unsigned value Dividend by the 32-bit
260 unsigned value Divisor and generates a 32-bit remainder. This function
261 returns the 32-bit unsigned remainder.
263 @param Dividend A 64-bit unsigned value.
264 @param Divisor A 32-bit unsigned value.
266 @return Dividend % Divisor
271 InternalMathModU64x32 (
276 return (UINT32
)(Dividend
% Divisor
);
280 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
281 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
283 This function divides the 64-bit unsigned value Dividend by the 32-bit
284 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
285 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
286 This function returns the 64-bit unsigned quotient.
288 @param Dividend A 64-bit unsigned value.
289 @param Divisor A 32-bit unsigned value.
290 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
291 optional and may be NULL.
293 @return Dividend / Divisor
298 InternalMathDivRemU64x32 (
301 OUT UINT32
*Remainder OPTIONAL
304 if (Remainder
!= NULL
) {
305 *Remainder
= (UINT32
)(Dividend
% Divisor
);
307 return Dividend
/ Divisor
;
311 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
312 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
314 This function divides the 64-bit unsigned value Dividend by the 64-bit
315 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
316 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
317 This function returns the 64-bit unsigned quotient.
319 @param Dividend A 64-bit unsigned value.
320 @param Divisor A 64-bit unsigned value.
321 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
322 optional and may be NULL.
324 @return Dividend / Divisor
329 InternalMathDivRemU64x64 (
332 OUT UINT64
*Remainder OPTIONAL
335 if (Remainder
!= NULL
) {
336 *Remainder
= Dividend
% Divisor
;
338 return Dividend
/ Divisor
;
342 Divides a 64-bit signed integer by a 64-bit signed integer and
343 generates a 64-bit signed result and a optional 64-bit signed remainder.
345 This function divides the 64-bit signed value Dividend by the 64-bit
346 signed value Divisor and generates a 64-bit signed quotient. If Remainder
347 is not NULL, then the 64-bit signed remainder is returned in Remainder.
348 This function returns the 64-bit signed quotient.
350 @param Dividend A 64-bit signed value.
351 @param Divisor A 64-bit signed value.
352 @param Remainder A pointer to a 64-bit signed value. This parameter is
353 optional and may be NULL.
355 @return Dividend / Divisor
360 InternalMathDivRemS64x64 (
363 OUT INT64
*Remainder OPTIONAL
366 if (Remainder
!= NULL
) {
367 *Remainder
= Dividend
% Divisor
;
369 return Dividend
/ Divisor
;