CACAO
Scalar.hpp
Go to the documentation of this file.
1 /* src/vm/jit/loop/Scalar.hpp
2 
3  Copyright (C) 1996-2012
4  CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 
6  This file is part of CACAO.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2, or (at
11  your option) any later version.
12 
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 
23 */
24 
25 #ifndef _SCALAR_HPP
26 #define _SCALAR_HPP
27 
28 #include <limits>
29 #include <iostream>
30 #include <cassert>
31 
32 #include "vm/types.hpp"
33 #include "NumericInstruction.hpp"
34 
35 
36 /**
37  * An integral value of the form
38  * Constant + NumericInstruction.
39  */
40 class Scalar
41 {
44 
45 public:
46 
47  static s4 min() { return std::numeric_limits<s4>::min(); }
48  static s4 max() { return std::numeric_limits<s4>::max(); }
49 
50  /**
51  * Creates a scalar which equals zero.
52  */
54  : _constant(0)
55  , _instruction(NumericInstruction::newZero())
56  {}
57 
58  explicit Scalar(s4 constant)
59  : _constant(constant)
60  , _instruction(NumericInstruction::newZero())
61  {}
62 
64  : _constant(0)
65  , _instruction(instruction)
66  {}
67 
68  /**
69  * Creates a scalar which equals constant + instruction.
70  * The expression must not over- or underflow!
71  */
73  : _constant(constant)
74  , _instruction(instruction)
75  {
76 #if !defined(NDEBUG)
77  s8 cu = static_cast<s8>(constant) + instruction.upper();
78  s8 cl = static_cast<s8>(constant) + instruction.lower();
79 #endif
80 
81  // check for overflow
84  }
85 
86  s4 constant() const { return _constant; }
88 
89  s4 lower() const { return _constant + _instruction.lower(); }
90  s4 upper() const { return _constant + _instruction.upper(); }
91 
92  /**
93  * Computes an upper bound of the minimum of this and the specified scalar.
94  * The result is stored in this object.
95  */
96  void upperBoundOfMinimumWith(const Scalar&);
97 
98  /**
99  * Computes a lower bound of the maximum of this and the specified scalar.
100  * The result is stored in this object.
101  */
102  void lowerBoundOfMaximumWith(const Scalar&);
103 
104  /**
105  * Computes an upper bound of the maximum of this and the specified scalar.
106  * The result is stored in this object.
107  */
108  void upperBoundOfMaximumWith(const Scalar&);
109 
110  /**
111  * Computes a lower bound of the minimum of this and the specified scalar.
112  * The result is stored in this object.
113  */
114  void lowerBoundOfMinimumWith(const Scalar&);
115 
116  /**
117  * Tries to add a scalar to this scalar.
118  * If an overflow can happen or the result is not representable as a scalar,
119  * this scalar will not be changed and false will be returned.
120  * Otherwise the return value is true.
121  */
122  bool tryAdd(const Scalar&);
123 
124  /**
125  * Tries to subtract a scalar from this scalar.
126  * If an overflow can happen or the result is not representable as a scalar,
127  * this scalar will not be changed and false will be returned.
128  * Otherwise the return value is true.
129  */
130  bool trySubtract(const Scalar&);
131 };
132 
133 
134 inline void Scalar::upperBoundOfMinimumWith(const Scalar& other)
135 {
136  if (_constant > other._constant)
137  *this = other;
138 }
139 
140 inline void Scalar::lowerBoundOfMaximumWith(const Scalar& other)
141 {
142  if (_constant < other._constant)
143  *this = other;
144 }
145 
146 // True if a equals b for every possible value of the instruction, false otherwise.
147 inline bool operator==(const Scalar& a, const Scalar& b)
148 {
149  return a.constant() == b.constant() && a.instruction() == b.instruction();
150 }
151 
152 // True if a does not equal b for every possible value of the instruction, false otherwise.
153 inline bool operator!=(const Scalar& a, const Scalar& b)
154 {
155  return a.constant() != b.constant() || a.instruction() != b.instruction();
156 }
157 
158 std::ostream& operator<<(std::ostream&, const Scalar&);
159 
160 #endif
161 
162 /*
163  * These are local overrides for various environment variables in Emacs.
164  * Please do not remove this and leave it at the end of the file, where
165  * Emacs will automagically detect them.
166  * ---------------------------------------------------------------------
167  * Local variables:
168  * mode: c++
169  * indent-tabs-mode: t
170  * c-basic-offset: 4
171  * tab-width: 4
172  * End:
173  * vim:noexpandtab:sw=4:ts=4:
174  */
175 
void lowerBoundOfMinimumWith(const Scalar &)
Computes a lower bound of the minimum of this and the specified scalar.
Definition: Scalar.cpp:59
#define max(a, b)
Definition: lsra.hpp:80
s4 upper() const
Definition: Scalar.hpp:90
s4 _constant
Definition: Scalar.hpp:42
Scalar()
Creates a scalar which equals zero.
Definition: Scalar.hpp:53
Represents a constant numeric instruction.
static s4 min()
Definition: Scalar.hpp:47
Scalar(s4 constant)
Definition: Scalar.hpp:58
int64_t s8
Definition: types.hpp:48
An integral value of the form Constant + NumericInstruction.
Definition: Scalar.hpp:40
bool operator!=(const ordered_list< T, Allocator > &lhs, const ordered_list< T, Allocator > &rhs)
inequality
#define min(a, b)
Definition: lsra.hpp:79
s4 lower() const
The smallest value this instruction can return.
static s4 max()
Definition: Scalar.hpp:48
int32_t s4
Definition: types.hpp:45
OStream & operator<<(OStream &OS, const std::string &t)
Definition: OStream.hpp:459
void upperBoundOfMaximumWith(const Scalar &)
Computes an upper bound of the maximum of this and the specified scalar.
Definition: Scalar.cpp:30
Scalar(const NumericInstruction &instruction)
Definition: Scalar.hpp:63
bool operator==(const ordered_list< T, Allocator > &lhs, const ordered_list< T, Allocator > &rhs)
equality
NumericInstruction instruction() const
Definition: Scalar.hpp:87
bool tryAdd(const Scalar &)
Tries to add a scalar to this scalar.
Definition: Scalar.cpp:88
void upperBoundOfMinimumWith(const Scalar &)
Computes an upper bound of the minimum of this and the specified scalar.
Definition: Scalar.hpp:134
Scalar(s4 constant, const NumericInstruction &instruction)
Creates a scalar which equals constant + instruction.
Definition: Scalar.hpp:72
s4 upper() const
The largest value this instruction can return.
void lowerBoundOfMaximumWith(const Scalar &)
Computes a lower bound of the maximum of this and the specified scalar.
Definition: Scalar.hpp:140
NumericInstruction _instruction
Definition: Scalar.hpp:43
bool trySubtract(const Scalar &)
Tries to subtract a scalar from this scalar.
Definition: Scalar.cpp:118
s4 lower() const
Definition: Scalar.hpp:89
s4 constant() const
Definition: Scalar.hpp:86