ExpressionPtr AndExpression::getDNFImpl() const { UnaryExpression * leftLeaf = dynamic_cast<UnaryExpression*>(left()); UnaryExpression * rightLeaf = dynamic_cast<UnaryExpression*>(right()); if (leftLeaf && rightLeaf) { return clone(); } // (A or B) and C => (A and C) or (B and C) if (OrExpression * leftOr = dynamic_cast<OrExpression*>(left())) { return Or(And(leftOr->left(), right()), And(leftOr->right(), right())); } // A and (B or C) -> (A and B) or (A and C) if (OrExpression * rightOr = dynamic_cast<OrExpression*>(right())) { return Or(And(left(), rightOr->left()), And(left(), rightOr->right())); } return And(left()->getDNF(), right()->getDNF()); }
void circuits( SD sd, Signal Init, Signal Clock, Signal w, Signal x, Signal y, Signal z ) { Module( (sd, "circuits"), (Init, Clock), (w, x, y, z) ); // Insert your declarations for any auxiliary Signals here Signal W, X, Y, Z; // Insert your DFFs here Dff( SD("1b"), ( Init, W, Clock, Zero ), w ); Dff( SD("2b"), ( Zero, X, Clock, Init ), x ); Dff( SD("3b"), ( Init, Y, Clock, Zero ), y ); Dff( SD("4b"), ( Zero, Z, Clock, Init ), z ); // Insert your combinational logic here (Not, And, Or gates) Signal notx, noty, notz; Not ( SD(sd,"1d"), x, notx ); Not ( SD(sd,"2d"), y, noty ); Not ( SD(sd,"3d"), z, notz ); Signal and1, and2, and3, and4; And ( SD(sd,"1e"), (notx, noty), and1 ); And ( SD(sd,"2e"), (x, notz), and2 ); And ( SD(sd,"3e"), (w, y), and3 ); And ( SD(sd,"4e"), (noty, notz), and4 ); Or ( SD(sd,"1f"), (and1, and1), W ); Or ( SD(sd,"2f"), (and2, and3), X ); Or ( SD(sd,"3f"), (z, z), Y ); Or ( SD(sd,"4f"), (and4, and4), Z ); }
ExpressionPtr OrExpression::getDNFImpl() const { UnaryExpression * leftLeaf = dynamic_cast<UnaryExpression*>(left()); UnaryExpression * rightLeaf = dynamic_cast<UnaryExpression*>(right()); // A or B if (leftLeaf && rightLeaf) { return clone(); } // A or (B and C) => (A and B) or (A and C) BinaryExpression * rightAnd = dynamic_cast<AndExpression*>(right()); if (leftLeaf && rightAnd) { return Or(And(leftLeaf, rightAnd->left()), And(leftLeaf, rightAnd->right())); } // (A and B) or C => (A and C) or (B and C) BinaryExpression * leftAnd = dynamic_cast<AndExpression*>(left()); if (leftAnd && rightLeaf) { return Or(And(leftAnd->left(), rightLeaf), And(leftAnd->right(), rightLeaf)); } return Or(left()->getDNF(), right()->getDNF()); }
void alu(SD(sd), const Signals& A, const Signals& B, const Signal& Cin, const Signal& Ainvert, const Signal& Binvert, const Signal& Unsgn, const Signals& Op, const Signals& Res, const Signal& C, const Signal& V) { Module((sd, "32 Bit ALU"), (A,B,Cin,Ainvert,Binvert,Unsgn,Op), (Res,C,V) ); Signal Cout(NUM_BITS); Signal lessIn; Signal lessOut; Signal notV; Signal notMSB; Signal notUnsgn; Signal lessJunk(NUM_BITS-1); Signal set; Signal setIntrm(3); Signal Prod1; Signal Prod2; oneBitALU(SD(sd, "1d"), A[0], B[0], Cin, lessIn, Ainvert, Binvert, Op, Res[0], Cout[0], lessJunk[0]); for (int i = 1; i < NUM_BITS-1; ++i) oneBitALU(SD(sd,"1d"), A[i], B[i], Cout[i-1], Zero, Ainvert, Binvert, Op, Res[i], Cout[i], lessJunk[i]); oneBitALU(SD(sd,"1d"), A[NUM_BITS-1], B[NUM_BITS-1], Cout[NUM_BITS-2], Zero, Ainvert, Binvert, Op, Res[NUM_BITS-1], C, lessOut); Not(SD(sd, "1d"), V, notV); Not(SD(sd, "1d"), Res[NUM_BITS-1], notMSB); Not(SD(sd, "1d"), Unsgn, notUnsgn); And(SD(sd, "1d"), (notUnsgn, notV, Res[NUM_BITS-1]), setIntrm[0]); And(SD(sd, "1d"), (notUnsgn, V, notMSB), setIntrm[1]); And(SD(sd, "1d"), (Unsgn, V), setIntrm[2]); Or(SD(sd, "1d"), setIntrm, set); And(SD(sd, "1d"), (set, lessOut), lessIn); Iand(SD(sd, "1d"), (Res[NUM_BITS-1]), (A[NUM_BITS-1],B[NUM_BITS-1]), Prod1); Iand(SD(sd, "1d"), (A[NUM_BITS-1], B[NUM_BITS-1]), (Res[NUM_BITS-1]), Prod2); Or(SD(sd, "1d"), (Prod1, Prod2), V); }
void circuit( SD sd, Signal a, Signal b, Signal c, Signal d, Signal F ) { Module( (sd, "circuit"), (a, b, c, d), (F) ); Signal notb, notc, notd; Signal and1, and2; // Intermediate objects Not ( SD(sd,"2a"), b, notb ); // NOT gates Not ( SD(sd,"3a"), c, notc ); Not ( SD(sd,"4a"), d, notd ); And ( SD(sd,"2c"), (a, notb), and1 ); // AND gates And ( SD(sd,"3c"), (b, notc, notd), and2 ); Or ( SD(sd,"2e"), (and1, and2), F ); // OR gate }
int main(int argc, char const *argv[]) { int n, m; int A[MAXNUM], B[MAXNUM]; int C[MAXNUM * 2]; int i, num; scanf("%d", &n); for (i = 0; i < n; ++i) scanf("%d", &A[i]); scanf("%d", &m); for (i = 0; i < m; ++i) scanf("%d", &B[i]); num = intersection(A, B, C, n, m); sort(C, 0, num - 1); for (i = 0; i < num; ++i) printf("%d ", C[i]); putchar('\n'); num = And(A, B, C, n, m); sort(C, 0, num - 1); for (i = 0; i < num; ++i) printf("%d ", C[i]); putchar('\n'); num = ComplementarySet(A, B, C, n, m); sort(C, 0, num - 1); for (i = 0; i < num; ++i) printf("%d ", C[i]); putchar('\n'); return 0; }//main
//腐蚀 void Erode(double *src,int s_width,int s_height,double *dst,int d_width,int d_height,double *se,int se_width,int se_height,Position *center){ if(center==NULL){ Position temp; temp.x=se_width/2; temp.y=se_height/2; center=&temp; } MoveDirection m; double *temp=(double *)malloc(sizeof(double)*d_width*d_height); double *tempdst=(double *)malloc(sizeof(double)*d_width*d_height); double *realdst=(double *)malloc(sizeof(double)*d_width*d_height); Zero(realdst, d_width, d_height); Zoom(src,s_width,s_height,temp,d_width,d_height); for(int i=0;i<se_width;i++){ for(int j=0;j<se_height;j++){ if(se[j*se_width+i]>100.0){ m.x=center->x-i; m.y=center->y-j; Translation(temp,tempdst,d_width,d_height, &m); And(tempdst, realdst, realdst,d_width,d_height); } } } matrixCopy(realdst, dst, d_width, d_height); free(temp); free(realdst); free(tempdst); }
template <bool align> SIMD_INLINE void SquaredDifferenceSum16f(const uint16_t * a, const uint16_t * b, size_t size, float * sum) { assert(size >= F); if (align) assert(Aligned(a) && Aligned(b)); size_t partialAlignedSize = AlignLo(size, F); size_t fullAlignedSize = AlignLo(size, DF); size_t i = 0; float32x4_t sums[2] = { vdupq_n_f32(0), vdupq_n_f32(0) }; if (fullAlignedSize) { for (; i < fullAlignedSize; i += DF) { SquaredDifferenceSum16f<align>(a, b, i + F * 0, sums[0]); SquaredDifferenceSum16f<align>(a, b, i + F * 1, sums[1]); } sums[0] = vaddq_f32(sums[0], sums[1]); } for (; i < partialAlignedSize; i += F) SquaredDifferenceSum16f<align>(a, b, i, sums[0]); if (partialAlignedSize != size) { float32x4_t tailMask = RightNotZero(size - partialAlignedSize); float32x4_t _a = vcvt_f32_f16((float16x4_t)LoadHalf<align>(a + size - F)); float32x4_t _b = vcvt_f32_f16((float16x4_t)LoadHalf<align>(a + size - F)); float32x4_t _d = And(vsubq_f32(_a, _b), tailMask); sums[0] = vaddq_f32(sums[0], vmulq_f32(_d, _d)); } *sum = ExtractSum32f(sums[0]); }
func Flacker() { if(Not(Random(15))) And(CastObjects(SU3V,Sum(1,Random(1)),Sum(5,Random(15)),Sum(-14,Random(28)),Sum(1,Random(2))),Sound("Spark*")); if(Not(Random(2))) return(0); if(Random(6)) return(ObjectSetAction(Local(0),"Neon")&&SetAction("FlackerAn")); if(Random(6)) return(ObjectSetAction(Local(0),"Aus")&&SetAction("FlackerAus")); return(1); }
BITBOARD ValidateComputeRookAttacks(int square) { BITBOARD attacks, temp_attacks; BITBOARD temp1, temp8; attacks=rook_attacks[square]; temp_attacks=And(attacks,Compl(Occupied)); temp_attacks=Compl(Or(temp_attacks,Compl(rook_attacks[square]))); temp1=And(temp_attacks,plus1dir[square]); temp8=And(temp_attacks,plus8dir[square]); attacks=Xor(attacks,plus1dir[FirstOne(temp1)]); attacks=Xor(attacks,plus8dir[FirstOne(temp8)]); temp1=And(temp_attacks,minus1dir[square]); temp8=And(temp_attacks,minus8dir[square]); attacks=Xor(attacks,minus1dir[LastOne(temp1)]); attacks=Xor(attacks,minus8dir[LastOne(temp8)]); return(attacks); }
BITBOARD ValidateComputeBishopAttacks(int square) { BITBOARD attacks, temp_attacks; BITBOARD temp7, temp9; attacks=bishop_attacks[square]; temp_attacks=And(attacks,Compl(Occupied)); temp_attacks=Compl(Or(temp_attacks,Compl(bishop_attacks[square]))); temp7=And(temp_attacks,plus7dir[square]); temp9=And(temp_attacks,plus9dir[square]); attacks=Xor(attacks,plus7dir[FirstOne(temp7)]); attacks=Xor(attacks,plus9dir[FirstOne(temp9)]); temp7=And(temp_attacks,minus7dir[square]); temp9=And(temp_attacks,minus9dir[square]); attacks=Xor(attacks,minus7dir[LastOne(temp7)]); attacks=Xor(attacks,minus9dir[LastOne(temp9)]); return(attacks); }
arithmtype Xor(void) { arithmtype Res = And(); while(1) { if ( ErrorDesc ) break; if (*Expr=='^') { Expr++; Res = Res ^ And(); } else { break; } } return Res; }
//击中与击不中 void HitorMiss(double *src,int width,int height,double *se1,int se1_width,int se1_height,double *se2,int se2_width,int se2_height,double *dst,Position *se1center,Position *se2center){ double *temp1=(double *)malloc(sizeof(double)*width*height); double *temp2=(double *)malloc(sizeof(double)*width*height); Erode(src, width, height, temp1, width, height, se1, se1_width, se1_height, se1center); Not(src, temp2, width, height); Erode(temp2, width, height, temp2, width, height, se2, se2_width, se2_height, se2center); And(temp1, temp2, dst, width, height); free(temp1); free(temp2); }
int TestAND1() { START_TEST_CASE; char *word = "life"; temp_list = NULL; And(word, ptr); SHOULD_BE(temp_list == NULL); END_TEST_CASE; }
Expression *AndExp::optimize(int result) { Expression *e; e1 = e1->optimize(result); e2 = e2->optimize(result); if (e1->isConst() == 1 && e2->isConst() == 1) e = And(type, e1, e2); else e = this; return e; }
unsigned int Query::Clone(const Query *other) { // TRACE << "Cloning " << other->ToString() << "\n"; for (restrictions_t::const_iterator i = other->m_restrictions.begin(); i != other->m_restrictions.end(); ++i) { if (i->is_string) Restrict(i->which, i->rt, i->sval); else Restrict(i->which, i->rt, i->ival); } for (relations_t::const_iterator i = other->m_relations.begin(); i != other->m_relations.end(); ++i) { if (i->anditive) And(Subexpression((int)i->a), Subexpression((int)i->b)); else Or(Subexpression((int)i->a), Subexpression((int)i->b)); } unsigned int rc = Where(Subexpression(other->m_root)); if (rc) return rc; for (orderby_t::const_iterator i = other->m_orderby.begin(); i != other->m_orderby.end(); ++i) { rc = OrderBy(*i); if (rc) return rc; } for (orderby_t::const_iterator i = other->m_collateby.begin(); i != other->m_collateby.end(); ++i) { rc = CollateBy(*i); if (rc) return rc; } // TRACE << "Cloned: " << ToString() << "\n"; return 0; }
void fullAdder(SD(sd), // Inputs: const Signal& A, const Signal& B, const Signal& Cin, // Outputs: const Signal& Sum, const Signal& Cout) { // This is a Module constructor. // The first argument is the display name for the module. // The second argument lists all of the input Signals. // The third argument lists all of the output Signals. // If fullAdder() is called directly by simnet(), this will put a "black box" // named "Full Adder" with three input leads and two output leads in the // simulation window. Module((sd, "Full Adder"), (A, B, Cin), (Sum, Cout) ); // Intermediate signals Signal AxorB; Signal AandB; Signal CandAxorB; // Because these components are inside a Module, they will not be visible in // the simulation. Xor(SD(sd, "1b"), (A, B), AxorB); And(SD(sd, "1b"), (A, B), AandB); And(SD(sd, "1b"), (AxorB, Cin), CandAxorB); Or (SD(sd, "1b"), (AandB, CandAxorB), Cout); Xor(SD(sd, "1b"), (Cin, AxorB), Sum); }
//连通分量获取 void GetConCompG_Onent(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double*)malloc(sizeof(double)*width*height); temp[seed->y*width+seed->x]=255.0; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, src, temp, width, height); } matrixCopy(temp, dst, width, height); free(temp); free(lasttemp); }
int TestAND2() { START_TEST_CASE; DocumentNode *dn = (DocumentNode *)calloc(1, sizeof(DocumentNode)); dn->doc_id = 1467; dn->freq = 2; char *word = "bumble"; temp_list = dn; And(word, ptr); SHOULD_BE(temp_list == NULL); END_TEST_CASE; }
int maximalRectangle(vector<vector<char> > &matrix) { int H = matrix.size(), W = H ? matrix[0].size() : 0; int max_rect = 0; for (int i = 0; i < H; ++i) { vector<char> all_ones(W, '1'); for (int j = i; j < H; ++j) { all_ones = And(all_ones, matrix[j]); // Cannot exceed max_rect for this j if ((j - i + 1) * W <= max_rect) continue; int max_run = maxRun(all_ones); // Cannot exceed max_rect for the rest of j for this i if ((H - i) * max_run < max_rect) break; max_rect = max(max_rect, (j - i + 1) * max_run); } } return max_rect; }
//孔洞填充 void FillHole(double *src,double *dst,int width,int height,Position *seed){ double *temp=(double *)malloc(sizeof(double)*width*height); Zero(temp, width, height); double *lasttemp=(double *)malloc(sizeof(double)*width*height); double *nsrc=(double *)malloc(sizeof(double)*width*height); Not(src, nsrc, width, height); temp[seed->y*width+seed->x]=255.; double se[9]={255.,255.,255.,255.,255.,255.,255.,255.,255.}; while(!matrixisEqu(lasttemp, temp, width, height)){ matrixCopy(temp, lasttemp, width, height); Dilate(temp, width, height, temp, width, height, se, 3, 3, NULL); And(temp, nsrc, temp, width, height); } Or(temp, src, dst, width, height); free(temp); free(lasttemp); free(nsrc); }
int TestAND3() { START_TEST_CASE; DocumentNode *dn = (DocumentNode *)calloc(1, sizeof(DocumentNode)); dn->doc_id = 1467; dn->freq = 2; char *word = "libation"; // The query we want to test with. temp_list = dn; And(word, ptr); SHOULD_BE(temp_list != NULL); SHOULD_BE(temp_list->doc_id == 1467); SHOULD_BE(temp_list->freq == 3); // Frequency is the sum of the two. free(dn); // Cleanup. END_TEST_CASE; }
//重建开操作 void reBuildOpen(double *src,double *dst,double *ground,int width,int height,double *dilateSE,int dse_width,int dse_height,double *erodeSE,int ese_width,int ese_height,int eroden){ double *temp=(double*)malloc(sizeof(double)*width*height); double *temp_last=(double*)malloc(sizeof(double)*width*height); matrixCopy(src, temp, width, height); for(int i=0;i<eroden;i++){ Erode(temp, width, height, temp,width, height, erodeSE, ese_height, ese_height, NULL); } while(!matrixisEqu(temp, temp_last,width,height)){ matrixCopy(temp, temp_last, width, height); Dilate(temp, width, height, temp, width, height, dilateSE, dse_width, dse_height, NULL); And(temp, ground, temp, width, height); } matrixCopy(temp, dst, width, height); free(temp); free(temp_last); }
Predicate Predicate::reduceToDependencies(const TemplateVarArray& array, const bool conservativeDefault) const { switch (kind()) { case TRUE: case FALSE: case SELFCONST: { return copy(); } case AND: { return And(andLeft().reduceToDependencies(array, conservativeDefault), andRight().reduceToDependencies(array, conservativeDefault)); } case OR: { return Or(orLeft().reduceToDependencies(array, conservativeDefault), orRight().reduceToDependencies(array, conservativeDefault)); } case SATISFIES: { if (satisfiesType()->dependsOnOnly(array) && satisfiesRequirement()->dependsOnOnly(array)) { return copy(); } else { return Predicate::FromBool(conservativeDefault); } } case VARIABLE: { if (array.contains(variableTemplateVar())) { return copy(); } else { return Predicate::FromBool(conservativeDefault); } } } locic_unreachable("Unknown predicate kind."); }
iboolean operator&& (iboolean x, iboolean y) // AND { return And(x,y); }
iboolean Restrict(iboolean x, iboolean y) // return x and !y { return And(x,!y);}
// return x and !y iboolean Restrict(iboolean x, iboolean y) { return And(x,!y); }
/* ******************************************************************************** * * * SwapXray() is used to determine if a piece is "behind" the piece on * * <from>, and this piece would attack <to> if the piece on <from> were moved * * (as in playing out sequences of swaps). if so, this indirect attacker is * * added to the list of attackers bearing to <to>. * * * ******************************************************************************** */ BITBOARD SwapXray(BITBOARD attacks, int from, int direction) { switch (direction) { case 1: return(Or(attacks, And(And(AttacksRank(from),RooksQueens),plus1dir[from]))); case 7: return(Or(attacks, And(And(AttacksDiaga1(from),BishopsQueens),plus7dir[from]))); case 8: return(Or(attacks, And(And(AttacksFile(from),RooksQueens),plus8dir[from]))); case 9: return(Or(attacks, And(And(AttacksDiagh1(from),BishopsQueens),plus9dir[from]))); case -1: return(Or(attacks, And(And(AttacksRank(from),RooksQueens),minus1dir[from]))); case -7: return(Or(attacks, And(And(AttacksDiaga1(from),BishopsQueens),minus7dir[from]))); case -8: return(Or(attacks, And(And(AttacksFile(from),RooksQueens),minus8dir[from]))); case -9: return(Or(attacks, And(And(AttacksDiagh1(from),BishopsQueens),minus9dir[from]))); } return(attacks); }
/* ******************************************************************************** * * * Swap() is used to analyze capture moves to see whether or not they appear * * to be profitable. the basic algorithm is extremely fast since it uses the * * bitmaps to determine which squares are attacking the [target] square. * * * * the algorithm is quite simple. using the attack bitmaps, we enumerate all * * the pieces that are attacking [target] for either side. then we simply * * use the lowest piece (value) for the correct side to capture on [target]. * * we continually "flip" sides taking the lowest piece each time. * * * * as a piece is used, if it is a sliding piece (pawn, bishop, rook or queen) * * we "peek" behind it to see if it is attacked by a sliding piece in the * * direction away from the piece being captured. if so, and that sliding * * piece moves in this direction, then it is added to the list of attackers * * since its attack has been "uncovered" by moving the capturing piece. * * * ******************************************************************************** */ int Swap(int source, int target, int wtm) { register BITBOARD attacks; register int attacked_piece; register int square, direction; register int sign, color, next_capture=1; int swap_list[32]; /* ---------------------------------------------------------- | | | determine which squares attack <target> for each side. | | | ---------------------------------------------------------- */ attacks=AttacksTo(target); /* ---------------------------------------------------------- | | | initialize by placing the piece on <target> first in | | the list as it is being captured to start things off. | | | ---------------------------------------------------------- */ attacked_piece=p_values[PieceOnSquare(target)+7]; /* ---------------------------------------------------------- | | | the first piece to capture on <target> is the piece | | standing on <source>. | | | ---------------------------------------------------------- */ color=ChangeSide(wtm); swap_list[0]=attacked_piece; sign=-1; attacked_piece=p_values[PieceOnSquare(source)+7]; Clear(source,attacks); direction=directions[target][source]; if (direction) attacks=SwapXray(attacks,source,direction); /* ---------------------------------------------------------- | | | now pick out the least valuable piece for the correct | | side that is bearing on <target>. as we find one, we | | call SwapXray() to add the piece behind this piece | | that is indirectly bearing on <target> (if any). | | | ---------------------------------------------------------- */ while (attacks) { if (color) { if (And(WhitePawns,attacks)) square=FirstOne(And(WhitePawns,attacks)); else if (And(WhiteKnights,attacks)) square=FirstOne(And(WhiteKnights,attacks)); else if (And(WhiteBishops,attacks)) square=FirstOne(And(WhiteBishops,attacks)); else if (And(WhiteRooks,attacks)) square=FirstOne(And(WhiteRooks,attacks)); else if (And(WhiteQueens,attacks)) square=FirstOne(And(WhiteQueens,attacks)); else if (And(WhiteKing,attacks)) square=WhiteKingSQ; else break; } else { if (And(BlackPawns,attacks)) square=FirstOne(And(BlackPawns,attacks)); else if (And(BlackKnights,attacks)) square=FirstOne(And(BlackKnights,attacks)); else if (And(BlackBishops,attacks)) square=FirstOne(And(BlackBishops,attacks)); else if (And(BlackRooks,attacks)) square=FirstOne(And(BlackRooks,attacks)); else if (And(BlackQueens,attacks)) square=FirstOne(And(BlackQueens,attacks)); else if (And(BlackKing,attacks)) square=BlackKingSQ; else break; } /* ------------------------------------------------ | | | located the least valuable piece bearing on | | <target>. remove it from the list and then | | find out if a sliding piece behind it attacks | | through this piece. | | | ------------------------------------------------ */ swap_list[next_capture]=swap_list[next_capture-1]+sign*attacked_piece; attacked_piece=p_values[PieceOnSquare(square)+7]; Clear(square,attacks); direction=directions[target][square]; if (direction) attacks=SwapXray(attacks,square,direction); next_capture++; sign=-sign; color=ChangeSide(color); } /* ---------------------------------------------------------- | | | starting at the end of the sequence of values, use a | | "minimax" like procedure to decide where the captures | | will stop. | | | ---------------------------------------------------------- */ next_capture--; if(next_capture&1) sign=-1; else sign=1; while (next_capture) { if (sign < 0) { if(swap_list[next_capture] <= swap_list[next_capture-1]) swap_list[next_capture-1]=swap_list[next_capture]; } else { if(swap_list[next_capture] >= swap_list[next_capture-1]) swap_list[next_capture-1]=swap_list[next_capture]; } next_capture--; sign=-sign; } return (swap_list[0]); }
void ValidatePosition(int ply, int move, char *caller) { BITBOARD temp, temp1, temp_occ, temp_occ_rl90, temp_occ_rl45; BITBOARD temp_occ_rr45, temp_occx, cattacks, rattacks; int i,square,error; int temp_score; /* first, test w_occupied and b_occupied */ error=0; temp_occ=Or(Or(Or(Or(Or(WhitePawns,WhiteKnights),WhiteBishops), WhiteRooks),WhiteQueens),WhiteKing); if(Xor(WhitePieces,temp_occ)) { Print(1,"ERROR white occupied squares is bad!\n"); Display2BitBoards(temp_occ,WhitePieces); error=1; } temp_occ=Or(Or(Or(Or(Or(BlackPawns,BlackKnights),BlackBishops), BlackRooks),BlackQueens),BlackKing); if(Xor(BlackPieces,temp_occ)) { Print(1,"ERROR black occupied squares is bad!\n"); Display2BitBoards(temp_occ,BlackPieces); error=1; } /* now test rotated occupied bitboards. */ temp_occ_rl90=0; temp_occ_rl45=0; temp_occ_rr45=0; for (i=0;i<64;i++) { if (PieceOnSquare(i)) { temp_occ_rl90=Or(temp_occ_rl90,set_mask_rl90[i]); temp_occ_rl45=Or(temp_occ_rl45,set_mask_rl45[i]); temp_occ_rr45=Or(temp_occ_rr45,set_mask_rr45[i]); } } if(Xor(OccupiedRL90,temp_occ_rl90)) { Print(1,"ERROR occupied squares (rotated left 90) is bad!\n"); Display2BitBoards(temp_occ_rl90,OccupiedRL90); error=1; } if(Xor(OccupiedRL45,temp_occ_rl45)) { Print(1,"ERROR occupied squares (rotated left 45) is bad!\n"); Display2BitBoards(temp_occ_rl45,OccupiedRL45); error=1; } if(Xor(OccupiedRR45,temp_occ_rr45)) { Print(1,"ERROR occupied squares (rotated right 45) is bad!\n"); Display2BitBoards(temp_occ_rr45,OccupiedRR45); error=1; } /* now test bishops_queens and rooks_queens */ temp_occ=Or(Or(Or(WhiteBishops,WhiteQueens),BlackBishops), BlackQueens); if(Xor(BishopsQueens,temp_occ)) { Print(1,"ERROR bishops_queens is bad!\n"); Display2BitBoards(temp_occ,BishopsQueens); error=1; } temp_occ=Or(Or(Or(WhiteRooks,WhiteQueens),BlackRooks), BlackQueens); if(Xor(RooksQueens,temp_occ)) { Print(1,"ERROR rooks_queens is bad!\n"); Display2BitBoards(temp_occ,RooksQueens); error=1; } /* check individual piece bit-boards to make sure two pieces don't occupy the same square (bit) */ temp_occ=Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor(Xor( WhitePawns,WhiteKnights),WhiteBishops),WhiteRooks), WhiteQueens),BlackPawns),BlackKnights),BlackBishops), BlackRooks),BlackQueens),WhiteKing),BlackKing); temp_occx=Or(Or(Or(Or(Or(Or(Or(Or(Or(Or(Or( WhitePawns,WhiteKnights),WhiteBishops),WhiteRooks), WhiteQueens),BlackPawns),BlackKnights),BlackBishops), BlackRooks),BlackQueens),WhiteKing),BlackKing); if(Xor(temp_occ,temp_occx)) { Print(1,"ERROR two pieces on same square\n"); error=1; } /* test material_evaluation */ temp_score=PopCnt(WhitePawns)*PAWN_VALUE; temp_score-=PopCnt(BlackPawns)*PAWN_VALUE; temp_score+=PopCnt(WhiteKnights)*KNIGHT_VALUE; temp_score-=PopCnt(BlackKnights)*KNIGHT_VALUE; temp_score+=PopCnt(WhiteBishops)*BISHOP_VALUE; temp_score-=PopCnt(BlackBishops)*BISHOP_VALUE; temp_score+=PopCnt(WhiteRooks)*ROOK_VALUE; temp_score-=PopCnt(BlackRooks)*ROOK_VALUE; temp_score+=PopCnt(WhiteQueens)*QUEEN_VALUE; temp_score-=PopCnt(BlackQueens)*QUEEN_VALUE; if(temp_score != Material) { Print(1,"ERROR material_evaluation is wrong, good=%d, bad=%d\n", temp_score,Material); error=1; } temp_score=PopCnt(WhiteKnights)*knight_v; temp_score+=PopCnt(WhiteBishops)*bishop_v; temp_score+=PopCnt(WhiteRooks)*rook_v; temp_score+=PopCnt(WhiteQueens)*queen_v; if(temp_score != TotalWhitePieces) { Print(1,"ERROR white_pieces is wrong, good=%d, bad=%d\n", temp_score,TotalWhitePieces); error=1; } temp_score=PopCnt(WhitePawns); if(temp_score != TotalWhitePawns) { Print(1,"ERROR white_pawns is wrong, good=%d, bad=%d\n", temp_score,TotalWhitePawns); error=1; } temp_score=PopCnt(BlackKnights)*knight_v; temp_score+=PopCnt(BlackBishops)*bishop_v; temp_score+=PopCnt(BlackRooks)*rook_v; temp_score+=PopCnt(BlackQueens)*queen_v; if(temp_score != TotalBlackPieces) { Print(1,"ERROR black_pieces is wrong, good=%d, bad=%d\n", temp_score,TotalBlackPieces); error=1; } temp_score=PopCnt(BlackPawns); if(temp_score != TotalBlackPawns) { Print(1,"ERROR black_pawns is wrong, good=%d, bad=%d\n", temp_score,TotalBlackPawns); error=1; } /* now test the board[...] to make sure piece values are correct. */ /* test pawn locations */ temp=WhitePawns; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != pawn) { Print(1,"ERROR! board[%d]=%d, should be 1\n",square, PieceOnSquare(square)); error=1; } Clear(square,temp); } temp=BlackPawns; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -pawn) { Print(1,"ERROR! board[%d]=%d, should be -1\n",square, PieceOnSquare(square)); error=1; } Clear(square,temp); } /* test knight locations */ temp=WhiteKnights; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != knight) { Print(1,"ERROR! board[%d]=%d, should be 2\n",square, PieceOnSquare(square)); error=1; } Clear(square,temp); } temp=BlackKnights; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -knight) { Print(1,"ERROR! board[%d]=%d, should be -2\n",square, PieceOnSquare(square)); error=1; } Clear(square,temp); } /* test bishop locations */ temp=WhiteBishops; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != bishop) { Print(1,"ERROR! board[%d]=%d, should be 3\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksBishop(square); cattacks=ValidateComputeBishopAttacks(square); if (rattacks != cattacks) { Print(1,"ERROR! bishop attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } temp=BlackBishops; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -bishop) { Print(1,"ERROR! board[%d]=%d, should be -3\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksBishop(square); cattacks=ValidateComputeBishopAttacks(square); if (rattacks != cattacks) { Print(1,"ERROR! bishop attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } /* test rook locations */ temp=WhiteRooks; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != rook) { Print(1,"ERROR! board[%d]=%d, should be 4\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksRook(square); cattacks=ValidateComputeRookAttacks(square); if (rattacks != cattacks) { Print(1,"ERROR! Rook attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } temp=BlackRooks; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -rook) { Print(1,"ERROR! board[%d]=%d, should be -4\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksRook(square); cattacks=ValidateComputeRookAttacks(square); if (rattacks != cattacks) { Print(1,"ERROR! Rook attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } /* test queen locations */ temp=WhiteQueens; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != queen) { Print(1,"ERROR! board[%d]=%d, should be 5\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksQueen(square); cattacks=Or(ValidateComputeRookAttacks(square),ValidateComputeBishopAttacks(square)); if (rattacks != cattacks) { Print(1,"ERROR! queen attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } temp=BlackQueens; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -queen) { Print(1,"ERROR! board[%d]=%d, should be -5\n",square, PieceOnSquare(square)); error=1; } rattacks=AttacksQueen(square); cattacks=Or(ValidateComputeRookAttacks(square),ValidateComputeBishopAttacks(square)); if (rattacks != cattacks) { Print(1,"ERROR! queen attacks wrong, square=%d\n",square); Display2BitBoards(rattacks,cattacks); error=1; } Clear(square,temp); } /* test king locations */ temp=WhiteKing; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != king) { Print(1,"ERROR! board[%d]=%d, should be 6\n",square, PieceOnSquare(square)); error=1; } if (WhiteKingSQ != square) { Print(1,"ERROR! white_king is %d, should be %d\n", WhiteKingSQ,square); error=1; } Clear(square,temp); } temp=BlackKing; while(temp) { square=FirstOne(temp); if (PieceOnSquare(square) != -king) { Print(1,"ERROR! board[%d]=%d, should be -6\n",square, PieceOnSquare(square)); error=1; } if (BlackKingSQ != square) { Print(1,"ERROR! black_king is %d, should be %d\n", BlackKingSQ,square); error=1; } Clear(square,temp); } /* test board[i] fully now. */ for (i=0;i<64;i++) switch (PieceOnSquare(i)) { case -king: if (!And(BlackKing,set_mask[i])) { Print(1,"ERROR! b_king/board[%d] don't agree!\n",i); error=1; } break; case -queen: if (!And(BlackQueens,set_mask[i])) { Print(1,"ERROR! b_queen/board[%d] don't agree!\n",i); error=1; } break; case -rook: if (!And(BlackRooks,set_mask[i])) { Print(1,"ERROR! b_rook/board[%d] don't agree!\n",i); error=1; } break; case -bishop: if (!And(BlackBishops,set_mask[i])) { Print(1,"ERROR! b_bishop/board[%d] don't agree!\n",i); error=1; } break; case -knight: if (!And(BlackKnights,set_mask[i])) { Print(1,"ERROR! b_knight/board[%d] don't agree!\n",i); error=1; } break; case -pawn: if (!And(BlackPawns,set_mask[i])) { Print(1,"ERROR! b_pawn/board[%d] don't agree!\n",i); error=1; } break; case king: if (!And(WhiteKing,set_mask[i])) { Print(1,"ERROR! w_king/board[%d] don't agree!\n",i); error=1; } break; case queen: if (!And(WhiteQueens,set_mask[i])) { Print(1,"ERROR! w_queen/board[%d] don't agree!\n",i); error=1; } break; case rook: if (!And(WhiteRooks,set_mask[i])) { Print(1,"ERROR! w_rook/board[%d] don't agree!\n",i); error=1; } break; case bishop: if (!And(WhiteBishops,set_mask[i])) { Print(1,"ERROR! w_bishop/board[%d] don't agree!\n",i); error=1; } break; case knight: if (!And(WhiteKnights,set_mask[i])) { Print(1,"ERROR! w_knight/board[%d] don't agree!\n",i); error=1; } break; case pawn: if (!And(WhitePawns,set_mask[i])) { Print(1,"ERROR! w_pawn/board[%d] don't agree!\n",i); error=1; } break; } /* test empty squares now */ temp=Compl(Or(temp_occ,temp_occx)); while(temp) { square=FirstOne(temp); if (PieceOnSquare(square)) { Print(1,"ERROR! board[%d]=%d, should be 0\n",square, PieceOnSquare(square)); error=1; } Clear(square,temp); } /* test total piece count now */ temp=PopCnt(Occupied); if (temp != TotalPieces) { Print(1,"ERROR! TotalPieces is wrong, correct=%d bad=%d\n", temp,TotalPieces); error=1; } /* test hash key */ temp=0; temp1=0; for (i=0;i<64;i++) { switch (PieceOnSquare(i)) { case king: temp=Xor(temp,w_king_random[i]); break; case queen: temp=Xor(temp,w_queen_random[i]); break; case rook: temp=Xor(temp,w_rook_random[i]); break; case bishop: temp=Xor(temp,w_bishop_random[i]); break; case knight: temp=Xor(temp,w_knight_random[i]); break; case pawn: temp=Xor(temp,w_pawn_random[i]); temp1=Xor(temp1,w_pawn_random[i]); break; case -pawn: temp=Xor(temp,b_pawn_random[i]); temp1=Xor(temp1,b_pawn_random[i]); break; case -knight: temp=Xor(temp,b_knight_random[i]); break; case -bishop: temp=Xor(temp,b_bishop_random[i]); break; case -rook: temp=Xor(temp,b_rook_random[i]); break; case -queen: temp=Xor(temp,b_queen_random[i]); break; case -king: temp=Xor(temp,b_king_random[i]); break; default: break; } } if (EnPassant(ply)) HashEP(EnPassant(ply),temp); if (!(WhiteCastle(ply)&1)) HashCastleW(0,temp); if (!(WhiteCastle(ply)&2)) HashCastleW(1,temp); if (!(BlackCastle(ply)&1)) HashCastleB(0,temp); if (!(BlackCastle(ply)&2)) HashCastleB(1,temp); if(Xor(temp,HashKey)) { Print(1,"ERROR! hash_key is bad.\n"); error=1; } if(Xor(temp1,PawnHashKey)) { Print(1,"ERROR! pawn_hash_key is bad.\n"); error=1; } if (error) { /* Print(0,"active path:\n"); for (i=1;i<=ply;i++) DisplayChessMove("move=",move); */ Print(0,"current move:\n"); DisplayChessMove("move=",move); DisplayChessBoard(stdout,search); Print(0,"called from %s, ply=%d\n",caller,ply); Print(0,"node=%d\n",nodes_searched+q_nodes_searched); exit(1); } }