merge ROS Shell without integrated explorer part into trunk
[reactos.git] / reactos / lib / kernel32 / misc / muldiv.c
1 /* $Id$
2 *
3 */
4 #include <windows.h>
5
6
7 /***********************************************************************
8 * MulDiv (KERNEL32.@)
9 * RETURNS
10 * Result of multiplication and division
11 * -1: Overflow occurred or Divisor was 0
12 * FIXME! move to correct file
13 *
14 * @implemented
15 */
16 INT STDCALL MulDiv(
17 INT nMultiplicand,
18 INT nMultiplier,
19 INT nDivisor)
20 {
21 #if SIZEOF_LONG_LONG >= 8
22 long long ret;
23
24 if (!nDivisor) return -1;
25
26 /* We want to deal with a positive divisor to simplify the logic. */
27 if (nDivisor < 0)
28 {
29 nMultiplicand = - nMultiplicand;
30 nDivisor = -nDivisor;
31 }
32
33 /* If the result is positive, we "add" to round. else, we subtract to round. */
34 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
35 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
36 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
37 else
38 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
39
40 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
41 return ret;
42 #else
43 if (!nDivisor) return -1;
44
45 /* We want to deal with a positive divisor to simplify the logic. */
46 if (nDivisor < 0)
47 {
48 nMultiplicand = - nMultiplicand;
49 nDivisor = -nDivisor;
50 }
51
52 /* If the result is positive, we "add" to round. else, we subtract to round. */
53 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
54 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
55 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
56
57 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
58
59 #endif
60 }