]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Math64.c
MdeModulePkg: 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
28 IN UINT64 Operand,\r
29 IN UINTN Count\r
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
51 IN UINT64 Operand,\r
52 IN UINTN Count\r
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
74 IN UINT64 Operand,\r
75 IN UINTN Count\r
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
98\r
99/**\r
100 Rotates a 64-bit integer left between 0 and 63 bits, filling\r
101 the low bits with the high bits that were rotated.\r
102\r
103 This function rotates the 64-bit value Operand to the left by Count bits. The\r
104 low Count bits are fill with the high Count bits of Operand. The rotated\r
105 value is returned.\r
106\r
107 @param Operand The 64-bit operand to rotate left.\r
108 @param Count The number of bits to rotate left.\r
109\r
127010dd 110 @return Operand <<< Count.\r
e1f414b6 111\r
112**/\r
113UINT64\r
114EFIAPI\r
115InternalMathLRotU64 (\r
116 IN UINT64 Operand,\r
117 IN UINTN Count\r
118 )\r
119{\r
120 return (Operand << Count) | (Operand >> (64 - Count));\r
121}\r
122\r
123/**\r
124 Rotates a 64-bit integer right between 0 and 63 bits, filling\r
125 the high bits with the high low bits that were rotated.\r
126\r
127 This function rotates the 64-bit value Operand to the right by Count bits.\r
128 The high Count bits are fill with the low Count bits of Operand. The rotated\r
129 value is returned.\r
130\r
131 @param Operand The 64-bit operand to rotate right.\r
132 @param Count The number of bits to rotate right.\r
133\r
127010dd 134 @return Operand >>> Count.\r
e1f414b6 135\r
136**/\r
137UINT64\r
138EFIAPI\r
139InternalMathRRotU64 (\r
140 IN UINT64 Operand,\r
141 IN UINTN Count\r
142 )\r
143{\r
144 return (Operand >> Count) | (Operand << (64 - Count));\r
145}\r
146\r
147/**\r
148 Switches the endianess of a 64-bit integer.\r
149\r
150 This function swaps the bytes in a 64-bit unsigned value to switch the value\r
151 from little endian to big endian or vice versa. The byte swapped value is\r
152 returned.\r
153\r
154 @param Operand A 64-bit unsigned value.\r
155\r
2fc60b70 156 @return The byte swapped Operand.\r
e1f414b6 157\r
158**/\r
159UINT64\r
160EFIAPI\r
161InternalMathSwapBytes64 (\r
162 IN UINT64 Operand\r
163 )\r
164{\r
165 UINT64 LowerBytes;\r
166 UINT64 HigherBytes;\r
167\r
168 LowerBytes = (UINT64) SwapBytes32 ((UINT32) Operand);\r
169 HigherBytes = (UINT64) SwapBytes32 ((UINT32) (Operand >> 32));\r
170\r
171 return (LowerBytes << 32 | HigherBytes);\r
172}\r
173\r
174/**\r
127010dd 175 Multiplies a 64-bit unsigned integer by a 32-bit unsigned integer\r
e1f414b6 176 and generates a 64-bit unsigned result.\r
177\r
127010dd 178 This function multiplies the 64-bit unsigned value Multiplicand by the 32-bit\r
e1f414b6 179 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
180 bit unsigned result is returned.\r
181\r
182 @param Multiplicand A 64-bit unsigned value.\r
183 @param Multiplier A 32-bit unsigned value.\r
184\r
185 @return Multiplicand * Multiplier\r
186\r
187**/\r
188UINT64\r
189EFIAPI\r
190InternalMathMultU64x32 (\r
191 IN UINT64 Multiplicand,\r
192 IN UINT32 Multiplier\r
193 )\r
194{\r
195 return Multiplicand * Multiplier;\r
196}\r
197\r
198\r
199/**\r
127010dd 200 Multiplies a 64-bit unsigned integer by a 64-bit unsigned integer\r
e1f414b6 201 and generates a 64-bit unsigned result.\r
202\r
127010dd 203 This function multiplies the 64-bit unsigned value Multiplicand by the 64-bit\r
e1f414b6 204 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
205 bit unsigned result is returned.\r
206\r
207 @param Multiplicand A 64-bit unsigned value.\r
208 @param Multiplier A 64-bit unsigned value.\r
209\r
127010dd 210 @return Multiplicand * Multiplier.\r
e1f414b6 211\r
212**/\r
213UINT64\r
214EFIAPI\r
215InternalMathMultU64x64 (\r
216 IN UINT64 Multiplicand,\r
217 IN UINT64 Multiplier\r
218 )\r
219{\r
220 return Multiplicand * Multiplier;\r
221}\r
222\r
223/**\r
224 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
225 generates a 64-bit unsigned result.\r
226\r
227 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
228 unsigned value Divisor and generates a 64-bit unsigned quotient. This\r
229 function returns the 64-bit unsigned quotient.\r
230\r
2fc60b70 231 @param Dividend A 64-bit unsigned value.\r
e1f414b6 232 @param Divisor A 32-bit unsigned value.\r
233\r
127010dd 234 @return Dividend / Divisor.\r
e1f414b6 235\r
236**/\r
237UINT64\r
238EFIAPI\r
239InternalMathDivU64x32 (\r
240 IN UINT64 Dividend,\r
241 IN UINT32 Divisor\r
242 )\r
243{\r
244 return Dividend / Divisor;\r
245}\r
246\r
247/**\r
2fc60b70 248 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
249 generates a 32-bit unsigned remainder.\r
e1f414b6 250\r
251 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
252 unsigned value Divisor and generates a 32-bit remainder. This function\r
253 returns the 32-bit unsigned remainder.\r
254\r
255 @param Dividend A 64-bit unsigned value.\r
256 @param Divisor A 32-bit unsigned value.\r
257\r
127010dd 258 @return Dividend % Divisor.\r
e1f414b6 259\r
260**/\r
261UINT32\r
262EFIAPI\r
263InternalMathModU64x32 (\r
264 IN UINT64 Dividend,\r
265 IN UINT32 Divisor\r
266 )\r
267{\r
268 return (UINT32)(Dividend % Divisor);\r
269}\r
270\r
271/**\r
272 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
273 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.\r
274\r
275 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
276 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
277 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.\r
278 This function returns the 64-bit unsigned quotient.\r
279\r
280 @param Dividend A 64-bit unsigned value.\r
281 @param Divisor A 32-bit unsigned value.\r
282 @param Remainder A pointer to a 32-bit unsigned value. This parameter is\r
283 optional and may be NULL.\r
284\r
127010dd 285 @return Dividend / Divisor.\r
e1f414b6 286\r
287**/\r
288UINT64\r
289EFIAPI\r
290InternalMathDivRemU64x32 (\r
291 IN UINT64 Dividend,\r
292 IN UINT32 Divisor,\r
2fc60b70 293 OUT UINT32 *Remainder OPTIONAL\r
e1f414b6 294 )\r
295{\r
296 if (Remainder != NULL) {\r
297 *Remainder = (UINT32)(Dividend % Divisor);\r
298 }\r
299 return Dividend / Divisor;\r
300}\r
301\r
302/**\r
303 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and\r
304 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.\r
305\r
306 This function divides the 64-bit unsigned value Dividend by the 64-bit\r
307 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
308 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.\r
309 This function returns the 64-bit unsigned quotient.\r
310\r
311 @param Dividend A 64-bit unsigned value.\r
312 @param Divisor A 64-bit unsigned value.\r
313 @param Remainder A pointer to a 64-bit unsigned value. This parameter is\r
314 optional and may be NULL.\r
315\r
316 @return Dividend / Divisor\r
317\r
318**/\r
319UINT64\r
320EFIAPI\r
321InternalMathDivRemU64x64 (\r
322 IN UINT64 Dividend,\r
323 IN UINT64 Divisor,\r
2fc60b70 324 OUT UINT64 *Remainder OPTIONAL\r
e1f414b6 325 )\r
326{\r
327 if (Remainder != NULL) {\r
328 *Remainder = Dividend % Divisor;\r
329 }\r
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
353 IN INT64 Dividend,\r
354 IN INT64 Divisor,\r
355 OUT INT64 *Remainder OPTIONAL\r
356 )\r
357{\r
358 if (Remainder != NULL) {\r
359 *Remainder = Dividend % Divisor;\r
360 }\r
361 return Dividend / Divisor;\r
362}\r