Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Cost.h

    rb10c39a0 rc378e5e  
    1010// Created On       : Sun May 17 09:39:50 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Apr 29 18:33:44 2019
    13 // Update Count     : 49
     12// Last Modified On : Sun Apr 28 18:26:36 2019
     13// Update Count     : 29
    1414//
    1515
     
    1818#include <iostream>
    1919#include <cassert>
    20 #include <climits>
    2120
    2221namespace ResolvExpr {
     
    217216                          << cost.referenceCost << " )";
    218217        }
    219 
    220 #else
     218#endif // 0
    221219
    222220        //*************************** NEW ***************************
    223 
    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.
    226221
    227222        class Cost {
     
    239234                                unsigned char unsafeCost;                               ///< Unsafe (narrowing) conversions
    240235                        #else
    241                                 #error Cost BIG_ENDIAN unsupported
     236                                #error BIG_ENDIAN unsupported
    242237                        #endif
    243238                        } v;
    244239                        uint64_t all;
    245240                };
    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
    248241          public:
    249242                // Compiler adjusts constants for correct endian.
    250243                enum : uint64_t {
    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                 }; //'
     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                }; //
    261255
    262256                Cost( uint64_t all ) { Cost::all = all; }
     
    264258                        // Assume little-endian => first value is low priority and last is high priority.
    265259                        v = {
    266                         #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
    267260                                (unsigned char)0,                                               // padding
    268                                 (unsigned char)referenceCost,                   // low priority
    269                                 (unsigned char)(specCost + correctb),   // correct for signedness
     261                                (unsigned char)referenceCost, 
     262                                (unsigned char)(specCost + 0xff ),              // correct for signedness
    270263                                (unsigned char)varCost,
    271264                                (unsigned char)signCost,
    272265                                (unsigned char)safeCost,
    273266                                (unsigned char)polyCost,
    274                                 (unsigned char)unsafeCost,                              // high priority
    275                         #else
    276                                 #error Cost BIG_ENDIAN unsupported
    277                         #endif
     267                                (unsigned char)unsafeCost,
    278268                        };
    279269                }
     
    284274                int get_signCost() const { return v.signCost; }
    285275                int get_varCost() const { return v.varCost; }
    286                 int get_specCost() const { return -(correctb - v.specCost); }
     276                int get_specCost() const { return -(0xff - v.specCost); }
    287277                int get_referenceCost() const { return v.referenceCost; }
    288278
     
    305295                                return *this;
    306296                        }
    307                         all += rhs.all - correctw;                                      // correct for negative spec cost
     297                        all += rhs.all - correction;                            // correct for negative spec cost
    308298                        return *this;
    309299                }
    310300
    311301                Cost incUnsafe( int inc = 1 ) {
    312                         if ( all != infinity ) { assert( v.unsafeCost + inc <= UCHAR_MAX ); v.unsafeCost += inc; }
     302                        if ( all != infinity ) { assert( v.unsafeCost + inc < 256 ); v.unsafeCost += inc; }
    313303                        return *this;
    314304                }
    315305
    316306                Cost incPoly( int inc = 1 ) {
    317                         if ( all != infinity ) { assert( v.polyCost + inc <= UCHAR_MAX ); v.polyCost += inc; }
     307                        if ( all != infinity ) { assert( v.polyCost + inc < 256 ); v.polyCost += inc; }
    318308                        return *this;
    319309                }
    320310
    321311                Cost incSafe( int inc = 1 ) {
    322                         if ( all != infinity ) { assert( v.safeCost + inc <= UCHAR_MAX ); v.safeCost += inc; }
     312                        if ( all != infinity ) { assert( v.safeCost + inc < 256 ); v.safeCost += inc; }
    323313                        return *this;
    324314                }
    325315
    326316                Cost incSign( int inc = 1 ) {
    327                         if ( all != infinity ) { assert( v.signCost + inc <= UCHAR_MAX ); v.signCost += inc; }
     317                        if ( all != infinity ) { assert( v.signCost + inc < 256 ); v.signCost += inc; }
    328318                        return *this;
    329319                }
    330320
    331321                Cost incVar( int inc = 1 ) {
    332                         if ( all != infinity ) { assert( v.varCost + inc <= UCHAR_MAX ); v.varCost += inc; }
     322                        if ( all != infinity ) { assert( v.varCost + inc < 256 ); v.varCost += inc; }
    333323                        return *this;
    334324                }
    335325
    336326                Cost decSpec( int dec = 1 ) {
    337                         if ( all != infinity ) { assert( v.specCost - dec >= 0 ); v.specCost -= dec; }
     327                        if ( all != infinity ) { v.specCost -= dec; }
    338328                        return *this;
    339329                }
    340330
    341331                Cost incReference( int inc = 1 ) {
    342                         if ( all != infinity ) { assert( v.referenceCost + inc <= UCHAR_MAX ); v.referenceCost += inc; }
     332                        if ( all != infinity ) { assert( v.referenceCost + inc < 256 ); v.referenceCost += inc; }
    343333                        return *this;
    344334                }
     
    363353        inline Cost operator+( const Cost lhs, const Cost rhs ) {
    364354                if ( lhs.all == Cost::infinity || rhs.all == Cost::infinity ) return Cost{ Cost::infinity };
    365                 return Cost{ lhs.all + rhs.all - Cost::correctw }; // correct for negative spec cost
     355                return Cost{ lhs.all + rhs.all - Cost::correction }; // correct for negative spec cost
    366356        }
    367357
     
    371361                                  << ", " << cost.get_referenceCost() << " )";
    372362        }
    373 #endif // 0
    374363} // namespace ResolvExpr
    375364
Note: See TracChangeset for help on using the changeset viewer.