--- /dev/null
+
+#define B0 (0x55555555)
+#define B1 (0x33333333)
+#define B2 (0x0F0F0F0F)
+#define B3 (0x001F001F)
+#define B4 (0x0000003F)
+
+int countbits(int x) {
+ x = (x & B0) + ((x >> 1) & B0);
+ x = (x & B1) + ((x >> 2) & B1);
+
+ x = (x + (x >> 4)) & B2;
+ x = (x + (x >> 8)) /*& B3 */
+ x = (x + (x >> 16)) & B4;
+ return x;
+}
+
+int computeparity(int x)
+ x ^= x >> 16;
+ x ^= x >> 8;
+ x ^= x >> 4;
+ x &= 0xf;
+ return (0x6996 >> x) & 1;
+}
+
+void main(void) {
+ countbits(B0);
+ computeparity(B0);
+ computeparity(B0+1);
+}
--- /dev/null
+
+.data
+.align 32
+;#define B0 (0x55555555)
+B0:
+.dw 0x55555555
+;#define B1 (0x33333333)
+B1:
+.dw 0x33333333
+;#define B2 (0x0F0F0F0F)
+B2:
+.dw 0x0F0F0F0F
+;#define B3 (0x001F001F)
+B3:
+.dw 0x001F001F
+;#define B4 (0x0000003F)
+B4:
+.dw 0x0000003F
+
+.text
+
+;int countbits(int x) {
+countbits:
+ ;x == r1
+
+ ;load address of B0 into r3
+ ldil r3,low(B0);
+ ldih r3,high(B0);
+
+ ;x = (x & B0) + ((x >> 1) & B0);
+ ;r0 = (x >> 1)
+ lrs r0,r1,1;
+
+ ;load B0 into r2
+ ldx r2, r3;
+
+ ;(x >> 1) & B0
+ and r0, r0, r2;
+
+ ;x & B0
+ and r1, r1, r2;
+
+ ;x = (x & B0) + ((x >> 1) & B0);
+ add r0, r0, r1;
+
+ ;load B1 into r2, use displacment for address of B1 (or add?)
+ ldx r2, (r3)(B1-B0);
+
+ ;x = (x & B1) + ((x >> 2) & B1);
+ lrs r1, r0, 2;
+ and r1, r1, r2;
+
+ and r0, r0, r2;
+
+ add r0, r0, r1;
+
+ ;x = (x + (x >> 4)) & B2;
+ ldx r2, (r3)(B2-B0);
+
+ lrs r1, r0, 4;
+ and r1, r1, r2;
+ and r0, r0, r2;
+
+ add r0, r0, r1;
+
+ ;x = (x + (x >> 8)) /*& B3 */
+ lrs r1, r0, 8;
+ add r0, r0, r1;
+
+ ldx r2, (r3)(B4-B0);
+ ;x = (x + (x >> 16)) & B4;
+ lrs r1, r0, 16;
+ add r0, r0, r1;
+ and r0, r0, r2;
+
+ ;return x;
+ ret;
+;}
+
+int computeparity(int x)
+ x ^= x >> 16;
+ x ^= x >> 8;
+ x ^= x >> 4;
+ x &= 0xf;
+ return (0x6996 >> x) & 1;
+}
+
+void main(void) {
+ countbits(B0);
+ computeparity(B0);
+ computeparity(B0+1);
+}