Changeset c378e5e for src/ResolvExpr/Cost.h
 Timestamp:
 Apr 28, 2019, 6:50:13 PM (5 years ago)
 Branches:
 ADT, armeh, astexperimental, cleanupdtors, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 9795142
 Parents:
 bd405fa
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/ResolvExpr/Cost.h
rbd405fa rc378e5e 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 // Littleendian => 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 littleendian => 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.