additional fixes to checkstate
[deliverable/titan.core.git] / compiler2 / Int.hh
1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Bibo, Zoltan
11 * Forstner, Matyas
12 * Kovacs, Ferenc
13 * Raduly, Csaba
14 * Szabo, Janos Zoltan – initial implementation
15 *
16 ******************************************************************************/
17 #ifndef Int_HH_
18 #define Int_HH_
19
20 #include "Real.hh"
21 #include "string.hh"
22
23 #include <openssl/bn.h>
24
25 #include <limits.h>
26
27 // Better together.
28 #if !defined(LLONG_MAX) || !defined(LLONG_MIN)
29 #define LLONG_MAX 9223372036854775807LL
30 #define LLONG_MIN (-LLONG_MAX-1)
31 #else
32 #if __GNUC__ < 3
33 #undef LLONG_MIN
34 #define LLONG_MIN ((long long)(-LLONG_MAX-1))
35 #endif
36 #endif
37
38 namespace Common {
39
40 class Location;
41
42 typedef long long Int;
43 typedef int RInt;
44
45 // Converts the Common::Int value to string.
46 string Int2string(const Int& i);
47
48 // Converts the string value to Common::Int. Throws Error_Int if conversion
49 // is not possible (e.g. value is out of range, or s is not a valid integer
50 // representation).
51 Int string2Int(const char *s, const Location& loc);
52 inline Int string2Int(const string& s, const Location& loc)
53 { return string2Int(s.c_str(), loc); }
54
55 class int_val_t
56 {
57 private:
58 bool native_flag;
59 union {
60 Int native;
61 BIGNUM *openssl;
62 } val;
63 // Hide all BIGNUM related stuff here.
64 BIGNUM *to_openssl() const;
65 BIGNUM *get_val_openssl() const;
66 explicit int_val_t(BIGNUM *v) : native_flag(false), val() { val.openssl = v; }
67
68 public:
69 int_val_t();
70 int_val_t(const int_val_t& v);
71 int_val_t(const char *s, const Location& loc);
72 explicit int_val_t(Int v) : native_flag(true), val() { val.native = v; }
73 ~int_val_t();
74 string t_str() const;
75 int_val_t operator-() const;
76 int_val_t operator>>(Int right) const;
77 int_val_t operator+(const int_val_t& right) const;
78 int_val_t operator-(const int_val_t& right) const;
79 inline int_val_t operator+(Int right) const { return operator+(int_val_t(right)); }
80 inline int_val_t operator-(Int right) const { return operator-(int_val_t(right)); }
81 int_val_t operator*(const int_val_t& right) const;
82 int_val_t operator/(const int_val_t& right) const;
83 int_val_t operator&(Int right) const;
84 bool operator<(const int_val_t& right) const;
85 inline bool operator<(Int right) const { return *this < int_val_t(right); }
86 inline bool operator>(Int right) const { return *this > int_val_t(right); }
87 inline bool operator>(const int_val_t& right) const { return *this != right && !(*this < right); }
88 inline bool operator<=(Int right) const { return *this == right || *this < right; }
89 inline bool operator>=(Int right) const { return *this == right || *this > right; }
90 inline bool operator<=(const int_val_t& right) const { return *this == right || *this < right; }
91 inline bool operator>=(const int_val_t& right) const { return *this == right || *this > right; }
92 inline bool operator!=(Int right) const { return !(*this == right); }
93 inline bool operator!=(const int_val_t& right) const { return !(*this == right); }
94 bool operator==(const int_val_t& right) const;
95 bool operator==(Int right) const;
96 int_val_t& operator=(const int_val_t& right);
97 int_val_t& operator+=(Int right);
98 int_val_t& operator<<=(Int right);
99 int_val_t& operator>>=(Int right);
100 const Int& get_val() const;
101 Real to_real() const;
102 inline bool is_native_fit() const
103 // It seems to give correct results (-2147483648..2147483647).
104 { return native_flag && val.native == static_cast<RInt>(val.native); }
105 inline bool is_native() const { return native_flag; }
106 inline bool is_openssl() const { return !native_flag; }
107 inline bool is_negative() const
108 { return native_flag ? val.native < 0 : BN_is_negative(val.openssl); }
109 };
110
111 } // Common
112
113 #endif // Int_HH_
This page took 0.033371 seconds and 5 git commands to generate.