45a1b872421bd8deee1560aaccb72bdb9c0dcf62
[reactos.git] / reactos / dll / win32 / kernel32 / wine / muldiv.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/misc/muldiv.c
5 * PURPOSE:
6 * PROGRAMMER: Casper S. Hornstrup
7 * Gunnar Andre Dalsnes
8 * UPDATE HISTORY:
9 * Created 06/12/2002
10 */
11
12 #include <k32.h>
13
14
15 /***********************************************************************
16 * MulDiv (KERNEL32.@)
17 * RETURNS
18 * Result of multiplication and division
19 * -1: Overflow occurred or Divisor was 0
20 * FIXME! move to correct file
21 *
22 * @implemented
23 */
24 INT
25 WINAPI
26 MulDiv(INT nNumber,
27 INT nNumerator,
28 INT nDenominator)
29 {
30 LARGE_INTEGER Result;
31 LONG Negative;
32
33 /* Find out if this will be a negative result */
34 Negative = nNumber ^ nNumerator ^ nDenominator;
35
36 /* Turn all the parameters into absolute values */
37 if (nNumber < 0) nNumber *= -1;
38 if (nNumerator < 0) nNumerator *= -1;
39 if (nDenominator < 0) nDenominator *= -1;
40
41 /* Calculate the result */
42 Result.QuadPart = Int32x32To64(nNumber, nNumerator) + (nDenominator / 2);
43
44 /* Now check for overflow */
45 if (nDenominator > Result.HighPart)
46 {
47 /* Divide the product to get the quotient and remainder */
48 Result.LowPart = RtlEnlargedUnsignedDivide(*(PULARGE_INTEGER)&Result,
49 (ULONG)nDenominator,
50 (PULONG)&Result.HighPart);
51
52 /* Do the sign changes */
53 if ((LONG)Result.LowPart >= 0)
54 {
55 return (Negative >= 0) ? (LONG)Result.LowPart : -(LONG)Result.LowPart;
56 }
57 }
58
59 /* Return overflow */
60 return - 1;
61 }
62