Example #1
0
void *sieve(void *k)
{
    int ret = 0;
    int tid = (int)k;

    for (long i = 2; i < ceil(sqrt(MAX)); ++i) {
        // Mark all multiples of the number
        if (!BITTEST(bitarray, i)) {
            // Make sure the number we are using is not divisible by
            // any other number currently being processed.
            for (int v = 0; v < NUM_THREADS; ++v) {
                if((i % working[v]) == 0) continue;
            }

            working[tid] = i;

            for(long j = i + i; 0 < j && j < MAX; j += i) {
                ret = pthread_mutex_lock(&mutexes[BITSLOT(j)]);
                if (ret != 0) errExit("pthread_mutex_lock");

                BITSET(bitarray, j);

                ret = pthread_mutex_unlock(&mutexes[BITSLOT(j)]);
                if (ret != 0) errExit("pthread_mutex_lock");
            }
        }
    }

    return NULL;
}
Example #2
0
File: bit.c Project: the-mrd/culzw
int Bit_count_set_bits_up_to(unsigned char* A, unsigned int length, unsigned char p) {
  
  assert(A);
  assert(length);
  
  unsigned char* temparray;
  temparray = malloc(length);
  //memset(temparray, 0, length );
  memcpy(temparray, A, length);
  
  unsigned int i;
  
  //printf("maxlength: %d\n", length*CHAR_BIT);
  for (i = BITSLOT(p) + 1; i < length; i++)
    temparray[i] = 0;
  temparray[BITSLOT(p)] = temparray[BITSLOT(p)] << ((BITSLOT(p) + 1) * CHAR_BIT - p);

//
 // for ( i = p ; i < length * CHAR_BIT; i++)		//better shift length - p...
 //   BITCLEAR(temparray, i);
  
  int ret = Bit_popcount(temparray, length);
  
  free(temparray);
  return ret;
  
}