]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
Removed MdePkg usage of ModuleName: in file headers
[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 **/
15
16 //
17 // Include common header file for this module.
18 //
19
20
21 #include "BaseLibInternals.h"
22
23 /**
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.
26
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.
29
30 @param Operand The 64-bit operand to shift left.
31 @param Count The number of bits to shift left.
32
33 @return Operand << Count
34
35 **/
36 UINT64
37 EFIAPI
38 InternalMathLShiftU64 (
39 IN UINT64 Operand,
40 IN UINTN Count
41 )
42 {
43 return Operand << Count;
44 }
45
46 /**
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.
49
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.
52
53 @param Operand The 64-bit operand to shift right.
54 @param Count The number of bits to shift right.
55
56 @return Operand >> Count
57
58 **/
59 UINT64
60 EFIAPI
61 InternalMathRShiftU64 (
62 IN UINT64 Operand,
63 IN UINTN Count
64 )
65 {
66 return Operand >> Count;
67 }
68
69 /**
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.
72
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.
75
76 If Count is greater than 63, then ASSERT().
77
78 @param Operand The 64-bit operand to shift right.
79 @param Count The number of bits to shift right.
80
81 @return Operand arithmetically shifted right by Count
82
83 **/
84 UINT64
85 EFIAPI
86 InternalMathARShiftU64 (
87 IN UINT64 Operand,
88 IN UINTN Count
89 )
90 {
91 INTN TestValue;
92
93 //
94 // Test if this compiler supports arithmetic shift
95 //
96 TestValue = (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
97 if (TestValue == -1) {
98 //
99 // Arithmetic shift is supported
100 //
101 return (UINT64)((INT64)Operand >> Count);
102 }
103
104 //
105 // Arithmetic is not supported
106 //
107 return (Operand >> Count) |
108 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
109 }
110
111
112 /**
113 Rotates a 64-bit integer left between 0 and 63 bits, filling
114 the low bits with the high bits that were rotated.
115
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
118 value is returned.
119
120 @param Operand The 64-bit operand to rotate left.
121 @param Count The number of bits to rotate left.
122
123 @return Operand <<< Count
124
125 **/
126 UINT64
127 EFIAPI
128 InternalMathLRotU64 (
129 IN UINT64 Operand,
130 IN UINTN Count
131 )
132 {
133 return (Operand << Count) | (Operand >> (64 - Count));
134 }
135
136 /**
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.
139
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
142 value is returned.
143
144 @param Operand The 64-bit operand to rotate right.
145 @param Count The number of bits to rotate right.
146
147 @return Operand >>> Count
148
149 **/
150 UINT64
151 EFIAPI
152 InternalMathRRotU64 (
153 IN UINT64 Operand,
154 IN UINTN Count
155 )
156 {
157 return (Operand >> Count) | (Operand << (64 - Count));
158 }
159
160 /**
161 Switches the endianess of a 64-bit integer.
162
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
165 returned.
166
167 @param Operand A 64-bit unsigned value.
168
169 @return The byte swaped Operand.
170
171 **/
172 UINT64
173 EFIAPI
174 InternalMathSwapBytes64 (
175 IN UINT64 Operand
176 )
177 {
178 UINT64 LowerBytes;
179 UINT64 HigherBytes;
180
181 LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);
182 HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));
183
184 return (LowerBytes << 32 | HigherBytes);
185 }
186
187 /**
188 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
189 and generates a 64-bit unsigned result.
190
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.
194
195 @param Multiplicand A 64-bit unsigned value.
196 @param Multiplier A 32-bit unsigned value.
197
198 @return Multiplicand * Multiplier
199
200 **/
201 UINT64
202 EFIAPI
203 InternalMathMultU64x32 (
204 IN UINT64 Multiplicand,
205 IN UINT32 Multiplier
206 )
207 {
208 return Multiplicand * Multiplier;
209 }
210
211
212 /**
213 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
214 and generates a 64-bit unsigned result.
215
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.
219
220 @param Multiplicand A 64-bit unsigned value.
221 @param Multiplier A 64-bit unsigned value.
222
223 @return Multiplicand * Multiplier
224
225 **/
226 UINT64
227 EFIAPI
228 InternalMathMultU64x64 (
229 IN UINT64 Multiplicand,
230 IN UINT64 Multiplier
231 )
232 {
233 return Multiplicand * Multiplier;
234 }
235
236 /**
237 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
238 generates a 64-bit unsigned result.
239
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.
243
244 @param Dividend A 64-bit unsigned value.
245 @param Divisor A 32-bit unsigned value.
246
247 @return Dividend / Divisor
248
249 **/
250 UINT64
251 EFIAPI
252 InternalMathDivU64x32 (
253 IN UINT64 Dividend,
254 IN UINT32 Divisor
255 )
256 {
257 return Dividend / Divisor;
258 }
259
260 /**
261 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
262 and generates a 32-bit unsigned remainder.
263
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.
267
268 @param Dividend A 64-bit unsigned value.
269 @param Divisor A 32-bit unsigned value.
270
271 @return Dividend % Divisor
272
273 **/
274 UINT32
275 EFIAPI
276 InternalMathModU64x32 (
277 IN UINT64 Dividend,
278 IN UINT32 Divisor
279 )
280 {
281 return (UINT32)(Dividend % Divisor);
282 }
283
284 /**
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.
287
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.
292
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.
297
298 @return Dividend / Divisor
299
300 **/
301 UINT64
302 EFIAPI
303 InternalMathDivRemU64x32 (
304 IN UINT64 Dividend,
305 IN UINT32 Divisor,
306 OUT UINT32 *Remainder OPTIONAL
307 )
308 {
309 if (Remainder != NULL) {
310 *Remainder = (UINT32)(Dividend % Divisor);
311 }
312 return Dividend / Divisor;
313 }
314
315 /**
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.
318
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.
323
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.
328
329 @return Dividend / Divisor
330
331 **/
332 UINT64
333 EFIAPI
334 InternalMathDivRemU64x64 (
335 IN UINT64 Dividend,
336 IN UINT64 Divisor,
337 OUT UINT64 *Remainder OPTIONAL
338 )
339 {
340 if (Remainder != NULL) {
341 *Remainder = Dividend % Divisor;
342 }
343 return Dividend / Divisor;
344 }
345
346 /**
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.
349
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.
354
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.
359
360 @return Dividend / Divisor
361
362 **/
363 INT64
364 InternalMathDivRemS64x64 (
365 IN INT64 Dividend,
366 IN INT64 Divisor,
367 OUT INT64 *Remainder OPTIONAL
368 )
369 {
370 if (Remainder != NULL) {
371 *Remainder = Dividend % Divisor;
372 }
373 return Dividend / Divisor;
374 }