[a32b204] | 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 | // |
---|
[b46e3bd] | 7 | // Cost.h -- |
---|
[a32b204] | 8 | // |
---|
[c378e5e] | 9 | // Author : Peter Buhr and Aaron Moss |
---|
[a32b204] | 10 | // Created On : Sun May 17 09:39:50 2015 |
---|
[cdcddfe1] | 11 | // Last Modified By : Peter A. Buhr |
---|
[b10c39a0] | 12 | // Last Modified On : Mon Apr 29 18:33:44 2019 |
---|
| 13 | // Update Count : 49 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
| 18 | #include <iostream> |
---|
[c378e5e] | 19 | #include <cassert> |
---|
[b10c39a0] | 20 | #include <climits> |
---|
[51b7345] | 21 | |
---|
| 22 | namespace ResolvExpr { |
---|
[c378e5e] | 23 | #if 0 |
---|
| 24 | |
---|
| 25 | //*************************** OLD *************************** |
---|
| 26 | |
---|
[a32b204] | 27 | class Cost { |
---|
[89be1c68] | 28 | private: |
---|
[cdcddfe1] | 29 | Cost( int unsafeCost, int polyCost, int safeCost, int signCost, |
---|
[c378e5e] | 30 | int varCost, int specCost, int referenceCost ); |
---|
[89be1c68] | 31 | public: |
---|
| 32 | Cost & incUnsafe( int inc = 1 ); |
---|
| 33 | Cost & incPoly( int inc = 1 ); |
---|
[d97c3a4] | 34 | Cost & incSafe( int inc = 1 ); |
---|
[cdcddfe1] | 35 | Cost & incSign( int inc = 1 ); |
---|
[1dd1bd2] | 36 | Cost & incVar( int inc = 1 ); |
---|
| 37 | Cost & decSpec( int inc = 1 ); |
---|
[7ebaa56] | 38 | Cost & incReference( int inc = 1 ); |
---|
[b46e3bd] | 39 | |
---|
[ddf8a29] | 40 | int get_unsafeCost() const { return unsafeCost; } |
---|
| 41 | int get_polyCost() const { return polyCost; } |
---|
[d97c3a4] | 42 | int get_safeCost() const { return safeCost; } |
---|
[cdcddfe1] | 43 | int get_signCost() const { return signCost; } |
---|
[1dd1bd2] | 44 | int get_varCost() const { return varCost; } |
---|
| 45 | int get_specCost() const { return specCost; } |
---|
[ddf8a29] | 46 | int get_referenceCost() const { return referenceCost; } |
---|
| 47 | |
---|
[a32b204] | 48 | Cost operator+( const Cost &other ) const; |
---|
| 49 | Cost &operator+=( const Cost &other ); |
---|
| 50 | bool operator<( const Cost &other ) const; |
---|
| 51 | bool operator==( const Cost &other ) const; |
---|
| 52 | bool operator!=( const Cost &other ) const; |
---|
| 53 | friend std::ostream &operator<<( std::ostream &os, const Cost &cost ); |
---|
[6d6e829] | 54 | // returns negative for *this < other, 0 for *this == other, positive for *this > other |
---|
| 55 | int compare( const Cost &other ) const; |
---|
[b46e3bd] | 56 | |
---|
[a32b204] | 57 | static const Cost zero; |
---|
| 58 | static const Cost infinity; |
---|
[7ebaa56] | 59 | |
---|
[b46e3bd] | 60 | static const Cost unsafe; |
---|
| 61 | static const Cost poly; |
---|
[d97c3a4] | 62 | static const Cost safe; |
---|
[cdcddfe1] | 63 | static const Cost sign; |
---|
[1dd1bd2] | 64 | static const Cost var; |
---|
| 65 | static const Cost spec; |
---|
[7ebaa56] | 66 | static const Cost reference; |
---|
[a32b204] | 67 | |
---|
[6d6e829] | 68 | private: |
---|
[1dd1bd2] | 69 | int unsafeCost; ///< Unsafe (narrowing) conversions |
---|
| 70 | int polyCost; ///< Count of parameters and return values bound to some poly type |
---|
[d97c3a4] | 71 | int safeCost; ///< Safe (widening) conversions |
---|
[cdcddfe1] | 72 | int signCost; ///< Count of safe sign conversions |
---|
[1dd1bd2] | 73 | int varCost; ///< Count of polymorphic type variables |
---|
| 74 | int specCost; ///< Polymorphic type specializations (type assertions), negative cost |
---|
| 75 | int referenceCost; ///< reference conversions |
---|
[a32b204] | 76 | }; |
---|
| 77 | |
---|
[cdcddfe1] | 78 | inline Cost::Cost( int unsafeCost, int polyCost, int safeCost, int signCost, |
---|
[c378e5e] | 79 | int varCost, int specCost, int referenceCost ) |
---|
[cdcddfe1] | 80 | : unsafeCost( unsafeCost ), polyCost( polyCost ), safeCost( safeCost ), signCost( signCost ), |
---|
| 81 | varCost( varCost ), specCost( specCost ), referenceCost( referenceCost ) {} |
---|
[a32b204] | 82 | |
---|
[89be1c68] | 83 | inline Cost & Cost::incUnsafe( int inc ) { |
---|
[17f22e78] | 84 | if ( *this == infinity ) return *this; |
---|
[b46e3bd] | 85 | unsafeCost += inc; |
---|
[89be1c68] | 86 | return *this; |
---|
[a32b204] | 87 | } |
---|
| 88 | |
---|
[89be1c68] | 89 | inline Cost & Cost::incPoly( int inc ) { |
---|
[17f22e78] | 90 | if ( *this == infinity ) return *this; |
---|
[b46e3bd] | 91 | polyCost += inc; |
---|
[89be1c68] | 92 | return *this; |
---|
[a32b204] | 93 | } |
---|
| 94 | |
---|
[d97c3a4] | 95 | inline Cost & Cost::incSafe( int inc ) { |
---|
[1dd1bd2] | 96 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 97 | safeCost += inc; |
---|
[1dd1bd2] | 98 | return *this; |
---|
| 99 | } |
---|
| 100 | |
---|
[cdcddfe1] | 101 | inline Cost & Cost::incSign( int inc ) { |
---|
| 102 | if ( *this == infinity ) return *this; |
---|
| 103 | signCost += inc; |
---|
| 104 | return *this; |
---|
| 105 | } |
---|
| 106 | |
---|
[d97c3a4] | 107 | inline Cost & Cost::incVar( int inc ) { |
---|
[1dd1bd2] | 108 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 109 | varCost += inc; |
---|
[1dd1bd2] | 110 | return *this; |
---|
| 111 | } |
---|
| 112 | |
---|
[d97c3a4] | 113 | inline Cost& Cost::decSpec( int dec ) { |
---|
[17f22e78] | 114 | if ( *this == infinity ) return *this; |
---|
[d97c3a4] | 115 | specCost -= dec; |
---|
[89be1c68] | 116 | return *this; |
---|
[a32b204] | 117 | } |
---|
| 118 | |
---|
[7ebaa56] | 119 | inline Cost & Cost::incReference( int inc ) { |
---|
[17f22e78] | 120 | if ( *this == infinity ) return *this; |
---|
[7ebaa56] | 121 | referenceCost += inc; |
---|
| 122 | return *this; |
---|
| 123 | } |
---|
| 124 | |
---|
[a32b204] | 125 | inline Cost Cost::operator+( const Cost &other ) const { |
---|
[17f22e78] | 126 | if ( *this == infinity || other == infinity ) return infinity; |
---|
[cdcddfe1] | 127 | return Cost{ |
---|
| 128 | unsafeCost + other.unsafeCost, polyCost + other.polyCost, safeCost + other.safeCost, |
---|
[c378e5e] | 129 | signCost + other.signCost, varCost + other.varCost, specCost + other.specCost, |
---|
| 130 | referenceCost + other.referenceCost }; |
---|
[a32b204] | 131 | } |
---|
| 132 | |
---|
| 133 | inline Cost &Cost::operator+=( const Cost &other ) { |
---|
[17f22e78] | 134 | if ( *this == infinity ) return *this; |
---|
| 135 | if ( other == infinity ) { |
---|
| 136 | *this = infinity; |
---|
| 137 | return *this; |
---|
| 138 | } |
---|
[b46e3bd] | 139 | unsafeCost += other.unsafeCost; |
---|
| 140 | polyCost += other.polyCost; |
---|
[d97c3a4] | 141 | safeCost += other.safeCost; |
---|
[cdcddfe1] | 142 | signCost += other.signCost; |
---|
[1dd1bd2] | 143 | varCost += other.varCost; |
---|
| 144 | specCost += other.specCost; |
---|
[7ebaa56] | 145 | referenceCost += other.referenceCost; |
---|
[a32b204] | 146 | return *this; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | inline bool Cost::operator<( const Cost &other ) const { |
---|
[b46e3bd] | 150 | if ( *this == infinity ) return false; |
---|
| 151 | if ( other == infinity ) return true; |
---|
| 152 | |
---|
| 153 | if ( unsafeCost > other.unsafeCost ) { |
---|
[a32b204] | 154 | return false; |
---|
[b46e3bd] | 155 | } else if ( unsafeCost < other.unsafeCost ) { |
---|
[a32b204] | 156 | return true; |
---|
[b46e3bd] | 157 | } else if ( polyCost > other.polyCost ) { |
---|
[a32b204] | 158 | return false; |
---|
[b46e3bd] | 159 | } else if ( polyCost < other.polyCost ) { |
---|
[a32b204] | 160 | return true; |
---|
[d97c3a4] | 161 | } else if ( safeCost > other.safeCost ) { |
---|
| 162 | return false; |
---|
| 163 | } else if ( safeCost < other.safeCost ) { |
---|
| 164 | return true; |
---|
[cdcddfe1] | 165 | } else if ( signCost > other.signCost ) { |
---|
| 166 | return false; |
---|
| 167 | } else if ( signCost < other.signCost ) { |
---|
| 168 | return true; |
---|
[1dd1bd2] | 169 | } else if ( varCost > other.varCost ) { |
---|
| 170 | return false; |
---|
| 171 | } else if ( varCost < other.varCost ) { |
---|
| 172 | return true; |
---|
| 173 | } else if ( specCost > other.specCost ) { |
---|
| 174 | return false; |
---|
| 175 | } else if ( specCost > other.specCost ) { |
---|
| 176 | return true; |
---|
[7ebaa56] | 177 | } else if ( referenceCost > other.referenceCost ) { |
---|
| 178 | return false; |
---|
| 179 | } else if ( referenceCost < other.referenceCost ) { |
---|
| 180 | return true; |
---|
[b46e3bd] | 181 | } else { |
---|
[a32b204] | 182 | return false; |
---|
[b46e3bd] | 183 | } // if |
---|
[d9a0e76] | 184 | } |
---|
| 185 | |
---|
[6d6e829] | 186 | inline int Cost::compare( const Cost &other ) const { |
---|
| 187 | if ( *this == infinity ) return +1; |
---|
| 188 | if ( other == infinity ) return -1; |
---|
| 189 | |
---|
| 190 | int c = unsafeCost - other.unsafeCost; if ( c ) return c; |
---|
| 191 | c = polyCost - other.polyCost; if ( c ) return c; |
---|
[d97c3a4] | 192 | c = safeCost - other.safeCost; if ( c ) return c; |
---|
[cdcddfe1] | 193 | c = signCost - other.signCost; if ( c ) return c; |
---|
[6d6e829] | 194 | c = varCost - other.varCost; if ( c ) return c; |
---|
| 195 | c = specCost - other.specCost; if ( c ) return c; |
---|
| 196 | return referenceCost - other.referenceCost; |
---|
| 197 | } |
---|
| 198 | |
---|
[a32b204] | 199 | inline bool Cost::operator==( const Cost &other ) const { |
---|
[b46e3bd] | 200 | return unsafeCost == other.unsafeCost |
---|
| 201 | && polyCost == other.polyCost |
---|
[d97c3a4] | 202 | && safeCost == other.safeCost |
---|
[cdcddfe1] | 203 | && signCost == other.signCost |
---|
[1dd1bd2] | 204 | && varCost == other.varCost |
---|
| 205 | && specCost == other.specCost |
---|
[7ebaa56] | 206 | && referenceCost == other.referenceCost; |
---|
[a32b204] | 207 | } |
---|
[d9a0e76] | 208 | |
---|
[a32b204] | 209 | inline bool Cost::operator!=( const Cost &other ) const { |
---|
| 210 | return !( *this == other ); |
---|
| 211 | } |
---|
[d9a0e76] | 212 | |
---|
[a32b204] | 213 | inline std::ostream &operator<<( std::ostream &os, const Cost &cost ) { |
---|
[cdcddfe1] | 214 | return os << "( " << cost.unsafeCost << ", " << cost.polyCost << ", " |
---|
| 215 | << cost.safeCost << ", " << cost.signCost << ", " |
---|
| 216 | << cost.varCost << ", " << cost.specCost << ", " |
---|
[d97c3a4] | 217 | << cost.referenceCost << " )"; |
---|
[a32b204] | 218 | } |
---|
[b10c39a0] | 219 | |
---|
| 220 | #else |
---|
[c378e5e] | 221 | |
---|
| 222 | //*************************** NEW *************************** |
---|
| 223 | |
---|
[b10c39a0] | 224 | // To maximize performance and space, the 7 resolution costs are packed into a single 64-bit word. However, the |
---|
| 225 | // specialization cost is a negative value so a correction is needed is a few places. |
---|
| 226 | |
---|
[c378e5e] | 227 | class Cost { |
---|
| 228 | union { |
---|
| 229 | struct { |
---|
| 230 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
---|
| 231 | // Little-endian => first value is low priority and last is high priority. |
---|
| 232 | unsigned char padding; ///< unused |
---|
| 233 | unsigned char referenceCost; ///< reference conversions |
---|
| 234 | unsigned char specCost; ///< Polymorphic type specializations (type assertions), negative cost |
---|
| 235 | unsigned char varCost; ///< Count of polymorphic type variables |
---|
| 236 | unsigned char signCost; ///< Count of safe sign conversions |
---|
| 237 | unsigned char safeCost; ///< Safe (widening) conversions |
---|
| 238 | unsigned char polyCost; ///< Count of parameters and return values bound to some poly type |
---|
| 239 | unsigned char unsafeCost; ///< Unsafe (narrowing) conversions |
---|
| 240 | #else |
---|
[b10c39a0] | 241 | #error Cost BIG_ENDIAN unsupported |
---|
[c378e5e] | 242 | #endif |
---|
| 243 | } v; |
---|
| 244 | uint64_t all; |
---|
| 245 | }; |
---|
[b10c39a0] | 246 | static const unsigned char correctb = 0xff; // byte correction for negative spec cost |
---|
| 247 | static const uint64_t correctw = 0x00'00'00'00'00'ff'00'00; //' word correction for negative spec cost |
---|
[c378e5e] | 248 | public: |
---|
| 249 | // Compiler adjusts constants for correct endian. |
---|
| 250 | enum : uint64_t { |
---|
[b10c39a0] | 251 | zero = 0x00'00'00'00'00'ff'00'00, |
---|
| 252 | infinity = 0xff'ff'ff'ff'ff'00'ff'ff, |
---|
| 253 | unsafe = 0x01'00'00'00'00'ff'00'00, |
---|
| 254 | poly = 0x00'01'00'00'00'ff'00'00, |
---|
| 255 | safe = 0x00'00'01'00'00'ff'00'00, |
---|
| 256 | sign = 0x00'00'00'01'00'ff'00'00, |
---|
| 257 | var = 0x00'00'00'00'01'ff'00'00, |
---|
| 258 | spec = 0x00'00'00'00'00'fe'00'00, |
---|
| 259 | reference = 0x00'00'00'00'00'ff'01'00, |
---|
| 260 | }; //' |
---|
[c378e5e] | 261 | |
---|
| 262 | Cost( uint64_t all ) { Cost::all = all; } |
---|
| 263 | Cost( int unsafeCost, int polyCost, int safeCost, int signCost, int varCost, int specCost, int referenceCost ) { |
---|
| 264 | // Assume little-endian => first value is low priority and last is high priority. |
---|
| 265 | v = { |
---|
[b10c39a0] | 266 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
---|
[c378e5e] | 267 | (unsigned char)0, // padding |
---|
[b10c39a0] | 268 | (unsigned char)referenceCost, // low priority |
---|
| 269 | (unsigned char)(specCost + correctb), // correct for signedness |
---|
[c378e5e] | 270 | (unsigned char)varCost, |
---|
| 271 | (unsigned char)signCost, |
---|
| 272 | (unsigned char)safeCost, |
---|
| 273 | (unsigned char)polyCost, |
---|
[b10c39a0] | 274 | (unsigned char)unsafeCost, // high priority |
---|
| 275 | #else |
---|
| 276 | #error Cost BIG_ENDIAN unsupported |
---|
| 277 | #endif |
---|
[c378e5e] | 278 | }; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | int get_unsafeCost() const { return v.unsafeCost; } |
---|
| 282 | int get_polyCost() const { return v.polyCost; } |
---|
| 283 | int get_safeCost() const { return v.safeCost; } |
---|
| 284 | int get_signCost() const { return v.signCost; } |
---|
| 285 | int get_varCost() const { return v.varCost; } |
---|
[b10c39a0] | 286 | int get_specCost() const { return -(correctb - v.specCost); } |
---|
[c378e5e] | 287 | int get_referenceCost() const { return v.referenceCost; } |
---|
| 288 | |
---|
| 289 | friend bool operator==( const Cost, const Cost ); |
---|
| 290 | friend bool operator!=( const Cost lhs, const Cost rhs ); |
---|
| 291 | // returns negative for *this < rhs, 0 for *this == rhs, positive for *this > rhs |
---|
| 292 | int compare( const Cost rhs ) const { |
---|
| 293 | if ( all == infinity ) return 1; |
---|
| 294 | if ( rhs.all == infinity ) return -1; |
---|
| 295 | return all > rhs.all ? 1 : all == rhs.all ? 0 : -1; |
---|
| 296 | } |
---|
| 297 | friend bool operator<( const Cost lhs, const Cost rhs ); |
---|
| 298 | |
---|
| 299 | friend Cost operator+( const Cost lhs, const Cost rhs ); |
---|
| 300 | |
---|
| 301 | Cost operator+=( const Cost rhs ) { |
---|
| 302 | if ( all == infinity ) return *this; |
---|
| 303 | if ( rhs.all == infinity ) { |
---|
| 304 | all = infinity; |
---|
| 305 | return *this; |
---|
| 306 | } |
---|
[b10c39a0] | 307 | all += rhs.all - correctw; // correct for negative spec cost |
---|
[c378e5e] | 308 | return *this; |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | Cost incUnsafe( int inc = 1 ) { |
---|
[b10c39a0] | 312 | if ( all != infinity ) { assert( v.unsafeCost + inc <= UCHAR_MAX ); v.unsafeCost += inc; } |
---|
[c378e5e] | 313 | return *this; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | Cost incPoly( int inc = 1 ) { |
---|
[b10c39a0] | 317 | if ( all != infinity ) { assert( v.polyCost + inc <= UCHAR_MAX ); v.polyCost += inc; } |
---|
[c378e5e] | 318 | return *this; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | Cost incSafe( int inc = 1 ) { |
---|
[b10c39a0] | 322 | if ( all != infinity ) { assert( v.safeCost + inc <= UCHAR_MAX ); v.safeCost += inc; } |
---|
[c378e5e] | 323 | return *this; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | Cost incSign( int inc = 1 ) { |
---|
[b10c39a0] | 327 | if ( all != infinity ) { assert( v.signCost + inc <= UCHAR_MAX ); v.signCost += inc; } |
---|
[c378e5e] | 328 | return *this; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | Cost incVar( int inc = 1 ) { |
---|
[b10c39a0] | 332 | if ( all != infinity ) { assert( v.varCost + inc <= UCHAR_MAX ); v.varCost += inc; } |
---|
[c378e5e] | 333 | return *this; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | Cost decSpec( int dec = 1 ) { |
---|
[b10c39a0] | 337 | if ( all != infinity ) { assert( v.specCost - dec >= 0 ); v.specCost -= dec; } |
---|
[c378e5e] | 338 | return *this; |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | Cost incReference( int inc = 1 ) { |
---|
[b10c39a0] | 342 | if ( all != infinity ) { assert( v.referenceCost + inc <= UCHAR_MAX ); v.referenceCost += inc; } |
---|
[c378e5e] | 343 | return *this; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | friend std::ostream & operator<<( std::ostream & os, const Cost cost ); |
---|
| 347 | }; |
---|
| 348 | |
---|
| 349 | inline bool operator==( const Cost lhs, const Cost rhs ) { |
---|
| 350 | return lhs.all == rhs.all; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | inline bool operator!=( const Cost lhs, const Cost rhs ) { |
---|
| 354 | return !( lhs.all == rhs.all ); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | inline bool operator<( const Cost lhs, const Cost rhs ) { |
---|
| 358 | if ( lhs.all == Cost::infinity ) return false; |
---|
| 359 | if ( rhs.all == Cost::infinity ) return true; |
---|
| 360 | return lhs.all < rhs.all; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | inline Cost operator+( const Cost lhs, const Cost rhs ) { |
---|
| 364 | if ( lhs.all == Cost::infinity || rhs.all == Cost::infinity ) return Cost{ Cost::infinity }; |
---|
[b10c39a0] | 365 | return Cost{ lhs.all + rhs.all - Cost::correctw }; // correct for negative spec cost |
---|
[c378e5e] | 366 | } |
---|
| 367 | |
---|
| 368 | inline std::ostream & operator<<( std::ostream & os, const Cost cost ) { |
---|
| 369 | return os << "( " << cost.get_unsafeCost() << ", " << cost.get_polyCost() << ", " << cost.get_safeCost() |
---|
| 370 | << ", " << cost.get_signCost() << ", " << cost.get_varCost() << ", " << cost.get_specCost() |
---|
| 371 | << ", " << cost.get_referenceCost() << " )"; |
---|
| 372 | } |
---|
[b10c39a0] | 373 | #endif // 0 |
---|
[51b7345] | 374 | } // namespace ResolvExpr |
---|
| 375 | |
---|
[a32b204] | 376 | // Local Variables: // |
---|
| 377 | // tab-width: 4 // |
---|
| 378 | // mode: c++ // |
---|
| 379 | // compile-command: "make install" // |
---|
| 380 | // End: // |
---|