]>
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.
19 #include "BaseLibInternals.h"
22 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
23 are filled with zeros. The shifted value is returned.
25 This function shifts the 64-bit value Operand to the left by Count bits. The
26 low Count bits are set to zero. The shifted value is returned.
28 @param Operand The 64-bit operand to shift left.
29 @param Count The number of bits to shift left.
31 @return Operand << Count
36 InternalMathLShiftU64 (
41 return Operand
<< Count
;
45 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
46 are filled with zeros. The shifted value is returned.
48 This function shifts the 64-bit value Operand to the right by Count bits. The
49 high Count bits are set to zero. The shifted value is returned.
51 @param Operand The 64-bit operand to shift right.
52 @param Count The number of bits to shift right.
54 @return Operand >> Count
59 InternalMathRShiftU64 (
64 return Operand
>> Count
;
68 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
69 are filled with original integer's bit 63. The shifted value is returned.
71 This function shifts the 64-bit value Operand to the right by Count bits. The
72 high Count bits are set to bit 63 of Operand. The shifted value is returned.
74 If Count is greater than 63, then ASSERT().
76 @param Operand The 64-bit operand to shift right.
77 @param Count The number of bits to shift right.
79 @return Operand arithmetically shifted right by Count
84 InternalMathARShiftU64 (
92 // Test if this compiler supports arithmetic shift
94 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
95 if (TestValue
== -1) {
97 // Arithmetic shift is supported
99 return (UINT64
)((INT64
)Operand
>> Count
);
103 // Arithmetic is not supported
105 return (Operand
>> Count
) |
106 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
111 Rotates a 64-bit integer left between 0 and 63 bits, filling
112 the low bits with the high bits that were rotated.
114 This function rotates the 64-bit value Operand to the left by Count bits. The
115 low Count bits are fill with the high Count bits of Operand. The rotated
118 @param Operand The 64-bit operand to rotate left.
119 @param Count The number of bits to rotate left.
121 @return Operand <<< Count
126 InternalMathLRotU64 (
131 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
135 Rotates a 64-bit integer right between 0 and 63 bits, filling
136 the high bits with the high low bits that were rotated.
138 This function rotates the 64-bit value Operand to the right by Count bits.
139 The high Count bits are fill with the low Count bits of Operand. The rotated
142 @param Operand The 64-bit operand to rotate right.
143 @param Count The number of bits to rotate right.
145 @return Operand >>> Count
150 InternalMathRRotU64 (
155 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
159 Switches the endianess of a 64-bit integer.
161 This function swaps the bytes in a 64-bit unsigned value to switch the value
162 from little endian to big endian or vice versa. The byte swapped value is
165 @param Operand A 64-bit unsigned value.
167 @return The byte swaped Operand.
172 InternalMathSwapBytes64 (
179 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
180 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
182 return (LowerBytes
<< 32 | HigherBytes
);
186 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
187 and generates a 64-bit unsigned result.
189 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
190 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
191 bit unsigned result is returned.
193 @param Multiplicand A 64-bit unsigned value.
194 @param Multiplier A 32-bit unsigned value.
196 @return Multiplicand * Multiplier
201 InternalMathMultU64x32 (
202 IN UINT64 Multiplicand
,
206 return Multiplicand
* Multiplier
;
211 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
212 and generates a 64-bit unsigned result.
214 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
215 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
216 bit unsigned result is returned.
218 @param Multiplicand A 64-bit unsigned value.
219 @param Multiplier A 64-bit unsigned value.
221 @return Multiplicand * Multiplier
226 InternalMathMultU64x64 (
227 IN UINT64 Multiplicand
,
231 return Multiplicand
* Multiplier
;
235 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
236 generates a 64-bit unsigned result.
238 This function divides the 64-bit unsigned value Dividend by the 32-bit
239 unsigned value Divisor and generates a 64-bit unsigned quotient. This
240 function returns the 64-bit unsigned quotient.
242 @param Dividend A 64-bit unsigned value.
243 @param Divisor A 32-bit unsigned value.
245 @return Dividend / Divisor
250 InternalMathDivU64x32 (
255 return Dividend
/ Divisor
;
259 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
260 and generates a 32-bit unsigned remainder.
262 This function divides the 64-bit unsigned value Dividend by the 32-bit
263 unsigned value Divisor and generates a 32-bit remainder. This function
264 returns the 32-bit unsigned remainder.
266 @param Dividend A 64-bit unsigned value.
267 @param Divisor A 32-bit unsigned value.
269 @return Dividend % Divisor
274 InternalMathModU64x32 (
279 return (UINT32
)(Dividend
% Divisor
);
283 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
284 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
286 This function divides the 64-bit unsigned value Dividend by the 32-bit
287 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
288 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
289 This function returns the 64-bit unsigned quotient.
291 @param Dividend A 64-bit unsigned value.
292 @param Divisor A 32-bit unsigned value.
293 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
294 optional and may be NULL.
296 @return Dividend / Divisor
301 InternalMathDivRemU64x32 (
304 OUT UINT32
*Remainder OPTIONAL
307 if (Remainder
!= NULL
) {
308 *Remainder
= (UINT32
)(Dividend
% Divisor
);
310 return Dividend
/ Divisor
;
314 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
315 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
317 This function divides the 64-bit unsigned value Dividend by the 64-bit
318 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
319 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
320 This function returns the 64-bit unsigned quotient.
322 @param Dividend A 64-bit unsigned value.
323 @param Divisor A 64-bit unsigned value.
324 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
325 optional and may be NULL.
327 @return Dividend / Divisor
332 InternalMathDivRemU64x64 (
335 OUT UINT64
*Remainder OPTIONAL
338 if (Remainder
!= NULL
) {
339 *Remainder
= Dividend
% Divisor
;
341 return Dividend
/ Divisor
;
345 Divides a 64-bit signed integer by a 64-bit signed integer and
346 generates a 64-bit signed result and a optional 64-bit signed remainder.
348 This function divides the 64-bit signed value Dividend by the 64-bit
349 signed value Divisor and generates a 64-bit signed quotient. If Remainder
350 is not NULL, then the 64-bit signed remainder is returned in Remainder.
351 This function returns the 64-bit signed quotient.
353 @param Dividend A 64-bit signed value.
354 @param Divisor A 64-bit signed value.
355 @param Remainder A pointer to a 64-bit signed value. This parameter is
356 optional and may be NULL.
358 @return Dividend / Divisor
363 InternalMathDivRemS64x64 (
366 OUT INT64
*Remainder OPTIONAL
369 if (Remainder
!= NULL
) {
370 *Remainder
= Dividend
% Divisor
;
372 return Dividend
/ Divisor
;