summaryrefslogtreecommitdiff
path: root/nss/lib/freebl/mpi/utils/sieve.c
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-01-04 14:24:24 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-01-04 14:24:24 +0000
commitdc1565216a5d20ae0d75872151523252309a1292 (patch)
treed57454ba9a40386552179eddf60d28bd1e8f3d54 /nss/lib/freebl/mpi/utils/sieve.c
parent26c046fbc57d53136b4fb3b5e0d18298318125d4 (diff)
downloadnss-dc1565216a5d20ae0d75872151523252309a1292.tar.gz
nss-3.28.1nss-3.28.1
Diffstat (limited to 'nss/lib/freebl/mpi/utils/sieve.c')
-rw-r--r--nss/lib/freebl/mpi/utils/sieve.c288
1 files changed, 148 insertions, 140 deletions
diff --git a/nss/lib/freebl/mpi/utils/sieve.c b/nss/lib/freebl/mpi/utils/sieve.c
index 71a17c8..57768af 100644
--- a/nss/lib/freebl/mpi/utils/sieve.c
+++ b/nss/lib/freebl/mpi/utils/sieve.c
@@ -28,14 +28,14 @@
#include <stdlib.h>
#include <limits.h>
-typedef unsigned char byte;
+typedef unsigned char byte;
typedef struct {
- int size;
- byte *bits;
- long base;
- int next;
- int nbits;
+ int size;
+ byte *bits;
+ long base;
+ int next;
+ int nbits;
} sieve;
void sieve_init(sieve *sp, long base, int nbits);
@@ -45,191 +45,199 @@ void sieve_reset(sieve *sp, long base);
void sieve_cross(sieve *sp, long val);
void sieve_clear(sieve *sp);
-#define S_ISSET(S, B) (((S)->bits[(B)/CHAR_BIT]>>((B)%CHAR_BIT))&1)
-#define S_SET(S, B) ((S)->bits[(B)/CHAR_BIT]|=(1<<((B)%CHAR_BIT)))
-#define S_CLR(S, B) ((S)->bits[(B)/CHAR_BIT]&=~(1<<((B)%CHAR_BIT)))
-#define S_VAL(S, B) ((S)->base+(2*(B)))
-#define S_BIT(S, V) (((V)-((S)->base))/2)
+#define S_ISSET(S, B) (((S)->bits[(B) / CHAR_BIT] >> ((B) % CHAR_BIT)) & 1)
+#define S_SET(S, B) ((S)->bits[(B) / CHAR_BIT] |= (1 << ((B) % CHAR_BIT)))
+#define S_CLR(S, B) ((S)->bits[(B) / CHAR_BIT] &= ~(1 << ((B) % CHAR_BIT)))
+#define S_VAL(S, B) ((S)->base + (2 * (B)))
+#define S_BIT(S, V) (((V) - ((S)->base)) / 2)
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
- sieve s;
- long pr, *p;
- int c, ix, cur = 0;
+ sieve s;
+ long pr, *p;
+ int c, ix, cur = 0;
- if(argc < 2) {
- fprintf(stderr, "Usage: %s <width>\n", argv[0]);
- return 1;
- }
+ if (argc < 2) {
+ fprintf(stderr, "Usage: %s <width>\n", argv[0]);
+ return 1;
+ }
- c = atoi(argv[1]);
- if(c < 0) c = -c;
+ c = atoi(argv[1]);
+ if (c < 0)
+ c = -c;
- fprintf(stderr, "%s: sieving to %d positions\n", argv[0], c);
+ fprintf(stderr, "%s: sieving to %d positions\n", argv[0], c);
- sieve_init(&s, 3, c);
+ sieve_init(&s, 3, c);
- c = 0;
- while((pr = sieve_next(&s)) > 0) {
- ++c;
- }
+ c = 0;
+ while ((pr = sieve_next(&s)) > 0) {
+ ++c;
+ }
- p = calloc(c, sizeof(long));
- if(!p) {
- fprintf(stderr, "%s: out of memory after first half\n", argv[0]);
- sieve_clear(&s);
- exit(1);
- }
+ p = calloc(c, sizeof(long));
+ if (!p) {
+ fprintf(stderr, "%s: out of memory after first half\n", argv[0]);
+ sieve_clear(&s);
+ exit(1);
+ }
+
+ fprintf(stderr, "%s: half done ... \n", argv[0]);
- fprintf(stderr, "%s: half done ... \n", argv[0]);
+ for (ix = 0; ix < s.nbits; ix++) {
+ if (S_ISSET(&s, ix)) {
+ p[cur] = S_VAL(&s, ix);
+ printf("%ld\n", p[cur]);
+ ++cur;
+ }
+ }
- for(ix = 0; ix < s.nbits; ix++) {
- if(S_ISSET(&s, ix)) {
- p[cur] = S_VAL(&s, ix);
- printf("%ld\n", p[cur]);
- ++cur;
+ sieve_reset(&s, p[cur - 1]);
+ fprintf(stderr, "%s: crossing off %d found primes ... \n", argv[0], cur);
+ for (ix = 0; ix < cur; ix++) {
+ sieve_cross(&s, p[ix]);
+ if (!(ix % 1000))
+ fputc('.', stderr);
}
- }
-
- sieve_reset(&s, p[cur - 1]);
- fprintf(stderr, "%s: crossing off %d found primes ... \n", argv[0], cur);
- for(ix = 0; ix < cur; ix++) {
- sieve_cross(&s, p[ix]);
- if(!(ix % 1000))
- fputc('.', stderr);
- }
- fputc('\n', stderr);
-
- free(p);
-
- fprintf(stderr, "%s: sieving again from %ld ... \n", argv[0], p[cur - 1]);
- c = 0;
- while((pr = sieve_next(&s)) > 0) {
- ++c;
- }
-
- fprintf(stderr, "%s: done!\n", argv[0]);
- for(ix = 0; ix < s.nbits; ix++) {
- if(S_ISSET(&s, ix)) {
- printf("%ld\n", S_VAL(&s, ix));
+ fputc('\n', stderr);
+
+ free(p);
+
+ fprintf(stderr, "%s: sieving again from %ld ... \n", argv[0], p[cur - 1]);
+ c = 0;
+ while ((pr = sieve_next(&s)) > 0) {
+ ++c;
+ }
+
+ fprintf(stderr, "%s: done!\n", argv[0]);
+ for (ix = 0; ix < s.nbits; ix++) {
+ if (S_ISSET(&s, ix)) {
+ printf("%ld\n", S_VAL(&s, ix));
+ }
}
- }
- sieve_clear(&s);
+ sieve_clear(&s);
- return 0;
+ return 0;
}
-void sieve_init(sieve *sp, long base, int nbits)
+void
+sieve_init(sieve *sp, long base, int nbits)
{
- sp->size = (nbits / CHAR_BIT);
-
- if(nbits % CHAR_BIT)
- ++sp->size;
-
- sp->bits = calloc(sp->size, sizeof(byte));
- memset(sp->bits, UCHAR_MAX, sp->size);
- if(!(base & 1))
- ++base;
- sp->base = base;
-
- sp->next = 0;
- sp->nbits = sp->size * CHAR_BIT;
+ sp->size = (nbits / CHAR_BIT);
+
+ if (nbits % CHAR_BIT)
+ ++sp->size;
+
+ sp->bits = calloc(sp->size, sizeof(byte));
+ memset(sp->bits, UCHAR_MAX, sp->size);
+ if (!(base & 1))
+ ++base;
+ sp->base = base;
+
+ sp->next = 0;
+ sp->nbits = sp->size * CHAR_BIT;
}
-void sieve_grow(sieve *sp, int nbits)
+void
+sieve_grow(sieve *sp, int nbits)
{
- int ns = (nbits / CHAR_BIT);
+ int ns = (nbits / CHAR_BIT);
- if(nbits % CHAR_BIT)
- ++ns;
+ if (nbits % CHAR_BIT)
+ ++ns;
- if(ns > sp->size) {
- byte *tmp;
- int ix;
+ if (ns > sp->size) {
+ byte *tmp;
+ int ix;
- tmp = calloc(ns, sizeof(byte));
- if(tmp == NULL) {
- fprintf(stderr, "Error: out of memory in sieve_grow\n");
- return;
- }
+ tmp = calloc(ns, sizeof(byte));
+ if (tmp == NULL) {
+ fprintf(stderr, "Error: out of memory in sieve_grow\n");
+ return;
+ }
- memcpy(tmp, sp->bits, sp->size);
- for(ix = sp->size; ix < ns; ix++) {
- tmp[ix] = UCHAR_MAX;
- }
+ memcpy(tmp, sp->bits, sp->size);
+ for (ix = sp->size; ix < ns; ix++) {
+ tmp[ix] = UCHAR_MAX;
+ }
- free(sp->bits);
- sp->bits = tmp;
- sp->size = ns;
+ free(sp->bits);
+ sp->bits = tmp;
+ sp->size = ns;
- sp->nbits = sp->size * CHAR_BIT;
- }
+ sp->nbits = sp->size * CHAR_BIT;
+ }
}
-long sieve_next(sieve *sp)
+long
+sieve_next(sieve *sp)
{
- long out;
- int ix = 0;
- long val;
+ long out;
+ int ix = 0;
+ long val;
- if(sp->next > sp->nbits)
- return -1;
+ if (sp->next > sp->nbits)
+ return -1;
- out = S_VAL(sp, sp->next);
+ out = S_VAL(sp, sp->next);
#ifdef DEBUG
- fprintf(stderr, "Sieving %ld\n", out);
+ fprintf(stderr, "Sieving %ld\n", out);
#endif
- /* Sieve out all multiples of the current prime */
- val = out;
- while(ix < sp->nbits) {
- val += out;
- ix = S_BIT(sp, val);
- if((val & 1) && ix < sp->nbits) { /* && S_ISSET(sp, ix)) { */
- S_CLR(sp, ix);
+ /* Sieve out all multiples of the current prime */
+ val = out;
+ while (ix < sp->nbits) {
+ val += out;
+ ix = S_BIT(sp, val);
+ if ((val & 1) && ix < sp->nbits) { /* && S_ISSET(sp, ix)) { */
+ S_CLR(sp, ix);
#ifdef DEBUG
- fprintf(stderr, "Crossing out %ld (bit %d)\n", val, ix);
+ fprintf(stderr, "Crossing out %ld (bit %d)\n", val, ix);
#endif
+ }
}
- }
- /* Scan ahead to the next prime */
- ++sp->next;
- while(sp->next < sp->nbits && !S_ISSET(sp, sp->next))
+ /* Scan ahead to the next prime */
++sp->next;
+ while (sp->next < sp->nbits && !S_ISSET(sp, sp->next))
+ ++sp->next;
- return out;
+ return out;
}
-void sieve_cross(sieve *sp, long val)
+void
+sieve_cross(sieve *sp, long val)
{
- int ix = 0;
- long cur = val;
+ int ix = 0;
+ long cur = val;
- while(cur < sp->base)
- cur += val;
+ while (cur < sp->base)
+ cur += val;
- ix = S_BIT(sp, cur);
- while(ix < sp->nbits) {
- if(cur & 1)
- S_CLR(sp, ix);
- cur += val;
ix = S_BIT(sp, cur);
- }
+ while (ix < sp->nbits) {
+ if (cur & 1)
+ S_CLR(sp, ix);
+ cur += val;
+ ix = S_BIT(sp, cur);
+ }
}
-void sieve_reset(sieve *sp, long base)
+void
+sieve_reset(sieve *sp, long base)
{
- memset(sp->bits, UCHAR_MAX, sp->size);
- sp->base = base;
- sp->next = 0;
+ memset(sp->bits, UCHAR_MAX, sp->size);
+ sp->base = base;
+ sp->next = 0;
}
-void sieve_clear(sieve *sp)
+void
+sieve_clear(sieve *sp)
{
- if(sp->bits)
- free(sp->bits);
+ if (sp->bits)
+ free(sp->bits);
- sp->bits = NULL;
+ sp->bits = NULL;
}