ComplexPair BandStopTransform::transform (complex_t c) { if (c == infinity()) c = -1; else c = (1. + c) / (1. - c); // bilinear complex_t u (0); u = addmul (u, 4 * (b2 + a2 - 1), c); u += 8 * (b2 - a2 + 1); u *= c; u += 4 * (a2 + b2 - 1); u = std::sqrt (u); complex_t v = u * -.5; v += a; v = addmul (v, -a, c); u *= .5; u += a; u = addmul (u, -a, c); complex_t d (b + 1); d = addmul (d, b-1, c); return ComplexPair (u/d, v/d); }
complex_t BiquadBase::response (double normalizedFrequency) const { const double a0 = getA0 (); const double a1 = getA1 (); const double a2 = getA2 (); const double b0 = getB0 (); const double b1 = getB1 (); const double b2 = getB2 (); const double w = 2 * doublePi * normalizedFrequency; const complex_t czn1 = std::polar (1., -w); const complex_t czn2 = std::polar (1., -2 * w); complex_t ch (1); complex_t cbot (1); complex_t ct (b0/a0); complex_t cb (1); ct = addmul (ct, b1/a0, czn1); ct = addmul (ct, b2/a0, czn2); cb = addmul (cb, a1/a0, czn1); cb = addmul (cb, a2/a0, czn2); ch *= ct; cbot *= cb; return ch / cbot; }
ComplexPair BandPassTransform::transform (complex_t c) { if (c == infinity()) return ComplexPair (-1, 1); c = (1. + c) / (1. - c); // bilinear complex_t v = 0; v = addmul (v, 4 * (b2 * (a2 - 1) + 1), c); v += 8 * (b2 * (a2 - 1) - 1); v *= c; v += 4 * (b2 * (a2 - 1) + 1); v = std::sqrt (v); complex_t u = -v; u = addmul (u, ab_2, c); u += ab_2; v = addmul (v, ab_2, c); v += ab_2; complex_t d = 0; d = addmul (d, 2 * (b - 1), c) + 2 * (1 + b); return ComplexPair (u/d, v/d); }
void fun(ulong a, ulong b) { LOCAL_HIREMAINDER; LOCAL_OVERFLOW; addll(a,b); addllx(a,b); mulll(a,b); addmul(a,b); #if 0 bfffo(a); #endif }
complex_t Cascade::response (double normalizedFrequency) const { double w = 2 * doublePi * normalizedFrequency; const complex_t czn1 = std::polar (1., -w); const complex_t czn2 = std::polar (1., -2 * w); complex_t ch (1); complex_t cbot (1); const Biquad* stage = m_stageArray; for (int i = m_numStages; --i >=0; ++stage) { complex_t cb (1); complex_t ct (stage->getB0()/stage->getA0()); ct = addmul (ct, stage->getB1()/stage->getA0(), czn1); ct = addmul (ct, stage->getB2()/stage->getA0(), czn2); cb = addmul (cb, stage->getA1()/stage->getA0(), czn1); cb = addmul (cb, stage->getA2()/stage->getA0(), czn2); ch *= ct; cbot *= cb; } return ch / cbot; }
/* Handle additional data, same for en/de crypt This does not use ChaCha data so need not be rerun when you have done auth & want to reset ChaCha for the decryption */ static void do_additional( const byte *ad, u64 adlen ) { u64 n ; assert( ad != NULL ) ; memset( accum, 0, AES_BYTES ) ; while (adlen > 0) { n = (u64) ((adlen >= AES_BYTES)? AES_BYTES : adlen) ; addmul((byte *) accum,ad,n,(byte *) H); ad += n ; adlen -= n; } #ifdef TESTING printf("accum after ad: %08x %08x %08x %08x\n", accum[0], accum[1], accum[2], accum[3] ) ; #endif }
/* simple interface to auth.c call addmul() with full block of data operate on global accum[] */ static void mix_in( u32 *data ) { addmul( (byte *) accum, (byte *) data, AES_BYTES, (byte *) H ) ; }
int crypto_aead_decrypt( unsigned char *m,unsigned long long *outputmlen, unsigned char *nsec, const unsigned char *c,unsigned long long clen, const unsigned char *ad,unsigned long long adlen, const unsigned char *npub, const unsigned char *k ) { u64 mlen, n, origmlen, start ; const byte *ptr, *origc; byte temp[AES_BYTES], *p ; int i ; assert( m != NULL ) ; assert( outputmlen != NULL ) ; assert( c != NULL ) ; assert( ad != NULL ) ; assert( npub != NULL ) ; assert( k != NULL ) ; if( clen < AUTH_BYTES) return -1; /* where auth data should be */ mlen = clen - AUTH_BYTES ; ptr = c + mlen ; *outputmlen = mlen; setup(mlen, adlen, npub, k) ; do_additional( ad, adlen ) ; origc = c; origmlen = mlen; #ifdef TESTING printf("decrypt: total %llu text %llu ad %llu\n", clen, mlen, adlen ) ; printf("decrypt: ciphertext starts with %08xu\n", ((u32 *) origc)[0] ) ; printf("decrypt: starting authentication check %08x\n", ((u32 *) ptr)[0] ) ; #endif /* authenticate */ start = mlen ; while( (mlen > 0) && (mlen <= start) ) { n = (u64) ((mlen >= AES_BYTES) ? AES_BYTES : mlen) ; addmul( (byte *) accum, c, n, (byte *) H) ; c += n; mlen -= n; } if( mlen > start ) { #ifdef TESTING printf("decrypt: mlen out of bounds\n" ) ; #endif return -2 ; } #ifdef TESTING printf("decrypt: auth before finalisation %08x %08x\n", accum[0], accum[1] ) ; #endif mix_in(finalblock) ; xor_128( accum, I ) ; /* see if we have a match */ if( memcmp((void *) accum, (void *) ptr, AUTH_BYTES) != 0) { #ifdef TESTING printf("decrypt: auth failed, result here %08x\n", accum[0] ) ; #endif return -3; } #ifdef TESTING printf("decrypt: auth matched, going to decrypt\n" ) ; printf("decrypt: 1st ciphertext word %08x\n", *((u32 *) c) ) ; #endif /* reset things start encryption with correct ChaCha state */ c = origc; mlen = origmlen; setup(mlen, adlen, npub, k) ; /* decrypt, simpler than encryption no authentication to do */ start = mlen ; while( (mlen > 0) && (mlen <= start) ) { n = (u64) ((mlen >= AES_BYTES)? AES_BYTES : mlen) ; white_aes( temp ) ; for( i = 0, p = temp ; i < n ; i++, c++, m++, p++ ) *m = *c ^ *p ; mlen -= n; } if( mlen > start ) { #ifdef TESTING printf("decrypt: mlen out of bounds\n" ) ; #endif return -2 ; } #ifdef TESTING printf("decrypt: reached end, showing first words\n" ) ; printf("plaintext %08x ciphertext %08x auth %08x\n",*((u32 *) m), *((u32 *) c), accum[0] ) ; #endif memset( temp, 0, AES_BYTES ) ; cleanup() ; return 0; }
/* declarations for this and decrypt copied from Bernstein's example AES-GCM code to be sure they match test framework */ int crypto_aead_encrypt( unsigned char *c,unsigned long long *clen, const unsigned char *m, unsigned long long mlen, const unsigned char *ad,unsigned long long adlen, const unsigned char *nsec, const unsigned char *npub, const unsigned char *k ) { u64 n, start ; byte temp[AES_BYTES] ; byte *p, *q, *ptr ; int i ; #ifdef TESTING byte *origm, *origc ; assert( c != NULL ) ; /* are framework declarations compatible with mine? */ assert( sizeof(long long) == sizeof( u64) ) ; printf("encrypt: plaintext %llu ad %llu\n", mlen, adlen ) ; origm = m ; origc = c ; #endif /* Bernstein's example aes-gcm code sets *clen I copy that, assuming the interface needs it */ *clen = mlen+AUTH_BYTES ; /* address for authentication data */ ptr = c + mlen ; /* key schedule */ setup( mlen, (u64) adlen, npub, k) ; /* data authenticated but not encrypted */ do_additional( ad, (u64) adlen ) ; /* encryption mlen is unsigned so always > = 0 use start for an error check */ start = mlen ; while( (mlen != 0) && (mlen <= start) ) { n = (u64) ((mlen >= AES_BYTES)? AES_BYTES : mlen) ; white_aes( temp ) ; for( i = 0, p = temp, q = c ; i < n ; i++, q++, m++, p++ ) *q = *m ^ *p ; addmul( (byte *) accum, c, n, (byte *) H) ; mlen -= n; c += n ; } if( mlen > start ) { #ifdef TESTING printf("encrypt: mlen out of bounds\n" ) ; #endif return -2 ; } #ifdef TESTING printf("encrypt: auth before finalisation %08x %08x\n", accum[0], accum[1] ) ; #endif /* finish authentication */ mix_in(finalblock) ; xor_128( accum, I ) ; memcpy( ptr, (void *) accum, AES_BYTES ) ; #ifdef TESTING printf("encrypt: reached end, showing first words\n" ) ; printf("plaintext %08x ciphertext %08x auth %08x\n", *((u32 *) origm), *((u32 *) origc), accum[0] ) ; #endif memset( temp, 0, AES_BYTES ) ; cleanup() ; return 0 ; }
int NewtonCuda::oneStep() { //Timer timer; std::vector<Vector3f> force = m->getForce(); float E = m->getEnergy(); std::vector<float> val; //timer.start(); if (m_fixRigidMotion) { m->getStiffnessSparse(val, false,true,true); } else { m->getStiffnessSparse(val, false,true); } //timer.end(); //std::cout << "assembly time: " << timer.getSeconds() << "\n"; int ndof = 3 * (int)m->x.size(); std::vector<float> bb(ndof); for (unsigned int ii = 0; ii<m->x.size(); ii++){ for (int jj = 0; jj<3; jj++){ int row = 3 * ii + jj; if (m->fixed[ii]){ bb[row] = 0; } else{ bb[row] = force[ii][jj]; } } } if (m_fixRigidMotion) { for(int ii =0;ii<6;ii++){ bb.push_back(0.f); } } //timer.start(); solver.solve(val, &(bb[0])); //timer.end(); //std::cout << "lin solve time: " << timer.getSeconds() << "\n"; //timer.start(); double totalMag = 0; for (int ii = 0; ii<ndof; ii++){ totalMag += std::abs(bb[ii]); } totalMag /= ndof; for (unsigned int ii = 0; ii<m->x.size(); ii++){ for (int jj = 0; jj<3; jj++){ force[ii][jj] = (float)bb[3 * ii + jj]; } } //std::cout << "total disp: " << totalMag << " ,h: " << h << "\n"; std::cout<< "E: " << E << "\n"; //line search std::vector<Vector3f> x0 = m->x; h = 1.0f; float E1 = E; bool valid = true; while (1){ if (totalMag * h<dx_tol){ return -1; } m->x = x0; addmul(m->x, h, force); E1 = m->getEnergy(); if (E1>E || fem_error){ fem_error = 0; h = 0.5f* h; if (h<1e-12){ valid = false; break; } } else{ break; } } //timer.end(); //std::cout << "line search time: " << timer.getSeconds() << "\n"; if (!valid){ return 1; } return 0; }
int main() { cELF *elf_a = new cELF((char *)"testlib_a.elf"); // get text segment cSection *text = elf_a->getSectionByName((char *)".text"); printf(".text section: fileoffset=%p\n", text->getFileOffset()); unsigned char *textptr = elf_a->image + text->getFileOffset(); // apply relocation records for .text inside our "image" (textptr points into it) int test_a = 10; int test_b = 100; //test_a = test_b = 0; elf_a->importSymbol((int)text->getFileOffset(), (char *)"_test_a", &test_a); elf_a->importSymbol((int)text->getFileOffset(), (char *)"_test_b", &test_b); elf_a->importSymbol((int)text->getFileOffset(), (char *)"_printf", (void *)printf); elf_a->importSymbol((int)text->getFileOffset(), (char *)"_puts", (void *)puts); elf_a->importSymbol((int)text->getFileOffset(), (char *)"_vprintf", (void *)vprintf); cSymbol *d1 = elf_a->getSymbolByName((char *)"_someData1"); cSymbol *d2 = elf_a->getSymbolByName((char *)"_someData2"); cSymbol *d3 = elf_a->getSymbolByName((char *)"_someData3"); printf("someData1 symbol segment offset: %llp symbol->getFileOffset()=%llp text=%s\n", d1->getSegmentOffset(), d1->getFileOffset(), d1->getAbsoluteFileOffset() ); printf("someData2 symbol segment offset: %llp symbol->getFileOffset()=%llp text=%s\n", d2->getSegmentOffset(), d2->getFileOffset(), d2->getAbsoluteFileOffset() ); printf("someData3 symbol segment offset: %llp symbol->getFileOffset()=%llp text=%s\n", d3->getSegmentOffset(), d3->getFileOffset(), d3->getAbsoluteFileOffset() ); printf("should be .text : file_offset=%p\n", elf_a->getSectionByInternalID(0)->getFileOffset()); printf("should be .data : file_offset=%p\n", elf_a->getSectionByInternalID(1)->getFileOffset()); printf("should be .bss : file_offset=%p\n", elf_a->getSectionByInternalID(2)->getFileOffset()); printf("should be .rdata: file_offset=%p\n", elf_a->getSectionByInternalID(3)->getFileOffset()); // get the function pointers int (*add)(int a, int b); int (*mul)(int a, int b); int (*printsomething)(char *msg, ...); void (*printglobals)(); *(int *)&add = elf_a->getProcAddress((char *)"_add"); *(int *)&mul = elf_a->getProcAddress((char *)"_mul"); *(int *)&printsomething = elf_a->getProcAddress((char *)"_printsomething"); *(int *)&printglobals = elf_a->getProcAddress((char *)"_printglobals"); // call it printf("add=%p mul=%p\n", add, mul); printf("lib.c add(2,2) = %d\n", add(2,2)); printf("lib.c mul(3,3) = %d\n", mul(3,3)); printsomething((char *)"haiiiii! 123=%d\n", 123); elf_a->relocateTextForRdata(); elf_a->relocateTextForData(); printglobals(); printglobals(); printglobals(); printf("End!\n"); #if 0 cELF *elf_b = new cELF((char *)"testlib_b.elf"); int (*addmul)(int a, int b, int factor); elf_b->importSymbol((char *)"_add", (void *)add); elf_b->importSymbol((char *)"_mul", (void *)mul); *(int *)&addmul = elf_b->getProcAddress((char *)"_addmul"); printf("addmul: %p\n", addmul); printf("lib.c addmul(3,3, 2) = %d\n", addmul(3, 3, 2)); printf("Address of printf=%p real printf=%p\n", GetProcAddress(NULL, "printf"), printf); #endif return 0; }