]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
Optimized HighBitSetXX() functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / Math64.c
1 /** @file
2 Leaf math worker functions that require 64-bit arithmetic support from the
3 compiler.
4
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
10
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.
13
14 Module Name: Math64.c
15
16 **/
17
18 #include "BaseLibInternals.h"
19
20 /**
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.
23
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.
26
27 @param Operand The 64-bit operand to shift left.
28 @param Count The number of bits to shift left.
29
30 @return Operand << Count
31
32 **/
33 UINT64
34 EFIAPI
35 InternalMathLShiftU64 (
36 IN UINT64 Operand,
37 IN UINTN Count
38 )
39 {
40 return Operand << Count;
41 }
42
43 /**
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.
46
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.
49
50 @param Operand The 64-bit operand to shift right.
51 @param Count The number of bits to shift right.
52
53 @return Operand >> Count
54
55 **/
56 UINT64
57 EFIAPI
58 InternalMathRShiftU64 (
59 IN UINT64 Operand,
60 IN UINTN Count
61 )
62 {
63 return Operand >> Count;
64 }
65
66 /**
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.
69
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.
72
73 If Count is greater than 63, then ASSERT().
74
75 @param Operand The 64-bit operand to shift right.
76 @param Count The number of bits to shift right.
77
78 @return Operand arithmetically shifted right by Count
79
80 **/
81 UINT64
82 EFIAPI
83 InternalMathARShiftU64 (
84 IN UINT64 Operand,
85 IN UINTN Count
86 )
87 {
88 //
89 // Test if this compiler supports arithmetic shift
90 //
91 if ((((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1)) == -1) {
92 //
93 // Arithmetic shift is supported
94 //
95 return (UINT64)((INT64)Operand >> Count);
96 }
97
98 //
99 // Arithmetic is not supported
100 //
101 return (Operand >> Count) |
102 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
103 }
104
105
106 /**
107 Rotates a 64-bit integer left between 0 and 63 bits, filling
108 the low bits with the high bits that were rotated.
109
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
112 value is returned.
113
114 @param Operand The 64-bit operand to rotate left.
115 @param Count The number of bits to rotate left.
116
117 @return Operand <<< Count
118
119 **/
120 UINT64
121 EFIAPI
122 InternalMathLRotU64 (
123 IN UINT64 Operand,
124 IN UINTN Count
125 )
126 {
127 return (Operand << Count) | (Operand >> (64 - Count));
128 }
129
130 /**
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.
133
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
136 value is returned.
137
138 @param Operand The 64-bit operand to rotate right.
139 @param Count The number of bits to rotate right.
140
141 @return Operand >>> Count
142
143 **/
144 UINT64
145 EFIAPI
146 InternalMathRRotU64 (
147 IN UINT64 Operand,
148 IN UINTN Count
149 )
150 {
151 return (Operand >> Count) | (Operand << (64 - Count));
152 }
153
154 /**
155 Switches the endianess of a 64-bit integer.
156
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
159 returned.
160
161 @param Operand A 64-bit unsigned value.
162
163 @return The byte swaped Operand.
164
165 **/
166 UINT64
167 EFIAPI
168 InternalMathSwapBytes64 (
169 IN UINT64 Operand
170 )
171 {
172 return (UINT64)(
173 ((UINT64)SwapBytes32 ((UINT32)Operand) << 32) |
174 ((UINT64)SwapBytes32 ((UINT32)(Operand >> 32)))
175 );
176 }
177
178 /**
179 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
180 and generates a 64-bit unsigned result.
181
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.
185
186 @param Multiplicand A 64-bit unsigned value.
187 @param Multiplier A 32-bit unsigned value.
188
189 @return Multiplicand * Multiplier
190
191 **/
192 UINT64
193 EFIAPI
194 InternalMathMultU64x32 (
195 IN UINT64 Multiplicand,
196 IN UINT32 Multiplier
197 )
198 {
199 return Multiplicand * Multiplier;
200 }
201
202
203 /**
204 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
205 and generates a 64-bit unsigned result.
206
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.
210
211 @param Multiplicand A 64-bit unsigned value.
212 @param Multiplier A 64-bit unsigned value.
213
214 @return Multiplicand * Multiplier
215
216 **/
217 UINT64
218 EFIAPI
219 InternalMathMultU64x64 (
220 IN UINT64 Multiplicand,
221 IN UINT64 Multiplier
222 )
223 {
224 return Multiplicand * Multiplier;
225 }
226
227 /**
228 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
229 generates a 64-bit unsigned result.
230
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.
234
235 @param Dividend A 64-bit unsigned value.
236 @param Divisor A 32-bit unsigned value.
237
238 @return Dividend / Divisor
239
240 **/
241 UINT64
242 EFIAPI
243 InternalMathDivU64x32 (
244 IN UINT64 Dividend,
245 IN UINT32 Divisor
246 )
247 {
248 return Dividend / Divisor;
249 }
250
251 /**
252 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
253 and generates a 32-bit unsigned remainder.
254
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.
258
259 @param Dividend A 64-bit unsigned value.
260 @param Divisor A 32-bit unsigned value.
261
262 @return Dividend % Divisor
263
264 **/
265 UINT32
266 EFIAPI
267 InternalMathModU64x32 (
268 IN UINT64 Dividend,
269 IN UINT32 Divisor
270 )
271 {
272 return (UINT32)(Dividend % Divisor);
273 }
274
275 /**
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.
278
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.
283
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.
288
289 @return Dividend / Divisor
290
291 **/
292 UINT64
293 EFIAPI
294 InternalMathDivRemU64x32 (
295 IN UINT64 Dividend,
296 IN UINT32 Divisor,
297 OUT UINT32 *Remainder OPTIONAL
298 )
299 {
300 if (Remainder != NULL) {
301 *Remainder = (UINT32)(Dividend % Divisor);
302 }
303 return Dividend / Divisor;
304 }
305
306 /**
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.
309
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.
314
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.
319
320 @return Dividend / Divisor
321
322 **/
323 UINT64
324 EFIAPI
325 InternalMathDivRemU64x64 (
326 IN UINT64 Dividend,
327 IN UINT64 Divisor,
328 OUT UINT64 *Remainder OPTIONAL
329 )
330 {
331 if (Remainder != NULL) {
332 *Remainder = Dividend % Divisor;
333 }
334 return Dividend / Divisor;
335 }
336
337 /**
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.
340
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.
345
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.
350
351 @return Dividend / Divisor
352
353 **/
354 INT64
355 InternalMathDivRemS64x64 (
356 IN INT64 Dividend,
357 IN INT64 Divisor,
358 OUT INT64 *Remainder OPTIONAL
359 )
360 {
361 if (Remainder != NULL) {
362 *Remainder = Dividend % Divisor;
363 }
364 return Dividend / Divisor;
365 }