Sync with trunk head (part 1 of 2)
[reactos.git] / subsystems / win32 / win32k / misc / math.c
1 /* Math functions for i387.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by John C. Bowman <bowman@ipp-garching.mpg.de>, 1995.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20 */
21
22 #include <win32k.h>
23
24 /*
25 * FIXME! Is there a better algorithm. like FT_MulDiv
26 *
27 * @implemented
28 */
29 INT APIENTRY EngMulDiv(
30 INT nMultiplicand,
31 INT nMultiplier,
32 INT nDivisor)
33 {
34 #if SIZEOF_LONG_LONG >= 8
35 long long ret;
36
37 if (!nDivisor) return -1;
38
39 /* We want to deal with a positive divisor to simplify the logic. */
40 if (nDivisor < 0)
41 {
42 nMultiplicand = - nMultiplicand;
43 nDivisor = -nDivisor;
44 }
45
46 /* If the result is positive, we "add" to round. else, we subtract to round. */
47 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
48 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
49 ret = (((long long)nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
50 else
51 ret = (((long long)nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
52
53 if ((ret > 2147483647) || (ret < -2147483647)) return -1;
54 return ret;
55 #else
56 if (!nDivisor) return -1;
57
58 /* We want to deal with a positive divisor to simplify the logic. */
59 if (nDivisor < 0)
60 {
61 nMultiplicand = - nMultiplicand;
62 nDivisor = -nDivisor;
63 }
64
65 /* If the result is positive, we "add" to round. else, we subtract to round. */
66 if ( ( (nMultiplicand < 0) && (nMultiplier < 0) ) ||
67 ( (nMultiplicand >= 0) && (nMultiplier >= 0) ) )
68 return ((nMultiplicand * nMultiplier) + (nDivisor/2)) / nDivisor;
69
70 return ((nMultiplicand * nMultiplier) - (nDivisor/2)) / nDivisor;
71
72 #endif
73 }