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