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