]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Ebc/Dxe/Ipf/IpfMath.c
f35f1b9ad1f007f5463972b39d84e6ba66fd8e3c
[mirror_edk2.git] / EdkModulePkg / Universal / Ebc / Dxe / Ipf / IpfMath.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Ipfmath.c
15
16 Abstract:
17
18 Math routines for IPF.
19
20 --*/
21
22 UINT64
23 LeftShiftU64 (
24 IN UINT64 Operand,
25 IN UINT64 Count
26 )
27 /*++
28
29 Routine Description:
30
31 Left-shift a 64 bit value.
32
33 Arguments:
34
35 Operand - 64-bit value to shift
36 Count - shift count
37
38 Returns:
39
40 Operand << Count
41
42 --*/
43 {
44 if (Count > 63) {
45 return 0;
46 }
47
48 return Operand << Count;
49 }
50
51 UINT64
52 RightShiftU64 (
53 IN UINT64 Operand,
54 IN UINT64 Count
55 )
56 /*++
57
58 Routine Description:
59
60 Right-shift a 64 bit value.
61
62 Arguments:
63
64 Operand - 64-bit value to shift
65 Count - shift count
66
67 Returns:
68
69 Operand >> Count
70
71 --*/
72 {
73 if (Count > 63) {
74 return 0;
75 }
76
77 return Operand >> Count;
78 }
79
80 INT64
81 ARightShift64 (
82 IN INT64 Operand,
83 IN UINT64 Count
84 )
85 /*++
86
87 Routine Description:
88
89 Right-shift a 64 bit signed value.
90
91 Arguments:
92
93 Operand - 64-bit value to shift
94 Count - shift count
95
96 Returns:
97
98 Operand >> Count
99
100 --*/
101 {
102 if (Count > 63) {
103
104 if (Operand & (0x01 << 63)) {
105 return (INT64)~0;
106 }
107
108 return 0;
109 }
110
111 return Operand >> Count;
112 }
113
114 #if 0
115 //
116 // The compiler generates true assembly for these, so we don't need them.
117 //
118 INT32
119 ARightShift32 (
120 IN INT32 Operand,
121 IN UINTN Count
122 )
123 /*++
124
125 Routine Description:
126
127 Right shift a 32-bit value
128
129 Arguments:
130
131 Operand - value to shift
132 Count - shift count
133
134 Returns:
135
136 Operand >> Count
137
138 --*/
139 {
140 return Operand >> (Count & 0x1f);
141 }
142
143 INT32
144 MulS32x32 (
145 INT32 Value1,
146 INT32 Value2,
147 INT32 *ResultHigh
148 )
149 /*++
150
151 Routine Description:
152
153 Multiply two signed 32-bit numbers.
154
155 Arguments:
156
157 Value1 - first value to multiply
158 Value2 - value to multiply Value1 by
159 ResultHigh - overflow
160
161 Returns:
162
163 Value1 * Value2
164
165 Notes:
166
167 The 64-bit result is the concatenation of *ResultHigh and the return value
168
169 The product fits in 32 bits if
170 (*ResultHigh == 0x00000000 AND *ResultLow_bit31 == 0)
171 OR
172 (*ResultHigh == 0xffffffff AND *ResultLow_bit31 == 1)
173
174 --*/
175 {
176 INT64 Rres64;
177 INT32 Result;
178
179 Res64 = (INT64) Value1 * (INT64) Value2;
180 *ResultHigh = (Res64 >> 32) & 0xffffffff;
181 Result = Res64 & 0xffffffff;
182 return Result;
183 }
184
185 UINT32
186 MulU32x32 (
187 UINT32 Value1,
188 UINT32 Value2,
189 UINT32 *ResultHigh
190 )
191 /*++
192
193 Routine Description:
194
195 Multiply two unsigned 32-bit values.
196
197 Arguments:
198
199 Value1 - first number
200 Value2 - number to multiply by Value1
201 ResultHigh - overflow
202
203 Returns:
204
205 Value1 * Value2
206
207 Notes:
208
209 The 64-bit result is the concatenation of *ResultHigh and the return value.
210 The product fits in 32 bits if *ResultHigh == 0x00000000
211
212 --*/
213 {
214 UINT64 Res64;
215 UINT32 Result;
216
217 Res64 = (INT64) Value1 * (INT64) Value2;
218 *ResultHigh = (Res64 >> 32) & 0xffffffff;
219 Result = Res64 & 0xffffffff;
220 return Result;
221 }
222
223 INT32
224 DivS32x32 (
225 INT32 Value1,
226 INT32 Value2,
227 INT32 *Remainder,
228 UINTN *error
229 )
230 //
231 // signed 32-bit by signed 32-bit divide; the 32-bit remainder is
232 // in *Remainder and the quotient is the return value; *error = 1 if the
233 // divisor is 0, and it is 1 otherwise
234 //
235 {
236 INT32 Result;
237
238 *error = 0;
239
240 if (Value2 == 0x0) {
241 *error = 1;
242 Result = 0x80000000;
243 *Remainder = 0x80000000;
244 } else {
245 Result = Value1 / Value2;
246 *Remainder = Value1 - Result * Value2;
247 }
248
249 return Result;
250 }
251
252 UINT32
253 DivU32x32 (
254 UINT32 Value1,
255 UINT32 Value2,
256 UINT32 *Remainder,
257 UINTN *Error
258 )
259 //
260 // unsigned 32-bit by unsigned 32-bit divide; the 32-bit remainder is
261 // in *Remainder and the quotient is the return value; *error = 1 if the
262 // divisor is 0, and it is 1 otherwise
263 //
264 {
265 UINT32 Result;
266
267 *Error = 0;
268
269 if (Value2 == 0x0) {
270 *Error = 1;
271 Result = 0x80000000;
272 *Remainder = 0x80000000;
273 } else {
274 Result = Value1 / Value2;
275 *Remainder = Value1 - Result * Value2;
276 }
277
278 return Result;
279 }
280
281 #endif
282
283 INT64
284 DivS64x64 (
285 INT64 Value1,
286 INT64 Value2,
287 INT64 *Remainder,
288 UINTN *Error
289 )
290 /*++
291
292 Routine Description:
293
294 Divide two 64-bit signed values.
295
296 Arguments:
297
298 Value1 - dividend
299 Value2 - divisor
300 Remainder - remainder of Value1/Value2
301 Error - to flag errors (divide-by-0)
302
303 Returns:
304
305 Value1 / Valu2
306
307 Note:
308
309 The 64-bit remainder is in *Remainder and the quotient is the return value.
310 *Error = 1 if the divisor is 0, and it is 1 otherwise
311
312 --*/
313 {
314 INT64 Result;
315
316 *Error = 0;
317
318 if (Value2 == 0x0) {
319 *Error = 1;
320 Result = 0x8000000000000000;
321 *Remainder = 0x8000000000000000;
322 } else {
323 Result = Value1 / Value2;
324 *Remainder = Value1 - Result * Value2;
325 }
326
327 return Result;
328 }
329
330 UINT64
331 DivU64x64 (
332 UINT64 Value1,
333 UINT64 Value2,
334 UINT64 *Remainder,
335 UINTN *Error
336 )
337 /*++
338
339 Routine Description:
340
341 Divide two 64-bit unsigned values.
342
343 Arguments:
344
345 Value1 - dividend
346 Value2 - divisor
347 Remainder - remainder of Value1/Value2
348 Error - to flag errors (divide-by-0)
349
350 Returns:
351
352 Value1 / Valu2
353
354 Note:
355
356 The 64-bit remainder is in *Remainder and the quotient is the return value.
357 *Error = 1 if the divisor is 0, and it is 1 otherwise
358
359 --*/
360 {
361 UINT64 Result;
362
363 *Error = 0;
364
365 if (Value2 == 0x0) {
366 *Error = 1;
367 Result = 0x8000000000000000;
368 *Remainder = 0x8000000000000000;
369 } else {
370 Result = Value1 / Value2;
371 *Remainder = Value1 - Result * Value2;
372 }
373
374 return Result;
375 }