]>
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.
17 // Include common header file for this module.
21 #include "BaseLibInternals.h"
24 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
25 are filled with zeros. The shifted value is returned.
27 This function shifts the 64-bit value Operand to the left by Count bits. The
28 low Count bits are set to zero. The shifted value is returned.
30 @param Operand The 64-bit operand to shift left.
31 @param Count The number of bits to shift left.
33 @return Operand << Count
38 InternalMathLShiftU64 (
43 return Operand
<< Count
;
47 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
48 are filled with zeros. The shifted value is returned.
50 This function shifts the 64-bit value Operand to the right by Count bits. The
51 high Count bits are set to zero. The shifted value is returned.
53 @param Operand The 64-bit operand to shift right.
54 @param Count The number of bits to shift right.
56 @return Operand >> Count
61 InternalMathRShiftU64 (
66 return Operand
>> Count
;
70 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
71 are filled with original integer's bit 63. The shifted value is returned.
73 This function shifts the 64-bit value Operand to the right by Count bits. The
74 high Count bits are set to bit 63 of Operand. The shifted value is returned.
76 If Count is greater than 63, then ASSERT().
78 @param Operand The 64-bit operand to shift right.
79 @param Count The number of bits to shift right.
81 @return Operand arithmetically shifted right by Count
86 InternalMathARShiftU64 (
94 // Test if this compiler supports arithmetic shift
96 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
97 if (TestValue
== -1) {
99 // Arithmetic shift is supported
101 return (UINT64
)((INT64
)Operand
>> Count
);
105 // Arithmetic is not supported
107 return (Operand
>> Count
) |
108 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
113 Rotates a 64-bit integer left between 0 and 63 bits, filling
114 the low bits with the high bits that were rotated.
116 This function rotates the 64-bit value Operand to the left by Count bits. The
117 low Count bits are fill with the high Count bits of Operand. The rotated
120 @param Operand The 64-bit operand to rotate left.
121 @param Count The number of bits to rotate left.
123 @return Operand <<< Count
128 InternalMathLRotU64 (
133 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
137 Rotates a 64-bit integer right between 0 and 63 bits, filling
138 the high bits with the high low bits that were rotated.
140 This function rotates the 64-bit value Operand to the right by Count bits.
141 The high Count bits are fill with the low Count bits of Operand. The rotated
144 @param Operand The 64-bit operand to rotate right.
145 @param Count The number of bits to rotate right.
147 @return Operand >>> Count
152 InternalMathRRotU64 (
157 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
161 Switches the endianess of a 64-bit integer.
163 This function swaps the bytes in a 64-bit unsigned value to switch the value
164 from little endian to big endian or vice versa. The byte swapped value is
167 @param Operand A 64-bit unsigned value.
169 @return The byte swaped Operand.
174 InternalMathSwapBytes64 (
181 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
182 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
184 return (LowerBytes
<< 32 | HigherBytes
);
188 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
189 and generates a 64-bit unsigned result.
191 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
192 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
193 bit unsigned result is returned.
195 @param Multiplicand A 64-bit unsigned value.
196 @param Multiplier A 32-bit unsigned value.
198 @return Multiplicand * Multiplier
203 InternalMathMultU64x32 (
204 IN UINT64 Multiplicand
,
208 return Multiplicand
* Multiplier
;
213 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
214 and generates a 64-bit unsigned result.
216 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
217 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
218 bit unsigned result is returned.
220 @param Multiplicand A 64-bit unsigned value.
221 @param Multiplier A 64-bit unsigned value.
223 @return Multiplicand * Multiplier
228 InternalMathMultU64x64 (
229 IN UINT64 Multiplicand
,
233 return Multiplicand
* Multiplier
;
237 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
238 generates a 64-bit unsigned result.
240 This function divides the 64-bit unsigned value Dividend by the 32-bit
241 unsigned value Divisor and generates a 64-bit unsigned quotient. This
242 function returns the 64-bit unsigned quotient.
244 @param Dividend A 64-bit unsigned value.
245 @param Divisor A 32-bit unsigned value.
247 @return Dividend / Divisor
252 InternalMathDivU64x32 (
257 return Dividend
/ Divisor
;
261 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
262 and generates a 32-bit unsigned remainder.
264 This function divides the 64-bit unsigned value Dividend by the 32-bit
265 unsigned value Divisor and generates a 32-bit remainder. This function
266 returns the 32-bit unsigned remainder.
268 @param Dividend A 64-bit unsigned value.
269 @param Divisor A 32-bit unsigned value.
271 @return Dividend % Divisor
276 InternalMathModU64x32 (
281 return (UINT32
)(Dividend
% Divisor
);
285 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
286 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
288 This function divides the 64-bit unsigned value Dividend by the 32-bit
289 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
290 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
291 This function returns the 64-bit unsigned quotient.
293 @param Dividend A 64-bit unsigned value.
294 @param Divisor A 32-bit unsigned value.
295 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
296 optional and may be NULL.
298 @return Dividend / Divisor
303 InternalMathDivRemU64x32 (
306 OUT UINT32
*Remainder OPTIONAL
309 if (Remainder
!= NULL
) {
310 *Remainder
= (UINT32
)(Dividend
% Divisor
);
312 return Dividend
/ Divisor
;
316 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
317 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
319 This function divides the 64-bit unsigned value Dividend by the 64-bit
320 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
321 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
322 This function returns the 64-bit unsigned quotient.
324 @param Dividend A 64-bit unsigned value.
325 @param Divisor A 64-bit unsigned value.
326 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
327 optional and may be NULL.
329 @return Dividend / Divisor
334 InternalMathDivRemU64x64 (
337 OUT UINT64
*Remainder OPTIONAL
340 if (Remainder
!= NULL
) {
341 *Remainder
= Dividend
% Divisor
;
343 return Dividend
/ Divisor
;
347 Divides a 64-bit signed integer by a 64-bit signed integer and
348 generates a 64-bit signed result and a optional 64-bit signed remainder.
350 This function divides the 64-bit unsigned value Dividend by the 64-bit
351 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
352 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
353 This function returns the 64-bit unsigned quotient.
355 @param Dividend A 64-bit signed value.
356 @param Divisor A 64-bit signed value.
357 @param Remainder A pointer to a 64-bit signed value. This parameter is
358 optional and may be NULL.
360 @return Dividend / Divisor
364 InternalMathDivRemS64x64 (
367 OUT INT64
*Remainder OPTIONAL
370 if (Remainder
!= NULL
) {
371 *Remainder
= Dividend
% Divisor
;
373 return Dividend
/ Divisor
;