Added some debugging output.

Pascal J. Bourguignon [2013-01-30 00:15]
Added some debugging output.
Filename
ast.c
diff --git a/ast.c b/ast.c
index 81c8af1..a68e75f 100644
--- a/ast.c
+++ b/ast.c
@@ -15,6 +15,7 @@ typedef enum {false=0,true=1} bool;


 void* memory_alloc(const char* fname,size_t size){
+    /*DEBUG*/printf("%s(%s,%zu)\n",__FUNCTION__,fname,size); fflush(stdout);
     void* memory=malloc(size);
     if(0==memory){
         fprintf(stderr,"%s: out of memory\n",fname);
@@ -23,6 +24,7 @@ void* memory_alloc(const char* fname,size_t size){


 int* compute_primes_to(int n){
+    /*DEBUG*/printf("%s(%d)\n",__FUNCTION__,n); fflush(stdout);
     if(n<2){
         int* primes=memory_alloc(__FUNCTION__,1);
         primes[0]=0;
@@ -50,41 +52,44 @@ int* compute_primes_to(int n){

         n-=(n%1)?3:2;
         bits_max=n/2;
-        words_max=bits_max/word_size;
+        words_max=(bits_max+word_size-1)/word_size;
         bits=memory_alloc(__FUNCTION__,words_max);
         for(int i=0;i<words_max;i++){
             bits[i]=(~0);}
         while(cur_prime<n){
-           bit=cur_prime+(cur_prime-3)/2;
-           while(bit<bits_max){
-               bits[bit/word_size]&=(~(1<<bit%word_size));
-               bit+=cur_prime;}
-           bit=1+((cur_prime-3)/2);
-           int pos=bit;
-           while((pos<=bits_max) and (0==(bits[pos/word_size]&(1<<pos%word_size)))){
+            bit=cur_prime+(cur_prime-3)/2;
+            while(bit<bits_max){
+                bits[bit/word_size]&=(~(1<<bit%word_size));
+                bit+=cur_prime;}
+            bit=1+((cur_prime-3)/2);
+            int pos=bit;
+            while((pos<=bits_max) and (0==(bits[pos/word_size]&(1<<pos%word_size)))){
                 pos++; }
-           bit=pos;
-           if(bit<bits_max){
-              cur_prime=bit+bit+3;
-              prime_count++;
-           }else{
-               cur_prime=n;
-           }}
-        primes=memory_alloc(__FUNCTION__,prime_count*sizeof(int));
+            bit=pos;
+            if(bit<bits_max){
+                cur_prime=bit+bit+3;
+                prime_count++;
+            }else{
+                cur_prime=n;
+            }}
+        /*DEBUG*/printf("%s(%d) prime_count=%d\n",__FUNCTION__,n,prime_count); fflush(stdout);
+        primes=memory_alloc(__FUNCTION__,(1+prime_count)*sizeof(int));
         primes[0]=prime_count;
         int curnum=1;
         primes[curnum++]=2;
         primes[curnum++]=3;
         cur_prime=3;
         bit=1;
-           while((bit<=bits_max) and (0==(bits[bit/word_size]&(1<<bit%word_size)))){
-                bit++; }
-        while(bit<bits_max){
-           cur_prime=bit+bit+3;
-           primes[curnum++]=cur_prime;
-           bit++;
-           while((bit<=bits_max) and (0==(bits[bit/word_size]&(1<<bit%word_size)))){
+        while((bit<=bits_max) and (0==(bits[bit/word_size]&(1<<bit%word_size)))){
+            bit++; }
+        while((curnum<=prime_count) and (bit<bits_max)){
+            cur_prime=bit+bit+3;
+            primes[curnum++]=cur_prime;
+            /*DEBUG*/printf("%s(%d) prime=%d\n",__FUNCTION__,n,cur_prime); fflush(stdout);
+            bit++;
+            while((bit<=bits_max) and (0==(bits[bit/word_size]&(1<<bit%word_size)))){
                 bit++; }}
+        /*DEBUG*/printf("%s(%d) exit\n",__FUNCTION__,n); fflush(stdout);
         return(primes);}}


@@ -92,6 +97,7 @@ int* compute_primes_to(int n){
 int* primes=0;

 int ceiling_to_nearest_prime(int n){
+    /*DEBUG*/printf("%s(%d)\n",__FUNCTION__,n); fflush(stdout);
     if((0==primes) or (primes[primes[0]-1]<n)){
         primes=compute_primes_to(n*2);
     }
@@ -100,10 +106,10 @@ int ceiling_to_nearest_prime(int n){
     int ind=(min+max)/2;
     int ord=(n==primes[ind])?0:(n<primes[ind])?-1:1;
     while((0!=ord) and (min!=ind)){
-       if(ord<0){
-                    max=ind;
+        if(ord<0){
+            max=ind;
         }else{
-        min=ind;
+            min=ind;
         }
         ind=(min+max)/2;
         ord=(n==primes[ind])?0:(n<primes[ind])?-1:1;}
@@ -111,13 +117,14 @@ int ceiling_to_nearest_prime(int n){
         ord=1;
         ind--;}
     if(ord<0){
-       return(primes[1]);
+        return(primes[1]);
     }else{
-       return(primes[ind+1]);
+        return(primes[ind+1]);
     }}


 int argv_count(const char* const* argv){
+    /*DEBUG*/printf("%s(…)\n",__FUNCTION__); fflush(stdout);
     int cnt=0;
     while(argv[cnt]){
         cnt++;
@@ -126,6 +133,7 @@ int argv_count(const char* const* argv){


 const char* const* argv_concat(const char* const* argv1,const char* const* argv2){
+    /*DEBUG*/printf("%s(…)\n",__FUNCTION__); fflush(stdout);
     int cnt1=argv_count(argv1);
     int cnt2=argv_count(argv2);
     const char** result=memory_alloc(__FUNCTION__,(1+cnt1+cnt2)*sizeof(char*));
@@ -178,6 +186,7 @@ typedef struct {


 object* box(object_type type,...){
+    /*DEBUG*/printf("%s(%s,…)\n",__FUNCTION__,type_name(type)); fflush(stdout);
     object* o=memory_alloc(__FUNCTION__,sizeof(*o));
     o->type=type;
     va_list ap;
@@ -574,9 +583,11 @@ int test_primes[]={

 #define testing(BODY)                                                   \
     do{                                                                 \
-        printf("%s",__FUNCTION__);                                      \
+        printf("%s ",__FUNCTION__);                                     \
+        fflush(stdout);                                                 \
         { BODY }                                                        \
-        printf(" [OK]\n");                                              \
+          printf(" [OK]\n");                                            \
+          fflush(stdout);                                               \
     } while(0)

ViewGit