CVector3D CConeProjectionGeometry3D::getProjectionDirection(int _iProjectionIndex, int _iDetectorIndex) const { float32 fSrcX = -m_fOriginSourceDistance; float32 fSrcY = 0.0f; float32 fSrcZ = 0.0f; float32 fDetX = m_fOriginDetectorDistance; float32 fDetY = 0.0f; float32 fDetZ = 0.0f; fDetY += indexToDetectorOffsetX(_iDetectorIndex); fDetZ += indexToDetectorOffsetY(_iDetectorIndex); float32 angle = m_pfProjectionAngles[_iProjectionIndex]; #define ROTATE(name,alpha) do { float32 tX = f##name##X * cos(alpha) - f##name##Y * sin(alpha); f##name##Y = f##name##X * sin(alpha) + f##name##Y * cos(alpha); f##name##X = tX; } while(0) ROTATE(Src, angle); ROTATE(Det, angle); #undef ROTATE CVector3D ret(fDetX - fSrcX, fDetY - fSrcY, fDetZ - fDetZ); return ret; }
static inline void chacha_round(uint32_t x[16], int a, int b, int c, int d) { x[a] += x[b]; x[d] ^= x[a]; ROTATE(x[d], 16); x[c] += x[d]; x[b] ^= x[c]; ROTATE(x[b], 12); x[a] += x[b]; x[d] ^= x[a]; ROTATE(x[d], 8); x[c] += x[d]; x[b] ^= x[c]; ROTATE(x[b], 7); }
void sha1_process_block(sha1_ctx_t *p, const byte *block) { static uint32 K[4] = { 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 }; uint32 a, b, c, d, e, f, k, temp; a = p->h[0]; b = p->h[1]; c = p->h[2]; d = p->h[3]; e = p->h[4]; uint32 w[80]; int i; for (i = 0; i < 16; i++) { w[i] = block[i*4+0] << 24; w[i] |= block[i*4+1] << 16; w[i] |= block[i*4+2] << 8; w[i] |= block[i*4+3]; } for (i = 16; i < 80; i++) w[i] = ROTATE((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1); for (i = 0; i < 80; i++) { if (0 <= i && i < 20) { f = (b & c) ^ ((~b) & d); k = K[0]; } else if (20 <= i && i < 40) { f = b ^ c ^ d; k = K[1]; } else if (40 <= i && i < 60) { f = (b & c) ^ (b & d) ^ (c & d); k = K[2]; } else if (60 <= i && i < 80) { f = b ^ c ^ d; k = K[3]; } temp = ROTATE(a, 5) + f + e + k + w[i]; e = d; d = c; c = ROTATE(b, 30); b = a; a = temp; } p->h[0] += a; p->h[1] += b; p->h[2] += c; p->h[3] += d; p->h[4] += e; }
void ECRYPT_Trivium::TRIVIUM_process_bytes( int action, void* ctxa, const u8* input, u8* output, u32 msglen) { TRIVIUM_ctx* ctx = (TRIVIUM_ctx*)ctxa; m64 s11, s12; m64 s21, s22; m64 s31, s32; LOAD(ctx->state); for (; (int)(msglen -= 16) >= 0; output += 16, input += 16) { m64 t1, t2, t3, z[2]; UPDATE(); z[0] = XOR(XOR(s12, s22), s32); ROTATE(); UPDATE(); z[1] = XOR(XOR(s12, s22), s32); ROTATE(); M64TO64_CONVERT(z[0]); ((m64*)output)[0] = XOR(((m64*)input)[0], z[0]); M64TO64_CONVERT(z[1]); ((m64*)output)[1] = XOR(((m64*)input)[1], z[1]); } for (msglen += 16; (int)msglen > 0; msglen -= 8, output += 8, input += 8) { m64 t1, t2, t3, z; UPDATE(); z = XOR(XOR(s12, s22), s32); if (msglen >= 8) { M64TO64_CONVERT(z); ((m64*)output)[0] = XOR(((m64*)input)[0], z); } else { u32 i; for (i = 0; i < msglen; ++i, z = SF(z, 8)) output[i] = input[i] ^ M8V(z); } ROTATE(); } STORE(ctx->state); EMPTY(); }
void md5_digest (md5_t* self) { uint32_t data[16], a, b, c, d; a = self->rawhash[0]; b = self->rawhash[1]; c = self->rawhash[2]; d = self->rawhash[3]; for (int i = 0, j = 0; i < 16; ++i, j+=4) data[i] = (self->buffer[j] ) + (self->buffer[j + 1] << 8) + (self->buffer[j + 2] << 16) + (self->buffer[j + 3] << 24); for (int i = 0; i < 64; ++i) { uint32_t func, temp; if (i < 16) func = F(b,c,d); else if (i < 32) func = G(b,c,d); else if (i < 48) func = H(b,c,d); else if (i < 64) func = I(b,c,d); temp = d; d = c; c = b; b = b + ROTATE(a + func + data[M[i]] + K[i], S[i]); a = temp; } self->rawhash[0] += a; self->rawhash[1] += b; self->rawhash[2] += c; self->rawhash[3] += d; }
void TERNARY(stack& s) { NULLARY(s); ROTATE(s); POP(s); POP(s); SWAP(s); POP(s); }
/* --------------------------------------------------------------------------- * rotates the contents of the pastebuffer */ void RotateBuffer (BufferType *Buffer, BYTE Number) { /* rotate vias */ VIA_LOOP (Buffer->Data); { r_delete_entry (Buffer->Data->via_tree, (BoxType *)via); ROTATE_VIA_LOWLEVEL (via, Buffer->X, Buffer->Y, Number); SetPinBoundingBox (via); r_insert_entry (Buffer->Data->via_tree, (BoxType *)via, 0); } END_LOOP; /* elements */ ELEMENT_LOOP (Buffer->Data); { RotateElementLowLevel (Buffer->Data, element, Buffer->X, Buffer->Y, Number); } END_LOOP; /* all layer related objects */ ALLLINE_LOOP (Buffer->Data); { r_delete_entry (layer->line_tree, (BoxType *)line); RotateLineLowLevel (line, Buffer->X, Buffer->Y, Number); r_insert_entry (layer->line_tree, (BoxType *)line, 0); } ENDALL_LOOP; ALLARC_LOOP (Buffer->Data); { r_delete_entry (layer->arc_tree, (BoxType *)arc); RotateArcLowLevel (arc, Buffer->X, Buffer->Y, Number); r_insert_entry (layer->arc_tree, (BoxType *)arc, 0); } ENDALL_LOOP; ALLTEXT_LOOP (Buffer->Data); { r_delete_entry (layer->text_tree, (BoxType *)text); RotateTextLowLevel (text, Buffer->X, Buffer->Y, Number); r_insert_entry (layer->text_tree, (BoxType *)text, 0); } ENDALL_LOOP; ALLPOLYGON_LOOP (Buffer->Data); { r_delete_entry (layer->polygon_tree, (BoxType *)polygon); RotatePolygonLowLevel (polygon, Buffer->X, Buffer->Y, Number); r_insert_entry (layer->polygon_tree, (BoxType *)polygon, 0); } ENDALL_LOOP; /* finally the origin and the bounding box */ ROTATE (Buffer->X, Buffer->Y, Buffer->X, Buffer->Y, Number); RotateBoxLowLevel (&Buffer->BoundingBox, Buffer->X, Buffer->Y, Number); SetCrosshairRangeToBuffer (); }
void SbMatrix::setTransform(const SbVec3f &translation, const SbRotation &rotation, const SbVec3f &scaleFactor, const SbRotation &scaleOrientation, const SbVec3f ¢er) { #define TRANSLATE(vec) m.setTranslate(vec), multLeft(m) #define ROTATE(rot) rot.getValue(m), multLeft(m) SbMatrix m; makeIdentity(); if (translation != SbVec3f(0,0,0)) TRANSLATE(translation); if (center != SbVec3f(0,0,0)) TRANSLATE(center); if (rotation != SbRotation(0,0,0,1)) ROTATE(rotation); if (scaleFactor != SbVec3f(1,1,1)) { SbRotation so = scaleOrientation; if (so != SbRotation(0,0,0,1)) ROTATE(so); m.setScale(scaleFactor); multLeft(m); if (so != SbRotation(0,0,0,1)) { so.invert(); ROTATE(so); } } if (center != SbVec3f(0,0,0)) TRANSLATE(-center); #undef TRANSLATE #undef ROTATE }
void ECRYPT_Trivium::ECRYPT_ivsetup(const u8* iv) { TRIVIUM_ctx* ctx = &_ctx; const u32 ivlen = LOAD_IVLEN(ctx->init[0]); u32 i; u8* s = (u8*)(ctx->state + 2) + 16 - ivlen; m64 s11, s12; m64 s21, s22; m64 s31, s32; ctx->state[0] = ctx->init[0]; ctx->state[1] = ctx->init[1]; ctx->state[4] = PADDING; ctx->state[5] = 0; ctx->state[2] = ctx->state[3] = 0; for (i = 0; i < ivlen; ++i, ++s) *s = iv[i]; ctx->state[2] = U64TO64_CONVERT(ctx->state[2]); ctx->state[3] = U64TO64_CONVERT(ctx->state[3]); LOAD(ctx->state); // default 9 for (i = 0; i < numRounds; ++i) { m64 t1, t2, t3; UPDATE(); ROTATE(); UPDATE(); ROTATE(); } STORE(ctx->state); EMPTY(); }
int hash(const char * word) { int i; long hv = 0; for (i=0; i < 4 && *word != 0; i++) hv = (hv << 8) | (*word++); while (*word != 0) { ROTATE(hv,ROTATE_LEN); hv ^= (*word++); } return (unsigned long) hv % tablesize; }
static void printit(const char *arg) { int ch, rot; if ((rot = atoi(arg)) < 0) errx(1, "bad rotation value"); while ((ch = getchar()) != EOF) putchar(ROTATE(ch, rot)); exit(0); }
static void ff_dct_calc_c(DCTContext *s, FFTSample *data) { int n = 1<<s->nbits; int i; #define ROTATE(i,n) (-M_PI*((n)-0.5f)*(i)/(n)) if (s->inverse) { for(i=0; i < n; i++) { s->data[i].re = (float) (2 * data[i] * cos(ROTATE(i,n))); s->data[i].im = (float) (2 * data[i] * sin(ROTATE(i,n))); } s->data[n].re = 0; s->data[n].im = 0; for(i=0; i<n-1; i++) { s->data[n+i+1].re = (float) (-2 * data[n - (i+1)] * cos(ROTATE(n+i+1,n))); s->data[n+i+1].im = (float) (-2 * data[n - (i+1)] * sin(ROTATE(n+i+1,n))); } }else{ for(i=0; i < n; i++) { s->data[i].re = data[n - (i+1)]; s->data[i].im = 0; s->data[n+i].re = data[i]; s->data[n+i].im = 0; } } ff_fft_permute(&s->fft, s->data); ff_fft_calc(&s->fft, s->data); if (s->inverse) { for(i=0; i < n; i++) data[i] = s->data[n-(i+1)].re / (2 * n); }else { for(i=0; i < n; i++) data[i] = (float) (s->data[i].re / (2 * cos(ROTATE(i,n)))); } #undef ROTATE }
/** Rotate marked items, refer to a rotation point at position offset * Note: because this function is used in global transform, * if force_all is true, all items will be rotated */ void RotateMarkedItems( MODULE* module, wxPoint offset, bool force_all ) { #define ROTATE( z ) RotatePoint( (&z), offset, 900 ) if( module == NULL ) return; if( module->Reference().IsSelected() || force_all ) module->Reference().Rotate( offset, 900 ); if( module->Value().IsSelected() || force_all ) module->Value().Rotate( offset, 900 ); for( D_PAD* pad = module->Pads(); pad; pad = pad->Next() ) { if( !pad->IsSelected() && !force_all ) continue; wxPoint pos = pad->GetPos0(); ROTATE( pos ); pad->SetPos0( pos ); pad->SetOrientation( pad->GetOrientation() + 900 ); pad->SetDrawCoord(); } for( EDA_ITEM* item = module->GraphicalItems(); item; item = item->Next() ) { if( !item->IsSelected() && !force_all ) continue; switch( item->Type() ) { case PCB_MODULE_EDGE_T: ((EDGE_MODULE*) item)->Rotate( offset, 900 ); break; case PCB_MODULE_TEXT_T: static_cast<TEXTE_MODULE*>( item )->Rotate( offset, 900 ); break; default: break; } } ClearMarkItems( module ); }
int jacobi(double** a, double* d, double** v) { int nrot = 0; const unsigned int nmax = 100, n = 3; double b[nmax], z[nmax], tresh,sm,g,h,t,theta,c,s,tau; int i,j,ip,iq; for(ip=1;ip<=n;ip++) { for(iq=1;iq<=n;iq++) v[ip][iq] = 0.0f; v[ip][ip] = 1.0f; } for(ip=1;ip<=n;ip++) { b[ip] = d[ip] = a[ip][ip]; z[ip] = 0.0f; } for(i=1;i<=50;i++) { sm = 0.0f; for(ip=1;ip<=n-1;ip++) { for(iq=ip+1;iq<=n;iq++) sm += fabs(a[ip][iq]); } if(sm == 0.0f) return(0); if(i < 4) { tresh = 0.2f * sm / ( n*n ); } else { tresh = 0.0f; } for(ip=1;ip<=n-1;ip++) { for(iq=ip+1;iq<=n;iq++) { g = 100.0f*fabs(a[ip][iq]); if( (i > 4) && (double)(fabs(d[ip])+g) == (double)fabs(d[ip]) && (double)(fabs(d[iq])+g) == (double)fabs(d[iq])) a[ip][iq] = 0.0f; else if( fabs(a[ip][iq]) > tresh ) { h = d[iq] - d[ip]; if( (fabs(h)+g) == fabs(h) ) t = a[ip][iq] / h; else { theta = 0.5f * h / (a[ip][iq]); t = 1.0f / (fabs(theta) + sqrt(1.0f + theta*theta)); if(theta < 0.0f) t *= -1.0f; } c = 1.0f / sqrt(1.0f + t*t); s = t * c; tau = s / (1.0f + c); h = t * a[ip][iq]; z[ip] -= h; z[iq] += h; d[ip] -= h; d[iq] += h; a[ip][iq] = 0.0f; for(j=1;j<=ip-1;j++) { ROTATE(a,j,ip,j,iq); } for(j=ip+1;j<=iq-1;j++) { ROTATE(a,ip,j,j,iq); } for(j=iq+1;j<=n;j++) { ROTATE(a,ip,j,iq,j); } for(j=1;j<=n;j++) { ROTATE(v,j,ip,j,iq); } ++nrot; } } } for(ip=1;ip<=n;ip++) { b[ip] += z[ip]; d[ip] = b[ip]; z[ip] = 0.0f; } } assert(i<50); return 1; }
void jacobi(Matrix<double> &a, int n, double* d, Matrix<double> &v, int nrot) { int i, j, iq, ip; double tresh, theta, tau, t, sm, s, h, g, c; double* b = new double[n + 1]; double* z = new double[n + 1]; for (ip = 1; ip <= n; ip++) { ZeroMemory(v[ip], sizeof(double) * (n + 1)); v[ip][ip]=1.0; } for (ip = 1; ip <= n; ip++) { b[ip] = d[ip] = a[ip][ip]; z[ip] = 0.0; } nrot = 0; for (i = 1; i <= 50; i++) { sm = 0.0; for (ip = 1; ip <= n - 1; ip++) { for (iq = ip + 1; iq <= n; iq++) { sm += fabs(a[ip][iq]); } } if (sm == 0.0) { delete[] b; delete[] z; return; } if (i < 4) { tresh = 0.2 * sm / (n * n); } else { tresh = 0.0; } for (ip = 1;ip <= n - 1; ip++) { for (iq = ip + 1; iq <= n; iq++) { g = 100.0 * fabs(a[ip][iq]); if ((i > 4) && (fabs(d[ip]) + g == fabs(d[ip])) && (fabs(d[iq]) + g == fabs(d[iq]))) { a[ip][iq] = 0.0; } else if (fabs(a[ip][iq]) > tresh) { h = d[iq] - d[ip]; if (fabs(h) + g == fabs(h)) { t = (a[ip][iq]) / h; } else { theta = 0.5 * h / (a[ip][iq]); t = 1.0 / (fabs(theta) + sqrt(1.0 + theta * theta)); if (theta < 0.0) { t = -t; } } c = 1.0 / sqrt(1 + t * t); s = t * c; tau = s / (1.0 + c); h = t * a[ip][iq]; z[ip] -= h; z[iq] += h; d[ip] -= h; d[iq] += h; a[ip][iq]=0.0; for (j = 1;j <= ip - 1; j++) { ROTATE(a, j, ip, j, iq, tau, s); } for (j = ip + 1; j <= iq - 1; j++) { ROTATE(a, ip, j, j, iq, tau, s); } for (j = iq + 1; j <= n; j++) { ROTATE(a, ip, j, iq, j, tau, s); } for (j = 1; j <= n; j++) { ROTATE(v, j, ip, j, iq, tau, s); } ++nrot; } } } for (ip = 1; ip <= n; ip++) { b[ip] += z[ip]; d[ip] = b[ip]; z[ip] = 0.0; } } delete[] b; delete[] z; }
//Algorithm from the Wikipedia entry on Jacobi decomposition void jacobi_decompose(double cov_matrix[][NUM_PARAMS], double *eigenvalues, double orth_matrix[][NUM_PARAMS]) { int i,j,k,l; int max_col[NUM_PARAMS]; int changed[NUM_PARAMS]; //To keep track of eigenvalues changing int n = NUM_PARAMS; int max_row; int state = 0; double max, c, s, t, u, a; int count = 0; //Set up the maximum value cache for (i=0; i<n; i++) { max=0; max_col[i] = i+1; for (j=i+1; j<n; j++) { if (fabs(cov_matrix[i][j])>max) { max_col[i] = j; max = fabs(cov_matrix[i][j]); } } } //Set up the orthogonal matrix as the identity: for (i=0; i<n; i++) for (j=0; j<n; j++) orth_matrix[i][j] = (i==j) ? 1 : 0; //Copy the diagonal values to the eigenvalue array: for (i=0; i<n; i++) { eigenvalues[i] = cov_matrix[i][i]; changed[i] = 1; for (j=0; j<n; j++) if (j!=i && cov_matrix[i][j]) break; if (j==n) { state--; changed[i] = 0; } } //Sweep time: iterate until the eigenvalues stop changing. state += n; while (state) { count++; //Find the largest nonzero element in the matrix: max = fabs(cov_matrix[0][max_col[0]]); max_row = 0; for (i=1; i<n-1; i++) { if (fabs(cov_matrix[i][max_col[i]])>max) { max = fabs(cov_matrix[i][max_col[i]]); max_row = i; } } k = max_row; l = max_col[k]; max = cov_matrix[k][l]; //Calculate the Jacobi rotation matrix //Tan 2phi = 2S_kl / (S_kk - S_ll) a = (eigenvalues[l] - eigenvalues[k]) * 0.5; t = fabsl(a) + sqrtl(max*max + a*a); s = sqrtl(max*max + t*t); c = t/s; s = max/s; t = max*max / t; if (a<0) { s = -s; t = -t; } //Update eigenvalues #define UPDATE(x,y) \ a = eigenvalues[x]; \ eigenvalues[x] += y; \ if (changed[x] && (eigenvalues[x]==a)) { /*Eigenvalue didn't change*/ \ changed[x]=0; \ state--; \ } else if (!changed[x] && (eigenvalues[x]!=a)) { /*Egval did change*/ \ changed[x] = 1; \ state++; \ } UPDATE(k, -t); UPDATE(l, t); //Update covariance matrix: cov_matrix[k][l] = 0; #define ROTATE(m,w,x,y,z,r) /*Perform a Jacobi rotation*/ \ t = m[w][x]; u = m[y][z]; \ m[w][x] = t*c - s*u; \ m[y][z] = s*t + c*u; \ if (r) { \ if (fabs(m[w][x])>fabs(m[w][max_col[w]])) \ max_col[w] = x; \ if (fabs(m[y][z])>fabs(m[y][max_col[y]])) \ max_col[y] = z; \ } for (i=0; i<k; i++) { ROTATE(cov_matrix,i,k,i,l,1); } for (i=k+1; i<l; i++) { ROTATE(cov_matrix,k,i,i,l,1); } for (i=l+1; i<n; i++) { ROTATE(cov_matrix,k,i,l,i,1); } for (i=0; i<n; i++) { ROTATE(orth_matrix,k,i,l,i,0); } #undef ROTATE #undef UPDATE } // Restore the matrix to its original form: for (k=0; k<n-1; k++) { for (l=k+1; l<n; l++) { cov_matrix[k][l] = cov_matrix[l][k]; } } }
void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) { register DES_LONG l, r, t, u; #ifdef DES_PTR register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans; #endif #ifndef DES_UNROLL register int i; #endif register DES_LONG *s; r = data[0]; l = data[1]; IP(r, l); /* * Things have been modified so that the initial rotate is done outside * the loop. This required the DES_SPtrans values in sp.h to be rotated * 1 bit to the right. One perl script later and things have a 5% speed * up on a sparc2. Thanks to Richard Outerbridge * <*****@*****.**> for pointing this out. */ /* clear the top bits on machines with 8byte longs */ /* shift left by 2 */ r = ROTATE(r, 29) & 0xffffffffL; l = ROTATE(l, 29) & 0xffffffffL; s = ks->ks->deslong; /* * I don't know if it is worth the effort of loop unrolling the inner * loop */ if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l, r, 0); /* 1 */ D_ENCRYPT(r, l, 2); /* 2 */ D_ENCRYPT(l, r, 4); /* 3 */ D_ENCRYPT(r, l, 6); /* 4 */ D_ENCRYPT(l, r, 8); /* 5 */ D_ENCRYPT(r, l, 10); /* 6 */ D_ENCRYPT(l, r, 12); /* 7 */ D_ENCRYPT(r, l, 14); /* 8 */ D_ENCRYPT(l, r, 16); /* 9 */ D_ENCRYPT(r, l, 18); /* 10 */ D_ENCRYPT(l, r, 20); /* 11 */ D_ENCRYPT(r, l, 22); /* 12 */ D_ENCRYPT(l, r, 24); /* 13 */ D_ENCRYPT(r, l, 26); /* 14 */ D_ENCRYPT(l, r, 28); /* 15 */ D_ENCRYPT(r, l, 30); /* 16 */ #else for (i = 0; i < 32; i += 4) { D_ENCRYPT(l, r, i + 0); /* 1 */ D_ENCRYPT(r, l, i + 2); /* 2 */ } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l, r, 30); /* 16 */ D_ENCRYPT(r, l, 28); /* 15 */ D_ENCRYPT(l, r, 26); /* 14 */ D_ENCRYPT(r, l, 24); /* 13 */ D_ENCRYPT(l, r, 22); /* 12 */ D_ENCRYPT(r, l, 20); /* 11 */ D_ENCRYPT(l, r, 18); /* 10 */ D_ENCRYPT(r, l, 16); /* 9 */ D_ENCRYPT(l, r, 14); /* 8 */ D_ENCRYPT(r, l, 12); /* 7 */ D_ENCRYPT(l, r, 10); /* 6 */ D_ENCRYPT(r, l, 8); /* 5 */ D_ENCRYPT(l, r, 6); /* 4 */ D_ENCRYPT(r, l, 4); /* 3 */ D_ENCRYPT(l, r, 2); /* 2 */ D_ENCRYPT(r, l, 0); /* 1 */ #else for (i = 30; i > 0; i -= 4) { D_ENCRYPT(l, r, i - 0); /* 16 */ D_ENCRYPT(r, l, i - 2); /* 15 */ } #endif } /* rotate and clear the top bits on machines with 8byte longs */ l = ROTATE(l, 3) & 0xffffffffL; r = ROTATE(r, 3) & 0xffffffffL; FP(r, l); data[0] = l; data[1] = r; l = r = t = u = 0; }
void fcrypt_body (DES_LONG * out, DES_key_schedule * ks, DES_LONG Eswap0, DES_LONG Eswap1) { register DES_LONG l, r, t, u; #ifdef DES_PTR register const unsigned char *des_SP = (const unsigned char *) DES_SPtrans; #endif register DES_LONG *s; register int j; register DES_LONG E0, E1; l = 0; r = 0; s = (DES_LONG *) ks; E0 = Eswap0; E1 = Eswap1; for (j = 0; j < 25; j++) { #ifndef DES_UNROLL register int i; for (i = 0; i < 32; i += 4) { D_ENCRYPT (l, r, i + 0); /* 1 */ D_ENCRYPT (r, l, i + 2); /* 2 */ } #else D_ENCRYPT (l, r, 0); /* 1 */ D_ENCRYPT (r, l, 2); /* 2 */ D_ENCRYPT (l, r, 4); /* 3 */ D_ENCRYPT (r, l, 6); /* 4 */ D_ENCRYPT (l, r, 8); /* 5 */ D_ENCRYPT (r, l, 10); /* 6 */ D_ENCRYPT (l, r, 12); /* 7 */ D_ENCRYPT (r, l, 14); /* 8 */ D_ENCRYPT (l, r, 16); /* 9 */ D_ENCRYPT (r, l, 18); /* 10 */ D_ENCRYPT (l, r, 20); /* 11 */ D_ENCRYPT (r, l, 22); /* 12 */ D_ENCRYPT (l, r, 24); /* 13 */ D_ENCRYPT (r, l, 26); /* 14 */ D_ENCRYPT (l, r, 28); /* 15 */ D_ENCRYPT (r, l, 30); /* 16 */ #endif t = l; l = r; r = t; } l = ROTATE (l, 3) & 0xffffffffL; r = ROTATE (r, 3) & 0xffffffffL; PERM_OP (l, r, t, 1, 0x55555555L); PERM_OP (r, l, t, 8, 0x00ff00ffL); PERM_OP (l, r, t, 2, 0x33333333L); PERM_OP (r, l, t, 16, 0x0000ffffL); PERM_OP (l, r, t, 4, 0x0f0f0f0fL); out[0] = r; out[1] = l; }
static void _getEigenVectors(Mat4* vout, Vec3* dout, Mat4 a) { int n = 3; int j,iq,ip,i; double tresh, theta, tau, t, sm, s, h, g, c; int nrot; Vec3 b; Vec3 z; Mat4 v; Vec3 d; v = Mat4::IDENTITY; for(ip = 0; ip < n; ip++) { _getElement(b, ip) = a.m[ip + 4 * ip]; _getElement(d, ip) = a.m[ip + 4 * ip]; _getElement(z, ip) = 0.0; } nrot = 0; for(i = 0; i < 50; i++) { sm = 0.0; for(ip = 0; ip < n; ip++) for(iq = ip+1; iq < n; iq++) sm += fabs(a.m[ip + 4 * iq]); if( fabs(sm) < FLT_EPSILON ) { v.transpose(); *vout = v; *dout = d; return; } if (i < 3) tresh = 0.2 * sm / (n*n); else tresh = 0.0; for(ip = 0; ip < n; ip++) { for(iq = ip + 1; iq < n; iq++) { g = 100.0 * fabs(a.m[ip + iq * 4]); float dmip = _getElement(d, ip); float dmiq = _getElement(d, iq); if( i>3 && fabs(dmip) + g == fabs(dmip) && fabs(dmiq) + g == fabs(dmiq) ) { a.m[ip + 4 * iq] = 0.0; } else if (fabs(a.m[ip + 4 * iq]) > tresh) { h = dmiq - dmip; if (fabs(h) + g == fabs(h)) { t=(a.m[ip + 4 * iq])/h; } else { theta = 0.5 * h / (a.m[ip + 4 * iq]); t=1.0 / (fabs(theta) + sqrt(1.0 + theta * theta)); if (theta < 0.0) t = -t; } c = 1.0 / sqrt(1+t*t); s = t*c; tau = s / (1.0+c); h = t * a.m[ip + 4 * iq]; _getElement(z, ip) -= (float)h; _getElement(z, iq) += (float)h; _getElement(d, ip) -= (float)h; _getElement(d, iq) += (float)h; a.m[ip + 4 * iq]=0.0; for(j = 0; j < ip; j++) { ROTATE(a,j,ip,j,iq); } for(j = ip + 1; j < iq; j++) { ROTATE(a,ip,j,j,iq); } for(j = iq + 1; j < n; j++) { ROTATE(a,ip,j,iq,j); } for(j = 0; j < n; j++) { ROTATE(v,j,ip,j,iq); } nrot++; } } } for(ip = 0; ip < n; ip++) { _getElement(b, ip) += _getElement(z, ip); _getElement(d, ip) = _getElement(b, ip); _getElement(z, ip) = 0.0f; } } v.transpose(); *vout = v; *dout = d; return; }
int jacobi(double (*a)[3], double d[3], double (*v)[3], int *nrot) { int j,iq,ip,i; double tresh,theta,tau,t,sm,s,h,g,c; static double b[3]; static double z[3]; for (ip = 0; ip < 3; ip++) { for (iq = 0; iq < 3; iq++) { v[ip][iq]=0.0; } v[ip][ip]=1.0; } for (ip = 0; ip < 3; ip++) { b[ip] = d[ip] = a[ip][ip]; z[ip] = 0.0; } *nrot=0; for (i = 0; i < ITERATIONMAX; i++) { sm = 0.0; for (ip = 0; ip < 3-1; ip++) { for (iq = ip+1; iq < 3; iq++) { sm += fabs(a[ip][iq]); } } /* the normal return, which relies on quadratic convergence to machine underflow. */ if (sm == 0.0) { return (0); } if (i < 3) { tresh=0.2*sm/(3*3); } else { tresh=0.0; } for (ip = 0; ip < 3-1; ip++) { for (iq = ip+1; iq < 3; iq++) { g=100.0*fabs(a[ip][iq]); if (i > 3 && (fabs(d[ip])+g) == fabs(d[ip]) && (fabs(d[iq])+g) == fabs(d[iq])) { a[ip][iq]=0.0; } else if (fabs(a[ip][iq]) > tresh) { h=d[iq]-d[ip]; if ((fabs(h)+g) == fabs(h)) { t=(a[ip][iq])/h; } else { theta=0.5*h/(a[ip][iq]); t=1.0/(fabs(theta)+sqrt(1.0+theta*theta)); if (theta < 0.0) { t = -t; } } c=1.0/sqrt(1+t*t); s=t*c; tau=s/(1.0+c); h=t*a[ip][iq]; z[ip] -= h; z[iq] += h; d[ip] -= h; d[iq] += h; a[ip][iq]=0.0; for (j = 0; j <= ip-1; j++) { ROTATE(a,j,ip,j,iq); } for (j = ip+1; j <= iq-1; j++) { ROTATE(a,ip,j,j,iq); } for (j = iq+1; j < 3; j++) { ROTATE(a,ip,j,iq,j); } for (j = 0; j < 3; j++) { ROTATE(v,j,ip,j,iq); } ++(*nrot); } } } for (ip = 0; ip < 3; ip++) { b[ip] += z[ip]; d[ip] = b[ip]; z[ip] = 0.0; } } fprintf(stderr, "jacobi(): too many iterations\n"); return (-1); }
void des_encrypt2(DES_LONG *data, des_key_schedule ks, int enc) { DES_LONG l,r,t,u; #ifdef DES_PTR const unsigned char *des_SP=(const unsigned char *)des_SPtrans; #endif #ifndef DES_UNROLL int i; #endif DES_LONG *s; r=data[0]; l=data[1]; /* Things have been modified so that the initial rotate is * done outside the loop. This required the * des_SPtrans values in sp.h to be rotated 1 bit to the right. * One perl script later and things have a 5% speed up on a sparc2. * Thanks to Richard Outerbridge <*****@*****.**> * for pointing this out. */ /* clear the top bits on machines with 8byte longs */ r=ROTATE(r,29)&0xffffffffL; l=ROTATE(l,29)&0xffffffffL; s=ks->ks.deslong; /* I don't know if it is worth the effort of loop unrolling the * inner loop */ if (enc) { #ifdef DES_UNROLL D_ENCRYPT(l,r, 0); /* 1 */ D_ENCRYPT(r,l, 2); /* 2 */ D_ENCRYPT(l,r, 4); /* 3 */ D_ENCRYPT(r,l, 6); /* 4 */ D_ENCRYPT(l,r, 8); /* 5 */ D_ENCRYPT(r,l,10); /* 6 */ D_ENCRYPT(l,r,12); /* 7 */ D_ENCRYPT(r,l,14); /* 8 */ D_ENCRYPT(l,r,16); /* 9 */ D_ENCRYPT(r,l,18); /* 10 */ D_ENCRYPT(l,r,20); /* 11 */ D_ENCRYPT(r,l,22); /* 12 */ D_ENCRYPT(l,r,24); /* 13 */ D_ENCRYPT(r,l,26); /* 14 */ D_ENCRYPT(l,r,28); /* 15 */ D_ENCRYPT(r,l,30); /* 16 */ #else for (i=0; i<32; i+=8) { D_ENCRYPT(l,r,i+0); /* 1 */ D_ENCRYPT(r,l,i+2); /* 2 */ D_ENCRYPT(l,r,i+4); /* 3 */ D_ENCRYPT(r,l,i+6); /* 4 */ } #endif } else { #ifdef DES_UNROLL D_ENCRYPT(l,r,30); /* 16 */ D_ENCRYPT(r,l,28); /* 15 */ D_ENCRYPT(l,r,26); /* 14 */ D_ENCRYPT(r,l,24); /* 13 */ D_ENCRYPT(l,r,22); /* 12 */ D_ENCRYPT(r,l,20); /* 11 */ D_ENCRYPT(l,r,18); /* 10 */ D_ENCRYPT(r,l,16); /* 9 */ D_ENCRYPT(l,r,14); /* 8 */ D_ENCRYPT(r,l,12); /* 7 */ D_ENCRYPT(l,r,10); /* 6 */ D_ENCRYPT(r,l, 8); /* 5 */ D_ENCRYPT(l,r, 6); /* 4 */ D_ENCRYPT(r,l, 4); /* 3 */ D_ENCRYPT(l,r, 2); /* 2 */ D_ENCRYPT(r,l, 0); /* 1 */ #else for (i=30; i>0; i-=8) { D_ENCRYPT(l,r,i-0); /* 16 */ D_ENCRYPT(r,l,i-2); /* 15 */ D_ENCRYPT(l,r,i-4); /* 14 */ D_ENCRYPT(r,l,i-6); /* 13 */ } #endif } /* rotate and clear the top bits on machines with 8byte longs */ data[0]=ROTATE(l,3)&0xffffffffL; data[1]=ROTATE(r,3)&0xffffffffL; l=r=t=u=0; }
// ./next_gen "# of permutations" stem0 stem1 stem2 ... int main(int argc, unsigned char **argv) { char phrase[59] = "I would much rather hear more about your whittling project"; unsigned int permutations = atou((char *)argv[1]); sseK00_19 = _mm_set1_epi32(0x5a827999); sseK20_39 = _mm_set1_epi32(0x6ed9eba1); sseK40_59 = _mm_set1_epi32(0x8f1bbcdc); sseK60_79 = _mm_set1_epi32(0xca62c1d6); best_stem = malloc(sizeof(char) * 5); shortest_d = 180; // Get finished context for phrase SHA_CTX *phrase_ctx = malloc(sizeof(SHA_CTX)); sha1_full(phrase_ctx, phrase); // Load prefixes unsigned char **prefixes = malloc(sizeof(char *) * PREFIX_COUNT); int p = 0; for(p = 0;p < PREFIX_COUNT;p++) prefixes[p] = argv[2 + p]; // Get chaining contexts for suffixes SHA_CTX *prefix_ctxs = malloc(sizeof(SHA_CTX) * PREFIX_COUNT); for(p = 0;p < PREFIX_COUNT;p++) sha1_partial(&prefix_ctxs[p], prefixes[p]); struct vector_ctx *vc = malloc(sizeof(struct vector_ctx) * PREFIX_COUNT/4); struct vector_ctx *vc_ptr = vc; SHA_CTX *prefix_ptr = prefix_ctxs; for(p = 0;p < PREFIX_COUNT;p+=4) { vectorize_prefixes(vc_ptr, prefix_ptr); prefix_ptr += 4; vc_ptr += 1; } // Allocate memory for expanded message template uint32_t *w = malloc(sizeof(uint32_t) * 80); int w_i = 0; // We only hash suffixes that are 5 bytes long // w[0] prefix_stem // w[1] current final char + some other stuff and zeros // w[2]-w[14] // Expanded message blocks 2-14 are always 0x0000000... for(w_i = 2;w_i < 15;w_i++) w[w_i] = 0; // w[15] is the size of the message w[15] = 552; // w[16] - stem constant - W13 ^ W8 ^ W2 ^ W0 => W0 <<< 1 // w[17] - changing lots // w[18] - constant w[18] = ROTATE(w[15], 1); // w[19] - stem constant // w[20] - changing lots uint32_t *stem_w = malloc(sizeof(uint32_t) * 80); uint32_t *stem_x = malloc(sizeof(uint32_t) * 80); init_lut(); struct vector_ctx *my_vc = malloc(sizeof(struct vector_ctx) * PREFIX_COUNT/2); int i = 0; char suffix_stem[5] = "!!!!"; for(i = 0;i < permutations;i++) { memcpy(stem_w, w, 80 * sizeof(uint32_t)); memcpy(my_vc, vc, sizeof(struct vector_ctx) * PREFIX_COUNT/2); int dist = shortest_distance(phrase_ctx, my_vc, suffix_stem, stem_w, stem_x); next_stem(suffix_stem); } free(vc); free(my_vc); free(w); free(stem_w); free(stem_x); // Print shortest_distance and the 5 char ending. printf("%d,%s,%d\n", shortest_d, best_stem, best_last+33); }
/** Rotate marked items, refer to a rotation point at position offset * Note: because this function is used in global transform, * if force_all is true, all items will be rotated */ void RotateMarkedItems( MODULE* module, wxPoint offset, bool force_all ) { #define ROTATE( z ) RotatePoint( (&z), offset, 900 ) if( module == NULL ) return; for( D_PAD* pad = module->Pads(); pad; pad = pad->Next() ) { if( !pad->IsSelected() && !force_all ) continue; wxPoint pos = pad->GetPosition(); ROTATE( pos ); pad->SetPosition( pos ); pad->SetPos0( pad->GetPosition() ); pad->SetOrientation( pad->GetOrientation() + 900 ); } for( EDA_ITEM* item = module->GraphicalItems(); item; item = item->Next() ) { if( !item->IsSelected() && !force_all) continue; switch( item->Type() ) { case PCB_MODULE_EDGE_T: { EDGE_MODULE* em = (EDGE_MODULE*) item; wxPoint tmp = em->GetStart(); ROTATE( tmp ); em->SetStart( tmp ); em->SetStart0( tmp ); tmp = em->GetEnd(); ROTATE( tmp ); em->SetEnd( tmp ); em->SetEnd0( tmp ); } break; case PCB_MODULE_TEXT_T: { TEXTE_MODULE* tm = (TEXTE_MODULE*) item; wxPoint pos = tm->GetTextPosition(); ROTATE( pos ); tm->SetTextPosition( pos ); tm->SetPos0( tm->GetTextPosition() ); tm->SetOrientation( tm->GetOrientation() + 900 ); } break; default: ; } item->ClearFlags(); } }
/* computes egenvectors and eigenvalues using Jacobian method * returns egenValues on succses and NULL of failiar * eigenVectors on succses will be filled with egenVectors corresponding to eigenvalues * NB n can be up to a maximum of 10 for speed increace (static alloc vs dynamic) */ static float* computeJacobianEigenValuesAndVectors(float* matrix, float** eigenVectors, int n) { int j,iq,ip,i,nrot; float tresh,theta,tau,t,sm,s,h,g,c,b[10],z[10],*d; float *eigenValues; *eigenVectors = (float*)malloc(sizeof(float) * n*n); toIdentity(*eigenVectors, n); d = eigenValues = (float*)malloc(sizeof(float) * n ); for (ip=0;ip<n;ip++) { b[ip]=d[ip]=matrix4Access(matrix, ip, ip); //Initialize b and d to the diagonal of a. z[ip]=0.0; //This vector will accumulate terms of the form tapq as in equation (11.1.14) } nrot=0; for (i=1;i<=50;i++) { sm=0.0; for (ip=0;ip<n-1;ip++) //Sum off-diagonal elements. { for (iq=ip+1;iq<n;iq++) sm += (float)fabs(matrix4Access(matrix, ip, iq)); } if (sm == 0.0) //The normal return, which relies on quadratic convergence to machine underflow. { //we only need the absolute values of eigenvalues for (ip=0;ip<n;ip++) d[ip]=(float)fabs(d[ip]); return eigenValues; } if (i < 4) tresh = 0.2f * sm/(float)(n*n); //...on the first three sweeps. else tresh = 0.0f; //...thereafter. for (ip=0;ip<n-1;ip++) { for (iq=ip+1;iq<n;iq++) { g=100.0f * (float)fabs(matrix4Access(matrix, ip, iq)); //After four sweeps, skip the rotation if the off-diagonal element is small. if (i > 4 && (float)(fabs(d[ip])+g) == (float)fabs(d[ip]) && (float)(fabs(d[iq])+g) == (float)fabs(d[iq])) { matrix4Access(matrix, ip, iq)=0.0f; } else if (fabs(matrix4Access(matrix, ip, iq)) > tresh) { h=d[iq]-d[ip]; if ((float)(fabs(h)+g) == (float)fabs(h)) t = matrix4Access(matrix, ip, iq )/h; //t = 1/(2¦theta) else { theta=0.5f * h/matrix4Access(matrix, ip, iq); //Equation (11.1.10). t=1.0f/(float)(fabs(theta)+sqrt(1.0f+theta*theta)); if (theta < 0.0) t = -t; } c=1.0f/(float)sqrt(1.0f+t*t); s=t*c; tau=s/(1.0f+c); h=t*matrix4Access(matrix, ip, iq); z[ip] -= h; z[iq] += h; d[ip] -= h; d[iq] += h; matrix4Access(matrix, ip, iq)=0.0; for (j=0;j<=ip-1;j++) //Case of rotations 1 <= j < p. { ROTATE(matrix,j,ip,j,iq); } for (j=ip+1;j<=iq-1;j++) //Case of rotations p < j < q. { ROTATE(matrix,ip,j,j,iq); } for (j=iq+1;j<n;j++) //Case of rotations q < j <= n. { ROTATE(matrix,ip,j,iq,j); } for (j=0;j<n;j++) { ROTATE((*eigenVectors),j,ip,j,iq); } ++nrot; } } } for (ip=0;ip<n;ip++) { b[ip]+=z[ip]; d[ip]=b[ip]; //Update d with the sum of tapq, z[ip]=0.0; //and reinitialize z. } } //Too many iterations in routine jacobi! free(eigenValues); free(*eigenVectors); return NULL; }
/* * calculates Eigenvalues using Jacobi algorithm * use only if Eigen() is not applicable * cf. http://en.wikipedia.org/wiki/Jacobi_eigenvalue_algorithm */ void Matrix33::Jacobi( std::vector<real>& Eval, std::vector<Vector3>& Evec, int &nrot ) { int j, iq, ip, i; double tresh, theta, tau, t, sm, s, h, g, c; double *b = new double[ 3 ], *z = new double[ 3 ]; //... set output matrix to identity matrix ... for ( ip = 0; ip < 3; ip++ ) { for ( iq = 0; iq < 3; iq++ ) Evec.at(ip)( iq ) = 0.0; Evec.at(ip)( ip ) = 1.0; } for ( ip = 0; ip < 3; ip++ ) { b[ ip ] = Eval[ip] = ( *this ) ( ip, ip ); z[ ip ] = 0.0; } nrot = 0; for ( i = 0; i < 50;i++ ) { sm = 0.0; for ( ip = 0; ip < 2;ip++ ) { for ( iq = ip + 1;iq < 3; ++iq ) sm += fabs( ( *this ) ( ip, iq ) ); } if ( sm == 0.0 ) { delete[] b; delete[] z; return ; } if ( i < 4 ) tresh = 0.2 * sm / ( 3 * 3 ); else tresh = 0.0; for ( ip = 0; ip < 2; ip++ ) { for ( iq = ip + 1; iq < 3; iq++ ) { g = 100.0 * fabs( ( *this ) ( ip, iq ) ); if ( i > 4 && ( double ) ( fabs( Eval[ip] ) + g ) == ( double ) fabs( Eval[ip] ) && ( double ) ( fabs( Eval[iq] ) + g ) == ( double ) fabs( Eval[iq] ) ) ( *this ) ( ip, iq ) = 0.0; else if ( fabs( ( *this ) ( ip, iq ) ) > tresh ) { h = Eval[iq] - Eval[ip]; if ( ( double ) ( fabs( h ) + g ) == ( double ) fabs( h ) ) t = ( ( *this ) ( ip, iq ) ) / h; else { theta = 0.5 * h / ( ( *this ) ( ip, iq ) ); t = 1.0 / ( fabs( theta ) + sqrt( 1.0 + theta * theta ) ); if ( theta < 0.0 ) t = -t; } c = 1.0 / sqrt( 1 + t * t ); s = t * c; tau = s / ( 1.0 + c ); h = t * ( ( *this ) ( ip, iq ) ); z[ ip ] -= h; z[ iq ] += h; Eval[ip] -= h; Eval[iq] += h; ( *this ) ( ip, iq ) = 0.0; for ( j = 0; j < ip - 1; ++j ) { ROTATE( *this, j, ip, j, iq, s, tau ); } for ( j = ip + 1;j <= iq - 1; ++j ) { ROTATE( *this, ip, j, j, iq, s, tau ); } for ( j = iq + 1;j < 3; ++j ) { ROTATE( *this, ip, j, iq, j, s, tau ); } for ( j = 0;j < 3; ++j ) { ROTATE( Evec, j, ip, j, iq, s, tau ); } ++nrot; } } } for ( ip = 0; ip < 3; ++ip ) { b[ ip ] += z[ ip ]; Eval[ip] = b[ ip ]; z[ ip ] = 0.0; } } std::cerr << "Error : Too many iterations in routine Jacobi"; delete[] b; delete[] z; }
TITLE_SCRIPT_LOAD, TITLE_SCRIPT_LOCATION, TITLE_SCRIPT_ROTATE, TITLE_SCRIPT_RESTART, }; #define WAIT(t) TITLE_SCRIPT_WAIT, t #define LOAD() TITLE_SCRIPT_LOAD #define LOCATION(x, y) TITLE_SCRIPT_LOCATION, x, y #define ROTATE(n) TITLE_SCRIPT_ROTATE, n #define RESTART() TITLE_SCRIPT_RESTART static const uint8 _magicMountainScript[] = { LOAD(), LOCATION(210, 112), WAIT(13), ROTATE(1), LOCATION(210, 112), WAIT(14), ROTATE(3), LOCATION(167, 180), WAIT(12), ROTATE(1), LOCATION(155, 189), WAIT(12), LOCATION(106, 39), WAIT(12), LOCATION(182, 50), WAIT(12), ROTATE(3), LOCATION(209, 47), WAIT(12), ROTATE(1), LOCATION(159, 93), WAIT(12), RESTART(), }; static const uint8* _currentScript; static int _scriptWaitCounter; static void title_init_showcase(); static void title_update_showcase(); static void title_play_music();
int ff_pca(PCA *pca, double *eigenvector, double *eigenvalue){ int i, j, k, pass; const int n= pca->n; double z[n]; memset(eigenvector, 0, sizeof(double)*n*n); for(j=0; j<n; j++){ pca->mean[j] /= pca->count; eigenvector[j + j*n] = 1.0; for(i=0; i<=j; i++){ pca->covariance[j + i*n] /= pca->count; pca->covariance[j + i*n] -= pca->mean[i] * pca->mean[j]; pca->covariance[i + j*n] = pca->covariance[j + i*n]; } eigenvalue[j]= pca->covariance[j + j*n]; z[j]= 0; } for(pass=0; pass < 50; pass++){ double sum=0; for(i=0; i<n; i++) for(j=i+1; j<n; j++) sum += fabs(pca->covariance[j + i*n]); if(sum == 0){ for(i=0; i<n; i++){ double maxvalue= -1; for(j=i; j<n; j++){ if(eigenvalue[j] > maxvalue){ maxvalue= eigenvalue[j]; k= j; } } eigenvalue[k]= eigenvalue[i]; eigenvalue[i]= maxvalue; for(j=0; j<n; j++){ double tmp= eigenvector[k + j*n]; eigenvector[k + j*n]= eigenvector[i + j*n]; eigenvector[i + j*n]= tmp; } } return pass; } for(i=0; i<n; i++){ for(j=i+1; j<n; j++){ double covar= pca->covariance[j + i*n]; double t,c,s,tau,theta, h; if(pass < 3 && fabs(covar) < sum / (5*n*n)) //FIXME why pass < 3 continue; if(fabs(covar) == 0.0) //FIXME shouldnt be needed continue; if(pass >=3 && fabs((eigenvalue[j]+z[j])/covar) > (1LL<<32) && fabs((eigenvalue[i]+z[i])/covar) > (1LL<<32)){ pca->covariance[j + i*n]=0.0; continue; } h= (eigenvalue[j]+z[j]) - (eigenvalue[i]+z[i]); theta=0.5*h/covar; t=1.0/(fabs(theta)+sqrt(1.0+theta*theta)); if(theta < 0.0) t = -t; c=1.0/sqrt(1+t*t); s=t*c; tau=s/(1.0+c); z[i] -= t*covar; z[j] += t*covar; #define ROTATE(a,i,j,k,l) {\ double g=a[j + i*n];\ double h=a[l + k*n];\ a[j + i*n]=g-s*(h+g*tau);\ a[l + k*n]=h+s*(g-h*tau); } for(k=0; k<n; k++) { if(k!=i && k!=j){ ROTATE(pca->covariance,FFMIN(k,i),FFMAX(k,i),FFMIN(k,j),FFMAX(k,j)) } ROTATE(eigenvector,k,i,k,j) } pca->covariance[j + i*n]=0.0; } } for (i=0; i<n; i++) { eigenvalue[i] += z[i]; z[i]=0.0; } } return -1; }
static void icvCalcDCTMatrix(work_t * cfs, int n) { static const double sqrt2 = 1.4142135623730950488016887242097; static const double pi = 3.1415926535897932384626433832795; static const double sincos[16 * 2] = { 1.00000000000000000, 0.00000000000000006, 0.70710678118654746, 0.70710678118654757, 0.49999999999999994, 0.86602540378443871, 0.38268343236508978, 0.92387953251128674, 0.30901699437494740, 0.95105651629515353, 0.25881904510252074, 0.96592582628906831, 0.22252093395631439, 0.97492791218182362, 0.19509032201612825, 0.98078528040323043, 0.17364817766693033, 0.98480775301220802, 0.15643446504023087, 0.98768834059513777, 0.14231483827328514, 0.98982144188093268, 0.13052619222005157, 0.99144486137381038, 0.12053668025532305, 0.99270887409805397, 0.11196447610330786, 0.99371220989324260, 0.10452846326765346, 0.99452189536827329, 0.09801714032956060, 0.99518472667219693, }; #define ROTATE(c, s, dc, ds) \ { \ t = c*dc - s*ds; \ s = c*ds + s*dc; \ c = t; \ } #define WRITE2(j, a, b) \ { \ cfs[j] = SCALE(a); \ cfs2[j] = SCALE(b); \ } double t, scale = 1. / sqrt( (double)n ); int i, j, m = n / 2; cfs[0] = SCALE( scale ); scale *= sqrt2; cfs[1] = SCALE( scale ); cfs += 2 - m; // n为输入DCT的长或宽,12 if (n > 1) { double a0, b0; double da0, db0; work_t *cfs2 = cfs + m * n; if( n <= 16 ) { da0 = a0 = sincos[2 * n - 1]; db0 = b0 = sincos[2 * n - 2]; } else { t = pi / (2 * n); da0 = a0 = cos( t ); db0 = b0 = sin( t ); } // 其它行,m=n/2 /* other rows */ for (i = 1; i <= m; i++) { double a = a0 * scale; double b = b0 * scale; double da = a0 * a0 - b0 * b0; double db = a0 * b0 + a0 * b0; cfs += m; cfs2 -= m; for (j = 0; j < m; j += 2) { WRITE2( j, a, b ); ROTATE( a, b, da, db ); if (j + 1 < m) { WRITE2( j + 1, a, -b ); ROTATE( a, b, da, db ); } } ROTATE( a0, b0, da0, db0 ); } } #undef ROTATE #undef WRITE2 }
void crypto_core_salsa_rounds(unsigned char in[64], unsigned int rounds) { unsigned int i = 0; uint32_t x[16], x_orig[16]; for (i = 0; i < 16; i ++) x_orig[i] = arch_mem_copy_vect2dword_little(&x[i], &in[i * 4]); for (i = rounds; i > 0; i -= 2) { x[ 4] ^= ROTATE(x[ 0] + x[12], 7); x[ 8] ^= ROTATE(x[ 4] + x[ 0], 9); x[12] ^= ROTATE(x[ 8] + x[ 4], 13); x[ 0] ^= ROTATE(x[12] + x[ 8], 18); x[ 9] ^= ROTATE(x[ 5] + x[ 1], 7); x[13] ^= ROTATE(x[ 9] + x[ 5], 9); x[ 1] ^= ROTATE(x[13] + x[ 9], 13); x[ 5] ^= ROTATE(x[ 1] + x[13], 18); x[14] ^= ROTATE(x[10] + x[ 6], 7); x[ 2] ^= ROTATE(x[14] + x[10], 9); x[ 6] ^= ROTATE(x[ 2] + x[14], 13); x[10] ^= ROTATE(x[ 6] + x[ 2], 18); x[ 3] ^= ROTATE(x[15] + x[11], 7); x[ 7] ^= ROTATE(x[ 3] + x[15], 9); x[11] ^= ROTATE(x[ 7] + x[ 3], 13); x[15] ^= ROTATE(x[11] + x[ 7], 18); x[ 1] ^= ROTATE(x[ 0] + x[ 3], 7); x[ 2] ^= ROTATE(x[ 1] + x[ 0], 9); x[ 3] ^= ROTATE(x[ 2] + x[ 1], 13); x[ 0] ^= ROTATE(x[ 3] + x[ 2], 18); x[ 6] ^= ROTATE(x[ 5] + x[ 4], 7); x[ 7] ^= ROTATE(x[ 6] + x[ 5], 9); x[ 4] ^= ROTATE(x[ 7] + x[ 6], 13); x[ 5] ^= ROTATE(x[ 4] + x[ 7], 18); x[11] ^= ROTATE(x[10] + x[ 9], 7); x[ 8] ^= ROTATE(x[11] + x[10], 9); x[ 9] ^= ROTATE(x[ 8] + x[11], 13); x[10] ^= ROTATE(x[ 9] + x[ 8], 18); x[12] ^= ROTATE(x[15] + x[14], 7); x[13] ^= ROTATE(x[12] + x[15], 9); x[14] ^= ROTATE(x[13] + x[12], 13); x[15] ^= ROTATE(x[14] + x[13], 18); } for (i = 0; i < 16; i ++) arch_mem_copy_dword2vect_little(&in[i * 4], x[i] + x_orig[i]); }
static void salsa20_wordtobyte(u8 output[64],const u32 input[16]) { u32 x[16]; unsigned int i; for (i = 0;i < 16;++i) x[i] = input[i]; for (i = 20;i > 0;i -= 2) { x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 0],x[12]), 7)); x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[ 4],x[ 0]), 9)); x[12] = XOR(x[12],ROTATE(PLUS(x[ 8],x[ 4]),13)); x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[12],x[ 8]),18)); x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 5],x[ 1]), 7)); x[13] = XOR(x[13],ROTATE(PLUS(x[ 9],x[ 5]), 9)); x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[13],x[ 9]),13)); x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 1],x[13]),18)); x[14] = XOR(x[14],ROTATE(PLUS(x[10],x[ 6]), 7)); x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[14],x[10]), 9)); x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 2],x[14]),13)); x[10] = XOR(x[10],ROTATE(PLUS(x[ 6],x[ 2]),18)); x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[15],x[11]), 7)); x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 3],x[15]), 9)); x[11] = XOR(x[11],ROTATE(PLUS(x[ 7],x[ 3]),13)); x[15] = XOR(x[15],ROTATE(PLUS(x[11],x[ 7]),18)); x[ 1] = XOR(x[ 1],ROTATE(PLUS(x[ 0],x[ 3]), 7)); x[ 2] = XOR(x[ 2],ROTATE(PLUS(x[ 1],x[ 0]), 9)); x[ 3] = XOR(x[ 3],ROTATE(PLUS(x[ 2],x[ 1]),13)); x[ 0] = XOR(x[ 0],ROTATE(PLUS(x[ 3],x[ 2]),18)); x[ 6] = XOR(x[ 6],ROTATE(PLUS(x[ 5],x[ 4]), 7)); x[ 7] = XOR(x[ 7],ROTATE(PLUS(x[ 6],x[ 5]), 9)); x[ 4] = XOR(x[ 4],ROTATE(PLUS(x[ 7],x[ 6]),13)); x[ 5] = XOR(x[ 5],ROTATE(PLUS(x[ 4],x[ 7]),18)); x[11] = XOR(x[11],ROTATE(PLUS(x[10],x[ 9]), 7)); x[ 8] = XOR(x[ 8],ROTATE(PLUS(x[11],x[10]), 9)); x[ 9] = XOR(x[ 9],ROTATE(PLUS(x[ 8],x[11]),13)); x[10] = XOR(x[10],ROTATE(PLUS(x[ 9],x[ 8]),18)); x[12] = XOR(x[12],ROTATE(PLUS(x[15],x[14]), 7)); x[13] = XOR(x[13],ROTATE(PLUS(x[12],x[15]), 9)); x[14] = XOR(x[14],ROTATE(PLUS(x[13],x[12]),13)); x[15] = XOR(x[15],ROTATE(PLUS(x[14],x[13]),18)); } for (i = 0;i < 16;++i) x[i] = PLUS(x[i],input[i]); for (i = 0;i < 16;++i) U32TO8_LITTLE(output + 4 * i,x[i]); }