2013-03-16 20:48:10 +00:00
|
|
|
/*
|
2006-05-25 19:50:19 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
2015-11-14 14:57:11 +00:00
|
|
|
* FILE: dll/win32/kernel32/wine/muldiv.c
|
2006-05-25 19:50:19 +00:00
|
|
|
* PURPOSE:
|
|
|
|
* PROGRAMMER: Casper S. Hornstrup
|
|
|
|
* Gunnar Andre Dalsnes
|
|
|
|
* UPDATE HISTORY:
|
|
|
|
* Created 06/12/2002
|
2002-12-06 13:14:14 +00:00
|
|
|
*/
|
2006-05-25 19:50:19 +00:00
|
|
|
|
2005-07-26 14:00:45 +00:00
|
|
|
#include <k32.h>
|
2002-12-06 13:14:14 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* MulDiv (KERNEL32.@)
|
|
|
|
* RETURNS
|
|
|
|
* Result of multiplication and division
|
|
|
|
* -1: Overflow occurred or Divisor was 0
|
2003-07-10 18:50:51 +00:00
|
|
|
* FIXME! move to correct file
|
|
|
|
*
|
|
|
|
* @implemented
|
2002-12-06 13:14:14 +00:00
|
|
|
*/
|
2005-10-09 22:12:56 +00:00
|
|
|
INT
|
|
|
|
WINAPI
|
|
|
|
MulDiv(INT nNumber,
|
|
|
|
INT nNumerator,
|
|
|
|
INT nDenominator)
|
2002-12-06 13:14:14 +00:00
|
|
|
{
|
2005-10-09 22:12:56 +00:00
|
|
|
LARGE_INTEGER Result;
|
|
|
|
LONG Negative;
|
|
|
|
|
|
|
|
/* Find out if this will be a negative result */
|
|
|
|
Negative = nNumber ^ nNumerator ^ nDenominator;
|
|
|
|
|
|
|
|
/* Turn all the parameters into absolute values */
|
|
|
|
if (nNumber < 0) nNumber *= -1;
|
|
|
|
if (nNumerator < 0) nNumerator *= -1;
|
|
|
|
if (nDenominator < 0) nDenominator *= -1;
|
|
|
|
|
|
|
|
/* Calculate the result */
|
|
|
|
Result.QuadPart = Int32x32To64(nNumber, nNumerator) + (nDenominator / 2);
|
|
|
|
|
|
|
|
/* Now check for overflow */
|
|
|
|
if (nDenominator > Result.HighPart)
|
2002-12-06 13:14:14 +00:00
|
|
|
{
|
2005-10-09 22:12:56 +00:00
|
|
|
/* Divide the product to get the quotient and remainder */
|
|
|
|
Result.LowPart = RtlEnlargedUnsignedDivide(*(PULARGE_INTEGER)&Result,
|
|
|
|
(ULONG)nDenominator,
|
2005-10-09 22:30:27 +00:00
|
|
|
(PULONG)&Result.HighPart);
|
2005-10-09 22:12:56 +00:00
|
|
|
|
|
|
|
/* Do the sign changes */
|
|
|
|
if ((LONG)Result.LowPart >= 0)
|
|
|
|
{
|
2008-12-03 17:33:13 +00:00
|
|
|
return (Negative >= 0) ? (LONG)Result.LowPart : -(LONG)Result.LowPart;
|
2005-10-09 22:12:56 +00:00
|
|
|
}
|
2002-12-06 13:14:14 +00:00
|
|
|
}
|
2005-10-09 22:12:56 +00:00
|
|
|
|
|
|
|
/* Return overflow */
|
|
|
|
return - 1;
|
2002-12-06 13:14:14 +00:00
|
|
|
}
|
2005-10-09 22:12:56 +00:00
|
|
|
|