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