3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: dll/win32/kernel32/misc/muldiv.c
7 * PROGRAMMER: Casper S. Hornstrup
16 /***********************************************************************
19 * Result of multiplication and division
20 * -1: Overflow occurred or Divisor was 0
21 * FIXME! move to correct file
34 /* Find out if this will be a negative result */
35 Negative
= nNumber
^ nNumerator
^ nDenominator
;
37 /* Turn all the parameters into absolute values */
38 if (nNumber
< 0) nNumber
*= -1;
39 if (nNumerator
< 0) nNumerator
*= -1;
40 if (nDenominator
< 0) nDenominator
*= -1;
42 /* Calculate the result */
43 Result
.QuadPart
= Int32x32To64(nNumber
, nNumerator
) + (nDenominator
/ 2);
45 /* Now check for overflow */
46 if (nDenominator
> Result
.HighPart
)
48 /* Divide the product to get the quotient and remainder */
49 Result
.LowPart
= RtlEnlargedUnsignedDivide(*(PULARGE_INTEGER
)&Result
,
51 (PULONG
)&Result
.HighPart
);
53 /* Do the sign changes */
54 if ((LONG
)Result
.LowPart
>= 0)
56 return (Negative
>= 0) ? (LONG
)Result
.LowPart
: -(LONG
)Result
.LowPart
;