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