int convolve_complex(const float xreal[], const float ximag[], const float yreal[], const float yimag[], float outreal[], float outimag[], size_t n, int procNumber) { int status = 0; size_t size; size_t i; float *xr, *xi, *yr, *yi; if (SIZE_MAX / sizeof(float) < n) return 0; size = n * sizeof(float); xr = memdup(xreal, size); xi = memdup(ximag, size); yr = memdup(yreal, size); yi = memdup(yimag, size); if (xr == NULL || xi == NULL || yr == NULL || yi == NULL) goto cleanup; if (!transform(xr, xi, n, procNumber)) goto cleanup; if (!transform(yr, yi, n, procNumber)) goto cleanup; int start, end; getVectorParcel(&start,&end, n, procNumber); for (i = start; i < end; i++) { float temp = xr[i] * yr[i] - xi[i] * yi[i]; xi[i] = xi[i] * yr[i] + xr[i] * yi[i]; xr[i] = temp; } incrementSemaphore(&secondSemaphore); acquireSemaphore(&secondSemaphore); if (!inverse_transform(xr, xi, n, procNumber)); goto cleanup; getVectorParcel(&start,&end, n, procNumber); for (i = start; i < end; i++) { // Scaling (because this FFT implementation omits it) outreal[i] = xr[i] / n; outimag[i] = xi[i] / n; } incrementSemaphore(&fourthSemaphore); acquireSemaphore(&fourthSemaphore); status = 1; cleanup: free(yi); free(yr); free(xi); free(xr); return status; }
void TwitterDataTypeSyncAdaptor::updateDataForAccount(int accountId) { Accounts::Account *account = Accounts::Account::fromId(m_accountManager, accountId, this); if (!account) { SOCIALD_LOG_ERROR("existing account with id" << accountId << "couldn't be retrieved"); setStatus(SocialNetworkSyncAdaptor::Error); decrementSemaphore(accountId); return; } // will be decremented by either signOnError or signOnResponse. incrementSemaphore(accountId); signIn(account); }
int transform_bluestein(float real[], float imag[], size_t n, int procNumber) { // Variables int status = 0; //float *cos_table, *sin_table; float *areal, *aimag; float *breal, *bimag; float *creal, *cimag; size_t m; size_t size_n, size_m; size_t i; // Find a power-of-2 convolution length m such that m >= n * 2 + 1 { size_t target; if (n > (SIZE_MAX - 1) / 2) return 0; target = n * 2 + 1; for (m = 1; m < target; m *= 2) { if (SIZE_MAX / 2 < m) return 0; } } // Allocate memory if (SIZE_MAX / sizeof(float) < n || SIZE_MAX / sizeof(float) < m) return 0; size_n = n * sizeof(float); size_m = m * sizeof(float); //cos_table = malloc(size_n); //sin_table = malloc(size_n); areal = calloc(m, sizeof(float)); aimag = calloc(m, sizeof(float)); breal = calloc(m, sizeof(float)); bimag = calloc(m, sizeof(float)); creal = malloc(size_m); cimag = malloc(size_m); if (areal == NULL || aimag == NULL || breal == NULL || bimag == NULL || creal == NULL || cimag == NULL) goto cleanup; // Trignometric tables /*for (i = 0; i < n; i++) { //float temp = M_PI * (size_t)((unsigned long long)i * i % ((unsigned long long)n * 2)) / n; // Less accurate version if long long is unavailable: float temp = M_PI * i * i / n; cos_table[i] = cos(temp); sin_table[i] = sin(temp); }*/ // Temporary vectors and preprocessing int start, end; getVectorParcel(&start,&end, n, procNumber); for (i = start; i < end; i++) { areal[i] = real[i] * cos_mod(i,n) + imag[i] * sin_mod(i,n); aimag[i] = -real[i] * sin_mod(i,n) + imag[i] * cos_mod(i,n); } incrementSemaphore(&thirdSemaphore); acquireSemaphore(&thirdSemaphore); breal[0] = cos_mod(0,n); bimag[0] = sin_mod(0,n); for (i = 1; i < n; i++) { breal[i] = breal[m - i] = cos_mod(i,n); bimag[i] = bimag[m - i] = sin_mod(i,n); } // Convolution if (!convolve_complex(areal, aimag, breal, bimag, creal, cimag, m, procNumber)) goto cleanup; // Postprocessing for (i = 0; i < n; i++) { real[i] = creal[i] * cos_mod(i,n) + cimag[i] * sin_mod(i,n); imag[i] = -creal[i] * sin_mod(i,n) + cimag[i] * cos_mod(i,n); } status = 1; // Deallocation cleanup: free(cimag); free(creal); free(bimag); free(breal); free(aimag); free(areal); //free(sin_table); //free(cos_table); return status; }
int transform_radix2(float real[], float imag[], size_t n, int procNumber) { // Variables int status = 0; unsigned int levels; //float *cos_table, *sin_table; size_t size; size_t i; // Compute levels = floor(log2(n)) { size_t temp = n; levels = 0; while (temp > 1) { levels++; temp >>= 1; } if (1u << levels != n) return 0; // n is not a power of 2 } // Trignometric tables if (SIZE_MAX / sizeof(float) < n / 2) return 0; /*size = (n / 2) * sizeof(float); cos_table = malloc(size); sin_table = malloc(size); if (cos_table == NULL || sin_table == NULL) goto cleanup; for (i = 0; i < n / 2; i++) { cos_table[i] = cos(2 * M_PI * i / n); sin_table[i] = sin(2 * M_PI * i / n); }*/ // Bit-reversed addressing permutation int start, end; getVectorParcel(&start,&end, n, procNumber); for (i = start; i < end; i++) { size_t j = reverse_bits(i, levels); if (j > i) { float temp = real[i]; real[i] = real[j]; real[j] = temp; temp = imag[i]; imag[i] = imag[j]; imag[j] = temp; } } incrementSemaphore(&firstSemaphore); acquireSemaphore(&firstSemaphore); // Cooley-Tukey decimation-in-time radix-2 FFT for (size = 2; size <= n; size *= 2) { size_t halfsize = size / 2; size_t tablestep = n / size; for (i = 0; i < n; i += size) { size_t j; size_t k; for (j = i, k = 0; j < i + halfsize; j++, k += tablestep) { float tpre = real[j+halfsize] * cos_mod_two(k,n) + imag[j+halfsize] * sin_mod_two(k,n); float tpim = -real[j+halfsize] * sin_mod_two(k,n) + imag[j+halfsize] * cos_mod_two(k,n); real[j + halfsize] = real[j] - tpre; imag[j + halfsize] = imag[j] - tpim; real[j] += tpre; imag[j] += tpim; } } if (size == n) // Prevent overflow in 'size *= 2' break; } status = 1; cleanup: //free(cos_table); //free(sin_table); return status; }