]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - OldMdePkg/Library/BaseLib/Math64.c
AsmReadKr1 is already defined in the baselib
[mirror_edk2.git] / OldMdePkg / Library / BaseLib / Math64.c
... / ...
CommitLineData
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#include "BaseLibInternals.h"\r
19\r
20/**\r
21 Shifts a 64-bit integer left between 0 and 63 bits. The low bits\r
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
33UINT64\r
34EFIAPI\r
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
43/**\r
44 Shifts a 64-bit integer right between 0 and 63 bits. This high bits\r
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
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
66/**\r
67 Shifts a 64-bit integer right between 0 and 63 bits. The high bits\r
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
81UINT64\r
82EFIAPI\r
83InternalMathARShiftU64 (\r
84 IN UINT64 Operand,\r
85 IN UINTN Count\r
86 )\r
87{\r
88 INTN TestValue;\r
89\r
90 //\r
91 // Test if this compiler supports arithmetic shift\r
92 //\r
93 TestValue = (((-1) << (sizeof (-1) * 8 - 1)) >> (sizeof (-1) * 8 - 1));\r
94 if (TestValue == -1) {\r
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
108\r
109/**\r
110 Rotates a 64-bit integer left between 0 and 63 bits, filling\r
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
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
133/**\r
134 Rotates a 64-bit integer right between 0 and 63 bits, filling\r
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
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
157/**\r
158 Switches the endianess of a 64-bit integer.\r
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
169UINT64\r
170EFIAPI\r
171InternalMathSwapBytes64 (\r
172 IN UINT64 Operand\r
173 )\r
174{\r
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
182}\r
183\r
184/**\r
185 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer\r
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
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
208\r
209/**\r
210 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer\r
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
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
233/**\r
234 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
235 generates a 64-bit unsigned result.\r
236\r
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
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
257/**\r
258 Divides a 64-bit unsigned integer by a 32-bit unsigned integer\r
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
271UINT32\r
272EFIAPI\r
273InternalMathModU64x32 (\r
274 IN UINT64 Dividend,\r
275 IN UINT32 Divisor\r
276 )\r
277{\r
278 return (UINT32)(Dividend % Divisor);\r
279}\r
280\r
281/**\r
282 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
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
298UINT64\r
299EFIAPI\r
300InternalMathDivRemU64x32 (\r
301 IN UINT64 Dividend,\r
302 IN UINT32 Divisor,\r
303 OUT UINT32 *Remainder OPTIONAL\r
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
312/**\r
313 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and\r
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
329UINT64\r
330EFIAPI\r
331InternalMathDivRemU64x64 (\r
332 IN UINT64 Dividend,\r
333 IN UINT64 Divisor,\r
334 OUT UINT64 *Remainder OPTIONAL\r
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
343/**\r
344 Divides a 64-bit signed integer by a 64-bit signed integer and\r
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
360INT64\r
361InternalMathDivRemS64x64 (\r
362 IN INT64 Dividend,\r
363 IN INT64 Divisor,\r
364 OUT INT64 *Remainder OPTIONAL\r
365 )\r
366{\r
367 if (Remainder != NULL) {\r
368 *Remainder = Dividend % Divisor;\r
369 }\r
370 return Dividend / Divisor;\r
371}\r