typedef unsigned long int word; typedef struct{word two; word one;} lump; static lump cook(word a, word b, word c) {lump w; w.two = a&b | a&c | b&c; w.one = a ^ b ^ c; return w;} static int gcount(word x) {int c=0; while(x){++c; x &= x-1;} return c;} int Gcount(word w) /* Ascribed to Gosper by Schroeppel */ {w -= (w & 0xaaaaaaaa)>>1; /* each 2bit field has count of its 1s */ w = (w & 0x33333333) + ((w>>2) & 0x33333333); /*ditto 4bit fields*/ w += w>>4; w &= 0x0f0f0f0f; /* 8 bit fields */ w += w>>8; w += w>>16; w &= 0xff; return w;} char T[256]; static void init(){int j; for(j=0; j<256; ++j) T[j] = gcount(j);} static int tcount(word w) {int c=0; while (w) {c += T[w&255]; w >>= 8;} return c;} static int dcount(word a, word b) {return gcount(a & b) + 64 - gcount(~a & ~b);} static int acount(word a[11]) {lump x = cook(a[0], a[1], a[2]), y = cook(a[3], a[4], a[5]), z = cook(a[6], a[7], a[8]), w2 = cook(x.two, y.two, z.two), w1 = cook(x.one, y.one, z.one), v = cook(a[9], a[10], w1.one), v2 = cook(w2.one, w1.two, v.two); return 4*dcount(w2.two, v2.two) + 2*tcount(v2.one) + tcount(v.one);} static int bcount(word a[11]) {word xtwo = a[0]&a[1] | a[0]&a[2] | a[1]&a[2], xone = a[0] ^ a[1] ^ a[2], ytwo = a[3]&a[4] | a[3]&a[5] | a[4]&a[5], yone = a[3] ^ a[4] ^ a[5], ztwo = a[6]&a[7] | a[6]&a[8] | a[7]&a[8], zone = a[6] ^ a[7] ^ a[8], w2two = xtwo&ytwo | xtwo&ztwo | ytwo&ztwo, w2one = xtwo ^ ytwo ^ ztwo, w1two = xone&yone | xone&zone | yone&zone, w1one = xone ^ yone ^ zone, vtwo = a[9]&a[10] | a[9]&w1one | a[10]&w1one, vone = a[9] ^ a[10] ^ w1one, v2two = w2one&w1two | w2one&vtwo | w1two&vtwo, v2one = w2one ^ w1two ^ vtwo; return 4*dcount(w2two, v2two) + 2*tcount(v2one) + tcount(vone);} #define pw 21 word t[pw][2]; static void IA(){int j; for(j=0; j #include static void chk(word v, int c) {if(tcount(v) != c || gcount(v) != c) printf("Failed for %0lx\n", v);} static void cka(word a[11]) {int k, s=0; for(k=0; k<11; ++k) s += gcount(a[k]); if(s != acount(a)) printf("Failed\n"); {int j=11; IA(); while(j--) yew(a[j]); if(s != report(-1)) printf("Loop failed. " "s= %d, report = %d\n", s, report(-1));} } #include clock_t start; static void Time(char * str, int count) {clock_t st = start; start = clock(); printf("%s = %d, %7.4f\n", str, count, (float)(start - st)/CLOCKS_PER_SEC);} #define pile 1000 int main(){init(); chk(-1, 64); chk(0,0); {word a[11] = {-1, 33, 65536, 0, -3, 1048576, 17, 512, 24, 0x70311004, -2}; cka(a); /* I count 32+2+1+0+30+1+2+1+2+8+31 = 110 bits. */} {word a[11] = {0,0,0,0,0,0,0,0,0,0,0}; cka(a);} {word a[11] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; cka(a);} {word ba[pile]; {int j=pile; while(j--) {word f = 0; int h=3; while(h--) f = (f<<20) ^ random(); ba[j] = f;}} {int j, s=0; for(j=0; j