]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Math64.c
1. Removed #ifndef to enable Capsule architecture protocol on IPF.
[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 INTN TestValue;
89
90 //
91 // Test if this compiler supports arithmetic shift
92 //
93 TestValue = (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));
94 if (TestValue == -1) {
95 //
96 // Arithmetic shift is supported
97 //
98 return (UINT64)((INT64)Operand >> Count);
99 }
100
101 //
102 // Arithmetic is not supported
103 //
104 return (Operand >> Count) |
105 ((INTN)Operand < 0 ? ~((UINTN)-1 >> Count) : 0);
106 }
107
108
109 /**
110 Rotates a 64-bit integer left between 0 and 63 bits, filling
111 the low bits with the high bits that were rotated.
112
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
115 value is returned.
116
117 @param Operand The 64-bit operand to rotate left.
118 @param Count The number of bits to rotate left.
119
120 @return Operand <<< Count
121
122 **/
123 UINT64
124 EFIAPI
125 InternalMathLRotU64 (
126 IN UINT64 Operand,
127 IN UINTN Count
128 )
129 {
130 return (Operand << Count) | (Operand >> (64 - Count));
131 }
132
133 /**
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.
136
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
139 value is returned.
140
141 @param Operand The 64-bit operand to rotate right.
142 @param Count The number of bits to rotate right.
143
144 @return Operand >>> Count
145
146 **/
147 UINT64
148 EFIAPI
149 InternalMathRRotU64 (
150 IN UINT64 Operand,
151 IN UINTN Count
152 )
153 {
154 return (Operand >> Count) | (Operand << (64 - Count));
155 }
156
157 /**
158 Switches the endianess of a 64-bit integer.
159
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
162 returned.
163
164 @param Operand A 64-bit unsigned value.
165
166 @return The byte swaped Operand.
167
168 **/
169 UINT64
170 EFIAPI
171 InternalMathSwapBytes64 (
172 IN UINT64 Operand
173 )
174 {
175 UINT64 LowerBytes;
176 UINT64 HigherBytes;
177
178 LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);
179 HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));
180
181 return (LowerBytes << 32 | HigherBytes);
182 }
183
184 /**
185 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer
186 and generates a 64-bit unsigned result.
187
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.
191
192 @param Multiplicand A 64-bit unsigned value.
193 @param Multiplier A 32-bit unsigned value.
194
195 @return Multiplicand * Multiplier
196
197 **/
198 UINT64
199 EFIAPI
200 InternalMathMultU64x32 (
201 IN UINT64 Multiplicand,
202 IN UINT32 Multiplier
203 )
204 {
205 return Multiplicand * Multiplier;
206 }
207
208
209 /**
210 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer
211 and generates a 64-bit unsigned result.
212
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.
216
217 @param Multiplicand A 64-bit unsigned value.
218 @param Multiplier A 64-bit unsigned value.
219
220 @return Multiplicand * Multiplier
221
222 **/
223 UINT64
224 EFIAPI
225 InternalMathMultU64x64 (
226 IN UINT64 Multiplicand,
227 IN UINT64 Multiplier
228 )
229 {
230 return Multiplicand * Multiplier;
231 }
232
233 /**
234 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and
235 generates a 64-bit unsigned result.
236
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.
240
241 @param Dividend A 64-bit unsigned value.
242 @param Divisor A 32-bit unsigned value.
243
244 @return Dividend / Divisor
245
246 **/
247 UINT64
248 EFIAPI
249 InternalMathDivU64x32 (
250 IN UINT64 Dividend,
251 IN UINT32 Divisor
252 )
253 {
254 return Dividend / Divisor;
255 }
256
257 /**
258 Divides a 64-bit unsigned integer by a 32-bit unsigned integer
259 and generates a 32-bit unsigned remainder.
260
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.
264
265 @param Dividend A 64-bit unsigned value.
266 @param Divisor A 32-bit unsigned value.
267
268 @return Dividend % Divisor
269
270 **/
271 UINT32
272 EFIAPI
273 InternalMathModU64x32 (
274 IN UINT64 Dividend,
275 IN UINT32 Divisor
276 )
277 {
278 return (UINT32)(Dividend % Divisor);
279 }
280
281 /**
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.
284
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.
289
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.
294
295 @return Dividend / Divisor
296
297 **/
298 UINT64
299 EFIAPI
300 InternalMathDivRemU64x32 (
301 IN UINT64 Dividend,
302 IN UINT32 Divisor,
303 OUT UINT32 *Remainder OPTIONAL
304 )
305 {
306 if (Remainder != NULL) {
307 *Remainder = (UINT32)(Dividend % Divisor);
308 }
309 return Dividend / Divisor;
310 }
311
312 /**
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.
315
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.
320
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.
325
326 @return Dividend / Divisor
327
328 **/
329 UINT64
330 EFIAPI
331 InternalMathDivRemU64x64 (
332 IN UINT64 Dividend,
333 IN UINT64 Divisor,
334 OUT UINT64 *Remainder OPTIONAL
335 )
336 {
337 if (Remainder != NULL) {
338 *Remainder = Dividend % Divisor;
339 }
340 return Dividend / Divisor;
341 }
342
343 /**
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.
346
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.
351
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.
356
357 @return Dividend / Divisor
358
359 **/
360 INT64
361 InternalMathDivRemS64x64 (
362 IN INT64 Dividend,
363 IN INT64 Divisor,
364 OUT INT64 *Remainder OPTIONAL
365 )
366 {
367 if (Remainder != NULL) {
368 *Remainder = Dividend % Divisor;
369 }
370 return Dividend / Divisor;
371 }