static int fun_C(int *bitfill, int gkw) { static int tab1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }; static int tab[] = { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19 }; unsigned int tmp; if (gkw == 0xfe) { if (nbits(bitfill, 1) == 0) return 7; else return -8; } if (gkw != 0xff) return 0xff; tmp = nbits(bitfill, 7) - 72; if (tmp > 43) return 0xff; refill(bitfill); return tab[tab1[tmp]]; }
static int fun_D(int *bitfill, int gkw) { if (gkw == 0xfd) { if (nbits(bitfill, 1) == 0) return 12; return -13; } if (gkw == 0xfc) { if (nbits(bitfill, 1) == 0) return 13; return -14; } if (gkw == 0xfe) { switch (nbits(bitfill, 2)) { case 0: return 14; case 1: return -15; case 2: return 15; case 3: return -16; } } if (gkw == 0xff) { switch (nbits(bitfill, 3)) { case 4: return 16; case 5: return -17; case 6: return 17; case 7: return -18; case 2: return _nbits(bitfill, 1) ? 0xed : 0x12; case 3: (*bitfill)--; return 18; } return 0xff; } return gkw; }
static ulong ildm(Ureg *ur, Instr i) { if((i & BIT(15)) == 0) return ur->pc+4; return *(multiaddr(ur, i) + nbits(i & BITS(15, 0))); }
static int fun_A(int *bitfill) { int ret; static int tab[] = { 12, 13, 14, 15, 16, 17, 18, 19, -12, -13, -14, -15, -16, -17, -18, -19, -19 }; ret = tab[nbits(bitfill, 4)]; refill(bitfill); return ret; }
int reverse(int x) { int count = nbits(x); int y=0,r=0; while( x != 0) { y = x % 10; x = x / 10; r += y * pow(10,--count); } return r; }
double pow(double x, int n) { int neg = ((n < 0) ? 1 : 0); if(x == 0) return 0; if(neg) n *= (-1); int nb = nbits(n); int bitval = 1; double cursol = 1.0, curpow = x; if(n & bitval) cursol *= curpow; for(int i=1; i <= nb; i++){ bitval = bitval << 1; curpow = curpow * curpow; if(n & bitval) cursol *= curpow; } return (neg ? (1.0/cursol) : cursol); }
static inline int is_valid_value(long value, size_t bitCount, int signP) { /* TODO 64 bit... */ /* cf) bitCount = 8, max = 256 */ unsigned long unsigned_max = (1 << bitCount) - 1; long signed_max = (1 << (bitCount - 1)) - 1; /* cf) bitCount = 8, max = 127 */ intptr_t min = -(1 << (bitCount - 1)); /* cf) bitCount = 8, max = -128 */ if ((size_t)nbits(value) > bitCount) { return FALSE; } /* check max and min value */ if (signP) { return min <= value && value <= signed_max; } else { /* unsigned min is always 0 */ return 0 <= value && (unsigned long)value <= unsigned_max; } }
int EvalHung(int side) /**************************************************************************** * * Calculate the number of hung pieces for a side. * ****************************************************************************/ { BitBoard c, n, b, r, q; int xside; xside = 1 ^ side; hunged[side] = 0; /* Knight */ n = (Ataks[xside][pawn] & board.b[side][knight]); n |= (Ataks[xside][0] & board.b[side][knight] & ~Ataks[side][0]); /* Bishop */ b = (Ataks[xside][pawn] & board.b[side][bishop]); b |= (Ataks[xside][0] & board.b[side][bishop] & ~Ataks[side][0]); /* Rook */ r = Ataks[xside][pawn] | Ataks[xside][knight] | Ataks[xside][bishop]; r = (r & board.b[side][rook]); r |= (Ataks[xside][0] & board.b[side][rook] & ~Ataks[side][0]); /* Queen */ q = Ataks[xside][pawn] | Ataks[xside][knight] | Ataks[xside][bishop] | Ataks[xside][rook]; q = (q & board.b[side][queen]); q |= (Ataks[xside][0] & board.b[side][queen] & ~Ataks[side][0]); c = n | b | r | q ; if (c) hunged[side] += nbits (c); /* King */ if (Ataks[xside][0] & board.b[side][king]) hunged[side] ++; return (hunged[side]); }
void gen_bitstable(int hdr) { int i; if (hdr) { printf("extern int8_t bitstable[256];\n"); return; } printf("/* bitstable[n] is the number of set bits\n" " * in the number 'n' */\n"); printf("int8_t bitstable[256] = {\n"); for (i = 0; i < 256; i++) { if (!(i%EFL)) printf("\t"); printf("%d, ", nbits(i)); if (!((i+1) % EFL)) printf("\n"); } printf("};\n\n"); }
void main() { unsigned int n1,n2; scanf("%d %d", &n1,&n2); unsigned int nb,np; while (n1!=0) { //do calc nb=nbits(n1);np=0; //printf("%d", nb); for (unsigned int i=n1;i<=n2;i++) { if (i>=bits[nb+1]) nb++; //printf("%d, %d\n",i,nb);} if (checkpal(i, nb)) np++;//printf("%d\n",i);} } //read next printf("%d\n",np); scanf("%d", &n1); if (n1!=0) scanf("%d",&n2); } }
static int fun_B(int *bitfill) { static int tab1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }; static int tab[] = { 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19 }; unsigned int tmp; tmp = nbits(bitfill, 7) - 68; refill(bitfill); if (tmp > 47) return 0xff; return tab[tab1[tmp]]; }
int ParseEPD (char *p) /************************************************************************** * * Parses an EPD input line. A few global variables are updated e.g. * current board, side to move, en passant, castling status, etc. * **************************************************************************/ { int r, c, sq; char *str_p; r = 56; c = 0; memset (&board, 0, sizeof (board)); while (p && *p != ' ') { sq = r + c; switch (*p) { case 'P' : SETBIT (board.b[white][pawn], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[white] += ValueP; break; case 'N' : SETBIT (board.b[white][knight], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[white] += ValueN; break; case 'B' : SETBIT (board.b[white][bishop], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[white] += ValueB; break; case 'R' : SETBIT (board.b[white][rook], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[white] += ValueR; break; case 'Q' : SETBIT (board.b[white][queen], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[white] += ValueQ; break; case 'K' : SETBIT (board.b[white][king], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); break; case 'p' : SETBIT (board.b[black][pawn], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[black] += ValueP; break; case 'n' : SETBIT (board.b[black][knight], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[black] += ValueN; break; case 'b' : SETBIT (board.b[black][bishop], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[black] += ValueB; break; case 'r' : SETBIT (board.b[black][rook], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[black] += ValueR; break; case 'q' : SETBIT (board.b[black][queen], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); board.material[black] += ValueQ; break; case 'k' : SETBIT (board.b[black][king], sq); SETBIT (board.blockerr90, r90[sq]); SETBIT (board.blockerr45, r45[sq]); SETBIT (board.blockerr315, r315[sq]); break; case '/' : r -= 8; c = -1; break; default : break; } if (isdigit (*p)) c += (*p - '0'); else c++; /* * Special case, a trailing "/" is accepted on the * end of the board settings. */ if (r == -8 && p[1] == ' ') r = 0; if (r < 0 || c > 8) return EPD_ERROR; if (c == 8 && p[1] != '/' && p[1] != ' ') return EPD_ERROR; p++; } board.pmaterial[white] = board.material[white] - nbits(board.b[white][pawn]) * ValueP; board.pmaterial[black] = board.material[black] - nbits(board.b[black][pawn]) * ValueP; board.king[white] = leadz (board.b[white][king]); board.king[black] = leadz (board.b[black][king]); UpdateFriends (); UpdateCBoard (); UpdateMvboard (); /* Get side to move */ if (!++p) return EPD_ERROR; if (*p == 'w') board.side = white; else if (*p == 'b') board.side = black; else return EPD_ERROR; /* Isn't this one cute? */ if (!++p || *p != ' ' || !++p) return EPD_ERROR; /* Castling status */ while (p && *p != ' ') { if (*p == 'K') board.flag |= WKINGCASTLE; else if (*p == 'Q') board.flag |= WQUEENCASTLE; else if (*p == 'k') board.flag |= BKINGCASTLE; else if (*p == 'q') board.flag |= BQUEENCASTLE; else if (*p == '-') { p++; break; } else return EPD_ERROR; p++; } if (!p || *p != ' ' || !++p) return EPD_ERROR; /* * En passant square, can only be '-' or [a-h][36] * In fact, one could add more sanity checks here. */ if (*p != '-') { if (!p[1] || *p < 'a' || *p > 'h' || !(p[1] == '3' || p[1] == '6')) return EPD_ERROR; board.ep = (*p - 'a') + (p[1] - '1')*8; p++; } else { board.ep = -1; } solution[0] = '\0'; id[0] = '\0'; if (!++p) return EPD_SUCCESS; /* The opcodes are optional, so we should not generate errors here */ /* Read in best move; "bm" operator */ str_p = strstr(p, "bm"); if (str_p) sscanf (str_p, "bm %63[^;];", solution); /* Read in the description; "id" operator */ str_p = strstr(p, "id"); if (str_p) sscanf (p, "id %31[^;];", id); phase = PHASE; return EPD_SUCCESS; }
zt_table * zt_table_init(char *name, zt_table_hash_func_cb func, zt_table_compare_cb cmp, size_t hint, int flags, void *cdata) { zt_table * table; size_t nbuckets = 0; char tname[1024]; zt_mem_pool * table_mem_pool; if ( (func == NULL) || (cmp == NULL)) { return NULL; } table_mem_pool = zt_mem_pool_get(ZT_DEFAULT_TABLE_POOL); if (table_mem_pool == NULL) { table_mem_pool = table_pool_init(2); } if ( (table = (zt_table *)zt_mem_pool_alloc(table_mem_pool)) == NULL) { return NULL; } table->flags = flags; table->cdata = cdata; if (flags & ZT_TABLE_SIZE_USE_HINT) { nbuckets = hint; } else { int i = 0; int len = sizeof_array(powers_of_2); for (i = 0; i < len; i++) { if (powers_of_2[i] > hint) { nbuckets = powers_of_2[i]; break; } } } table->nbits = nbits(nbuckets - 1); table->table_heap = zt_mem_heap_init(0, nbuckets * sizeof(struct table_node *)); table->buckets = zt_mem_heap_get_data(table->table_heap); memset(table->buckets, 0, nbuckets * sizeof(struct table_node *)); if (name) { table->name = XSTRDUP(name); } else { table->name = XSTRDUP("anonymous"); } snprintf(tname, sizeof_array(tname), "%s (table node pool)", name); table->node_pool = zt_mem_pool_init(tname, nbuckets, sizeof(struct table_node), NULL, NULL, 0); table->num_buckets = nbuckets; table->cmp = cmp; table->func = func; return table; } /* zt_table_init */