Changes in / [052cd71:9795142]
- Location:
- src/ResolvExpr
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ConversionCost.cc
r052cd71 r9795142 10 10 // Created On : Sun May 17 07:06:19 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Feb 14 17:04:31201913 // Update Count : 2 312 // Last Modified On : Fri Apr 26 16:33:04 2019 13 // Update Count : 24 14 14 // 15 15 … … 28 28 29 29 namespace ResolvExpr { 30 #if 0 30 31 const Cost Cost::zero = Cost{ 0, 0, 0, 0, 0, 0, 0 }; 31 32 const Cost Cost::infinity = Cost{ -1, -1, -1, -1, -1, 1, -1 }; … … 37 38 const Cost Cost::spec = Cost{ 0, 0, 0, 0, 0, -1, 0 }; 38 39 const Cost Cost::reference = Cost{ 0, 0, 0, 0, 0, 0, 1 }; 40 #endif 39 41 40 42 #if 0 -
src/ResolvExpr/Cost.h
r052cd71 r9795142 7 7 // Cost.h -- 8 8 // 9 // Author : Richard C. Bilson9 // Author : Peter Buhr and Aaron Moss 10 10 // Created On : Sun May 17 09:39:50 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Feb 7 20:54:29201913 // Update Count : 812 // Last Modified On : Sun Apr 28 18:26:36 2019 13 // Update Count : 29 14 14 // 15 15 … … 17 17 18 18 #include <iostream> 19 #include <cassert> 19 20 20 21 namespace ResolvExpr { 22 #if 0 23 24 //*************************** OLD *************************** 25 21 26 class Cost { 22 27 private: 23 28 Cost( int unsafeCost, int polyCost, int safeCost, int signCost, 24 int varCost, int specCost, int referenceCost );29 int varCost, int specCost, int referenceCost ); 25 30 public: 26 31 Cost & incUnsafe( int inc = 1 ); … … 71 76 72 77 inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost, 73 int varCost, int specCost, int referenceCost )78 int varCost, int specCost, int referenceCost ) 74 79 : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ), 75 80 varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {} … … 121 126 return Cost{ 122 127 unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, 123 signCost + other.signCost, varCost + other.varCost, specCost + other.specCost,124 referenceCost + other.referenceCost };128 signCost + other.signCost, varCost + other.varCost, specCost + other.specCost, 129 referenceCost + other.referenceCost }; 125 130 } 126 131 … … 211 216 << cost.referenceCost << " )"; 212 217 } 218 #endif // 0 219 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; ///< unused 228 unsigned char referenceCost; ///< reference conversions 229 unsigned char specCost; ///< Polymorphic type specializations (type assertions), negative cost 230 unsigned char varCost; ///< Count of polymorphic type variables 231 unsigned char signCost; ///< Count of safe sign conversions 232 unsigned char safeCost; ///< Safe (widening) conversions 233 unsigned char polyCost; ///< Count of parameters and return values bound to some poly type 234 unsigned char unsafeCost; ///< Unsafe (narrowing) conversions 235 #else 236 #error BIG_ENDIAN unsupported 237 #endif 238 } 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 cost 247 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, // padding 261 (unsigned char)referenceCost, 262 (unsigned char)(specCost + 0xff ), // correct for signedness 263 (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 > rhs 282 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 cost 298 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 cost 356 } 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 } 213 363 } // namespace ResolvExpr 214 364
Note: See TracChangeset
for help on using the changeset viewer.