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