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