]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
a8756967befaf73f6d2a4978ff4e0943393e05e7
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 Worker functons that 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
32 InternalMathLShiftU64 (
37 return Operand
<< Count
;
41 Worker functon that shifts a 64-bit integer right between 0 and 63 bits. This high bits
42 are filled with zeros. The shifted value is returned.
44 This function shifts the 64-bit value Operand to the right by Count bits. The
45 high Count bits are set to zero. The shifted value is returned.
47 @param Operand The 64-bit operand to shift right.
48 @param Count The number of bits to shift right.
50 @return Operand >> Count
55 InternalMathRShiftU64 (
60 return Operand
>> Count
;
64 Worker function that shifts a 64-bit integer right between 0 and 63 bits. The high bits
65 are filled with original integer's bit 63. The shifted value is returned.
67 This function shifts the 64-bit value Operand to the right by Count bits. The
68 high Count bits are set to bit 63 of Operand. The shifted value is returned.
70 If Count is greater than 63, then ASSERT().
72 @param Operand The 64-bit operand to shift right.
73 @param Count The number of bits to shift right.
75 @return Operand arithmetically shifted right by Count
80 InternalMathARShiftU64 (
86 // Test if this compiler supports arithmetic shift
88 if ((((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1)) == -1) {
90 // Arithmetic shift is supported
92 return (UINT64
)((INT64
)Operand
>> Count
);
96 // Arithmetic is not supported
98 return (Operand
>> Count
) |
99 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
104 Worker function that rotates a 64-bit integer left between 0 and 63 bits, filling
105 the low bits with the high bits that were rotated.
107 This function rotates the 64-bit value Operand to the left by Count bits. The
108 low Count bits are fill with the high Count bits of Operand. The rotated
111 @param Operand The 64-bit operand to rotate left.
112 @param Count The number of bits to rotate left.
114 @return Operand <<< Count
119 InternalMathLRotU64 (
124 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
128 Worker function that rotates a 64-bit integer right between 0 and 63 bits, filling
129 the high bits with the high low bits that were rotated.
131 This function rotates the 64-bit value Operand to the right by Count bits.
132 The high Count bits are fill with the low Count bits of Operand. The rotated
135 @param Operand The 64-bit operand to rotate right.
136 @param Count The number of bits to rotate right.
138 @return Operand >>> Count
143 InternalMathRRotU64 (
148 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
152 Worker function that switches the endianess of a 64-bit integer.
154 This function swaps the bytes in a 64-bit unsigned value to switch the value
155 from little endian to big endian or vice versa. The byte swapped value is
158 @param Operand A 64-bit unsigned value.
160 @return The byte swaped Operand.
165 InternalMathSwapBytes64 (
170 ((UINT64
)SwapBytes32 ((UINT32
)Operand
) << 32) |
171 ((UINT64
)SwapBytes32 ((UINT32
)(Operand
>> 32)))
176 Worker function that multiples a 64-bit unsigned integer by a 32-bit unsigned integer
177 and generates a 64-bit unsigned result.
179 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
180 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
181 bit unsigned result is returned.
183 @param Multiplicand A 64-bit unsigned value.
184 @param Multiplier A 32-bit unsigned value.
186 @return Multiplicand * Multiplier
191 InternalMathMultU64x32 (
192 IN UINT64 Multiplicand
,
196 return Multiplicand
* Multiplier
;
201 Worker function that multiples a 64-bit unsigned integer by a 64-bit unsigned integer
202 and generates a 64-bit unsigned result.
204 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
205 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
206 bit unsigned result is returned.
208 @param Multiplicand A 64-bit unsigned value.
209 @param Multiplier A 64-bit unsigned value.
211 @return Multiplicand * Multiplier
216 InternalMathMultU64x64 (
217 IN UINT64 Multiplicand
,
221 return Multiplicand
* Multiplier
;
225 Worker function that divides a 64-bit unsigned integer by a 32-bit unsigned integer and
226 generates a 64-bit unsigned result.
228 This function divides the 64-bit unsigned value Dividend by the 32-bit
229 unsigned value Divisor and generates a 64-bit unsigned quotient. This
230 function returns the 64-bit unsigned quotient.
232 @param Dividend A 64-bit unsigned value.
233 @param Divisor A 32-bit unsigned value.
235 @return Dividend / Divisor
240 InternalMathDivU64x32 (
245 return Dividend
/ Divisor
;
249 Worker function that divides a 64-bit unsigned integer by a 32-bit unsigned integer
250 and generates a 32-bit unsigned remainder.
252 This function divides the 64-bit unsigned value Dividend by the 32-bit
253 unsigned value Divisor and generates a 32-bit remainder. This function
254 returns the 32-bit unsigned remainder.
256 @param Dividend A 64-bit unsigned value.
257 @param Divisor A 32-bit unsigned value.
259 @return Dividend % Divisor
263 InternalMathModU64x32 (
268 return (UINT32
)(Dividend
% Divisor
);
272 Worker function that divides a 64-bit unsigned integer by a 32-bit unsigned integer and
273 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
275 This function divides the 64-bit unsigned value Dividend by the 32-bit
276 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
277 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
278 This function returns the 64-bit unsigned quotient.
280 @param Dividend A 64-bit unsigned value.
281 @param Divisor A 32-bit unsigned value.
282 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
283 optional and may be NULL.
285 @return Dividend / Divisor
289 InternalMathDivRemU64x32 (
292 OUT UINT32
*Remainder OPTIONAL
295 if (Remainder
!= NULL
) {
296 *Remainder
= (UINT32
)(Dividend
% Divisor
);
298 return Dividend
/ Divisor
;
302 Worker function that divides a 64-bit unsigned integer by a 64-bit unsigned integer and
303 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
305 This function divides the 64-bit unsigned value Dividend by the 64-bit
306 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
307 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
308 This function returns the 64-bit unsigned quotient.
310 @param Dividend A 64-bit unsigned value.
311 @param Divisor A 64-bit unsigned value.
312 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
313 optional and may be NULL.
315 @return Dividend / Divisor
319 InternalMathDivRemU64x64 (
322 OUT UINT64
*Remainder OPTIONAL
325 if (Remainder
!= NULL
) {
326 *Remainder
= Dividend
% Divisor
;
328 return Dividend
/ Divisor
;
332 Worker function that divides a 64-bit signed integer by a 64-bit signed integer and
333 generates a 64-bit signed result and a optional 64-bit signed remainder.
335 This function divides the 64-bit unsigned value Dividend by the 64-bit
336 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
337 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
338 This function returns the 64-bit unsigned quotient.
340 @param Dividend A 64-bit signed value.
341 @param Divisor A 64-bit signed value.
342 @param Remainder A pointer to a 64-bit signed value. This parameter is
343 optional and may be NULL.
345 @return Dividend / Divisor
349 InternalMathDivRemS64x64 (
352 OUT INT64
*Remainder OPTIONAL
355 if (Remainder
!= NULL
) {
356 *Remainder
= Dividend
% Divisor
;
358 return Dividend
/ Divisor
;