]>
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.
18 #include "BaseLibInternals.h"
21 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
22 are filled with zeros. The shifted value is returned.
24 This function shifts the 64-bit value Operand to the left by Count bits. The
25 low Count bits are set to zero. The shifted value is returned.
27 @param Operand The 64-bit operand to shift left.
28 @param Count The number of bits to shift left.
30 @return Operand << Count
35 InternalMathLShiftU64 (
40 return Operand
<< Count
;
44 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
45 are filled with zeros. The shifted value is returned.
47 This function shifts the 64-bit value Operand to the right by Count bits. The
48 high Count bits are set to zero. The shifted value is returned.
50 @param Operand The 64-bit operand to shift right.
51 @param Count The number of bits to shift right.
53 @return Operand >> Count
58 InternalMathRShiftU64 (
63 return Operand
>> Count
;
67 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
68 are filled with original integer's bit 63. The shifted value is returned.
70 This function shifts the 64-bit value Operand to the right by Count bits. The
71 high Count bits are set to bit 63 of Operand. The shifted value is returned.
73 If Count is greater than 63, then ASSERT().
75 @param Operand The 64-bit operand to shift right.
76 @param Count The number of bits to shift right.
78 @return Operand arithmetically shifted right by Count
83 InternalMathARShiftU64 (
91 // Test if this compiler supports arithmetic shift
93 TestValue
= (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
94 if (TestValue
== -1) {
96 // Arithmetic shift is supported
98 return (UINT64
)((INT64
)Operand
>> Count
);
102 // Arithmetic is not supported
104 return (Operand
>> Count
) |
105 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
110 Rotates a 64-bit integer left between 0 and 63 bits, filling
111 the low bits with the high bits that were rotated.
113 This function rotates the 64-bit value Operand to the left by Count bits. The
114 low Count bits are fill with the high Count bits of Operand. The rotated
117 @param Operand The 64-bit operand to rotate left.
118 @param Count The number of bits to rotate left.
120 @return Operand <<< Count
125 InternalMathLRotU64 (
130 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
134 Rotates a 64-bit integer right between 0 and 63 bits, filling
135 the high bits with the high low bits that were rotated.
137 This function rotates the 64-bit value Operand to the right by Count bits.
138 The high Count bits are fill with the low Count bits of Operand. The rotated
141 @param Operand The 64-bit operand to rotate right.
142 @param Count The number of bits to rotate right.
144 @return Operand >>> Count
149 InternalMathRRotU64 (
154 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
158 Switches the endianess of a 64-bit integer.
160 This function swaps the bytes in a 64-bit unsigned value to switch the value
161 from little endian to big endian or vice versa. The byte swapped value is
164 @param Operand A 64-bit unsigned value.
166 @return The byte swaped Operand.
171 InternalMathSwapBytes64 (
178 LowerBytes
= (UINT64
) SwapBytes32 ((UINT32
) Operand
);
179 HigherBytes
= (UINT64
) SwapBytes32 ((UINT32
) (Operand
>> 32));
181 return (LowerBytes
<< 32 | HigherBytes
);
185 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
186 and generates a 64-bit unsigned result.
188 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
189 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
190 bit unsigned result is returned.
192 @param Multiplicand A 64-bit unsigned value.
193 @param Multiplier A 32-bit unsigned value.
195 @return Multiplicand * Multiplier
200 InternalMathMultU64x32 (
201 IN UINT64 Multiplicand
,
205 return Multiplicand
* Multiplier
;
210 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
211 and generates a 64-bit unsigned result.
213 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
214 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
215 bit unsigned result is returned.
217 @param Multiplicand A 64-bit unsigned value.
218 @param Multiplier A 64-bit unsigned value.
220 @return Multiplicand * Multiplier
225 InternalMathMultU64x64 (
226 IN UINT64 Multiplicand
,
230 return Multiplicand
* Multiplier
;
234 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
235 generates a 64-bit unsigned result.
237 This function divides the 64-bit unsigned value Dividend by the 32-bit
238 unsigned value Divisor and generates a 64-bit unsigned quotient. This
239 function returns the 64-bit unsigned quotient.
241 @param Dividend A 64-bit unsigned value.
242 @param Divisor A 32-bit unsigned value.
244 @return Dividend / Divisor
249 InternalMathDivU64x32 (
254 return Dividend
/ Divisor
;
258 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
259 and generates a 32-bit unsigned remainder.
261 This function divides the 64-bit unsigned value Dividend by the 32-bit
262 unsigned value Divisor and generates a 32-bit remainder. This function
263 returns the 32-bit unsigned remainder.
265 @param Dividend A 64-bit unsigned value.
266 @param Divisor A 32-bit unsigned value.
268 @return Dividend % Divisor
273 InternalMathModU64x32 (
278 return (UINT32
)(Dividend
% Divisor
);
282 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
283 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
285 This function divides the 64-bit unsigned value Dividend by the 32-bit
286 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
287 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
288 This function returns the 64-bit unsigned quotient.
290 @param Dividend A 64-bit unsigned value.
291 @param Divisor A 32-bit unsigned value.
292 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
293 optional and may be NULL.
295 @return Dividend / Divisor
300 InternalMathDivRemU64x32 (
303 OUT UINT32
*Remainder OPTIONAL
306 if (Remainder
!= NULL
) {
307 *Remainder
= (UINT32
)(Dividend
% Divisor
);
309 return Dividend
/ Divisor
;
313 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
314 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
316 This function divides the 64-bit unsigned value Dividend by the 64-bit
317 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
318 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
319 This function returns the 64-bit unsigned quotient.
321 @param Dividend A 64-bit unsigned value.
322 @param Divisor A 64-bit unsigned value.
323 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
324 optional and may be NULL.
326 @return Dividend / Divisor
331 InternalMathDivRemU64x64 (
334 OUT UINT64
*Remainder OPTIONAL
337 if (Remainder
!= NULL
) {
338 *Remainder
= Dividend
% Divisor
;
340 return Dividend
/ Divisor
;
344 Divides a 64-bit signed integer by a 64-bit signed integer and
345 generates a 64-bit signed result and a optional 64-bit signed remainder.
347 This function divides the 64-bit unsigned value Dividend by the 64-bit
348 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
349 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
350 This function returns the 64-bit unsigned quotient.
352 @param Dividend A 64-bit signed value.
353 @param Divisor A 64-bit signed value.
354 @param Remainder A pointer to a 64-bit signed value. This parameter is
355 optional and may be NULL.
357 @return Dividend / Divisor
361 InternalMathDivRemS64x64 (
364 OUT INT64
*Remainder OPTIONAL
367 if (Remainder
!= NULL
) {
368 *Remainder
= Dividend
% Divisor
;
370 return Dividend
/ Divisor
;