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