Count Leading Zeros

Medium Assembly Optimization Asked at Qualcomm
Solve this problem

Count the leading zeros of a 32-bit register without using the CLZ instruction, using a binary search so the cost is fixed at five tests.

Constraints

  • Input in r0, result in r0.
  • Must return 32 for an input of zero.
  • Do not use the CLZ instruction.
  • Must be branch-light: at most 5 conditional steps.
  • Preserve all registers except r0 to r3.
  • Thumb-2 or ARM syntax both acceptable.
Starter codeAssembly
1.global count_leading_zeros2count_leading_zeros:3    @ TODO4    bx lr