How to "optimize" self-defined 32bit shift-left routine ?

Dear all,
I encountered one problem about "executing self-defined 32bit shift-left routine too long"...

That is, I built my own routine to execute 32-bit shift left operation and found it takes too long(Due to some reason 32bit shift left is NOT allowed in my project currently...)

[code body]

UINT32 ShiftLeft32Bits(UINT32 Operand, UINT8 ShiftValue){
        bit     Carry;
        OperandDW = Operand;
        while(ShiftValue>0){
                Carry=0;
                if(OperandDWLo&0x8000)
                        Carry=1;
                OperandDWLo<<=1;
                OperandDWHi = (OperandDWHi<<1) | Carry;
                ShiftValue-=1;
        }
        return OperandDW;
}

[Statistics]
if "Operand = ShiftLeft32Bits(x1, 31);"
=> cost 60us ( where Operand,x1 are 4-byte xdata variables)

My question is: is it possible to "optimize" above code segments(favor speed) ? Thanks in advance...

p.s I am implementing sha256 calculation...

Parents
  • (Due to some reason 32bit shift left is NOT allowed in my project currently...)

    Why wouldn't

    operand = operand << shift_value;
    


    be "NOT allowed"? That doesn't make sense. The shift operator is a basic component of the C language.

    Anyway. If you want to "roll your own" shift routine, here are my two cents:

    1. Write it in assembly. That way, at least you have direct access to the carry flag.

    and/or

    2. If you have a general idea of the distribution of values of shift_value, you might be able to reduce the average time consumed by the shift routine by doing byte-wise shifts first as long as shift_value is larger than 7. A left-shift of 20 bits would therefore be composed of two byte-wise left shifts and four single-bit left shifts.

Reply
  • (Due to some reason 32bit shift left is NOT allowed in my project currently...)

    Why wouldn't

    operand = operand << shift_value;
    


    be "NOT allowed"? That doesn't make sense. The shift operator is a basic component of the C language.

    Anyway. If you want to "roll your own" shift routine, here are my two cents:

    1. Write it in assembly. That way, at least you have direct access to the carry flag.

    and/or

    2. If you have a general idea of the distribution of values of shift_value, you might be able to reduce the average time consumed by the shift routine by doing byte-wise shifts first as long as shift_value is larger than 7. A left-shift of 20 bits would therefore be composed of two byte-wise left shifts and four single-bit left shifts.

Children
More questions in this forum