]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
0b127c70b1037541ab948c8e229d2bc6b200b98f
[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 - 2008, 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 #include "BaseLibInternals.h"
17
18 /**
19 Shifts a 64-bit integer left between 0 and 63 bits. The low bits
20 are filled with zeros. The shifted value is returned.
21
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.
24
25 @param Operand The 64-bit operand to shift left.
26 @param Count The number of bits to shift left.
27
28 @return Operand << Count
29
30 **/
31 UINT64
32 EFIAPI
33 InternalMathLShiftU64 (
34 IN UINT64 Operand,
35 IN UINTN Count
36 )
37 {
38 return Operand << Count;
39 }
40
41 /**
42 Shifts a 64-bit integer right between 0 and 63 bits. This high bits
43 are filled with zeros. The shifted value is returned.
44
45 This function shifts the 64-bit value Operand to the right by Count bits. The
46 high Count bits are set to zero. The shifted value is returned.
47
48 @param Operand The 64-bit operand to shift right.
49 @param Count The number of bits to shift right.
50
51 @return Operand >> Count
52
53 **/
54 UINT64
55 EFIAPI
56 InternalMathRShiftU64 (
57 IN UINT64 Operand,
58 IN UINTN Count
59 )
60 {
61 return Operand >> Count;
62 }
63
64 /**
65 Shifts a 64-bit integer right between 0 and 63 bits. The high bits
66 are filled with original integer's bit 63. The shifted value is returned.
67
68 This function shifts the 64-bit value Operand to the right by Count bits. The
69 high Count bits are set to bit 63 of Operand. The shifted value is returned.
70
71 If Count is greater than 63, then ASSERT().
72
73 @param Operand The 64-bit operand to shift right.
74 @param Count The number of bits to shift right.
75
76 @return Operand arithmetically shifted right by Count
77
78 **/
79 UINT64
80 EFIAPI
81 InternalMathARShiftU64 (
82 IN UINT64 Operand,
83 IN UINTN Count
84 )
85 {
86 INTN TestValue;
87
88 //
89 // Test if this compiler supports arithmetic shift
90 //
91 TestValue = (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
92 if (TestValue == -1) {
93 //
94 // Arithmetic shift is supported
95 //
96 return (UINT64)((INT64)Operand >> Count);
97 }
98
99 //
100 // Arithmetic is not supported
101 //
102 return (Operand >> Count) |
103 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
104 }
105
106
107 /**
108 Rotates a 64-bit integer left between 0 and 63 bits, filling
109 the low bits with the high bits that were rotated.
110
111 This function rotates the 64-bit value Operand to the left by Count bits. The
112 low Count bits are fill with the high Count bits of Operand. The rotated
113 value is returned.
114
115 @param Operand The 64-bit operand to rotate left.
116 @param Count The number of bits to rotate left.
117
118 @return Operand <<< Count
119
120 **/
121 UINT64
122 EFIAPI
123 InternalMathLRotU64 (
124 IN UINT64 Operand,
125 IN UINTN Count
126 )
127 {
128 return (Operand << Count) | (Operand >> (64 - Count));
129 }
130
131 /**
132 Rotates a 64-bit integer right between 0 and 63 bits, filling
133 the high bits with the high low bits that were rotated.
134
135 This function rotates the 64-bit value Operand to the right by Count bits.
136 The high Count bits are fill with the low Count bits of Operand. The rotated
137 value is returned.
138
139 @param Operand The 64-bit operand to rotate right.
140 @param Count The number of bits to rotate right.
141
142 @return Operand >>> Count
143
144 **/
145 UINT64
146 EFIAPI
147 InternalMathRRotU64 (
148 IN UINT64 Operand,
149 IN UINTN Count
150 )
151 {
152 return (Operand >> Count) | (Operand << (64 - Count));
153 }
154
155 /**
156 Switches the endianess of a 64-bit integer.
157
158 This function swaps the bytes in a 64-bit unsigned value to switch the value
159 from little endian to big endian or vice versa. The byte swapped value is
160 returned.
161
162 @param Operand A 64-bit unsigned value.
163
164 @return The byte swaped Operand.
165
166 **/
167 UINT64
168 EFIAPI
169 InternalMathSwapBytes64 (
170 IN UINT64 Operand
171 )
172 {
173 UINT64 LowerBytes;
174 UINT64 HigherBytes;
175
176 LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);
177 HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));
178
179 return (LowerBytes << 32 | HigherBytes);
180 }
181
182 /**
183 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
184 and generates a 64-bit unsigned result.
185
186 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit
187 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
188 bit unsigned result is returned.
189
190 @param Multiplicand A 64-bit unsigned value.
191 @param Multiplier A 32-bit unsigned value.
192
193 @return Multiplicand * Multiplier
194
195 **/
196 UINT64
197 EFIAPI
198 InternalMathMultU64x32 (
199 IN UINT64 Multiplicand,
200 IN UINT32 Multiplier
201 )
202 {
203 return Multiplicand * Multiplier;
204 }
205
206
207 /**
208 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
209 and generates a 64-bit unsigned result.
210
211 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit
212 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-
213 bit unsigned result is returned.
214
215 @param Multiplicand A 64-bit unsigned value.
216 @param Multiplier A 64-bit unsigned value.
217
218 @return Multiplicand * Multiplier
219
220 **/
221 UINT64
222 EFIAPI
223 InternalMathMultU64x64 (
224 IN UINT64 Multiplicand,
225 IN UINT64 Multiplier
226 )
227 {
228 return Multiplicand * Multiplier;
229 }
230
231 /**
232 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
233 generates a 64-bit unsigned result.
234
235 This function divides the 64-bit unsigned value Dividend by the 32-bit
236 unsigned value Divisor and generates a 64-bit unsigned quotient. This
237 function returns the 64-bit unsigned quotient.
238
239 @param Dividend A 64-bit unsigned value.
240 @param Divisor A 32-bit unsigned value.
241
242 @return Dividend / Divisor
243
244 **/
245 UINT64
246 EFIAPI
247 InternalMathDivU64x32 (
248 IN UINT64 Dividend,
249 IN UINT32 Divisor
250 )
251 {
252 return Dividend / Divisor;
253 }
254
255 /**
256 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
257 and generates a 32-bit unsigned remainder.
258
259 This function divides the 64-bit unsigned value Dividend by the 32-bit
260 unsigned value Divisor and generates a 32-bit remainder. This function
261 returns the 32-bit unsigned remainder.
262
263 @param Dividend A 64-bit unsigned value.
264 @param Divisor A 32-bit unsigned value.
265
266 @return Dividend % Divisor
267
268 **/
269 UINT32
270 EFIAPI
271 InternalMathModU64x32 (
272 IN UINT64 Dividend,
273 IN UINT32 Divisor
274 )
275 {
276 return (UINT32)(Dividend % Divisor);
277 }
278
279 /**
280 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
281 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.
282
283 This function divides the 64-bit unsigned value Dividend by the 32-bit
284 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
285 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.
286 This function returns the 64-bit unsigned quotient.
287
288 @param Dividend A 64-bit unsigned value.
289 @param Divisor A 32-bit unsigned value.
290 @param Remainder A pointer to a 32-bit unsigned value. This parameter is
291 optional and may be NULL.
292
293 @return Dividend / Divisor
294
295 **/
296 UINT64
297 EFIAPI
298 InternalMathDivRemU64x32 (
299 IN UINT64 Dividend,
300 IN UINT32 Divisor,
301 OUT UINT32 *Remainder OPTIONAL
302 )
303 {
304 if (Remainder != NULL) {
305 *Remainder = (UINT32)(Dividend % Divisor);
306 }
307 return Dividend / Divisor;
308 }
309
310 /**
311 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and
312 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.
313
314 This function divides the 64-bit unsigned value Dividend by the 64-bit
315 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder
316 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.
317 This function returns the 64-bit unsigned quotient.
318
319 @param Dividend A 64-bit unsigned value.
320 @param Divisor A 64-bit unsigned value.
321 @param Remainder A pointer to a 64-bit unsigned value. This parameter is
322 optional and may be NULL.
323
324 @return Dividend / Divisor
325
326 **/
327 UINT64
328 EFIAPI
329 InternalMathDivRemU64x64 (
330 IN UINT64 Dividend,
331 IN UINT64 Divisor,
332 OUT UINT64 *Remainder OPTIONAL
333 )
334 {
335 if (Remainder != NULL) {
336 *Remainder = Dividend % Divisor;
337 }
338 return Dividend / Divisor;
339 }
340
341 /**
342 Divides a 64-bit signed integer by a 64-bit signed integer and
343 generates a 64-bit signed result and a optional 64-bit signed remainder.
344
345 This function divides the 64-bit signed value Dividend by the 64-bit
346 signed value Divisor and generates a 64-bit signed quotient. If Remainder
347 is not NULL, then the 64-bit signed remainder is returned in Remainder.
348 This function returns the 64-bit signed quotient.
349
350 @param Dividend A 64-bit signed value.
351 @param Divisor A 64-bit signed value.
352 @param Remainder A pointer to a 64-bit signed value. This parameter is
353 optional and may be NULL.
354
355 @return Dividend / Divisor
356
357 **/
358 INT64
359 EFIAPI
360 InternalMathDivRemS64x64 (
361 IN INT64 Dividend,
362 IN INT64 Divisor,
363 OUT INT64 *Remainder OPTIONAL
364 )
365 {
366 if (Remainder != NULL) {
367 *Remainder = Dividend % Divisor;
368 }
369 return Dividend / Divisor;
370 }