]>
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 (
89 // Test if this compiler supports arithmetic shift
91 if ((((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1)) == -1) {
93 // Arithmetic shift is supported
95 return (UINT64
)((INT64
)Operand
>> Count
);
99 // Arithmetic is not supported
101 return (Operand
>> Count
) |
102 ((INTN
)Operand
< 0 ? ~((UINTN
)-1 >> Count
) : 0);
107 Rotates a 64-bit integer left between 0 and 63 bits, filling
108 the low bits with the high bits that were rotated.
110 This function rotates the 64-bit value Operand to the left by Count bits. The
111 low Count bits are fill with the high Count bits of Operand. The rotated
114 @param Operand The 64-bit operand to rotate left.
115 @param Count The number of bits to rotate left.
117 @return Operand <<< Count
122 InternalMathLRotU64 (
127 return (Operand
<< Count
) | (Operand
>> (64 - Count
));
131 Rotates a 64-bit integer right between 0 and 63 bits, filling
132 the high bits with the high low bits that were rotated.
134 This function rotates the 64-bit value Operand to the right by Count bits.
135 The high Count bits are fill with the low Count bits of Operand. The rotated
138 @param Operand The 64-bit operand to rotate right.
139 @param Count The number of bits to rotate right.
141 @return Operand >>> Count
146 InternalMathRRotU64 (
151 return (Operand
>> Count
) | (Operand
<< (64 - Count
));
155 Switches the endianess of a 64-bit integer.
157 This function swaps the bytes in a 64-bit unsigned value to switch the value
158 from little endian to big endian or vice versa. The byte swapped value is
161 @param Operand A 64-bit unsigned value.
163 @return The byte swaped Operand.
168 InternalMathSwapBytes64 (
173 ((UINT64
)SwapBytes32 ((UINT32
)Operand
) << 32) |
174 ((UINT64
)SwapBytes32 ((UINT32
)(Operand
>> 32)))
179 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
180 and generates a 64-bit unsigned result.
182 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
183 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
184 bit unsigned result is returned.
186 @param Multiplicand A 64-bit unsigned value.
187 @param Multiplier A 32-bit unsigned value.
189 @return Multiplicand * Multiplier
194 InternalMathMultU64x32 (
195 IN UINT64 Multiplicand
,
199 return Multiplicand
* Multiplier
;
204 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
205 and generates a 64-bit unsigned result.
207 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
208 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
209 bit unsigned result is returned.
211 @param Multiplicand A 64-bit unsigned value.
212 @param Multiplier A 64-bit unsigned value.
214 @return Multiplicand * Multiplier
219 InternalMathMultU64x64 (
220 IN UINT64 Multiplicand
,
224 return Multiplicand
* Multiplier
;
228 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
229 generates a 64-bit unsigned result.
231 This function divides the 64-bit unsigned value Dividend by the 32-bit
232 unsigned value Divisor and generates a 64-bit unsigned quotient. This
233 function returns the 64-bit unsigned quotient.
235 @param Dividend A 64-bit unsigned value.
236 @param Divisor A 32-bit unsigned value.
238 @return Dividend / Divisor
243 InternalMathDivU64x32 (
248 return Dividend
/ Divisor
;
252 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
253 and generates a 32-bit unsigned remainder.
255 This function divides the 64-bit unsigned value Dividend by the 32-bit
256 unsigned value Divisor and generates a 32-bit remainder. This function
257 returns the 32-bit unsigned remainder.
259 @param Dividend A 64-bit unsigned value.
260 @param Divisor A 32-bit unsigned value.
262 @return Dividend % Divisor
267 InternalMathModU64x32 (
272 return (UINT32
)(Dividend
% Divisor
);
276 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
277 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
279 This function divides the 64-bit unsigned value Dividend by the 32-bit
280 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
281 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
282 This function returns the 64-bit unsigned quotient.
284 @param Dividend A 64-bit unsigned value.
285 @param Divisor A 32-bit unsigned value.
286 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
287 optional and may be NULL.
289 @return Dividend / Divisor
294 InternalMathDivRemU64x32 (
297 OUT UINT32
*Remainder OPTIONAL
300 if (Remainder
!= NULL
) {
301 *Remainder
= (UINT32
)(Dividend
% Divisor
);
303 return Dividend
/ Divisor
;
307 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
308 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
310 This function divides the 64-bit unsigned value Dividend by the 64-bit
311 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
312 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
313 This function returns the 64-bit unsigned quotient.
315 @param Dividend A 64-bit unsigned value.
316 @param Divisor A 64-bit unsigned value.
317 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
318 optional and may be NULL.
320 @return Dividend / Divisor
325 InternalMathDivRemU64x64 (
328 OUT UINT64
*Remainder OPTIONAL
331 if (Remainder
!= NULL
) {
332 *Remainder
= Dividend
% Divisor
;
334 return Dividend
/ Divisor
;
338 Divides a 64-bit signed integer by a 64-bit signed integer and
339 generates a 64-bit signed result and a optional 64-bit signed remainder.
341 This function divides the 64-bit unsigned value Dividend by the 64-bit
342 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
343 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
344 This function returns the 64-bit unsigned quotient.
346 @param Dividend A 64-bit signed value.
347 @param Divisor A 64-bit signed value.
348 @param Remainder A pointer to a 64-bit signed value. This parameter is
349 optional and may be NULL.
351 @return Dividend / Divisor
355 InternalMathDivRemS64x64 (
358 OUT INT64
*Remainder OPTIONAL
361 if (Remainder
!= NULL
) {
362 *Remainder
= Dividend
% Divisor
;
364 return Dividend
/ Divisor
;