isa: martins sample
[calu.git] / 2_isa / src / bittwiddling.c
1
2 #define B0 (0x55555555)
3 #define B1 (0x33333333)
4 #define B2 (0x0F0F0F0F)
5 #define B3 (0x001F001F)
6 #define B4 (0x0000003F)
7
8 int countbits(int x) {
9         x = (x & B0) + ((x >>  1) & B0);
10         x = (x & B1) + ((x >>  2) & B1);
11
12         x = (x + (x >>  4)) & B2;
13         x = (x + (x >>  8)) /*& B3 */
14         x = (x + (x >> 16)) & B4;
15         return x;
16 }
17
18 int computeparity(int x) 
19         x ^= x >> 16;
20         x ^= x >> 8;
21         x ^= x >> 4;
22         x &= 0xf;
23         return (0x6996 >> x) & 1;
24 }
25
26 void main(void) {
27         countbits(B0);
28         computeparity(B0);
29         computeparity(B0+1);
30 }