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