Bootstrap

ARM base instruction -- cls

Count Leading Sign bits counts the number of leading bits of the source register that have the same value as the most significant bit of the register, and writes the result to the destination register. This count does not include the most significant bit of the source register.


计数前导符号位对源寄存器中与寄存器最高有效位具有相同值的前导位数进行计数,并将结果写入目标寄存器。此计数不包括源寄存器的最高有效位。


32-bit variant
    Applies when sf == 0.
    CLS <Wd>, <Wn>

64-bit variant
    Applies when sf == 1.
    CLS <Xd>, <Xn>

Decode for all variants of this encoding
 integer d = UInt(Rd);
 integer n = UInt(Rn);
 integer datasize = if sf == '1' then 64 else 32;


Operation
 integer result;
 bits(datasize) operand1 = X[n];
 
 result = CountLeadingSignBits(operand1);
 
 X[d] = result<datasize-1:0>;


 integer CountLeadingSignBits(bits(N) x)
     return CountLeadingZeroBits(x<N-1:1> EOR x<N-2:0>);

 integer CountLeadingZeroBits(bits(N) x)
     return N - (HighestSetBit(x) + 1);

 integer HighestSetBit(bits(N) x)
     for i = N-1 downto 0
         if x<i> == '1' then return i;
     return -1;

;