/* Dispatch events to the correct handlers */ void do_hypervisor_callback(struct pt_regs *regs) { unsigned int pending_selector; unsigned int next_event_offset; vcpu_info_t *vcpu = &shared_info.vcpu_info[0]; /* Make sure we don't lose the edge on new events... */ vcpu->evtchn_upcall_pending = 0; /* Set the pending selector to 0 and get the old value atomically */ pending_selector = xchg(&vcpu->evtchn_pending_sel, 0); while(pending_selector != 0) { /* Get the first bit of the selector and clear it */ next_event_offset = first_bit(pending_selector); pending_selector &= ~(1 << next_event_offset); unsigned int event; /* While there are events pending on unmasked channels */ while(( event = (shared_info.evtchn_pending[pending_selector] & ~shared_info.evtchn_mask[pending_selector])) != 0) { /* Find the first waiting event */ unsigned int event_offset = first_bit(event); /* Combine the two offsets to get the port */ evtchn_port_t port = (pending_selector << 5) + event_offset; /* Handler the event */ handlers[port](port, regs); /* Clear the pending flag */ CLEAR_BIT(shared_info.evtchn_pending[0], event_offset); } } }
void rle_info(const unsigned char *inbuf, int n, int *first, int *last, int *width, int *length) { const char *ib= (const char *)inbuf; char cnt; int o= 0; int i=0,j,num,f=-1,l=0; *first= 0; *last= 0; *width= 0; if (n<=0) return; while (i<n) { cnt= ib[i]; if (cnt<0) { num= 1-cnt; if (inbuf[i+1]) { if (f<0) f=o*8 + first_bit(inbuf[i+1]); l= o*8 + last_bit(inbuf[i+1]); } o+= num; i+= 2; } else { num= cnt+1; for (j=0; j<num; j++) { if (inbuf[i+j+1]) { if (f<0) f=(o+j)*8 + first_bit(inbuf[i+j+1]); l= o*8 + last_bit(inbuf[i+j+1]); } } o+= num; i+= num+1; } } if (first) *first= f; if (last) *last= l; if (length) *length= o; if (width) *width= (f<0) ? 0 : l-f+1; }
bit_pos_iterator begin() const { size_t pos; if (first_bit(pos) == false) pos = size_t(-1); return bit_pos_iterator(this, pos); }
void init_prime_sieve(uint64_t pmax) { uint_fast32_t *sieve; uint32_t low_prime_limit, max_low_primes, *low_primes, low_prime_count; uint32_t sieve_index, max_prime, max_primes_in_table; uint32_t p, minp, i, composite; assert(primes_in_prime_table == 0); pmax = MAX(MINIMUM_PMAX,pmax); max_prime = sqrt(pmax)+1; low_prime_limit = sqrt(max_prime)+1; max_low_primes = primes_bound(low_prime_limit); low_primes = xmalloc(max_low_primes * sizeof(uint32_t)); low_primes[0] = 3; low_prime_count = 1; for (p = 5; p < low_prime_limit; p += 2) for (minp = 0; minp <= low_prime_count; minp++) { if (low_primes[minp] * low_primes[minp] > p) { low_primes[low_prime_count] = p; low_prime_count++; break; } if (p % low_primes[minp] == 0) break; } assert(low_prime_count <= max_low_primes); /* Divide max_prime by 2 to save memory, also because already know that all even numbers in the sieve are composite. */ sieve = make_bitmap(max_prime/2); fill_bits(sieve,1,max_prime/2-1); for (i = 0; i < low_prime_count; i++) { /* Get the current low prime. Start sieving at 3x that prime since 1x is prime and 2x is divisible by 2. sieve[1]=3, sieve[2]=5, etc. */ composite = 3*low_primes[i]; sieve_index = (composite-1)/2; while (composite < max_prime) { /* composite will always be odd, so add 2*low_primes[i] */ clear_bit(sieve,sieve_index); sieve_index += low_primes[i]; composite += 2*low_primes[i]; } } free(low_primes); max_primes_in_table = primes_bound(max_prime); prime_table = xmalloc(max_primes_in_table * sizeof(uint32_t)); for (i = first_bit(sieve); i < max_prime/2; i = next_bit(sieve,i+1)) { /* Convert the value back to an actual prime. */ prime_table[primes_in_prime_table] = 2*i + 1; primes_in_prime_table++; } assert(primes_in_prime_table <= max_primes_in_table); free(sieve); composite_table = xmalloc(primes_in_prime_table * sizeof(uint64_t)); }
void prime_sieve(uint64_t low_prime, uint64_t high_prime, void(*fun)(uint64_t), uint32_t mod, const uint_fast32_t *map) { uint64_t composite, prime, low_end_of_range, candidate; uint_fast32_t *sieve; uint32_t sieve_index, i, j, k; assert(primes_in_prime_table > 0); if (low_prime <= prime_table[primes_in_prime_table-1]) { /* Skip ahead to low_prime. A binary search would be faster. */ for (i = 0; prime_table[i] < low_prime; i++) ; while (i < primes_in_prime_table) { if (mod == 0) /* Null filter */ for (j = MIN(i+PROGRESS_STEP/3,primes_in_prime_table); i < j; i++) { if (prime_table[i] > high_prime) return; check_events(prime_table[i]); fun(prime_table[i]); } else if (map == NULL) /* Global GFN filter */ for (j = MIN(i+PROGRESS_STEP/3,primes_in_prime_table); i < j; i++) { if (prime_table[i] > high_prime) return; if (((uint_fast32_t)prime_table[i] & mod) == 0) { check_events(prime_table[i]); fun(prime_table[i]); } } else /* Global QR filter */ for (j = MIN(i+PROGRESS_STEP/3,primes_in_prime_table); i < j; i++) { if (prime_table[i] > high_prime) return; if (test_bit(map,prime_table[i]%mod)) { check_events(prime_table[i]); fun(prime_table[i]); } } check_progress(); } low_end_of_range = prime_table[primes_in_prime_table-1]+1; } else /* Set low_end_of_range to the greatest even number <= low_prime */ low_end_of_range = (low_prime | 1) - 1; sieve = make_bitmap(RANGE_SIZE); primes_used_in_range = 0; while (low_end_of_range <= high_prime) { setup_sieve(low_end_of_range); fill_bits(sieve,0,RANGE_SIZE-1); for (i = 0; i < primes_used_in_range; i++) { prime = prime_table[i]; composite = composite_table[i]; sieve_index = (composite - low_end_of_range)/2; while (composite < low_end_of_range + 2*RANGE_SIZE) { clear_bit(sieve,sieve_index); sieve_index += prime; composite += 2*prime; } composite_table[i] = composite; } k = MIN((high_prime-low_end_of_range+1)/2,RANGE_SIZE); i = first_bit(sieve); while (i < k) { if (mod == 0) /* Null filter */ for (j = MIN(i+PROGRESS_STEP,k); i < j; i = next_bit(sieve,i+1)) { candidate = low_end_of_range + 2*i + 1; check_events(candidate); fun(candidate); } else if (map == NULL) /* Global GFN filter */ for (j = MIN(i+PROGRESS_STEP,k); i < j; i = next_bit(sieve,i+1)) { candidate = low_end_of_range + 2*i + 1; if (((uint_fast32_t)candidate & mod) == 0) { check_events(candidate); fun(candidate); } } else /* Global QR filter */ for (j = MIN(i+PROGRESS_STEP,k); i < j; i = next_bit(sieve,i+1)) { candidate = low_end_of_range + 2*i + 1; if (test_bit(map,candidate%mod)) { check_events(candidate); fun(candidate); } } check_progress(); } low_end_of_range += 2*RANGE_SIZE; } free(sieve); }
static uint32_t first_n(uint32_t subseq) { return subseq_Q*(m_low+first_bit(SUBSEQ[subseq].M))+SUBSEQ[subseq].d; }