Changes in src/ResolvExpr/Cost.h [c378e5e:cdcddfe1]
- File:
-
- 1 edited
-
src/ResolvExpr/Cost.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Cost.h
rc378e5e rcdcddfe1 7 7 // Cost.h -- 8 8 // 9 // Author : Peter Buhr and Aaron Moss9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 09:39:50 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Apr 28 18:26:36201913 // Update Count : 2912 // Last Modified On : Thu Feb 7 20:54:29 2019 13 // Update Count : 8 14 14 // 15 15 … … 17 17 18 18 #include <iostream> 19 #include <cassert>20 19 21 20 namespace ResolvExpr { 22 #if 023 24 //*************************** OLD ***************************25 26 21 class Cost { 27 22 private: 28 23 Cost( int unsafeCost, int polyCost, int safeCost, int signCost, 29 int varCost, int specCost, int referenceCost );24 int varCost, int specCost, int referenceCost ); 30 25 public: 31 26 Cost & incUnsafe( int inc = 1 ); … … 76 71 77 72 inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost, 78 int varCost, int specCost, int referenceCost )73 int varCost, int specCost, int referenceCost ) 79 74 : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ), 80 75 varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {} … … 126 121 return Cost{ 127 122 unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, 128 signCost + other.signCost, varCost + other.varCost, specCost + other.specCost,129 referenceCost + other.referenceCost };123 signCost + other.signCost, varCost + other.varCost, specCost + other.specCost, 124 referenceCost + other.referenceCost }; 130 125 } 131 126 … … 216 211 << cost.referenceCost << " )"; 217 212 } 218 #endif // 0219 220 //*************************** NEW ***************************221 222 class Cost {223 union {224 struct {225 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__226 // Little-endian => first value is low priority and last is high priority.227 unsigned char padding; ///< unused228 unsigned char referenceCost; ///< reference conversions229 unsigned char specCost; ///< Polymorphic type specializations (type assertions), negative cost230 unsigned char varCost; ///< Count of polymorphic type variables231 unsigned char signCost; ///< Count of safe sign conversions232 unsigned char safeCost; ///< Safe (widening) conversions233 unsigned char polyCost; ///< Count of parameters and return values bound to some poly type234 unsigned char unsafeCost; ///< Unsafe (narrowing) conversions235 #else236 #error BIG_ENDIAN unsupported237 #endif238 } v;239 uint64_t all;240 };241 public:242 // Compiler adjusts constants for correct endian.243 enum : uint64_t {244 zero = 0x00'00'00'00'00'ff'00'00,245 infinity = 0xff'ff'ff'ff'ff'00'ff'ff,246 correction = 0x00'00'00'00'00'ff'00'00, // correct for negative spec cost247 unsafe = 0x01'00'00'00'00'ff'00'00,248 poly = 0x00'01'00'00'00'ff'00'00,249 safe = 0x00'00'01'00'00'ff'00'00,250 sign = 0x00'00'00'01'00'ff'00'00,251 var = 0x00'00'00'00'01'ff'00'00,252 spec = 0x00'00'00'00'00'fe'00'00,253 reference = 0x00'00'00'00'00'ff'01'00,254 }; //255 256 Cost( uint64_t all ) { Cost::all = all; }257 Cost( int unsafeCost, int polyCost, int safeCost, int signCost, int varCost, int specCost, int referenceCost ) {258 // Assume little-endian => first value is low priority and last is high priority.259 v = {260 (unsigned char)0, // padding261 (unsigned char)referenceCost,262 (unsigned char)(specCost + 0xff ), // correct for signedness263 (unsigned char)varCost,264 (unsigned char)signCost,265 (unsigned char)safeCost,266 (unsigned char)polyCost,267 (unsigned char)unsafeCost,268 };269 }270 271 int get_unsafeCost() const { return v.unsafeCost; }272 int get_polyCost() const { return v.polyCost; }273 int get_safeCost() const { return v.safeCost; }274 int get_signCost() const { return v.signCost; }275 int get_varCost() const { return v.varCost; }276 int get_specCost() const { return -(0xff - v.specCost); }277 int get_referenceCost() const { return v.referenceCost; }278 279 friend bool operator==( const Cost, const Cost );280 friend bool operator!=( const Cost lhs, const Cost rhs );281 // returns negative for *this < rhs, 0 for *this == rhs, positive for *this > rhs282 int compare( const Cost rhs ) const {283 if ( all == infinity ) return 1;284 if ( rhs.all == infinity ) return -1;285 return all > rhs.all ? 1 : all == rhs.all ? 0 : -1;286 }287 friend bool operator<( const Cost lhs, const Cost rhs );288 289 friend Cost operator+( const Cost lhs, const Cost rhs );290 291 Cost operator+=( const Cost rhs ) {292 if ( all == infinity ) return *this;293 if ( rhs.all == infinity ) {294 all = infinity;295 return *this;296 }297 all += rhs.all - correction; // correct for negative spec cost298 return *this;299 }300 301 Cost incUnsafe( int inc = 1 ) {302 if ( all != infinity ) { assert( v.unsafeCost + inc < 256 ); v.unsafeCost += inc; }303 return *this;304 }305 306 Cost incPoly( int inc = 1 ) {307 if ( all != infinity ) { assert( v.polyCost + inc < 256 ); v.polyCost += inc; }308 return *this;309 }310 311 Cost incSafe( int inc = 1 ) {312 if ( all != infinity ) { assert( v.safeCost + inc < 256 ); v.safeCost += inc; }313 return *this;314 }315 316 Cost incSign( int inc = 1 ) {317 if ( all != infinity ) { assert( v.signCost + inc < 256 ); v.signCost += inc; }318 return *this;319 }320 321 Cost incVar( int inc = 1 ) {322 if ( all != infinity ) { assert( v.varCost + inc < 256 ); v.varCost += inc; }323 return *this;324 }325 326 Cost decSpec( int dec = 1 ) {327 if ( all != infinity ) { v.specCost -= dec; }328 return *this;329 }330 331 Cost incReference( int inc = 1 ) {332 if ( all != infinity ) { assert( v.referenceCost + inc < 256 ); v.referenceCost += inc; }333 return *this;334 }335 336 friend std::ostream & operator<<( std::ostream & os, const Cost cost );337 };338 339 inline bool operator==( const Cost lhs, const Cost rhs ) {340 return lhs.all == rhs.all;341 }342 343 inline bool operator!=( const Cost lhs, const Cost rhs ) {344 return !( lhs.all == rhs.all );345 }346 347 inline bool operator<( const Cost lhs, const Cost rhs ) {348 if ( lhs.all == Cost::infinity ) return false;349 if ( rhs.all == Cost::infinity ) return true;350 return lhs.all < rhs.all;351 }352 353 inline Cost operator+( const Cost lhs, const Cost rhs ) {354 if ( lhs.all == Cost::infinity || rhs.all == Cost::infinity ) return Cost{ Cost::infinity };355 return Cost{ lhs.all + rhs.all - Cost::correction }; // correct for negative spec cost356 }357 358 inline std::ostream & operator<<( std::ostream & os, const Cost cost ) {359 return os << "( " << cost.get_unsafeCost() << ", " << cost.get_polyCost() << ", " << cost.get_safeCost()360 << ", " << cost.get_signCost() << ", " << cost.get_varCost() << ", " << cost.get_specCost()361 << ", " << cost.get_referenceCost() << " )";362 }363 213 } // namespace ResolvExpr 364 214
Note:
See TracChangeset
for help on using the changeset viewer.