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