OSDN Git Service

a252ab76eff5e68bde5e88e1de6f972caa0fd588
[mingw/mingw-org-wsl.git] / include / largeint.h
1 /**
2  * @file largeint.h
3  * @copy 2012 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _LARGEINT_H
25 #define _LARGEINT_H
26 #pragma GCC system_header
27 #include <_mingw.h>
28
29 /*
30  * Header for 64 bit integer arithmetics library
31  */
32
33 #include <windows.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 #ifdef _HAVE_INT64
40 #define _toi (__int64)
41 #define _toui (unsigned __int64)
42 #else
43 #error "64 bit integers not supported"
44 #endif
45
46 /*
47   We don't let the compiler see the prototypes if we are compiling the
48   library because if it does it will choke on conflicting types in the
49   prototypes.
50 */
51
52 #if defined(LARGEINT_PROTOS) || defined(__COMPILING_LARGEINT)
53
54 #ifndef __COMPILING_LARGEINT
55 /* addition/subtraction */
56 LARGE_INTEGER WINAPI LargeIntegerAdd (LARGE_INTEGER, LARGE_INTEGER);
57 LARGE_INTEGER WINAPI LargeIntegerSubtract (LARGE_INTEGER, LARGE_INTEGER);
58
59 /* bit operations */
60 LARGE_INTEGER WINAPI LargeIntegerArithmeticShift (LARGE_INTEGER, int);
61 LARGE_INTEGER WINAPI LargeIntegerShiftLeft (LARGE_INTEGER, int);
62 LARGE_INTEGER WINAPI LargeIntegerShiftRight (LARGE_INTEGER, int);
63 LARGE_INTEGER WINAPI LargeIntegerNegate (LARGE_INTEGER);
64
65 /* conversion */
66 LARGE_INTEGER WINAPI ConvertLongToLargeInteger (LONG);
67 LARGE_INTEGER WINAPI ConvertUlongToLargeInteger (ULONG);
68
69 /* multiplication */
70 LARGE_INTEGER WINAPI EnlargedIntegerMultiply (LONG, LONG);
71 LARGE_INTEGER WINAPI EnlargedUnsignedMultiply (ULONG, ULONG);
72 LARGE_INTEGER WINAPI ExtendedIntegerMultiply (LARGE_INTEGER, LONG);
73 /* FIXME: is this not part of largeint? */
74 LARGE_INTEGER WINAPI LargeIntegerMultiply (LARGE_INTEGER, LARGE_INTEGER);
75 #endif /* __COMPILING_LARGEINT */
76
77 #else
78
79 #define LargeIntegerAdd(a,b) (LARGE_INTEGER)(_toi(a) + _toi(b))
80 #define LargeIntegerSubtract(a,b) (LARGE_INTEGER)(_toi(a) - _toi(b))
81 #define LargeIntegerRightShift(i,n) (LARGE_INTEGER)(_toi(i) >> (n))
82 #define LargeIntegerArithmeticShift LargeIntegerRightShift
83 #define LargeIntegerLeftShift(i,n) (LARGE_INTEGER)(_toi(i) << (n))
84 #define LargeIntegerNegate(i) (LARGE_INTEGER)(- _toi(i))
85 #define EnlargedIntegerMultiply(a,b) (LARGE_INTEGER)(_toi(a) * _toi(b))
86 #define EnlargedUnsignedMultiply(a,b) (LARGE_INTEGER)(_toui(a) * _toui(b))
87 #define ExtendedIntegerMultiply(a,b) (LARGE_INTEGER)(_toi(a) * _toi(b))
88 /* FIXME: should this exist */
89 #define LargeIntegerMultiply(a,b) (LARGE_INTEGER)(_toi(a) * _toi(b))
90 #define ConvertLongToLargeInteger(l) (LARGE_INTEGER)(_toi(l))
91 #define ConvertUlongToLargeInteger(ul) (LARGE_INTEGER)(_toui(ul))
92
93 #endif /* LARGEINT_PROTOS || __COMPILING_LARGEINT */
94
95 #ifndef __COMPILING_LARGEINT
96 /* division; no macros of these because of multiple expansion */
97 LARGE_INTEGER WINAPI LargeIntegerDivide (LARGE_INTEGER, LARGE_INTEGER, PLARGE_INTEGER);
98 ULONG WINAPI EnlargedUnsignedDivide (ULARGE_INTEGER, ULONG, PULONG);
99 LARGE_INTEGER WINAPI ExtendedLargeIntegerDivide (LARGE_INTEGER, ULONG, PULONG);
100 LARGE_INTEGER WINAPI ExtendedMagicDivide (LARGE_INTEGER, LARGE_INTEGER, int);
101 #endif /* __COMPILING_LARGEINT */
102
103 #define LargeIntegerAnd(dest, src, m) \
104 { \
105   dest._STRUCT_NAME(u.)LowPart = s._STRUCT_NAME(u.)LowPart & m._STRUCT_NAME(u.)LowPart; \
106   dest._STRUCT_NAME(u.)HighPart = s._STRUCT_NAME(u.)HighPart & m._STRUCT_NAME(u.)HighPart; \
107 }
108
109 /* comparision */
110 #define LargeIntegerGreaterThan(a,b) (_toi(a) > _toi(b))
111 #define LargeIntegerGreaterThanOrEqual(a,b) (_toi(a) >= _toi(b))
112 #define LargeIntegerEqualTo(a,b) (_toi(a) == _toi(b))
113 #define LargeIntegerNotEqualTo(a,b) (_toi(a) != _toi(b))
114 #define LargeIntegerLessThan(a,b) (_toi(a) < _toi(b))
115 #define LargeIntegerLessThanOrEqualTo(a,b) (_toi(a) <= _toi(b))
116 #define LargeIntegerGreaterThanZero(a) (_toi(a) > 0)
117 #define LargeIntegerGreaterOrEqualToZero(a) ((a)._STRUCT_NAME(u.)HighPart > 0)
118 #define LargeIntegerEqualToZero(a) !((a)._STRUCT_NAME(u.)LowPart | (a)._STRUCT_NAME(u.)HighPart)
119 #define LargeIntegerNotEqualToZero(a) ((a)._STRUCT_NAME(u.)LowPart | (a)._STRUCT_NAME(u.)HighPart)
120 #define LargeIntegerLessThanZero(a) ((a)._STRUCT_NAME(u.)HighPart < 0)
121 #define LargeIntegerLessOrEqualToZero(a) (_toi(a) <= 0)
122
123 #ifndef __COMPILING_LARGEINT
124 #undef _toi
125 #undef _toui
126 #endif
127
128 #ifdef __cplusplus
129 }
130 #endif
131
132 #endif /* _LARGEINT_H */