67cff5846ee7d6c7f6c68432c6c8feee69eedf8a
1 /* $Id: muldiv.c,v 1.1 2002/12/06 13:14:14 robd Exp $
7 /***********************************************************************
10 * Result of multiplication and division
11 * -1: Overflow occurred or Divisor was 0
14 //FIXME! move to correct file
20 #if SIZEOF_LONG_LONG >= 8
23 if (!nDivisor
) return -1;
25 /* We want to deal with a positive divisor to simplify the logic. */
28 nMultiplicand
= - nMultiplicand
;
32 /* If the result is positive, we "add" to round. else, we subtract to round. */
33 if ( ( (nMultiplicand
< 0) && (nMultiplier
< 0) ) ||
34 ( (nMultiplicand
>= 0) && (nMultiplier
>= 0) ) )
35 ret
= (((long long)nMultiplicand
* nMultiplier
) + (nDivisor
/2)) / nDivisor
;
37 ret
= (((long long)nMultiplicand
* nMultiplier
) - (nDivisor
/2)) / nDivisor
;
39 if ((ret
> 2147483647) || (ret
< -2147483647)) return -1;
42 if (!nDivisor
) return -1;
44 /* We want to deal with a positive divisor to simplify the logic. */
47 nMultiplicand
= - nMultiplicand
;
51 /* If the result is positive, we "add" to round. else, we subtract to round. */
52 if ( ( (nMultiplicand
< 0) && (nMultiplier
< 0) ) ||
53 ( (nMultiplicand
>= 0) && (nMultiplier
>= 0) ) )
54 return ((nMultiplicand
* nMultiplier
) + (nDivisor
/2)) / nDivisor
;
56 return ((nMultiplicand
* nMultiplier
) - (nDivisor
/2)) / nDivisor
;