int cintura(int **A, int n) { /* condição garante mais chances de ter cintura o grafo */ if (n < 3 || A == NULL) { printf("ERROR OU INFINITO\n"); return 0; } int origem,girth = n+1; for (origem = 0; origem < n; origem++) { int cintura_origem = BSF(A,n,origem); /* percorre por largura */ /* Verifica se a cintura da origem é menor que a anterior, caso for substitui */ if (girth > cintura_origem) { girth = cintura_origem; } } return girth; }
int main() { int g ; stack s ; initialize_s(&s); printf("\nGive a goal state"); scanf("%d",&g); readGraph(); displayGraph(); if(!BSF(&s, 0, g)) printf("\nGoal state %d not found", g); printf("\n" ); return 0; }
/** * bsf(): * * Return the least-significant bit set in a register. */ __inline__ int8_t bsf(uint32_t r) { uint32_t index; uint32_t zf; /* Call the Bit Search Forward instruction. */ BSF(r, &index, &zf); /* If a bit set has been found, return its index. */ if (!zf) { return index; } /* The register contains only 0, then return -1. */ return ERR_UNKNOWN; }
typeMoveList *MyPositionalGain(typePos *Position, typeMoveList *List, int av) { uint64 empty = ~Position->OccupiedBW, U, T; int to, sq; typeMoveList *sm, *p, *q; int move; sm = List; for (U = ForwardShift(BitboardMyP & SecondSixthRanks) & empty; U; BitClear(sq, U)) { to = BSF(U); if (OnThirdRank(to) && Position->sq[Forward(to)] == 0) AddGain(List, (Backward(to) << 6) | Forward(to), EnumMyP, Forward(to)); AddGain(List, (Backward(to) << 6) | to, EnumMyP, to); } for (U = BitboardMyN; U; BitClear(sq, U)) { sq = BSF(U); T = AttN[sq] & empty; AddGainTo(T, EnumMyN); } for (U = BitboardMyBL; U; BitClear(sq, U)) { sq = BSF(U); T = AttB(sq) & empty; AddGainTo(T, EnumMyBL); } for (U = BitboardMyBD; U; BitClear(sq, U)) { sq = BSF(U); T = AttB(sq) & empty; AddGainTo(T, EnumMyBD); } for (U = BitboardMyR; U; BitClear(sq, U)) { sq = BSF(U); T = AttR(sq) & empty; AddGainTo(T, EnumMyR); } for (U = BitboardMyQ; U; BitClear(sq, U)) { sq = BSF(U); T = AttQ(sq) & empty; AddGainTo(T, EnumMyQ); } sq = MyKingSq; T = AttK[sq] & empty &(~OppAttacked); AddGainTo(T, EnumMyK); List->move = 0; Sort; return List; }
typeMoveList *MyQuietChecks(typePos *Position, typeMoveList *List, uint64 mask) { int opks, king, sq, to, fr, pi; uint64 U, T, V; typeMoveList *list; uint32 move; uint64 gcm; gcm = ~MyXray; mask = (~mask) &~MyOccupied; ; list = List; king = OppKingSq; list = List; for (U = MyXray & MyOccupied; U; BitClear(fr, U)) { fr = BSF(U); pi = Position->sq[fr]; if (pi == EnumMyP) { if (File(fr) != File(king) && !SeventhRank(fr) && Position->sq[Forward(fr)] == 0) { (List++)->move = (fr << 6) | Forward(fr); if (OnSecondRank(fr) && Position->sq[Forward2(fr)] == 0) (List++)->move = (fr << 6) | Forward2(fr); } if (CanCaptureRight && Rank(fr) != NumberRank7) (List++)->move = (fr << 6) | ForwardRight(fr); if (CanCaptureLeft && Rank(fr) != NumberRank7) (List++)->move = (fr << 6) | ForwardLeft(fr); } else if (pi == EnumMyN) { V = AttN[fr] & mask; while (V) { to = BSF(V); (List++)->move = (fr << 6) | to; BitClear(to, V); } } else if (pi == EnumMyBL || pi == EnumMyBD) { V = AttB(fr) & mask; while (V) { to = BSF(V); (List++)->move = (fr << 6) | to; BitClear(to, V); } } else if (pi == EnumMyR) { V = AttR(fr) & mask; while (V) { to = BSF(V); (List++)->move = (fr << 6) | to; BitClear(to, V); } } else if (pi == EnumMyK) { if (File(fr) == File(king) || Rank(fr) == Rank(king)) V = AttK[fr] & NonOrtho[king] & mask &(~OppAttacked); else V = AttK[fr] & NonDiag[king] & mask &(~OppAttacked); while (V) { to = BSF(V); (List++)->move = (fr << 6) | to; BitClear(to, V); } } } opks = OppKingSq; T = CaptureLeft &(~BitBoardEighthRank) & mask & OppOccupied & MyAttackedPawns[opks]; while (T) { to = BSF(T); (List++)->move = FromRight(to) | to; BitClear(to, T); } T = CaptureRight &(~BitBoardEighthRank) & mask & OppOccupied & MyAttackedPawns[opks]; while (T) { to = BSF(T); (List++)->move = FromLeft(to) | to; BitClear(to, T); } for (U = BitboardMyQ; U; BitClear(sq, U)) { sq = BSF(U); T = AttQ(sq) & AttQ(king) & mask; while (T) { to = BSF(T); BitClear(to, T); if ((OppAttackedPawns[to] & BitboardOppP & gcm) == 0 && (AttN[to] & BitboardOppN & gcm) == 0) { move = (sq << 6) | to; if (MySEE(Position, move)) (List++)->move = (sq << 6) | to; } } } for (U = BitboardMyR; U; BitClear(sq, U)) { sq = BSF(U); T = AttR(sq) & AttR(king) & mask; while (T) { to = BSF(T); BitClear(to, T); if ((OppAttackedPawns[to] & BitboardOppP & gcm) == 0 && (AttN[to] & BitboardOppN & gcm) == 0) { move = (sq << 6) | to; if (MySEE(Position, move)) (List++)->move = (sq << 6) | to; } } } for (U = BitboardMyB; U; BitClear(sq, U)) { sq = BSF(U); T = AttB(sq) & AttB(king) & mask; while (T) { to = BSF(T); BitClear(to, T); if ((OppAttackedPawns[to] & BitboardOppP & gcm) == 0) { move = (sq << 6) | to; if (MySEE(Position, move)) (List++)->move = (sq << 6) | to; } } } for (U = BitboardMyN; U; BitClear(sq, U)) { sq = BSF(U); T = AttN[sq] & AttN[king] & mask; while (T) { to = BSF(T); BitClear(to, T); if ((OppAttackedPawns[to] & BitboardOppP & gcm) == 0) { move = (sq << 6) | to; if (MySEE(Position, move)) (List++)->move = (sq << 6) | to; } } } if (BitboardOppK & FourthEighthRankNoH && Position->sq[BackRight(opks)] == 0) { if (Position->sq[BackRight2(opks)] == EnumMyP) { fr = BackRight2(opks); to = BackRight(opks); move = (fr << 6) | to; if (PawnGuard(to, fr) && MySEE(Position, move)) (List++)->move = move; } if (Rank(opks) == NumberRank5 && Position->sq[BackRight2(opks)] == 0 && Position->sq[BackRight3(opks)] == EnumMyP) { to = BackRight(opks); fr = BackRight3(opks); move = (fr << 6) | to; if (PawnGuard(to, fr) && MySEE(Position, move)) (List++)->move = move; } } if (BitboardOppK & FourthEighthRankNoA && Position->sq[BackLeft(opks)] == 0) { if (Position->sq[BackLeft2(opks)] == EnumMyP) { fr = BackLeft2(opks); to = BackLeft(opks); move = (fr << 6) | to; if (PawnGuard(to, fr) && MySEE(Position, move)) (List++)->move = move; } if (Rank(opks) == NumberRank5 && Position->sq[BackLeft2(opks)] == 0 && Position->sq[BackLeft3(opks)] == EnumMyP) { to = BackLeft(opks); fr = BackLeft3(opks); move = (fr << 6) | to; if (PawnGuard(to, fr) && MySEE(Position, move)) (List++)->move = move; } } List->move = MoveNone; return List; }
typeMoveList *MyOrdinary(typePos *Position, typeMoveList *List) { uint64 empty = ~Position->OccupiedBW, U, T, Rook, Bishop, Pawn; int to, sq, opks = OppKingSq; if (CastleOO && ((Position->OccupiedBW | OppAttacked) & WhiteF1G1) == 0) MoveAdd(List, FlagOO | (WhiteE1 << 6) | WhiteG1, EnumMyK, WhiteG1, 0); if (CastleOOO && (Position->OccupiedBW & WhiteB1C1D1) == 0 && (OppAttacked & WhiteC1D1) == 0) MoveAdd(List, FlagOO | (WhiteE1 << 6) | WhiteC1, EnumMyK, WhiteC1, 0); Pawn = MyAttackedPawns[opks]; if (BitboardMyQ | BitboardMyR) Rook = AttR(opks); if (BitboardMyQ | BitboardMyB) Bishop = AttB(opks); for (U = ForwardShift(BitboardMyP & SecondSixthRanks) & empty; U; BitClear(sq, U)) { to = BSF(U); if (OnThirdRank(to) && Position->sq[Forward(to)] == 0) MoveAdd(List, (Backward(to) << 6) | Forward(to), EnumMyP, Forward(to), Pawn); MoveAdd(List, (Backward(to) << 6) | to, EnumMyP, to, Pawn); } for (U = BitboardMyQ; U; BitClear(sq, U)) { sq = BSF(U); T = AttQ(sq) & empty; MovesTo(T, EnumMyQ, Rook | Bishop); } for (U = BitboardMyR; U; BitClear(sq, U)) { sq = BSF(U); T = AttR(sq) & empty; MovesTo(T, EnumMyR, Rook); } for (U = BitboardMyB; U; BitClear(sq, U)) { sq = BSF(U); T = AttB(sq) & empty; MovesTo(T, ((SqSet[sq] & Black) ? EnumMyBD : EnumMyBL), Bishop); } sq = BSF(BitboardMyK); T = AttK[sq] & empty &(~OppAttacked); MovesTo(T, EnumMyK, 0); for (U = BitboardMyN; U; BitClear(sq, U)) { sq = BSF(U); T = AttN[sq] & empty; MovesTo(T, EnumMyN, AttN[opks]); } for (U = BitboardMyP & BitBoardSeventhRank; U; BitClear(sq, U)) { sq = BSF(U); to = Forward(sq); if (Position->sq[to] == 0) UnderProm(); to = ForwardLeft(sq); if (sq != WhiteA7 && SqSet[to] & OppOccupied) UnderProm(); to = ForwardRight(sq); if (sq != WhiteH7 && SqSet[to] & OppOccupied) UnderProm(); } List->move = 0; return List; }
typeMoveList *MyCapture(typePos *Position, typeMoveList *List, uint64 mask) { uint64 U, T, AttR, AttB; int sq, to, c; to = Position->Dyn->ep; if (to) { if (CaptureLeft & SqSet[to]) Add(List, FlagEP | FromRight(to) | to, CaptureEP); if (CaptureRight & SqSet[to]) Add(List, FlagEP | FromLeft(to) | to, CaptureEP); } if ((mask & MyAttacked) == 0) goto NoTarget; T = CaptureLeft &(~BitBoardEighthRank) & mask; while (T) { to = BSF(T); c = Position->sq[to]; Add(List, FromRight(to) | to, CaptureValue[EnumMyP][c]); BitClear(to, T); } T = CaptureRight &(~BitBoardEighthRank) & mask; while (T) { to = BSF(T); c = Position->sq[to]; Add(List, FromLeft(to) | to, CaptureValue[EnumMyP][c]); BitClear(to, T); } for (U = BitboardMyN; U; BitClear(sq, U)) { sq = BSF(U); T = AttN[sq] & mask; AddTo(T, CaptureValue[EnumMyN][c]); } for (U = BitboardMyB; U; BitClear(sq, U)) { sq = BSF(U); AttB = AttB(sq); T = AttB & mask; AddTo(T, CaptureValue[EnumMyBL][c]); } for (U = BitboardMyR; U; BitClear(sq, U)) { sq = BSF(U); AttR = AttR(sq); T = AttR & mask; AddTo(T, CaptureValue[EnumMyR][c]); } for (U = BitboardMyQ; U; BitClear(sq, U)) { sq = BSF(U); AttR = AttR(sq); AttB = AttB(sq); T = (AttB | AttR) & mask; AddTo(T, CaptureValue[EnumMyQ][c]); } sq = BSF(BitboardMyK); T = AttK[sq] & mask &(~OppAttacked); AddTo(T, CaptureValue[EnumMyK][c]); NoTarget: for (U = BitboardMyP & BitBoardSeventhRank; U; BitClear(sq, U)) { sq = BSF(U); to = Forward(sq); if (Position->sq[to] == 0) { Add(List, FlagPromQ | (sq << 6) | to, PtoQ); if (AttN[to] & BitboardOppK) Add(List, FlagPromN | (sq << 6) | to, PtoN); } to = ForwardLeft(sq); if (sq != WhiteA7 && SqSet[to] & mask) { c = Position->sq[to]; Add(List, FlagPromQ | (sq << 6) | to, PromQueenCap); if (AttN[to] & BitboardOppK) Add(List, FlagPromN | (sq << 6) | to, PromKnightCap); } to = ForwardRight(sq); if (sq != WhiteH7 && SqSet[to] & mask) { c = Position->sq[to]; Add(List, FlagPromQ | (sq << 6) | to, PromQueenCap); if (AttN[to] & BitboardOppK) Add(List, FlagPromN | (sq << 6) | to, PromKnightCap); } } List->move = 0; return List; }
typeMoveList *MyEvasion(typePos *Position, typeMoveList *List, uint64 c2) { uint64 U, T, att, mask; int sq, to, fr, c, king, pi; king = MyKingSq; att = MyKingCheck; sq = BSF(att); pi = Position->sq[sq]; mask = (~OppAttacked) &(((pi == EnumOppP) ? AttK[king] : 0) | Evade(king, sq)) & (~MyOccupied) &c2; BitClear(sq, att); if (att) { sq = BSF(att); pi = Position->sq[sq]; mask = mask &(PieceIsOppPawn(pi) | Evade(king, sq)); sq = king; AddTo(mask, CaptureValue[EnumMyK][c]); List->move = 0; return List; } c2 &= InterPose(king, sq); sq = king; AddTo(mask, CaptureValue[EnumMyK][c]); if (!c2) { List->move = 0; return List; } if (CaptureRight &(c2 & OppOccupied)) { to = BSF(c2 & OppOccupied); c = Position->sq[to]; if (EighthRank(to)) { Add(List, FlagPromQ | FromLeft(to) | to, (0x20 << 24) + CaptureValue[EnumMyP][c]); Add(List, FlagPromN | FromLeft(to) | to, 0); Add(List, FlagPromR | FromLeft(to) | to, 0); Add(List, FlagPromB | FromLeft(to) | to, 0); } else Add(List, FromLeft(to) | to, CaptureValue[EnumMyP][c]); } if (CaptureLeft &(c2 & OppOccupied)) { to = BSF(c2 & OppOccupied); c = Position->sq[to]; if (EighthRank(to)) { Add(List, FlagPromQ | FromRight(to) | to, (0x20 << 24) + CaptureValue[EnumMyP][c]); Add(List, FlagPromN | FromRight(to) | to, 0); Add(List, FlagPromR | FromRight(to) | to, 0); Add(List, FlagPromB | FromRight(to) | to, 0); } else Add(List, FromRight(to) | to, CaptureValue[EnumMyP][c]); } to = Position->Dyn->ep; if (to) { if (CaptureRight & SqSet[to] && SqSet[Backward(to)] & c2) Add(List, FlagEP | FromLeft(to) | to, CaptureValue[EnumMyP][EnumOppP]); if (CaptureLeft & SqSet[to] && SqSet[Backward(to)] & c2) Add(List, FlagEP | FromRight(to) | to, CaptureValue[EnumMyP][EnumOppP]); } T = BitboardMyP & BackShift((c2 & OppOccupied) ^ c2); while (T) { fr = BSF(T); BitClear(fr, T); if (SeventhRank(fr)) { Add(List, FlagPromQ | (fr << 6) | Forward(fr), CaptureValue[EnumMyP][0]); Add(List, FlagPromN | (fr << 6) | Forward(fr), 0); Add(List, FlagPromR | (fr << 6) | Forward(fr), 0); Add(List, FlagPromB | (fr << 6) | Forward(fr), 0); } else Add(List, (fr << 6) | Forward(fr), CaptureValue[EnumMyP][0]); } T = BitboardMyP & BackShift2((c2 & OppOccupied) ^ c2) & SecondRank & BackShift(~Position->OccupiedBW); while (T) { fr = BSF(T); BitClear(fr, T); Add(List, (fr << 6) | Forward2(fr), CaptureValue[EnumMyP][0]); } for (U = BitboardMyN; U; BitClear(sq, U)) { sq = BSF(U); T = AttN[sq] & c2; AddTo(T, CaptureValue[EnumMyN][c]); } for (U = BitboardMyB; U; BitClear(sq, U)) { sq = BSF(U); T = AttB(sq) & c2; AddTo(T, CaptureValue[EnumMyBL][c]); } for (U = BitboardMyR; U; BitClear(sq, U)) { sq = BSF(U); T = AttR(sq) & c2; AddTo(T, CaptureValue[EnumMyR][c]); } for (U = BitboardMyQ; U; BitClear(sq, U)) { sq = BSF(U); T = AttQ(sq) & c2; AddTo(T, CaptureValue[EnumMyQ][c]); } List->move = 0; return List; }
void PIC::decodeCmd(int pc) { // { qDebug() << "CMDLIST" <<m_CmdList[pc]; //qDebug() << pc << "PC"; k_long=m_CmdList[pc] & 0x7FF; //qDebug() << k_long << "klong"; k=m_CmdList[pc] & 0xFF; qDebug() << k << "k"; f=m_CmdList[pc] & 0x7F; //qDebug() << f <<"f"; d=m_CmdList[pc] & 0x80; d=(d>>7); //Test l=d; b=m_CmdList[pc] & 0x380; b = b / 128; //qDebug() << b << "b"; qDebug() << PreScalerWert << "PreScalerWert"; PIC::getPreScaler(); PIC::SetBank(); PIC::ChkIndirect(); CheckIndirect(); int ByteCmd=m_CmdList[pc] & 0x3F00; //qDebug() << ByteCmd << "byteCMD"; int BitCmd=m_CmdList[pc] & 0x3C00; //qDebug() << BitCmd << "BitCMD"; int ShrtCmd=m_CmdList[pc] & 0x3800; //qDebug() << ShrtCmd << "ShrtCMD"; if(ByteCmd == 0x0700 ) ADDWF(); else if(ByteCmd == 0x0500) ANDWF(); else if((m_CmdList[pc] & 0x03F80) == 0x0180) CLRF(); else if(ByteCmd == 0x0100) CLRW(); else if(ByteCmd == 0x0900) COMF(); else if(ByteCmd == 0x0300) DECF(); else if(ByteCmd == 0x0B00) DECFSZ(); else if(ByteCmd == 0x0A00) INCF(); else if(ByteCmd == 0x0F00) INCFSZ(); else if(ByteCmd == 0x0400) IORWF(); else if(ByteCmd == 0x0800) MOVF(); else if((m_CmdList[pc] & 0x3F80) == 0x0080) MOVWF(); else if((m_CmdList[pc] & 0x3F9F) == 0x0000) NOP(); else if(ByteCmd == 0x0D00) RLF(); else if(ByteCmd == 0x0C00) RRF(); else if(ByteCmd == 0x0200) SUBWF(); else if(ByteCmd == 0x0E00) SWAPF(); else if(ByteCmd == 0x0600) XORWF(); else if(BitCmd == 0x1000) BCF(); else if(BitCmd == 0x1400) BSF(); else if(BitCmd == 0x1800) BTFSC(); else if(BitCmd == 0x1C00) BTFSS(); //ADDLW kann durch don't care Bit 3E bzw. 3F sein else if((m_CmdList[pc] & 0x3E00 ) == 0x3E00) ADDLW(); else if((m_CmdList[pc] & 0x3F00 ) == 0x3F00) ADDLW(); else if(ByteCmd == 0x3900) ANDLW(); else if(ShrtCmd == 0x2000) CALL(); else if((m_CmdList[pc]& 0XFFFF) == 0x0064) CLRWDT(); else if(ShrtCmd == 0x2800) GOTO(); else if((ByteCmd) == 0x3A00) XORLW(); else if((m_CmdList[pc] & 0x3E00 ) == 0x3C00) SUBLW(); else if((m_CmdList[pc] & 0xFFFF ) == 0x0063) SLEEP(); else if((m_CmdList[pc] & 0xFFFF ) == 0x0008) RETURN(); else if((BitCmd) == 0x3400) RETURNLW(); else if((m_CmdList[pc] & 0xFFFF ) == 0x0009) RETURNFIE(); else if((ByteCmd) == 0x3000) MOVLW(); else if((ByteCmd) == 0x3100) MOVLW(); else if((ByteCmd) == 0x3200) MOVLW(); else if((ByteCmd) == 0x3300) MOVLW(); else if((ShrtCmd) == 0x3800) IORLW(); //zählt nach jeder Befehlsabarbeitung einen Programmzyklus hoch, bizyklische Befehle zählen zusätzlich während des Befehls rauf PIC::ExtClock(); PIC::IncrementCycles(); PIC::setTmr0(); PIC::LaufZeit(); //PreScalerCounter++; PIC::SyncSpecialReg(); PIC::RBPeakAnalyzer(); PIC::InterruptAnalyzer(); //Diagnoseausgaben qDebug() << "---------------------------------"; //qDebug() << regModel->reg[bank][PORTB] << "PortB"; qDebug() << regModel->reg[bank][INDIRECT] << "INDIRECT"; qDebug() << regModel->reg[bank][FSR] << "FSR"; //qDebug() << regModel->reg[bank][0x15] << "15h"; qDebug() << cycles << "Programmzyklen"; qDebug() << "---------------------------------"; }