Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/modules/Libefi/efiMath.c

1/*++
2
3Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4This program and the accompanying materials
5are licensed and made available under the terms and conditions of the BSD License
6which accompanies this distribution. The full text of the license may be found at
7http://opensource.org/licenses/bsd-license.php
8
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12Module Name:
13
14 Math.c
15
16Abstract:
17
18 Math worker functions.
19
20--*/
21
22#include <efi.h>
23
24UINT64
25LShiftU64 (
26 IN UINT64 Operand,
27 IN UINTN Count
28 )
29/*++
30
31Routine Description:
32
33 This routine allows a 64 bit value to be left shifted by 32 bits and
34 returns the shifted value.
35 Count is valid up 63. (Only Bits 0-5 is valid for Count)
36
37Arguments:
38
39 Operand - Value to be shifted
40 Count - Number of times to shift left.
41
42Returns:
43
44 Value shifted left identified by the Count.
45
46--*/
47{
48 return Operand << Count;
49}
50
51UINT64
52MultU64x32 (
53 IN UINT64 Multiplicand,
54 IN UINTN Multiplier
55 )
56/*++
57
58Routine Description:
59
60 This routine allows a 64 bit value to be multiplied with a 32 bit
61 value returns 64bit result.
62 No checking if the result is greater than 64bits
63
64Arguments:
65
66 Multiplicand - multiplicand
67 Multiplier - multiplier
68
69Returns:
70
71 Multiplicand * Multiplier
72
73--*/
74{
75 return Multiplicand * Multiplier;
76}
77
78UINT64
79Power10U64 (
80 IN UINT64 Operand,
81 IN UINTN Power
82 )
83{
84 UINT64 Result;
85
86 Result = Operand;
87 while (Power-- > 0) {
88 Result *= 10;
89 }
90 return Result;
91}
92
93UINT64
94RShiftU64 (
95 IN UINT64 Operand,
96 IN UINTN Count
97 )
98/*++
99
100Routine Description:
101
102 This routine allows a 64 bit value to be right shifted by 32 bits and returns the
103 shifted value.
104 Count is valid up 63. (Only Bits 0-5 is valid for Count)
105
106Arguments:
107
108 Operand - Value to be shifted
109 Count - Number of times to shift right.
110
111Returns:
112
113 Value shifted right identified by the Count.
114
115--*/
116{
117 return Operand >> Count;
118}
119
120UINT64
121DivU64x32 (
122 IN UINT64 Dividend,
123 IN UINTN Divisor,
124 OUT UINTN *Remainder OPTIONAL
125 )
126/*++
127
128Routine Description:
129
130 This routine allows a 64 bit value to be divided with a 32 bit value returns
131 64bit result and the Remainder.
132
133Arguments:
134
135 Dividend - dividend
136 Divisor - divisor
137 Remainder - buffer for remainder
138
139Returns:
140
141 Dividend / Divisor
142 Remainder = Dividend mod Divisor
143
144--*/
145{
146 if (Remainder != NULL) {
147 *Remainder = Dividend % Divisor;
148 }
149
150 return Dividend / Divisor;
151}
152
153UINT8
154Log2 (
155 IN UINT64 Operand
156 )
157/*++
158
159Routine Description:
160
161 This function computes rounded down log2 of the Operand. This is an equivalent
162 of the position of the highest bit set in the Operand treated as a mask.
163 E.g., Log2 (0x0001) == 0, Log2 (0x0002) == 1, Log2 (0x0003) == 1, Log2 (0x0005) == 2
164 Log2 (0x4000) == 14, Log2 (0x8000) == 15, Log2 (0xC000) == 15, Log2 (0xFFFF) == 15, etc.
165
166Arguments:
167 Operand - value of which the Log2 is to be computed.
168
169Returns:
170 Rounded down log2 of the Operand or 0xFF if zero passed in.
171
172--*/
173{
174 UINT8 Bitpos;
175 Bitpos = 0;
176
177 if (Operand == 0) {
178 return (0xff);
179 }
180
181 while (Operand != 0) {
182 Operand >>= 1;
183 Bitpos++;
184 }
185 return (UINT8)(Bitpos - 1);
186
187}
188
189UINT64
190GetPowerOfTwo (
191 IN UINT64 Operand
192 )
193/*++
194
195Routine Description:
196
197 Calculates the largest integer that is both
198 a power of two and less than Input
199
200Arguments:
201
202 Operand - value to calculate power of two
203
204Returns:
205
206 the largest integer that is both a power of
207 two and less than Input
208
209--*/
210{
211 UINT8 Bitpos;
212 Bitpos = 0;
213
214 if (Operand == 0) {
215 return 0;
216 }
217
218 while (Operand != 0) {
219 Operand >>= 1;
220 Bitpos++;
221 }
222
223 Operand = 1;
224 Bitpos--;
225 while (Bitpos != 0) {
226 Operand <<= 1;
227 Bitpos--;
228 }
229
230 return Operand;
231}
232

Archive Download this file

Revision: 2182