libStatGen Software  1
LongInt.h
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __LONGINT_H__
00019 #define __LONGINT_H__
00020 
00021 #ifdef  __USE_LONGINT
00022 #ifndef __USE_LONG_INT
00023 #define __USE_LONG_INT
00024 #endif
00025 #endif
00026 
00027 #ifndef __USE_LONG_INT /* longints not enabled */
00028 
00029 #define NOTZERO   ~0
00030 #define NOTONE    ~1
00031 typedef int longint;
00032 
00033 #else /* longints enabled */
00034 
00035 /* GNU C supports long long ... */
00036 
00037 #ifdef __GNUC__
00038 #define __USE_LONG_LONG__
00039 #endif
00040 
00041 /* And so does the Intel Compiler ... */
00042 
00043 #ifdef __INTEL_COMPILER
00044 #define __USE_LONG_LONG__
00045 #endif
00046 
00047 /* And the SUN Pro Compiler ... */
00048 
00049 #ifdef __SUNPRO_CC
00050 #define __USE_LONG_LONG__
00051 #endif
00052 
00053 /* And the Digital Mars Compiler ... */
00054 
00055 #ifdef __DMC__
00056 #ifdef _INTEGRAL_MAX_BITS
00057 #if   (_INTEGRAL_MAX_BITS >= 64)
00058 #define __USE_LONG_LONG__
00059 #endif
00060 #endif
00061 #endif
00062 
00063 /* Check for other compilers that support the C99 standard */
00064 
00065 #include <limits.h>
00066 #ifdef __LLONG_MAX
00067 #define __USE_LONG_LONG__
00068 #endif
00069 
00070 #ifdef __USE_LONG_LONG__
00071 
00072 /* If the long long type is supported natively */
00073 
00074 #define NOTZERO   ~(0ULL)
00075 #define NOTONE    ~(1ULL)
00076 typedef long long longint;
00077 
00078 #else
00079 
00080 /* Define a home brew long integer type */
00081 
00082 #define NOTZERO   longint (~0,~0)
00083 #define NOTONE    longint (~0,~1)
00084 
00085 class longint
00086 {
00087 public:
00088     longint() {}
00089 
00090     longint(unsigned int low)
00091     {
00092         lo = low;
00093         hi = 0;
00094     }
00095 
00096     longint(unsigned int high, unsigned int low)
00097     {
00098         hi = high;
00099         lo = low;
00100     }
00101 
00102     longint(const longint & source)
00103     {
00104         hi = source.hi;
00105         lo = source.lo;
00106     }
00107 
00108     operator int()
00109     {
00110         return lo;
00111     }
00112     operator bool()
00113     {
00114         return lo != 0 || hi != 0;
00115     }
00116 
00117     longint operator ~()
00118     {
00119         return longint(~hi, ~lo);
00120     }
00121 
00122     longint operator ^(const longint & rhs)
00123     {
00124         return longint(hi ^ rhs.hi, lo ^ rhs.lo);
00125     }
00126 
00127     longint operator & (const longint & rhs)
00128     {
00129         return longint(hi & rhs.hi, lo & rhs.lo);
00130     }
00131 
00132     longint operator | (const longint & rhs)
00133     {
00134         return longint(hi | rhs.hi, lo | rhs.lo);
00135     }
00136 
00137     bool operator != (const longint & rhs)
00138     {
00139         return lo != rhs.lo || hi != rhs.hi;
00140     }
00141 
00142     bool operator != (unsigned int rhs)
00143     {
00144         return lo != rhs || hi != 0;
00145     }
00146 
00147     bool operator != (int rhs)
00148     {
00149         return lo != (unsigned int) rhs || hi != 0;
00150     }
00151 
00152     bool operator == (const longint & rhs) const
00153     {
00154         return lo == rhs.lo && hi == rhs.hi;
00155     }
00156 
00157     bool operator == (const unsigned int rhs) const
00158     {
00159         return lo == rhs && hi == 0;
00160     }
00161 
00162     bool operator == (const int rhs) const
00163     {
00164         return lo == (unsigned int) rhs && hi == 0;
00165     }
00166 
00167     longint & operator = (const longint & rhs)
00168     {
00169         lo = rhs.lo;
00170         hi = rhs.hi;
00171         return *this;
00172     }
00173 
00174     longint & operator = (unsigned int rhs)
00175     {
00176         lo = rhs;
00177         hi = 0;
00178         return *this;
00179     }
00180 
00181     longint & operator = (int rhs)
00182     {
00183         lo = rhs;
00184         hi = 0;
00185         return *this;
00186     }
00187 
00188     longint & operator ^= (const longint & rhs)
00189     {
00190         hi ^= rhs.hi;
00191         lo ^= rhs.lo;
00192         return *this;
00193     }
00194 
00195     longint & operator |= (const longint & rhs)
00196     {
00197         hi |= rhs.hi;
00198         lo |= rhs.lo;
00199         return *this;
00200     }
00201 
00202     longint  operator &= (const longint & rhs)
00203     {
00204         hi &= rhs.hi;
00205         lo &= rhs.lo;
00206         return *this;
00207     }
00208 
00209     longint operator << (int bits)
00210     {
00211         longint result(*this);
00212         result <<= bits;
00213         return result;
00214     }
00215 
00216     longint & operator <<= (int bits)
00217     {
00218         if (bits <= 0)
00219             return *this;
00220         else
00221         {
00222             hi = (hi << 1) + ((lo & 0x80000000) != 0);
00223             lo <<= 1;
00224             return *this <<= bits - 1;
00225         }
00226     }
00227 
00228     longint operator >> (int bits)
00229     {
00230         longint result(*this);
00231         result >>= bits;
00232         return result;
00233     }
00234 
00235     longint & operator >>= (int bits)
00236     {
00237         if (bits <= 0)
00238             return *this;
00239         else
00240         {
00241             lo = (lo >> 1) + (hi & 1 ? 0x80000000 : 0);
00242             hi >>= 1;
00243             return *this >>= bits - 1;
00244         }
00245     }
00246 
00247     longint operator - (unsigned int rhs)
00248     {
00249         int high = (rhs > lo) ? hi - 1 : hi;
00250         return longint(high, lo - rhs);
00251     }
00252 
00253     longint operator - (int rhs)
00254     {
00255         int high = ((unsigned int) rhs > lo) ? hi - 1 : hi;
00256         return longint(high, lo - rhs);
00257     }
00258 
00259 private:
00260     unsigned int hi, lo;
00261 };
00262 
00263 #endif      /* __GNUC__ */
00264 
00265 #endif      /* __USE_LONG_INT */
00266 
00267 #endif      /* __LONGINT_H__ */
00268 
00269 
00270 
00271 
00272 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends