void search(int n,i64 vec[MAXN],int step,int now,i64 mymask){ i64 newmask; if (step<0){ max=now; return; } if (mymask&(((i64)1)<<step)){ newmask=mymask&vec[step]; if (now+1+numbits(newmask)>max) search(n,vec,step-1,now+1,newmask); } newmask=mymask&~(((i64)1)<<step); if (now+numbits(newmask)>max) search(n,vec,step-1,now,newmask); }
static void emitnum(int n) { int bits = numbits(n); int marker; for(marker = 0; 1 << (MIN_SCALE+marker*MARK_SCALE) < n; marker++) ; /* emit 1*0 length marker */ emit(((1<<marker)-1) << 1, marker+1); if (bits) emit(n, bits); }
int newton(int num) // http://stackoverflow.com/questions/1623375/writing-your-own-square-root-function { if ( num <= 0 ) return -1; int x = pow(2, numbits(num)/2); int y; while (1) { y = floor((x + floor(num/x))/2); if ( y >= x ) return x; x = y; } }
void lz_output(struct lz_state *sp) { struct freq freq[16]; int i; int maxcode; int pixbits; accum = 0; accbptr = 32; bitptr = 0; outbytes = 0; lz_compress(sp); for(i = 0; i < 16; i++) { freq[i].sym = i; freq[i].count = 0; freq[i].code = i; } for(i = 0; i < sp->tokptr; i++) { if (sp->toks[i].type == LZ_PTR) continue; freq[sp->toks[i].u.data].count++; } printf("#define MIN_DELTA\t%d\n", MIN_DELTA); printf("#define MIN_RUN\t\t%d\n", MIN_RUN); printf("#define MAX_RUN\t\t%d\n", MAX_RUN); printf("#define BIT_SCALE\t%d\n", BIT_SCALE); printf("#define MARK_SCALE\t%d\n", MARK_SCALE); printf("#define MIN_SCALE\t%d\n", MIN_SCALE); // sort by frequency qsort(freq, 16, sizeof(*freq), freq_sort_count); printf("static const unsigned char tokmap[] FLASH = {\n\t"); // map symbols to output codes for(maxcode = i = 0; i < 16; i++) { freq[i].code = i; if (freq[i].count != 0) maxcode = i; } for(i = 0; i <= maxcode; i++) printf("%d, ", freq[i].sym); printf("\n};\n"); pixbits = numbits(maxcode); #ifdef FIXPIX printf("#define GETPIX() (LPM(&tokmap[getbits(%d)]))\n", pixbits); #else printf("#define GETPIX() (LPM(&tokmap[getnum()]))\n"); #endif // index by symbols again qsort(freq, 16, sizeof(*freq), freq_sort_sym); printf("static const unsigned char lz_data[] FLASH = {\n\t"); for(i = 0; i < sp->tokptr; i++) { struct lztok *tp = &sp->toks[i]; tp->offset = bitptr; switch (tp->type) { case LZ_PTR: { int delta = (bitptr - sp->toks[tp->u.ptr.offset].offset); emit(1, 1); /* Pointer */ assert(delta > 0); emitnum(delta); assert(tp->u.ptr.len >= MIN_RUN); assert(tp->u.ptr.len < MAX_RUN); emitnum(tp->u.ptr.len - MIN_RUN); break; } case LZ_DATA: emit(0, 1); /* Data */ #ifdef FIXPIX emit(freq[tp->u.data].code, pixbits); #else emitnum(freq[tp->u.data].code); #endif break; } } while(accbptr < 32) { printf("0x%02x,%s", (accum >> 24) & 0xff, (outbytes++ % 8) == 7 ? "\n\t":" "); accum <<= 8; accbptr += 8; } printf("\n};\n"); printf("/* %d tokens, %d bits (%d bytes) */\n", sp->tokptr, bitptr, outbytes); }
double sqaure_root_of(double value) { assert(value >= 1); double lo = 1.0; double hi = value; while( hi - lo > 0.00001) { double mid = lo + (hi - lo) / 2 ; std::cout << lo << "," << hi << "," << mid << std::endl; if( mid * mid - value > 0.00001) //this is the predictors we are using { hi = mid; } else { lo = mid; } } return lo; } x = 2^ceil(numbits(N)/2) loop: y = floor((x + floor(N/x))/2) if y >= x return x x = y