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

Counting modified bits in a 32bit data word

I have to count modified bits in a volatile 32 bit data word.

Example:
Initial counter32[32] := 0; olddata := 0x00000010; newdata := 0x0000000001;

So we have moddata := newdata xor olddata => moddata := 00000011.

So bit0 and bit1 have changed.
Now I would have counter[1]++ and counter[0]++;

How this can be done on a Cortex-M0+ fastes way?

Best regards Juergen

Parents
  • I think something like this.
    But this is tooo slow for our needs and I have no knowledge of ARM Assembler...
    MCU is a STM32G0 with Cortex-M0+.

    uint32_t counter32[32];
    olddata = 0x00000001U;
    newdata = 0x00000002U;
    
    void CountBits(uint32_t old, uint32_t new)
    {
        uint32_t x;
        uint32_t mask = 0x00000001U;
        int      i;
    
        x = old ^ new;			// x == 0x00000003
        for (i = 0; i < 32; i++)
        {
            if (x & mask)
                counter[i]++;
            mask <<= 1;
        }        
    }
    
    
    void main (void)
    {
        CountBits (olddata, newdata);
    }

Reply
  • I think something like this.
    But this is tooo slow for our needs and I have no knowledge of ARM Assembler...
    MCU is a STM32G0 with Cortex-M0+.

    uint32_t counter32[32];
    olddata = 0x00000001U;
    newdata = 0x00000002U;
    
    void CountBits(uint32_t old, uint32_t new)
    {
        uint32_t x;
        uint32_t mask = 0x00000001U;
        int      i;
    
        x = old ^ new;			// x == 0x00000003
        for (i = 0; i < 32; i++)
        {
            if (x & mask)
                counter[i]++;
            mask <<= 1;
        }        
    }
    
    
    void main (void)
    {
        CountBits (olddata, newdata);
    }

Children
No data