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