Ignore:
Timestamp:
Jul 30, 2015, 12:30:21 PM (4 years ago)
Author:
cameron
Message:

Move add-with-carry into carry manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/pablo/carry_manager.cpp

    r4707 r4708  
    256256}
    257257
     258   
    258259/* maskSelectBitRange selects the bits of a pack from lo_bit through
    259  lo_bit + bitCount - 1, setting all other bits to zero.  */
    260 
     260   lo_bit + bitCount - 1, setting all other bits to zero.  */
     261   
    261262Value * CarryManager::maskSelectBitRange(Value * pack, unsigned lo_bit, unsigned bitCount) {
    262263    if (bitCount == PACK_SIZE) {
     
    267268    return mBuilder->CreateAnd(pack, mBuilder->getInt64(mask << lo_bit));
    268269}
    269 
     270   
    270271Value * CarryManager::getCarryInBits(unsigned carryBitPos, unsigned carryBitCount) {
    271272    unsigned packIndex = carryBitPos / PACK_SIZE;
     
    277278
    278279void CarryManager::extractAndSaveCarryOutBits(Value * bitblock, unsigned carryBit_pos, unsigned carryBitCount) {
    279    
    280280    unsigned packIndex = carryBit_pos / PACK_SIZE;
    281281    unsigned packOffset = carryBit_pos % PACK_SIZE;
     
    301301}
    302302   
    303     /*  NOTE: In the following the mCarryOutPack is an accumulator.
    304     It must be created at the appropriate outer level.  */
    305 void CarryManager::setCarryBits(unsigned carryBit_lo, unsigned carryRangeSize, Value * bits) {
    306    
    307     unsigned packIndex = carryBit_lo / PACK_SIZE;
    308     unsigned carryOffset = carryBit_lo % PACK_SIZE;
    309     if (carryOffset > 0) {
    310         bits = mBuilder->CreateShl(bits, mBuilder->getInt64(carryOffset));
    311     }
    312     if (mCarryOutPack[packIndex] == nullptr) {
    313         mCarryOutPack[packIndex] = bits;
    314         //std::cerr << "setCarryBits/initial , pack = " << packIndex << ", offset = " << carryOffset << ", count = " << carryRangeSize << std::endl;
    315     }
    316     else {
    317         //std::cerr << "setCarryBits/combine , pack = " << packIndex << ", offset = " << carryOffset << ", count = " << carryRangeSize << std::endl;
    318         mCarryOutPack[packIndex] = mBuilder->CreateOr(mCarryOutPack[packIndex], bits);
    319     }
    320 }
    321    
    322303   
    323304/* Methods for getting and setting individual carry values. */
     
    336317    unsigned posn = carryOpPosition(localIndex);
    337318#ifdef PACKING
    338     Value * field = iBuilder->mvmd_extract(PACK_SIZE, carry_out_strm, 0);
    339     //setCarryBits(posn, mBuilder->CreateLShr(field, mBuilder->getInt64(PACK_SIZE - 1)));
    340     setCarryBits(posn, 1, field);
    341 #else
    342     mCarryOutPack[posn] = carry_out_strm;
     319    extractAndSaveCarryOutBits(carry_out_strm, posn, 1);
     320#else
     321    Value * carry_bit = mBuilder->CreateLShr(mBuilder->CreateBitCast(carry_out_strm, mBuilder->getIntNTy(BLOCK_SIZE)), 127);
     322    mCarryOutPack[posn] = mBuilder->CreateBitCast(carry_bit, mBitBlockType);
    343323    if (mCarryInfo->getWhileDepth() == 0) {
    344324        storeCarryPack(posn);
     
    347327}
    348328
    349    
     329Value* CarryManager::genShiftLeft64(Value* e) {
     330    Value* i128_val = mBuilder->CreateBitCast(e, mBuilder->getIntNTy(BLOCK_SIZE));
     331    return mBuilder->CreateBitCast(mBuilder->CreateShl(i128_val, 64), mBitBlockType);
     332}
     333
     334Value * CarryManager::addCarryInCarryOut(int localIndex, Value* e1, Value* e2) {
     335    Value * carryq_value = getCarryOpCarryIn(localIndex);
     336    #if (BLOCK_SIZE == 128)
     337    //calculate carry through logical ops
     338    Value* carrygen = mBuilder->CreateAnd(e1, e2, "carrygen");
     339    Value* carryprop = mBuilder->CreateOr(e1, e2, "carryprop");
     340    Value* digitsum = mBuilder->CreateAdd(e1, e2, "digitsum");
     341    Value* partial = mBuilder->CreateAdd(digitsum, carryq_value, "partial");
     342    Value* digitcarry = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(partial)));
     343    Value* mid_carry_in = genShiftLeft64(mBuilder->CreateLShr(digitcarry, 63));
     344    Value* sum = mBuilder->CreateAdd(partial, mBuilder->CreateBitCast(mid_carry_in, mBitBlockType), "sum");
     345    Value* carry_out_strm = mBuilder->CreateOr(carrygen, mBuilder->CreateAnd(carryprop, mBuilder->CreateNot(sum)));
     346
     347    #else
     348    //BLOCK_SIZE == 256, there is no other implementation
     349    static_assert(false, "Add with carry for 256-bit bitblock requires USE_UADD_OVERFLOW");
     350    #endif         
     351    setCarryOpCarryOut(localIndex, carry_out_strm);
     352    return sum;
     353}
     354
     355
    350356Value * CarryManager::advanceCarryInCarryOut(int localIndex, int shift_amount, Value * strm) {
    351357    if (shift_amount == 1) {
Note: See TracChangeset for help on using the changeset viewer.