Friday, November 21, 2008

Sausages

So I was having endian problems moving it to SPARC while trying to use longs. Then I googled and saw this. Use a uint8_t, they said.

Sigh. So here it is with an array in 8 bit chunks.


#define UNSET_BIT(i,n) i[(n) / 8] &= ~(1 << (n) % 8)
#define GET_BIT(i,n) ((i[(n) / 8] & 1 << (n) % 8) != 0)

void get_primes_bv_odds(unsigned int n) {
uint8_t *sieve;
unsigned int i, j;

sieve = malloc((n+15)/16);
memset(sieve,~0,(n+15)/16);
UNSET_BIT(sieve, 1/2);
for (i = 3; i < sqrt(n); i += 2) {
if (GET_BIT(sieve, i / 2) == 0) continue;
for (j = i * i; j < n; j += i * 2) UNSET_BIT(sieve, j / 2);
}

printf("2\n");
for (i = 1; i < n; i += 2) if (GET_BIT(sieve, i / 2)) printf("%d\n", i);
}


First four lines:


6e cb b4 64 9a 12 6d 81 32 4c 4a 86 0d 82 96 21 c9 34 04 5a 20 61 89 a4 44 11
86 29 d1 82 28 4a 30 40 42 32 21 99 34 08 4b 06 25 42 84 48 8a 14 05 42 30 6c
08 b4 40 0b a0 08 51 12 28 89 04 65 98 30 4c 80 96 44 12 80 21 42 12 41 c9 04
21 c0 32 2d 98 00 00 49 04 08 81 96 68 82 b0 25 08 22 48 89 a2 40 59 26 04 90


Last entry about primes, I promise. Till the next one.

No comments: