bbfa3023a491e15cd1f8d83589ab2ae8b274f7d6
1 /* \$Id\$
2 *
3 */
4 #include <k32.h>
7 /***********************************************************************
8 * MulDiv (KERNEL32.@)
9 * RETURNS
10 * Result of multiplication and division
11 * -1: Overflow occurred or Divisor was 0
12 * FIXME! move to correct file
13 *
14 * @implemented
15 */
16 INT
17 WINAPI
18 MulDiv(INT nNumber,
19 INT nNumerator,
20 INT nDenominator)
21 {
22 LARGE_INTEGER Result;
23 LONG Negative;
25 /* Find out if this will be a negative result */
26 Negative = nNumber ^ nNumerator ^ nDenominator;
28 /* Turn all the parameters into absolute values */
29 if (nNumber < 0) nNumber *= -1;
30 if (nNumerator < 0) nNumerator *= -1;
31 if (nDenominator < 0) nDenominator *= -1;
33 /* Calculate the result */
34 Result.QuadPart = Int32x32To64(nNumber, nNumerator) + (nDenominator / 2);
36 /* Now check for overflow */
37 if (nDenominator > Result.HighPart)
38 {
39 /* Divide the product to get the quotient and remainder */
40 Result.LowPart = RtlEnlargedUnsignedDivide(*(PULARGE_INTEGER)&Result,
41 (ULONG)nDenominator,
42 &Result.HighPart);
44 /* Do the sign changes */
45 if ((LONG)Result.LowPart >= 0)
46 {
47 return (Negative >= 0) ? Result.LowPart : -(LONG)Result.LowPart;
48 }
49 }
51 /* Return overflow */
52 return - 1;
53 }