       (F_AP_BITS_PER_WORD-msb-1);
      a.get_pVal(msb_word) |= mask;
   } else {
      const uint64_t lsb_mask = ~0ULL >> (lsb) << (lsb);
      const uint64_t msb_mask = ~0ULL << (F_AP_BITS_PER_WORD-msb-1) >>
                                (F_AP_BITS_PER_WORD-msb-1);

      a.get_pVal(lsb_word) |= lsb_mask;
      for (int i=lsb_word+1; i<msb_word; i++) {
         a.set_pVal(i, ~0ULL);
      }
      a.get_pVal(msb_word) |= msb_mask;
   }
   a.clearUnusedBits();
}

// *****************************************************************************
// @brief clear all bits from mark1 to mark2 of the APBase object
// *****************************************************************************
inline void clear(APBase &a, const int mark1, const int mark2)
{
   const uint16_t lsb_word = mark2 /F_AP_BITS_PER_WORD;
   const uint16_t msb_word = mark1 / F_AP_BITS_PER_WORD;
   const uint16_t msb = mark1 % F_AP_BITS_PER_WORD;
   const uint16_t lsb = mark2 % F_AP_BITS_PER_WORD;

   if (msb_word == lsb_word) {
      const uint64_t mask = ~(~0ULL >> (lsb)<<(F_AP_BITS_PER_WORD-msb+lsb-1)>>
                              (F_AP_BITS_PER_WORD-msb-1));
      a.get_pVal(msb_word) &= mask;
   } else {
      const uint64_t lsb_mask = ~(~0ULL >> (lsb) << (lsb));
      const uint64_t msb_mask = ~(~0ULL << (F_AP_BITS_PER_WORD-msb-1)>>
                                  (F_AP_BITS_PER_WORD-msb-1));
      a.get_pVal(lsb_word) &=lsb_mask;
      for (int i=lsb_word+1; i<msb_word; i++) {
         a.get_pVal(i)=0;
      }
      a.get_pVal(msb_word) &= msb_mask;
   }
   a.clearUnusedBits();
}

// *****************************************************************************
// @brief set index-th bit of the APBase object
// *****************************************************************************
inline void set(APBase &a, const int index)
{
   uint16_t word = index/F_AP_BITS_PER_WORD;
   const uint64_t mask=1ULL << (index%F_AP_BITS_PER_WORD);
   a.get_pVal(word) |= mask;
   a.clearUnusedBits();
}

// *****************************************************************************
// @brief clear index-th bit of the APBase object
// *****************************************************************************
inline void clear(APBase &a, const int index)
{
   uint16_t word = index/F_AP_BITS_PER_WORD;
   const uint64_t mask=~(1ULL << (index%F_AP_BITS_PER_WORD));
   a.get_pVal(word) &= mask;
   a.clearUnusedBits();
}

} // namespace utilities
} // namespace hls

#endif // HLS_APBASE_UTIL_H


