plSHA1Checksum::plSHA1Checksum(size_t size, const uint8_t* buffer) { fValid = false; Start(); AddTo(size, buffer); Finish(); }
plMD5Checksum::plMD5Checksum(size_t size, uint8_t* buffer) { fValid = false; Start(); AddTo(size, buffer); Finish(); }
void plMD5Checksum::CalcFromStream(hsStream* stream) { uint32_t sPos = stream->GetPosition(); unsigned loadLen = 1024 * 1024; Start(); uint8_t *buf = new uint8_t[loadLen]; while (int read = stream->Read(loadLen, buf)) AddTo(read, buf); delete[] buf; Finish(); stream->SetPosition(sPos); }
void CalcForceGompper(Particle *xi, const int numNeighbors, Particle **const neighbors, const double kb) { // if ( xi->r.p[0] > maxX ) maxX = xi->r.p[0]; // DEBUG stuff /* for (int i=0; i < numNeighbors; i++) { Vector3D tmp; Subtr(tmp, xi, neighbors[i]); const double dist = Length(tmp); if ( dist > maxBendingDist ) maxBendingDist = dist; }*/ // mainNumerator: Will be set to one of the terms in the numerator. Vector3D mainNumerator; mainNumerator.el[0] = mainNumerator.el[1] = mainNumerator.el[2] = 0; // Will be set to the denominator. double denominator = 0; // We want to differentiate with respect to the i'th node ("virtual" index numDerivatives-1) and with respect to the neighbours of the i'th node (indecies 0<=l<=numDerivatives-2). const int numDerivatives = numNeighbors + 1; // The derivatives of the numerator in the energy with respect to the i'th node and its neighbours. Matrix3D *derivNumerators = new Matrix3D[numDerivatives]; // The derivatives of the denominator in the energy with respect to the i'th node and its neighbours. Vector3D *derivDenominators = new Vector3D[numDerivatives]; for (int i=0; i < numDerivatives; i++) { for ( int k=0; k < 3; k++) { derivDenominators[i].el[k] = 0; for (int l=0; l < 3; l++) derivNumerators[i].el[k][l] = 0; } } // We need to calculate several sums over the neighbouring sites of i. This is done in the for-loop. for (int curNeighborIdx = 0; curNeighborIdx < numNeighbors; ++curNeighborIdx) { // Get the neighbours of xi. const Particle *xj = neighbors[curNeighborIdx]; // Neighbor list with periodic mapping const Particle *xjM1 = (curNeighborIdx == 0) ? neighbors[numNeighbors-1] : neighbors[curNeighborIdx-1]; const Particle *xjP1 = (curNeighborIdx == numNeighbors-1) ? neighbors[0] : neighbors[curNeighborIdx+1]; Vector3D tmp; Subtr(tmp, xi, xj); const double dxijLen2 = LengthSqr(tmp); // Cosine of the angles between the neighbours. const double cosThetaM1 = CalcCosTheta(xi, xj, xjM1); const double cosThetaP1 = CalcCosTheta(xi, xj, xjP1); // The sum of the two cotangens. const double Tij = CalcCot(cosThetaM1) + CalcCot(cosThetaP1); Vector3D ijDifference; Subtr(ijDifference, xi, xj); // Update the two terms which are independent of the node xl (the derivatives are with respect to node xl). Multiply(tmp, ijDifference, Tij); AddTo( mainNumerator, tmp); denominator += dxijLen2 * Tij; // Now for the other terms, dependent on xl. // TODO: The only terms which are not 0 are jM1, j, jP1 and i for (int gradientNodeIdx = 0; gradientNodeIdx < numDerivatives; ++gradientNodeIdx) { const Particle *const xl = (gradientNodeIdx == numDerivatives-1) ? xi : neighbors[gradientNodeIdx]; // Derivatives of the two cotangens. Vector3D TijDeriv; CalcCotDerivativeGompperAnalyt(TijDeriv, xi, xj, xjM1, xl->p.identity); Vector3D tmp; CalcCotDerivativeGompperAnalyt(tmp, xi, xj, xjP1, xl->p.identity); AddTo( TijDeriv, tmp); // Kronecker Deltas. const int ilDelta = (xi->p.identity == xl->p.identity) ? 1 : 0; const int jlDelta = (xj->p.identity == xl->p.identity) ? 1 : 0; // Update Matrix3D tmpM; DyadicProduct(tmpM, ijDifference, TijDeriv); AddTo( derivNumerators[gradientNodeIdx], tmpM ); AddScalarTo( derivNumerators[gradientNodeIdx], Tij*(ilDelta-jlDelta) ); Multiply(tmp, ijDifference, Tij*2. * (ilDelta-jlDelta) ); AddTo( derivDenominators[gradientNodeIdx], tmp ); Multiply(tmp, TijDeriv, dxijLen2); AddTo( derivDenominators[gradientNodeIdx], tmp ); } } // Calculate the contribution to the force for each node. for (int gradientNodeIdx = 0; gradientNodeIdx < numDerivatives; ++gradientNodeIdx) { Particle *const xl = (gradientNodeIdx == numDerivatives-1) ? xi : neighbors[gradientNodeIdx]; // -1: The force is minus the gradient of the energy. // Note: left vector-matrix product: mainNumerator * derivNumerators[gradientNodeIdx] Vector3D force, tmp; LeftVectorMatrix(force, mainNumerator, derivNumerators[gradientNodeIdx]); Multiply(force, force, 4.0 / denominator ); Multiply(tmp, derivDenominators[gradientNodeIdx], - 2.0 * LengthSqr(mainNumerator) / (denominator*denominator)); AddTo(force, tmp); Multiply(force, force, (-1.0) * (kb / 2.0)); // if ( xl->p.identity == 0) // printf(" Adding to node = %d when treating node %d: f = %.12e %.12e %.12e\n", xl->p.identity, xi->p.identity, force.el[0], force.el[1], force.el[2]); xl->f.f[0] += force.el[0]; xl->f.f[1] += force.el[1]; xl->f.f[2] += force.el[2]; // DEBUG stuff /* for (int i=0; i < numNeighbors; i++) { const double f = Length(force); if ( f > maxBendingForce ) maxBendingForce = f; }*/ } // Clean up delete []derivNumerators; delete []derivDenominators; }
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; }
int Client::run() { // Create a reliable messaging sequence handle for client-initiated session soap_wsrm_sequence_handle seq; xsd__duration expires = 30000; /* 30000 ms = 30 seconds to expire */ const char *id = soap_wsa_rand_uuid(soap); double n; int retry; printf("\n**** Creating the Sequence\n"); // Create session for in-order messaging, init sequence handle if (soap_wsrm_create_offer(soap, serverURI, clientURI, id, expires, NoDiscard, soap_wsa_rand_uuid(soap), &seq)) { soap_stream_fault(std::cerr); soap_wsrm_seq_free(soap, seq); return soap->error; } // poll 100 times for .1 second until created for (retry = 100; retry && !soap_wsrm_seq_created(soap, seq); retry--) { if (poll(-100000)) return soap->error; } if (!retry) { fprintf(stderr, "CANNOT CREATE SEQUENCE - SERVER NOT RESPONDING\n"); exit(1); } // Reliable messaging request message if (soap_wsrm_request_acks(soap, seq, soap_wsa_rand_uuid(soap), "http://Microsoft.Samples.DualHttp/ICalculatorDuplex/AddTo")) { soap_stream_fault(std::cerr); return soap->error; } n = 3.14; _mssadh__AddTo addTo; addTo.n = &n; if (AddTo(&addTo) == SOAP_OK) std::cout << std::endl << "**** AddTo(" << *addTo.n << ")" << std::endl; else soap_stream_fault(std::cerr); destroy(); #ifndef CB_THREAD // callback polling: 500 ms polling cycle if (poll(-500000)) return soap->error; #endif // Reliable messaging request message if (soap_wsrm_request_acks(soap, seq, soap_wsa_rand_uuid(soap), "http://Microsoft.Samples.DualHttp/ICalculatorDuplex/SubtractFrom")) { soap_stream_fault(std::cerr); return soap->error; } n = 1.41; _mssadh__SubtractFrom subtractFrom; subtractFrom.n = &n; if (SubtractFrom(&subtractFrom) == SOAP_OK) std::cout << std::endl << "**** SubtractFrom(" << *subtractFrom.n << ")" << std::endl; else soap_stream_fault(std::cerr); destroy(); #ifndef CB_THREAD // callback polling: 500 ms polling cycle if (poll(-500000)) return soap->error; #endif // Reliable messaging request message if (soap_wsrm_request_acks(soap, seq, soap_wsa_rand_uuid(soap), "http://Microsoft.Samples.DualHttp/ICalculatorDuplex/Clear")) { soap_stream_fault(std::cerr); return soap->error; } _mssadh__Clear clear; if (Clear(&clear) == SOAP_OK) std::cout << std::endl << "**** Clear()" << std::endl; else soap_stream_fault(std::cerr); destroy(); #ifndef CB_THREAD // callback polling: 500 ms polling cycle if (poll(-500000)) return soap->error; #endif printf("\n**** Closing the Sequence\n"); if (soap_wsrm_close(soap, seq, soap_wsa_rand_uuid(soap))) { soap_stream_fault(std::cerr); soap_wsrm_seq_free(soap, seq); return soap->error; } for (retry = 10; retry; retry--) { // Receive more messages when last not yet received if (soap_wsrm_lastnum(seq) == 0) { #ifdef CB_THREAD // we want to pulse here to send acks for incoming messages to keep flow going soap_wsrm_pulse(soap, -500000); /* 500 ms */ #endif printf("\n**** Receiving Messages Until Last\n"); // callback polling or delay: 500 ms polling cycle if (poll(-500000)) return soap->error; } // Resend messages marked as non-acked (as an option) if (soap_wsrm_nack(seq)) { printf("\n**** Resending "SOAP_ULONG_FORMAT" Non-Acked Messages\n", soap_wsrm_nack(seq)); soap_wsrm_resend(soap, seq, 0, 0); /* 0 0 means full range of msg nums */ } } if (soap_wsrm_nack(seq) || soap_wsrm_lastnum(seq) == 0) { printf("\n**** Incomplete Sequence\n"); soap_wsrm_dump(soap, stdout); } printf("\n**** Terminating the Sequence\n"); // Termination fails if the server did not get all messages if (soap_wsrm_terminate(soap, seq, soap_wsa_rand_uuid(soap))) { soap_stream_fault(std::cerr); soap_wsrm_seq_free(soap, seq); return soap->error; } #ifdef CB_THREAD soap_wsrm_dump(soap, stdout); #endif // callback polling: 1 s polling cycle if (poll(1)) return soap->error; // Delete the reliable messaging session sequence (assuming no more inbound messages) soap_wsrm_seq_free(soap, seq); return 0; }