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