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