This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Count the number of trailing zeros without clz?

I am trying to count the number of trailing zeros in floating points without using the clz command. what would be the easiest way to rewrite this code?


ctz
        RSB      r1,r0,#0
        CMP      r0,#0
        AND      r0,r0,r1
        CLZ      r0,r0
        RSBNE    r0,r0,#0x1f
        BX    lr

hth

s.

Parents
  • If you use CLZ then using the RBIT instruction to reverse the order of bits first makes the job easy.

    Well there's lots of answers there though why you'd want to avoid the operation for the purpose I don't know. But it suggested a way to me to do CLZ using floating point that sure would obscure your code!

    Start with a 32 bit word that one wants to count trailing zeroes on

    Reverse the bits

    Load a double precision floating point register with 0 in the most significant word and the word to do CLZ on in the lower half

    Multiply by floating point 2 to the power of 52

    Copy the high half of the floating point number to a general register

    shift the word down by 20 to extract the exponent field and subtract it from 32

    which gives the answer with 32 if the source was zero

    And there you have it - a complete anti-answer to your question ;-)

Reply
  • If you use CLZ then using the RBIT instruction to reverse the order of bits first makes the job easy.

    Well there's lots of answers there though why you'd want to avoid the operation for the purpose I don't know. But it suggested a way to me to do CLZ using floating point that sure would obscure your code!

    Start with a 32 bit word that one wants to count trailing zeroes on

    Reverse the bits

    Load a double precision floating point register with 0 in the most significant word and the word to do CLZ on in the lower half

    Multiply by floating point 2 to the power of 52

    Copy the high half of the floating point number to a general register

    shift the word down by 20 to extract the exponent field and subtract it from 32

    which gives the answer with 32 if the source was zero

    And there you have it - a complete anti-answer to your question ;-)

Children
No data