45a1b872421bd8deee1560aaccb72bdb9c0dcf62
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: dll/win32/kernel32/misc/muldiv.c
6 * PROGRAMMER: Casper S. Hornstrup
15 /***********************************************************************
18 * Result of multiplication and division
19 * -1: Overflow occurred or Divisor was 0
20 * FIXME! move to correct file
33 /* Find out if this will be a negative result */
34 Negative
= nNumber
^ nNumerator
^ nDenominator
;
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;
41 /* Calculate the result */
42 Result
.QuadPart
= Int32x32To64(nNumber
, nNumerator
) + (nDenominator
/ 2);
44 /* Now check for overflow */
45 if (nDenominator
> Result
.HighPart
)
47 /* Divide the product to get the quotient and remainder */
48 Result
.LowPart
= RtlEnlargedUnsignedDivide(*(PULARGE_INTEGER
)&Result
,
50 (PULONG
)&Result
.HighPart
);
52 /* Do the sign changes */
53 if ((LONG
)Result
.LowPart
>= 0)
55 return (Negative
>= 0) ? (LONG
)Result
.LowPart
: -(LONG
)Result
.LowPart
;