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.
Hi willwow11,
jensbauer has a document A fairly quick Count Leading Zeroes for Cortex-M0. Read the section Trailer to use it for counting trailing zeroes.
This can help you too, Bit Twiddling Hacks. daith is also peddling this web page .
Regards,
Goodwin
.
In particular, you can change the following lines from the CLZ example:
value = data >> shift; // MOV Rv,Rd | // LSRS Rv,Rs |
to perform CTZ instead:
value = data << shift; // MOV Rv,Rd | // LSLS Rv,Rs |
Simon.
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 ;-)