source: src/ResolvExpr/Cost.h@ 47bfefd

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 47bfefd was cdcddfe1, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add signedness to cost model and _FloatNN

  • Property mode set to 100644
File size: 6.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Cost.h --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 09:39:50 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Feb 7 20:54:29 2019
13// Update Count : 8
14//
15
16#pragma once
17
18#include <iostream>
19
20namespace ResolvExpr {
21 class Cost {
22 private:
23 Cost( int unsafeCost, int polyCost, int safeCost, int signCost,
24 int varCost, int specCost, int referenceCost );
25 public:
26 Cost & incUnsafe( int inc = 1 );
27 Cost & incPoly( int inc = 1 );
28 Cost & incSafe( int inc = 1 );
29 Cost & incSign( int inc = 1 );
30 Cost & incVar( int inc = 1 );
31 Cost & decSpec( int inc = 1 );
32 Cost & incReference( int inc = 1 );
33
34 int get_unsafeCost() const { return unsafeCost; }
35 int get_polyCost() const { return polyCost; }
36 int get_safeCost() const { return safeCost; }
37 int get_signCost() const { return signCost; }
38 int get_varCost() const { return varCost; }
39 int get_specCost() const { return specCost; }
40 int get_referenceCost() const { return referenceCost; }
41
42 Cost operator+( const Cost &other ) const;
43 Cost &operator+=( const Cost &other );
44 bool operator<( const Cost &other ) const;
45 bool operator==( const Cost &other ) const;
46 bool operator!=( const Cost &other ) const;
47 friend std::ostream &operator<<( std::ostream &os, const Cost &cost );
48 // returns negative for *this < other, 0 for *this == other, positive for *this > other
49 int compare( const Cost &other ) const;
50
51 static const Cost zero;
52 static const Cost infinity;
53
54 static const Cost unsafe;
55 static const Cost poly;
56 static const Cost safe;
57 static const Cost sign;
58 static const Cost var;
59 static const Cost spec;
60 static const Cost reference;
61
62 private:
63 int unsafeCost; ///< Unsafe (narrowing) conversions
64 int polyCost; ///< Count of parameters and return values bound to some poly type
65 int safeCost; ///< Safe (widening) conversions
66 int signCost; ///< Count of safe sign conversions
67 int varCost; ///< Count of polymorphic type variables
68 int specCost; ///< Polymorphic type specializations (type assertions), negative cost
69 int referenceCost; ///< reference conversions
70 };
71
72 inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost,
73 int varCost, int specCost, int referenceCost )
74 : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ),
75 varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {}
76
77 inline Cost & Cost::incUnsafe( int inc ) {
78 if ( *this == infinity ) return *this;
79 unsafeCost += inc;
80 return *this;
81 }
82
83 inline Cost & Cost::incPoly( int inc ) {
84 if ( *this == infinity ) return *this;
85 polyCost += inc;
86 return *this;
87 }
88
89 inline Cost & Cost::incSafe( int inc ) {
90 if ( *this == infinity ) return *this;
91 safeCost += inc;
92 return *this;
93 }
94
95 inline Cost & Cost::incSign( int inc ) {
96 if ( *this == infinity ) return *this;
97 signCost += inc;
98 return *this;
99 }
100
101 inline Cost & Cost::incVar( int inc ) {
102 if ( *this == infinity ) return *this;
103 varCost += inc;
104 return *this;
105 }
106
107 inline Cost& Cost::decSpec( int dec ) {
108 if ( *this == infinity ) return *this;
109 specCost -= dec;
110 return *this;
111 }
112
113 inline Cost & Cost::incReference( int inc ) {
114 if ( *this == infinity ) return *this;
115 referenceCost += inc;
116 return *this;
117 }
118
119 inline Cost Cost::operator+( const Cost &other ) const {
120 if ( *this == infinity || other == infinity ) return infinity;
121 return Cost{
122 unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost,
123 signCost + other.signCost, varCost + other.varCost, specCost + other.specCost,
124 referenceCost + other.referenceCost };
125 }
126
127 inline Cost &Cost::operator+=( const Cost &other ) {
128 if ( *this == infinity ) return *this;
129 if ( other == infinity ) {
130 *this = infinity;
131 return *this;
132 }
133 unsafeCost += other.unsafeCost;
134 polyCost += other.polyCost;
135 safeCost += other.safeCost;
136 signCost += other.signCost;
137 varCost += other.varCost;
138 specCost += other.specCost;
139 referenceCost += other.referenceCost;
140 return *this;
141 }
142
143 inline bool Cost::operator<( const Cost &other ) const {
144 if ( *this == infinity ) return false;
145 if ( other == infinity ) return true;
146
147 if ( unsafeCost > other.unsafeCost ) {
148 return false;
149 } else if ( unsafeCost < other.unsafeCost ) {
150 return true;
151 } else if ( polyCost > other.polyCost ) {
152 return false;
153 } else if ( polyCost < other.polyCost ) {
154 return true;
155 } else if ( safeCost > other.safeCost ) {
156 return false;
157 } else if ( safeCost < other.safeCost ) {
158 return true;
159 } else if ( signCost > other.signCost ) {
160 return false;
161 } else if ( signCost < other.signCost ) {
162 return true;
163 } else if ( varCost > other.varCost ) {
164 return false;
165 } else if ( varCost < other.varCost ) {
166 return true;
167 } else if ( specCost > other.specCost ) {
168 return false;
169 } else if ( specCost > other.specCost ) {
170 return true;
171 } else if ( referenceCost > other.referenceCost ) {
172 return false;
173 } else if ( referenceCost < other.referenceCost ) {
174 return true;
175 } else {
176 return false;
177 } // if
178 }
179
180 inline int Cost::compare( const Cost &other ) const {
181 if ( *this == infinity ) return +1;
182 if ( other == infinity ) return -1;
183
184 int c = unsafeCost - other.unsafeCost; if ( c ) return c;
185 c = polyCost - other.polyCost; if ( c ) return c;
186 c = safeCost - other.safeCost; if ( c ) return c;
187 c = signCost - other.signCost; if ( c ) return c;
188 c = varCost - other.varCost; if ( c ) return c;
189 c = specCost - other.specCost; if ( c ) return c;
190 return referenceCost - other.referenceCost;
191 }
192
193 inline bool Cost::operator==( const Cost &other ) const {
194 return unsafeCost == other.unsafeCost
195 && polyCost == other.polyCost
196 && safeCost == other.safeCost
197 && signCost == other.signCost
198 && varCost == other.varCost
199 && specCost == other.specCost
200 && referenceCost == other.referenceCost;
201 }
202
203 inline bool Cost::operator!=( const Cost &other ) const {
204 return !( *this == other );
205 }
206
207 inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) {
208 return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", "
209 << cost.safeCost << ", " << cost.signCost << ", "
210 << cost.varCost << ", " << cost.specCost << ", "
211 << cost.referenceCost << " )";
212 }
213} // namespace ResolvExpr
214
215// Local Variables: //
216// tab-width: 4 //
217// mode: c++ //
218// compile-command: "make install" //
219// End: //
Note: See TracBrowser for help on using the repository browser.