Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <memory.h>
- #include <stdint.h>
- #include <math.h>
- #include <time.h>
- #include <sys/time.h>
- #include <assert.h>
- double dtime() {
- struct timeval mytime;
- gettimeofday(&mytime, (struct timezone *)0);
- return (double)(mytime.tv_sec + mytime.tv_usec*1e-6);
- }
- unsigned char tab0[256*8], tab1[256*8], tab2[256*8], tab3[256*8], tab4[256*8], tab5[256*8];
- unsigned char n[256];
- int16_t tabofs[256];
- #define COPY(d, s, n) \
- switch(n) { \
- case 8: *(d++) = *(s++); \
- case 7: *(d++) = *(s++); \
- case 6: *(d++) = *(s++); \
- case 5: *(d++) = *(s++); \
- case 4: *(d++) = *(s++); \
- case 3: *(d++) = *(s++); \
- case 2: *(d++) = *(s++); \
- case 1: *(d++) = *(s++); \
- case 0: break; \
- }
- // templated so we can call aggressively unrolled kernels (eventually)
- template<unsigned int N, unsigned int K> int enumerate(void) {
- int64_t x, res = 0, tmp, lb, hb, i;
- int64_t end = (1UL << N);
- unsigned char idx[K];
- x = (1UL << K)-1;
- while( x < end ) {
- unsigned char *dst=idx, *src;
- int64_t b, t, c, m, r,z;
- // get the positions of the set bits
- unsigned char zero, one, two, three, four, five;
- zero = x & 0x0000000000FFUL;
- one = (x & 0x00000000FF00UL) >> 8;
- two = (x & 0x000000FF0000UL) >> 16;
- three = (x & 0x0000FF000000UL) >> 24;
- four = (x & 0x00FF00000000UL) >> 32;
- five = (x & 0xFF0000000000UL) >> 40;
- src=tab0+tabofs[zero ]; COPY(dst, src, n[zero ]);
- src=tab1+tabofs[one ]; COPY(dst, src, n[one ]);
- src=tab2+tabofs[two ]; COPY(dst, src, n[two ]);
- src=tab3+tabofs[three]; COPY(dst, src, n[three]);
- src=tab4+tabofs[four ]; COPY(dst, src, n[four ]);
- src=tab5+tabofs[five ]; COPY(dst, src, n[five ]);
- // TODO: add code to actually use idx here :)
- // advance to next integer with K bits set in N
- b = x & -x;
- t = x + b;
- c = x^t;
- z = __builtin_ctz(x);
- m = c >> 2+z;
- x = t|m;
- // add a model
- res++;
- }
- return res;
- }
- int main(void) {
- // set up the tables
- int ofs=0;
- for(int i=0; i<256; i++) {
- unsigned char tmp=i, lb;
- n[i] = 0;
- tabofs[i] = ofs;
- while( tmp ) {
- tab0[ofs] = __builtin_ctz(tmp);
- tab1[ofs] = tab0[ofs]+8;
- tab2[ofs] = tab0[ofs]+16;
- tab3[ofs] = tab0[ofs]+24;
- tab4[ofs] = tab0[ofs]+32;
- tab5[ofs] = tab0[ofs]+40;
- ofs++;
- lb = tmp & -tmp;
- tmp ^= lb;
- n[i]++;
- }
- }
- double start = dtime();
- int64_t n = enumerate<40,10>(); // get the indices of all numbers with 10 out of 40 bits set
- printf("%i models took %.2f seconds\n", n, (dtime()-start));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement