// One's complement add with register slices. #include #include typedef unsigned int ui; typedef unsigned char uc; void ps(uc x[8], char * m){int j=8; while(j--) printf("%01x", 15&x[j]); printf(" %s\n", m);} ui rn(){return (random()<<4)^random();} // 32 random bits ui add(ui a, ui b){uc pa[8], pb[8], sl[8], sh[8]; {int j=8; while(j--) {pa[j] = 15 & (a>>(4*j)); pb[j] = 15 & (b>>(4*j));}} if(0){ps(pa, "pb"); printf("%08x\n", a); ps(pb, "pb"); printf("%08x\n", b);} // We have divided both of our arguments into 4 bit slices. // Next for each slice compute two 4 bit sums: {int j=8; while(j--) {sl[j] = pa[j]+pb[j]; sh[j] = pa[j]+pb[j]+1;}} if(0) ps(sl, "sl"); {char P=0, G=0, C=0; // Next compute P and G {int j=8; while(j--) {P = (P<<1) | sh[j]>>4; G = (G<<1) | sl[j]>>4;}} // Now compute C {int i=8; while(i--) {char b=0; {int j=8; while(j--) if(i!=j) { {char d=(G>>j) & 1; {int k=8; while(k--) { if(i>k) & 1;}} b |= d;}}} C = (C<<1) | b;}} if(0) printf("P=%02x G=%02x C=%02x\n", P, G, C); {ui ans = 0; {int j = 8; while(j--) ans |= (15&((C&(1<~b); if(soc != s) printf("%d %08x %08x %08x %08x\n", j, a, b, soc, s);} return 0;}