utmath.c revision 1.1.1.17 1 1.1 jruoho /*******************************************************************************
2 1.1 jruoho *
3 1.1 jruoho * Module Name: utmath - Integer math support routines
4 1.1 jruoho *
5 1.1 jruoho ******************************************************************************/
6 1.1 jruoho
7 1.1.1.16 christos /******************************************************************************
8 1.1.1.16 christos *
9 1.1.1.16 christos * 1. Copyright Notice
10 1.1.1.16 christos *
11 1.1.1.17 christos * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.
12 1.1 jruoho * All rights reserved.
13 1.1 jruoho *
14 1.1.1.16 christos * 2. License
15 1.1.1.16 christos *
16 1.1.1.16 christos * 2.1. This is your license from Intel Corp. under its intellectual property
17 1.1.1.16 christos * rights. You may have additional license terms from the party that provided
18 1.1.1.16 christos * you this software, covering your right to use that party's intellectual
19 1.1.1.16 christos * property rights.
20 1.1.1.16 christos *
21 1.1.1.16 christos * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 1.1.1.16 christos * copy of the source code appearing in this file ("Covered Code") an
23 1.1.1.16 christos * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 1.1.1.16 christos * base code distributed originally by Intel ("Original Intel Code") to copy,
25 1.1.1.16 christos * make derivatives, distribute, use and display any portion of the Covered
26 1.1.1.16 christos * Code in any form, with the right to sublicense such rights; and
27 1.1.1.16 christos *
28 1.1.1.16 christos * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 1.1.1.16 christos * license (with the right to sublicense), under only those claims of Intel
30 1.1.1.16 christos * patents that are infringed by the Original Intel Code, to make, use, sell,
31 1.1.1.16 christos * offer to sell, and import the Covered Code and derivative works thereof
32 1.1.1.16 christos * solely to the minimum extent necessary to exercise the above copyright
33 1.1.1.16 christos * license, and in no event shall the patent license extend to any additions
34 1.1.1.16 christos * to or modifications of the Original Intel Code. No other license or right
35 1.1.1.16 christos * is granted directly or by implication, estoppel or otherwise;
36 1.1.1.16 christos *
37 1.1.1.16 christos * The above copyright and patent license is granted only if the following
38 1.1.1.16 christos * conditions are met:
39 1.1.1.16 christos *
40 1.1.1.16 christos * 3. Conditions
41 1.1.1.16 christos *
42 1.1.1.16 christos * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 1.1.1.16 christos * Redistribution of source code of any substantial portion of the Covered
44 1.1.1.16 christos * Code or modification with rights to further distribute source must include
45 1.1.1.16 christos * the above Copyright Notice, the above License, this list of Conditions,
46 1.1.1.16 christos * and the following Disclaimer and Export Compliance provision. In addition,
47 1.1.1.16 christos * Licensee must cause all Covered Code to which Licensee contributes to
48 1.1.1.16 christos * contain a file documenting the changes Licensee made to create that Covered
49 1.1.1.16 christos * Code and the date of any change. Licensee must include in that file the
50 1.1.1.16 christos * documentation of any changes made by any predecessor Licensee. Licensee
51 1.1.1.16 christos * must include a prominent statement that the modification is derived,
52 1.1.1.16 christos * directly or indirectly, from Original Intel Code.
53 1.1.1.16 christos *
54 1.1.1.16 christos * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 1.1.1.16 christos * Redistribution of source code of any substantial portion of the Covered
56 1.1.1.16 christos * Code or modification without rights to further distribute source must
57 1.1.1.16 christos * include the following Disclaimer and Export Compliance provision in the
58 1.1.1.16 christos * documentation and/or other materials provided with distribution. In
59 1.1.1.16 christos * addition, Licensee may not authorize further sublicense of source of any
60 1.1.1.16 christos * portion of the Covered Code, and must include terms to the effect that the
61 1.1.1.16 christos * license from Licensee to its licensee is limited to the intellectual
62 1.1.1.16 christos * property embodied in the software Licensee provides to its licensee, and
63 1.1.1.16 christos * not to intellectual property embodied in modifications its licensee may
64 1.1.1.16 christos * make.
65 1.1.1.16 christos *
66 1.1.1.16 christos * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 1.1.1.16 christos * substantial portion of the Covered Code or modification must reproduce the
68 1.1.1.16 christos * above Copyright Notice, and the following Disclaimer and Export Compliance
69 1.1.1.16 christos * provision in the documentation and/or other materials provided with the
70 1.1.1.16 christos * distribution.
71 1.1.1.16 christos *
72 1.1.1.16 christos * 3.4. Intel retains all right, title, and interest in and to the Original
73 1.1.1.16 christos * Intel Code.
74 1.1.1.16 christos *
75 1.1.1.16 christos * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 1.1.1.16 christos * Intel shall be used in advertising or otherwise to promote the sale, use or
77 1.1.1.16 christos * other dealings in products derived from or relating to the Covered Code
78 1.1.1.16 christos * without prior written authorization from Intel.
79 1.1.1.16 christos *
80 1.1.1.16 christos * 4. Disclaimer and Export Compliance
81 1.1.1.16 christos *
82 1.1.1.16 christos * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 1.1.1.16 christos * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 1.1.1.16 christos * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 1.1.1.16 christos * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 1.1.1.16 christos * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 1.1.1.16 christos * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 1.1.1.16 christos * PARTICULAR PURPOSE.
89 1.1.1.16 christos *
90 1.1.1.16 christos * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 1.1.1.16 christos * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 1.1.1.16 christos * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 1.1.1.16 christos * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 1.1.1.16 christos * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 1.1.1.16 christos * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 1.1.1.16 christos * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 1.1.1.16 christos * LIMITED REMEDY.
98 1.1.1.16 christos *
99 1.1.1.16 christos * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 1.1.1.16 christos * software or system incorporating such software without first obtaining any
101 1.1.1.16 christos * required license or other approval from the U. S. Department of Commerce or
102 1.1.1.16 christos * any other agency or department of the United States Government. In the
103 1.1.1.16 christos * event Licensee exports any such software from the United States or
104 1.1.1.16 christos * re-exports any such software from a foreign destination, Licensee shall
105 1.1.1.16 christos * ensure that the distribution and export/re-export of the software is in
106 1.1.1.16 christos * compliance with all laws, regulations, orders, or other restrictions of the
107 1.1.1.16 christos * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 1.1.1.16 christos * any of its subsidiaries will export/re-export any technical data, process,
109 1.1.1.16 christos * software, or service, directly or indirectly, to any country for which the
110 1.1.1.16 christos * United States government or any agency thereof requires an export license,
111 1.1.1.16 christos * other governmental approval, or letter of assurance, without first obtaining
112 1.1.1.16 christos * such license, approval or letter.
113 1.1.1.16 christos *
114 1.1.1.16 christos *****************************************************************************
115 1.1.1.16 christos *
116 1.1.1.16 christos * Alternatively, you may choose to be licensed under the terms of the
117 1.1.1.16 christos * following license:
118 1.1.1.16 christos *
119 1.1.1.2 jruoho * Redistribution and use in source and binary forms, with or without
120 1.1.1.2 jruoho * modification, are permitted provided that the following conditions
121 1.1.1.2 jruoho * are met:
122 1.1.1.2 jruoho * 1. Redistributions of source code must retain the above copyright
123 1.1.1.2 jruoho * notice, this list of conditions, and the following disclaimer,
124 1.1.1.2 jruoho * without modification.
125 1.1.1.2 jruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 1.1.1.2 jruoho * substantially similar to the "NO WARRANTY" disclaimer below
127 1.1.1.2 jruoho * ("Disclaimer") and any redistribution must be conditioned upon
128 1.1.1.2 jruoho * including a substantially similar Disclaimer requirement for further
129 1.1.1.2 jruoho * binary redistribution.
130 1.1.1.2 jruoho * 3. Neither the names of the above-listed copyright holders nor the names
131 1.1.1.2 jruoho * of any contributors may be used to endorse or promote products derived
132 1.1.1.2 jruoho * from this software without specific prior written permission.
133 1.1.1.2 jruoho *
134 1.1.1.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 1.1.1.2 jruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 1.1.1.13 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 1.1.1.2 jruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 1.1.1.16 christos * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 1.1.1.16 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 1.1.1.16 christos * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 1.1.1.16 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 1.1.1.16 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 1.1.1.16 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 1.1.1.16 christos * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145 1.1.1.16 christos *
146 1.1.1.16 christos * Alternatively, you may choose to be licensed under the terms of the
147 1.1.1.16 christos * GNU General Public License ("GPL") version 2 as published by the Free
148 1.1.1.16 christos * Software Foundation.
149 1.1.1.16 christos *
150 1.1.1.16 christos *****************************************************************************/
151 1.1 jruoho
152 1.1 jruoho #include "acpi.h"
153 1.1 jruoho #include "accommon.h"
154 1.1 jruoho
155 1.1 jruoho
156 1.1 jruoho #define _COMPONENT ACPI_UTILITIES
157 1.1 jruoho ACPI_MODULE_NAME ("utmath")
158 1.1 jruoho
159 1.1.1.2 jruoho /* Structures used only for 64-bit divide */
160 1.1.1.2 jruoho
161 1.1.1.2 jruoho typedef struct uint64_struct
162 1.1.1.2 jruoho {
163 1.1.1.2 jruoho UINT32 Lo;
164 1.1.1.2 jruoho UINT32 Hi;
165 1.1.1.2 jruoho
166 1.1.1.2 jruoho } UINT64_STRUCT;
167 1.1.1.2 jruoho
168 1.1.1.2 jruoho typedef union uint64_overlay
169 1.1.1.2 jruoho {
170 1.1.1.2 jruoho UINT64 Full;
171 1.1.1.2 jruoho UINT64_STRUCT Part;
172 1.1.1.2 jruoho
173 1.1.1.2 jruoho } UINT64_OVERLAY;
174 1.1.1.2 jruoho
175 1.1.1.8 christos /*
176 1.1.1.8 christos * Optional support for 64-bit double-precision integer multiply and shift.
177 1.1.1.8 christos * This code is configurable and is implemented in order to support 32-bit
178 1.1.1.8 christos * kernel environments where a 64-bit double-precision math library is not
179 1.1.1.8 christos * available.
180 1.1.1.8 christos */
181 1.1.1.8 christos #ifndef ACPI_USE_NATIVE_MATH64
182 1.1.1.8 christos
183 1.1.1.8 christos /*******************************************************************************
184 1.1.1.8 christos *
185 1.1.1.8 christos * FUNCTION: AcpiUtShortMultiply
186 1.1.1.8 christos *
187 1.1.1.8 christos * PARAMETERS: Multiplicand - 64-bit multiplicand
188 1.1.1.8 christos * Multiplier - 32-bit multiplier
189 1.1.1.8 christos * OutProduct - Pointer to where the product is returned
190 1.1.1.8 christos *
191 1.1.1.8 christos * DESCRIPTION: Perform a short multiply.
192 1.1.1.8 christos *
193 1.1.1.8 christos ******************************************************************************/
194 1.1.1.8 christos
195 1.1.1.8 christos ACPI_STATUS
196 1.1.1.8 christos AcpiUtShortMultiply (
197 1.1.1.8 christos UINT64 Multiplicand,
198 1.1.1.8 christos UINT32 Multiplier,
199 1.1.1.8 christos UINT64 *OutProduct)
200 1.1.1.8 christos {
201 1.1.1.8 christos UINT64_OVERLAY MultiplicandOvl;
202 1.1.1.8 christos UINT64_OVERLAY Product;
203 1.1.1.8 christos UINT32 Carry32;
204 1.1.1.8 christos
205 1.1.1.8 christos
206 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortMultiply);
207 1.1.1.8 christos
208 1.1.1.8 christos
209 1.1.1.8 christos MultiplicandOvl.Full = Multiplicand;
210 1.1.1.8 christos
211 1.1.1.8 christos /*
212 1.1.1.8 christos * The Product is 64 bits, the carry is always 32 bits,
213 1.1.1.8 christos * and is generated by the second multiply.
214 1.1.1.8 christos */
215 1.1.1.8 christos ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Hi, Multiplier,
216 1.1.1.8 christos Product.Part.Hi, Carry32);
217 1.1.1.8 christos
218 1.1.1.8 christos ACPI_MUL_64_BY_32 (0, MultiplicandOvl.Part.Lo, Multiplier,
219 1.1.1.8 christos Product.Part.Lo, Carry32);
220 1.1.1.8 christos
221 1.1.1.8 christos Product.Part.Hi += Carry32;
222 1.1.1.8 christos
223 1.1.1.8 christos /* Return only what was requested */
224 1.1.1.8 christos
225 1.1.1.8 christos if (OutProduct)
226 1.1.1.8 christos {
227 1.1.1.8 christos *OutProduct = Product.Full;
228 1.1.1.8 christos }
229 1.1.1.8 christos
230 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
231 1.1.1.8 christos }
232 1.1.1.8 christos
233 1.1.1.8 christos
234 1.1.1.8 christos /*******************************************************************************
235 1.1.1.8 christos *
236 1.1.1.8 christos * FUNCTION: AcpiUtShortShiftLeft
237 1.1.1.8 christos *
238 1.1.1.8 christos * PARAMETERS: Operand - 64-bit shift operand
239 1.1.1.8 christos * Count - 32-bit shift count
240 1.1.1.8 christos * OutResult - Pointer to where the result is returned
241 1.1.1.8 christos *
242 1.1.1.8 christos * DESCRIPTION: Perform a short left shift.
243 1.1.1.8 christos *
244 1.1.1.8 christos ******************************************************************************/
245 1.1.1.8 christos
246 1.1.1.8 christos ACPI_STATUS
247 1.1.1.8 christos AcpiUtShortShiftLeft (
248 1.1.1.8 christos UINT64 Operand,
249 1.1.1.8 christos UINT32 Count,
250 1.1.1.8 christos UINT64 *OutResult)
251 1.1.1.8 christos {
252 1.1.1.8 christos UINT64_OVERLAY OperandOvl;
253 1.1.1.8 christos
254 1.1.1.8 christos
255 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortShiftLeft);
256 1.1.1.8 christos
257 1.1.1.8 christos
258 1.1.1.8 christos OperandOvl.Full = Operand;
259 1.1.1.8 christos
260 1.1.1.8 christos if ((Count & 63) >= 32)
261 1.1.1.8 christos {
262 1.1.1.8 christos OperandOvl.Part.Hi = OperandOvl.Part.Lo;
263 1.1.1.9 christos OperandOvl.Part.Lo = 0;
264 1.1.1.8 christos Count = (Count & 63) - 32;
265 1.1.1.8 christos }
266 1.1.1.8 christos ACPI_SHIFT_LEFT_64_BY_32 (OperandOvl.Part.Hi,
267 1.1.1.8 christos OperandOvl.Part.Lo, Count);
268 1.1.1.8 christos
269 1.1.1.8 christos /* Return only what was requested */
270 1.1.1.8 christos
271 1.1.1.8 christos if (OutResult)
272 1.1.1.8 christos {
273 1.1.1.8 christos *OutResult = OperandOvl.Full;
274 1.1.1.8 christos }
275 1.1.1.8 christos
276 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
277 1.1.1.8 christos }
278 1.1.1.8 christos
279 1.1.1.8 christos /*******************************************************************************
280 1.1.1.8 christos *
281 1.1.1.8 christos * FUNCTION: AcpiUtShortShiftRight
282 1.1.1.8 christos *
283 1.1.1.8 christos * PARAMETERS: Operand - 64-bit shift operand
284 1.1.1.8 christos * Count - 32-bit shift count
285 1.1.1.8 christos * OutResult - Pointer to where the result is returned
286 1.1.1.8 christos *
287 1.1.1.8 christos * DESCRIPTION: Perform a short right shift.
288 1.1.1.8 christos *
289 1.1.1.8 christos ******************************************************************************/
290 1.1.1.8 christos
291 1.1.1.8 christos ACPI_STATUS
292 1.1.1.8 christos AcpiUtShortShiftRight (
293 1.1.1.8 christos UINT64 Operand,
294 1.1.1.8 christos UINT32 Count,
295 1.1.1.8 christos UINT64 *OutResult)
296 1.1.1.8 christos {
297 1.1.1.8 christos UINT64_OVERLAY OperandOvl;
298 1.1.1.8 christos
299 1.1.1.8 christos
300 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortShiftRight);
301 1.1.1.8 christos
302 1.1.1.8 christos
303 1.1.1.8 christos OperandOvl.Full = Operand;
304 1.1.1.8 christos
305 1.1.1.8 christos if ((Count & 63) >= 32)
306 1.1.1.8 christos {
307 1.1.1.8 christos OperandOvl.Part.Lo = OperandOvl.Part.Hi;
308 1.1.1.9 christos OperandOvl.Part.Hi = 0;
309 1.1.1.8 christos Count = (Count & 63) - 32;
310 1.1.1.8 christos }
311 1.1.1.8 christos ACPI_SHIFT_RIGHT_64_BY_32 (OperandOvl.Part.Hi,
312 1.1.1.8 christos OperandOvl.Part.Lo, Count);
313 1.1.1.8 christos
314 1.1.1.8 christos /* Return only what was requested */
315 1.1.1.8 christos
316 1.1.1.8 christos if (OutResult)
317 1.1.1.8 christos {
318 1.1.1.8 christos *OutResult = OperandOvl.Full;
319 1.1.1.8 christos }
320 1.1.1.8 christos
321 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
322 1.1.1.8 christos }
323 1.1.1.8 christos #else
324 1.1.1.8 christos
325 1.1.1.8 christos /*******************************************************************************
326 1.1.1.8 christos *
327 1.1.1.8 christos * FUNCTION: AcpiUtShortMultiply
328 1.1.1.8 christos *
329 1.1.1.8 christos * PARAMETERS: See function headers above
330 1.1.1.8 christos *
331 1.1.1.8 christos * DESCRIPTION: Native version of the UtShortMultiply function.
332 1.1.1.8 christos *
333 1.1.1.8 christos ******************************************************************************/
334 1.1.1.8 christos
335 1.1.1.8 christos ACPI_STATUS
336 1.1.1.8 christos AcpiUtShortMultiply (
337 1.1.1.8 christos UINT64 Multiplicand,
338 1.1.1.8 christos UINT32 Multiplier,
339 1.1.1.8 christos UINT64 *OutProduct)
340 1.1.1.8 christos {
341 1.1.1.8 christos
342 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortMultiply);
343 1.1.1.8 christos
344 1.1.1.8 christos
345 1.1.1.8 christos /* Return only what was requested */
346 1.1.1.8 christos
347 1.1.1.8 christos if (OutProduct)
348 1.1.1.8 christos {
349 1.1.1.8 christos *OutProduct = Multiplicand * Multiplier;
350 1.1.1.8 christos }
351 1.1.1.8 christos
352 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
353 1.1.1.8 christos }
354 1.1.1.8 christos
355 1.1.1.8 christos /*******************************************************************************
356 1.1.1.8 christos *
357 1.1.1.8 christos * FUNCTION: AcpiUtShortShiftLeft
358 1.1.1.8 christos *
359 1.1.1.8 christos * PARAMETERS: See function headers above
360 1.1.1.8 christos *
361 1.1.1.8 christos * DESCRIPTION: Native version of the UtShortShiftLeft function.
362 1.1.1.8 christos *
363 1.1.1.8 christos ******************************************************************************/
364 1.1.1.8 christos
365 1.1.1.8 christos ACPI_STATUS
366 1.1.1.8 christos AcpiUtShortShiftLeft (
367 1.1.1.8 christos UINT64 Operand,
368 1.1.1.8 christos UINT32 Count,
369 1.1.1.8 christos UINT64 *OutResult)
370 1.1.1.8 christos {
371 1.1.1.8 christos
372 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortShiftLeft);
373 1.1.1.8 christos
374 1.1.1.8 christos
375 1.1.1.8 christos /* Return only what was requested */
376 1.1.1.8 christos
377 1.1.1.8 christos if (OutResult)
378 1.1.1.8 christos {
379 1.1.1.8 christos *OutResult = Operand << Count;
380 1.1.1.8 christos }
381 1.1.1.8 christos
382 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
383 1.1.1.8 christos }
384 1.1.1.8 christos
385 1.1.1.8 christos /*******************************************************************************
386 1.1.1.8 christos *
387 1.1.1.8 christos * FUNCTION: AcpiUtShortShiftRight
388 1.1.1.8 christos *
389 1.1.1.8 christos * PARAMETERS: See function headers above
390 1.1.1.8 christos *
391 1.1.1.8 christos * DESCRIPTION: Native version of the UtShortShiftRight function.
392 1.1.1.8 christos *
393 1.1.1.8 christos ******************************************************************************/
394 1.1.1.8 christos
395 1.1.1.8 christos ACPI_STATUS
396 1.1.1.8 christos AcpiUtShortShiftRight (
397 1.1.1.8 christos UINT64 Operand,
398 1.1.1.8 christos UINT32 Count,
399 1.1.1.8 christos UINT64 *OutResult)
400 1.1.1.8 christos {
401 1.1.1.8 christos
402 1.1.1.8 christos ACPI_FUNCTION_TRACE (UtShortShiftRight);
403 1.1.1.8 christos
404 1.1.1.8 christos
405 1.1.1.8 christos /* Return only what was requested */
406 1.1.1.8 christos
407 1.1.1.8 christos if (OutResult)
408 1.1.1.8 christos {
409 1.1.1.8 christos *OutResult = Operand >> Count;
410 1.1.1.8 christos }
411 1.1.1.8 christos
412 1.1.1.8 christos return_ACPI_STATUS (AE_OK);
413 1.1.1.8 christos }
414 1.1.1.8 christos #endif
415 1.1.1.8 christos
416 1.1.1.8 christos /*
417 1.1.1.8 christos * Optional support for 64-bit double-precision integer divide. This code
418 1.1.1.8 christos * is configurable and is implemented in order to support 32-bit kernel
419 1.1.1.8 christos * environments where a 64-bit double-precision math library is not available.
420 1.1.1.8 christos *
421 1.1.1.8 christos * Support for a more normal 64-bit divide/modulo (with check for a divide-
422 1.1.1.8 christos * by-zero) appears after this optional section of code.
423 1.1.1.8 christos */
424 1.1.1.8 christos #ifndef ACPI_USE_NATIVE_DIVIDE
425 1.1.1.8 christos
426 1.1.1.2 jruoho
427 1.1 jruoho /*******************************************************************************
428 1.1 jruoho *
429 1.1 jruoho * FUNCTION: AcpiUtShortDivide
430 1.1 jruoho *
431 1.1 jruoho * PARAMETERS: Dividend - 64-bit dividend
432 1.1 jruoho * Divisor - 32-bit divisor
433 1.1 jruoho * OutQuotient - Pointer to where the quotient is returned
434 1.1 jruoho * OutRemainder - Pointer to where the remainder is returned
435 1.1 jruoho *
436 1.1 jruoho * RETURN: Status (Checks for divide-by-zero)
437 1.1 jruoho *
438 1.1 jruoho * DESCRIPTION: Perform a short (maximum 64 bits divided by 32 bits)
439 1.1.1.3 christos * divide and modulo. The result is a 64-bit quotient and a
440 1.1 jruoho * 32-bit remainder.
441 1.1 jruoho *
442 1.1 jruoho ******************************************************************************/
443 1.1 jruoho
444 1.1 jruoho ACPI_STATUS
445 1.1 jruoho AcpiUtShortDivide (
446 1.1 jruoho UINT64 Dividend,
447 1.1 jruoho UINT32 Divisor,
448 1.1 jruoho UINT64 *OutQuotient,
449 1.1 jruoho UINT32 *OutRemainder)
450 1.1 jruoho {
451 1.1 jruoho UINT64_OVERLAY DividendOvl;
452 1.1 jruoho UINT64_OVERLAY Quotient;
453 1.1 jruoho UINT32 Remainder32;
454 1.1 jruoho
455 1.1 jruoho
456 1.1 jruoho ACPI_FUNCTION_TRACE (UtShortDivide);
457 1.1 jruoho
458 1.1 jruoho
459 1.1 jruoho /* Always check for a zero divisor */
460 1.1 jruoho
461 1.1 jruoho if (Divisor == 0)
462 1.1 jruoho {
463 1.1 jruoho ACPI_ERROR ((AE_INFO, "Divide by zero"));
464 1.1 jruoho return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
465 1.1 jruoho }
466 1.1 jruoho
467 1.1 jruoho DividendOvl.Full = Dividend;
468 1.1 jruoho
469 1.1 jruoho /*
470 1.1 jruoho * The quotient is 64 bits, the remainder is always 32 bits,
471 1.1 jruoho * and is generated by the second divide.
472 1.1 jruoho */
473 1.1 jruoho ACPI_DIV_64_BY_32 (0, DividendOvl.Part.Hi, Divisor,
474 1.1.1.6 christos Quotient.Part.Hi, Remainder32);
475 1.1.1.6 christos
476 1.1 jruoho ACPI_DIV_64_BY_32 (Remainder32, DividendOvl.Part.Lo, Divisor,
477 1.1.1.6 christos Quotient.Part.Lo, Remainder32);
478 1.1 jruoho
479 1.1 jruoho /* Return only what was requested */
480 1.1 jruoho
481 1.1 jruoho if (OutQuotient)
482 1.1 jruoho {
483 1.1 jruoho *OutQuotient = Quotient.Full;
484 1.1 jruoho }
485 1.1 jruoho if (OutRemainder)
486 1.1 jruoho {
487 1.1 jruoho *OutRemainder = Remainder32;
488 1.1 jruoho }
489 1.1 jruoho
490 1.1 jruoho return_ACPI_STATUS (AE_OK);
491 1.1 jruoho }
492 1.1 jruoho
493 1.1 jruoho
494 1.1 jruoho /*******************************************************************************
495 1.1 jruoho *
496 1.1 jruoho * FUNCTION: AcpiUtDivide
497 1.1 jruoho *
498 1.1 jruoho * PARAMETERS: InDividend - Dividend
499 1.1 jruoho * InDivisor - Divisor
500 1.1 jruoho * OutQuotient - Pointer to where the quotient is returned
501 1.1 jruoho * OutRemainder - Pointer to where the remainder is returned
502 1.1 jruoho *
503 1.1 jruoho * RETURN: Status (Checks for divide-by-zero)
504 1.1 jruoho *
505 1.1 jruoho * DESCRIPTION: Perform a divide and modulo.
506 1.1 jruoho *
507 1.1 jruoho ******************************************************************************/
508 1.1 jruoho
509 1.1 jruoho ACPI_STATUS
510 1.1 jruoho AcpiUtDivide (
511 1.1 jruoho UINT64 InDividend,
512 1.1 jruoho UINT64 InDivisor,
513 1.1 jruoho UINT64 *OutQuotient,
514 1.1 jruoho UINT64 *OutRemainder)
515 1.1 jruoho {
516 1.1 jruoho UINT64_OVERLAY Dividend;
517 1.1 jruoho UINT64_OVERLAY Divisor;
518 1.1 jruoho UINT64_OVERLAY Quotient;
519 1.1 jruoho UINT64_OVERLAY Remainder;
520 1.1 jruoho UINT64_OVERLAY NormalizedDividend;
521 1.1 jruoho UINT64_OVERLAY NormalizedDivisor;
522 1.1 jruoho UINT32 Partial1;
523 1.1 jruoho UINT64_OVERLAY Partial2;
524 1.1 jruoho UINT64_OVERLAY Partial3;
525 1.1 jruoho
526 1.1 jruoho
527 1.1 jruoho ACPI_FUNCTION_TRACE (UtDivide);
528 1.1 jruoho
529 1.1 jruoho
530 1.1 jruoho /* Always check for a zero divisor */
531 1.1 jruoho
532 1.1 jruoho if (InDivisor == 0)
533 1.1 jruoho {
534 1.1 jruoho ACPI_ERROR ((AE_INFO, "Divide by zero"));
535 1.1 jruoho return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
536 1.1 jruoho }
537 1.1 jruoho
538 1.1 jruoho Divisor.Full = InDivisor;
539 1.1 jruoho Dividend.Full = InDividend;
540 1.1 jruoho if (Divisor.Part.Hi == 0)
541 1.1 jruoho {
542 1.1 jruoho /*
543 1.1 jruoho * 1) Simplest case is where the divisor is 32 bits, we can
544 1.1 jruoho * just do two divides
545 1.1 jruoho */
546 1.1 jruoho Remainder.Part.Hi = 0;
547 1.1 jruoho
548 1.1 jruoho /*
549 1.1 jruoho * The quotient is 64 bits, the remainder is always 32 bits,
550 1.1 jruoho * and is generated by the second divide.
551 1.1 jruoho */
552 1.1 jruoho ACPI_DIV_64_BY_32 (0, Dividend.Part.Hi, Divisor.Part.Lo,
553 1.1.1.6 christos Quotient.Part.Hi, Partial1);
554 1.1.1.6 christos
555 1.1 jruoho ACPI_DIV_64_BY_32 (Partial1, Dividend.Part.Lo, Divisor.Part.Lo,
556 1.1.1.6 christos Quotient.Part.Lo, Remainder.Part.Lo);
557 1.1 jruoho }
558 1.1 jruoho
559 1.1 jruoho else
560 1.1 jruoho {
561 1.1 jruoho /*
562 1.1 jruoho * 2) The general case where the divisor is a full 64 bits
563 1.1 jruoho * is more difficult
564 1.1 jruoho */
565 1.1 jruoho Quotient.Part.Hi = 0;
566 1.1 jruoho NormalizedDividend = Dividend;
567 1.1 jruoho NormalizedDivisor = Divisor;
568 1.1 jruoho
569 1.1 jruoho /* Normalize the operands (shift until the divisor is < 32 bits) */
570 1.1 jruoho
571 1.1 jruoho do
572 1.1 jruoho {
573 1.1.1.6 christos ACPI_SHIFT_RIGHT_64 (
574 1.1.1.6 christos NormalizedDivisor.Part.Hi, NormalizedDivisor.Part.Lo);
575 1.1.1.6 christos ACPI_SHIFT_RIGHT_64 (
576 1.1.1.6 christos NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo);
577 1.1 jruoho
578 1.1 jruoho } while (NormalizedDivisor.Part.Hi != 0);
579 1.1 jruoho
580 1.1 jruoho /* Partial divide */
581 1.1 jruoho
582 1.1.1.6 christos ACPI_DIV_64_BY_32 (
583 1.1.1.6 christos NormalizedDividend.Part.Hi, NormalizedDividend.Part.Lo,
584 1.1.1.6 christos NormalizedDivisor.Part.Lo, Quotient.Part.Lo, Partial1);
585 1.1 jruoho
586 1.1 jruoho /*
587 1.1.1.6 christos * The quotient is always 32 bits, and simply requires
588 1.1.1.6 christos * adjustment. The 64-bit remainder must be generated.
589 1.1 jruoho */
590 1.1.1.6 christos Partial1 = Quotient.Part.Lo * Divisor.Part.Hi;
591 1.1 jruoho Partial2.Full = (UINT64) Quotient.Part.Lo * Divisor.Part.Lo;
592 1.1 jruoho Partial3.Full = (UINT64) Partial2.Part.Hi + Partial1;
593 1.1 jruoho
594 1.1 jruoho Remainder.Part.Hi = Partial3.Part.Lo;
595 1.1 jruoho Remainder.Part.Lo = Partial2.Part.Lo;
596 1.1 jruoho
597 1.1 jruoho if (Partial3.Part.Hi == 0)
598 1.1 jruoho {
599 1.1 jruoho if (Partial3.Part.Lo >= Dividend.Part.Hi)
600 1.1 jruoho {
601 1.1 jruoho if (Partial3.Part.Lo == Dividend.Part.Hi)
602 1.1 jruoho {
603 1.1 jruoho if (Partial2.Part.Lo > Dividend.Part.Lo)
604 1.1 jruoho {
605 1.1 jruoho Quotient.Part.Lo--;
606 1.1 jruoho Remainder.Full -= Divisor.Full;
607 1.1 jruoho }
608 1.1 jruoho }
609 1.1 jruoho else
610 1.1 jruoho {
611 1.1 jruoho Quotient.Part.Lo--;
612 1.1 jruoho Remainder.Full -= Divisor.Full;
613 1.1 jruoho }
614 1.1 jruoho }
615 1.1 jruoho
616 1.1.1.6 christos Remainder.Full = Remainder.Full - Dividend.Full;
617 1.1 jruoho Remainder.Part.Hi = (UINT32) -((INT32) Remainder.Part.Hi);
618 1.1 jruoho Remainder.Part.Lo = (UINT32) -((INT32) Remainder.Part.Lo);
619 1.1 jruoho
620 1.1 jruoho if (Remainder.Part.Lo)
621 1.1 jruoho {
622 1.1 jruoho Remainder.Part.Hi--;
623 1.1 jruoho }
624 1.1 jruoho }
625 1.1 jruoho }
626 1.1 jruoho
627 1.1 jruoho /* Return only what was requested */
628 1.1 jruoho
629 1.1 jruoho if (OutQuotient)
630 1.1 jruoho {
631 1.1 jruoho *OutQuotient = Quotient.Full;
632 1.1 jruoho }
633 1.1 jruoho if (OutRemainder)
634 1.1 jruoho {
635 1.1 jruoho *OutRemainder = Remainder.Full;
636 1.1 jruoho }
637 1.1 jruoho
638 1.1 jruoho return_ACPI_STATUS (AE_OK);
639 1.1 jruoho }
640 1.1 jruoho
641 1.1 jruoho #else
642 1.1 jruoho
643 1.1 jruoho /*******************************************************************************
644 1.1 jruoho *
645 1.1 jruoho * FUNCTION: AcpiUtShortDivide, AcpiUtDivide
646 1.1 jruoho *
647 1.1 jruoho * PARAMETERS: See function headers above
648 1.1 jruoho *
649 1.1 jruoho * DESCRIPTION: Native versions of the UtDivide functions. Use these if either
650 1.1 jruoho * 1) The target is a 64-bit platform and therefore 64-bit
651 1.1 jruoho * integer math is supported directly by the machine.
652 1.1 jruoho * 2) The target is a 32-bit or 16-bit platform, and the
653 1.1 jruoho * double-precision integer math library is available to
654 1.1 jruoho * perform the divide.
655 1.1 jruoho *
656 1.1 jruoho ******************************************************************************/
657 1.1 jruoho
658 1.1 jruoho ACPI_STATUS
659 1.1 jruoho AcpiUtShortDivide (
660 1.1 jruoho UINT64 InDividend,
661 1.1 jruoho UINT32 Divisor,
662 1.1 jruoho UINT64 *OutQuotient,
663 1.1 jruoho UINT32 *OutRemainder)
664 1.1 jruoho {
665 1.1 jruoho
666 1.1 jruoho ACPI_FUNCTION_TRACE (UtShortDivide);
667 1.1 jruoho
668 1.1 jruoho
669 1.1 jruoho /* Always check for a zero divisor */
670 1.1 jruoho
671 1.1 jruoho if (Divisor == 0)
672 1.1 jruoho {
673 1.1 jruoho ACPI_ERROR ((AE_INFO, "Divide by zero"));
674 1.1 jruoho return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
675 1.1 jruoho }
676 1.1 jruoho
677 1.1 jruoho /* Return only what was requested */
678 1.1 jruoho
679 1.1 jruoho if (OutQuotient)
680 1.1 jruoho {
681 1.1 jruoho *OutQuotient = InDividend / Divisor;
682 1.1 jruoho }
683 1.1 jruoho if (OutRemainder)
684 1.1 jruoho {
685 1.1 jruoho *OutRemainder = (UINT32) (InDividend % Divisor);
686 1.1 jruoho }
687 1.1 jruoho
688 1.1 jruoho return_ACPI_STATUS (AE_OK);
689 1.1 jruoho }
690 1.1 jruoho
691 1.1 jruoho ACPI_STATUS
692 1.1 jruoho AcpiUtDivide (
693 1.1 jruoho UINT64 InDividend,
694 1.1 jruoho UINT64 InDivisor,
695 1.1 jruoho UINT64 *OutQuotient,
696 1.1 jruoho UINT64 *OutRemainder)
697 1.1 jruoho {
698 1.1 jruoho ACPI_FUNCTION_TRACE (UtDivide);
699 1.1 jruoho
700 1.1 jruoho
701 1.1 jruoho /* Always check for a zero divisor */
702 1.1 jruoho
703 1.1 jruoho if (InDivisor == 0)
704 1.1 jruoho {
705 1.1 jruoho ACPI_ERROR ((AE_INFO, "Divide by zero"));
706 1.1 jruoho return_ACPI_STATUS (AE_AML_DIVIDE_BY_ZERO);
707 1.1 jruoho }
708 1.1 jruoho
709 1.1 jruoho
710 1.1 jruoho /* Return only what was requested */
711 1.1 jruoho
712 1.1 jruoho if (OutQuotient)
713 1.1 jruoho {
714 1.1 jruoho *OutQuotient = InDividend / InDivisor;
715 1.1 jruoho }
716 1.1 jruoho if (OutRemainder)
717 1.1 jruoho {
718 1.1 jruoho *OutRemainder = InDividend % InDivisor;
719 1.1 jruoho }
720 1.1 jruoho
721 1.1 jruoho return_ACPI_STATUS (AE_OK);
722 1.1 jruoho }
723 1.1 jruoho
724 1.1 jruoho #endif
725