libStatGen Software 1
LongInt.h
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __LONGINT_H__
19#define __LONGINT_H__
20
21#ifdef __USE_LONGINT
22#ifndef __USE_LONG_INT
23#define __USE_LONG_INT
24#endif
25#endif
26
27#ifndef __USE_LONG_INT /* longints not enabled */
28
29#define NOTZERO ~0
30#define NOTONE ~1
31typedef int longint;
32
33#else /* longints enabled */
34
35/* GNU C supports long long ... */
36
37#ifdef __GNUC__
38#define __USE_LONG_LONG__
39#endif
40
41/* And so does the Intel Compiler ... */
42
43#ifdef __INTEL_COMPILER
44#define __USE_LONG_LONG__
45#endif
46
47/* And the SUN Pro Compiler ... */
48
49#ifdef __SUNPRO_CC
50#define __USE_LONG_LONG__
51#endif
52
53/* And the Digital Mars Compiler ... */
54
55#ifdef __DMC__
56#ifdef _INTEGRAL_MAX_BITS
57#if (_INTEGRAL_MAX_BITS >= 64)
58#define __USE_LONG_LONG__
59#endif
60#endif
61#endif
62
63/* Check for other compilers that support the C99 standard */
64
65#include <limits.h>
66#ifdef __LLONG_MAX
67#define __USE_LONG_LONG__
68#endif
69
70#ifdef __USE_LONG_LONG__
71
72/* If the long long type is supported natively */
73
74#define NOTZERO ~(0ULL)
75#define NOTONE ~(1ULL)
76typedef long long longint;
77
78#else
79
80/* Define a home brew long integer type */
81
82#define NOTZERO longint (~0,~0)
83#define NOTONE longint (~0,~1)
84
85class longint
86{
87public:
88 longint() {}
89
90 longint(unsigned int low)
91 {
92 lo = low;
93 hi = 0;
94 }
95
96 longint(unsigned int high, unsigned int low)
97 {
98 hi = high;
99 lo = low;
100 }
101
102 longint(const longint & source)
103 {
104 hi = source.hi;
105 lo = source.lo;
106 }
107
108 operator int()
109 {
110 return lo;
111 }
112 operator bool()
113 {
114 return lo != 0 || hi != 0;
115 }
116
117 longint operator ~()
118 {
119 return longint(~hi, ~lo);
120 }
121
122 longint operator ^(const longint & rhs)
123 {
124 return longint(hi ^ rhs.hi, lo ^ rhs.lo);
125 }
126
127 longint operator & (const longint & rhs)
128 {
129 return longint(hi & rhs.hi, lo & rhs.lo);
130 }
131
132 longint operator | (const longint & rhs)
133 {
134 return longint(hi | rhs.hi, lo | rhs.lo);
135 }
136
137 bool operator != (const longint & rhs)
138 {
139 return lo != rhs.lo || hi != rhs.hi;
140 }
141
142 bool operator != (unsigned int rhs)
143 {
144 return lo != rhs || hi != 0;
145 }
146
147 bool operator != (int rhs)
148 {
149 return lo != (unsigned int) rhs || hi != 0;
150 }
151
152 bool operator == (const longint & rhs) const
153 {
154 return lo == rhs.lo && hi == rhs.hi;
155 }
156
157 bool operator == (const unsigned int rhs) const
158 {
159 return lo == rhs && hi == 0;
160 }
161
162 bool operator == (const int rhs) const
163 {
164 return lo == (unsigned int) rhs && hi == 0;
165 }
166
167 longint & operator = (const longint & rhs)
168 {
169 lo = rhs.lo;
170 hi = rhs.hi;
171 return *this;
172 }
173
174 longint & operator = (unsigned int rhs)
175 {
176 lo = rhs;
177 hi = 0;
178 return *this;
179 }
180
181 longint & operator = (int rhs)
182 {
183 lo = rhs;
184 hi = 0;
185 return *this;
186 }
187
188 longint & operator ^= (const longint & rhs)
189 {
190 hi ^= rhs.hi;
191 lo ^= rhs.lo;
192 return *this;
193 }
194
195 longint & operator |= (const longint & rhs)
196 {
197 hi |= rhs.hi;
198 lo |= rhs.lo;
199 return *this;
200 }
201
202 longint operator &= (const longint & rhs)
203 {
204 hi &= rhs.hi;
205 lo &= rhs.lo;
206 return *this;
207 }
208
209 longint operator << (int bits)
210 {
211 longint result(*this);
212 result <<= bits;
213 return result;
214 }
215
216 longint & operator <<= (int bits)
217 {
218 if (bits <= 0)
219 return *this;
220 else
221 {
222 hi = (hi << 1) + ((lo & 0x80000000) != 0);
223 lo <<= 1;
224 return *this <<= bits - 1;
225 }
226 }
227
228 longint operator >> (int bits)
229 {
230 longint result(*this);
231 result >>= bits;
232 return result;
233 }
234
235 longint & operator >>= (int bits)
236 {
237 if (bits <= 0)
238 return *this;
239 else
240 {
241 lo = (lo >> 1) + (hi & 1 ? 0x80000000 : 0);
242 hi >>= 1;
243 return *this >>= bits - 1;
244 }
245 }
246
247 longint operator - (unsigned int rhs)
248 {
249 int high = (rhs > lo) ? hi - 1 : hi;
250 return longint(high, lo - rhs);
251 }
252
253 longint operator - (int rhs)
254 {
255 int high = ((unsigned int) rhs > lo) ? hi - 1 : hi;
256 return longint(high, lo - rhs);
257 }
258
259private:
260 unsigned int hi, lo;
261};
262
263#endif /* __GNUC__ */
264
265#endif /* __USE_LONG_INT */
266
267#endif /* __LONGINT_H__ */
268
269
270
271
272
InputFile & operator<<(InputFile &stream, const std::string &str)
Write to a file using streaming.
Definition: InputFile.h:736
IFILE operator>>(IFILE stream, std::string &str)
Read a line from a file using streaming.
Definition: InputFile.h:724