Please note: We are aware of an issue affecting replies on the Arm Community forums, which may not be loading as expected.

We apologize for any inconvenience and appreciate your patience while we investigate and work to resolve the issue.

Thank you for your understanding.


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 this is a workable CM0 assembler version

    pop32	PROC
    		EXPORT pop32
    		ldr 	r1, =0x55555555
    		lsrs    r2, r0, #1
    		ands 	r1, r2
    		subs	r0, r1
    
    		ldr		r1, =0x33333333
    		lsrs    r2, r0, #2
    		ands	r0, r1
    		ands	r2, r1
    		adds	r0, r2
    		
    		lsrs	r1, r0, #4
    		ldr     r2, =0x0F0F0F0F
    		adds	r0, r1
    		ands	r0, r2
    		
    		lsrs	r1, r0, #8
    		adds	r0, r1
    		lsrs	r1, r0, #16
    		adds	r0, r1
    		
            movs	r1, #0x3f
    		ands	r0, r1
    		bx		lr
    		ENDP
    

Reply
  • I think this is a workable CM0 assembler version

    pop32	PROC
    		EXPORT pop32
    		ldr 	r1, =0x55555555
    		lsrs    r2, r0, #1
    		ands 	r1, r2
    		subs	r0, r1
    
    		ldr		r1, =0x33333333
    		lsrs    r2, r0, #2
    		ands	r0, r1
    		ands	r2, r1
    		adds	r0, r2
    		
    		lsrs	r1, r0, #4
    		ldr     r2, =0x0F0F0F0F
    		adds	r0, r1
    		ands	r0, r2
    		
    		lsrs	r1, r0, #8
    		adds	r0, r1
    		lsrs	r1, r0, #16
    		adds	r0, r1
    		
            movs	r1, #0x3f
    		ands	r0, r1
    		bx		lr
    		ENDP
    

Children
No data