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