mirror of
https://github.com/reactos/reactos.git
synced 2026-07-06 22:54:25 +08:00
62 lines
1.7 KiB
C
62 lines
1.7 KiB
C
/* $Id: muldiv.c,v 1.1 2002/12/06 13:14:14 robd Exp $
|
|
*
|
|
*/
|
|
#include <windows.h>
|
|
|
|
|
|
/***********************************************************************
|
|
* MulDiv (KERNEL32.@)
|
|
* RETURNS
|
|
* Result of multiplication and division
|
|
* -1: Overflow occurred or Divisor was 0
|
|
*/
|
|
|
|
//FIXME! move to correct file
|
|
INT STDCALL MulDiv(
|
|
INT nMultiplicand,
|
|
INT nMultiplier,
|
|
INT nDivisor)
|
|
{
|
|
#if SIZEOF_LONG_LONG >= 8
|
|
long long ret;
|
|
|
|
if (!nDivisor) return -1;
|
|
|
|
/* We want to deal with a positive divisor to simplify the logic. */
|
|
if (nDivisor < 0)
|
|
{
|
|
nMultiplicand = - nMultiplicand;
|
|
nDivisor = -nDivisor;
|
|
}
|
|
|
|
/* If the result is positive, we "add" to round. else, we subtract to round. */
|
|
if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
|
|
( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
|
|
ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
|
|
else
|
|
ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
|
|
|
|
if ((ret > 2147483647) || (ret < -2147483647)) return -1;
|
|
return ret;
|
|
#else
|
|
if (!nDivisor) return -1;
|
|
|
|
/* We want to deal with a positive divisor to simplify the logic. */
|
|
if (nDivisor < 0)
|
|
{
|
|
nMultiplicand = - nMultiplicand;
|
|
nDivisor = -nDivisor;
|
|
}
|
|
|
|
/* If the result is positive, we "add" to round. else, we subtract to round. */
|
|
if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
|
|
( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
|
|
return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
|
|
|
|
return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
|
|
|
|
#endif
|
|
}
|
|
|
|
|