How can CLZ equivalent be achieved on Cortex-M0 where this instruction is missing?

Looking for alternates for this instruction.

Parents
  • Hello,

    In CMSIS, Arm_Math.h, there is defined an alternative which you may use.  There are some #defines around it, so if you're having trouble linking it, make sure you have the correct definitions, or just copy and paste __CLZ into your code somewhere.

      static __INLINE uint32_t __CLZ(
      q31_t data)
      {
        uint32_t count = 0;
        uint32_t mask = 0x80000000;
    
        while((data & mask) == 0)
        {
          count += 1u;
          mask = mask >> 1u;
        }
    
        return (count);
    
      }
    

    Cheers,

    Dan

Reply
  • Hello,

    In CMSIS, Arm_Math.h, there is defined an alternative which you may use.  There are some #defines around it, so if you're having trouble linking it, make sure you have the correct definitions, or just copy and paste __CLZ into your code somewhere.

      static __INLINE uint32_t __CLZ(
      q31_t data)
      {
        uint32_t count = 0;
        uint32_t mask = 0x80000000;
    
        while((data & mask) == 0)
        {
          count += 1u;
          mask = mask >> 1u;
        }
    
        return (count);
    
      }
    

    Cheers,

    Dan

Children