int32_t choose_unfixed_variable(atom_table_t *atom_table) { samp_truth_value_t *assignment = atom_table->assignment; int32_t num_vars = atom_table->num_vars; int32_t num_unfixed_vars = atom_table->num_unfixed_vars; uint32_t var, d, y; if (num_unfixed_vars == 0) return -1; // var = random_uint(num_vars); var = genrand_uint(num_vars); if (unfixed_tval(assignment[var])) return var; // d = 1 + random_uint(num_vars - 1); d = 1 + genrand_uint(num_vars - 1); while (gcd32(d, num_vars) != 1) d--; y = var; do { y += d; if (y >= num_vars) y -= num_vars; assert(var != y); } while (!unfixed_tval(assignment[y])); return y; }
void rationalize32 (float f,uint32_t *numerator,uint32_t *denominator) { uint32_t i,j; // try x for 32 bit x j=1; i=nearbyintf(((float)j)*f); if (i==0){ i=nearbyintf(1000000000*f); if (i==0){ *numerator=0; *denominator=1; return; } } else if (fequal(f,(float)i)){ *numerator=i; *denominator=1; return; } // try x/y for 8 bit x,y for(j=2;j<256;j++){ i=nearbyintf(((float)j)*f); if (i<=0 || i>=256) continue; if (fequal(f,((float)i)/((float)j))){ *numerator=i; *denominator=j; return; } } // try 3(4??) digit decimal. for(j=1000;j<1000000000;j=j*10){ i=nearbyintf(((float)j)*f); if (i<=100 || i>10000) continue; if (fequal(f,((float)i)/((float)j))){ uint32_t c=gcd32(i,j); *numerator=i/c; *denominator=j/c; return; } } Abort("Attempt to reverse engineer %f failed",f); }
uint32_t lcm32(uint32_t a,uint32_t b){ return (a/gcd32(a,b))*b; }
uint32_t make_subseqs(int list) { uint32_t i, j, r, n, Q, s, s_count, t_count, tmp_low, tmp_high; uint32_t *g, *nmin, *subseq; subseq_t *tmp; #ifndef NDEBUG uint32_t old_count = 0; #endif if (subseq_force_base) Q = find_best_Q(subseq_force_base,list,&s_count,1); else Q = find_best_Q(subseq_limit_base,list,&s_count,0); if (list == 0 && Q == 1) /* Nothing to do */ return remaining_terms; tmp = xmalloc(s_count*sizeof(subseq_t)); tmp_low = n_min/Q; tmp_high = n_max/Q; g = xmalloc(Q*sizeof(uint32_t)); nmin = xmalloc(Q*sizeof(uint32_t)); subseq = xmalloc(Q*sizeof(uint32_t)); for (i = 0, s = 0; i < seq_count; i++) { for (j = 0; j < Q; j++) g[j] = 0, nmin[j] = UINT32_MAX; if (list) for (j = 0; j < SEQ[i].ncount; j++) { n = SEQ[i].N[j]; r = n % Q; nmin[r] = MIN(nmin[r],n); g[r] = gcd32(g[r],n-nmin[r]); } else for (n = first_n(i); n <= n_max; n = next_n(i,n)) { r = n % Q; nmin[r] = MIN(nmin[r],n); g[r] = gcd32(g[r],n-nmin[r]); } for (r = 0; r < Q; r++) if (nmin[r] != UINT32_MAX) { tmp[s].seq = i; tmp[s].d = r; tmp[s].filter = 0; tmp[s].M = make_bitmap(tmp_high-tmp_low+1); tmp[s].mcount = 0; tmp[s].a = MAX(1,g[r]); tmp[s].b = nmin[r]%tmp[s].a; subseq[r] = s++; } assert (s <= s_count); if (list) for (j = 0; j < SEQ[i].ncount; j++) { n = SEQ[i].N[j]; r = n % Q; set_bit(tmp[subseq[r]].M,n/Q-tmp_low); tmp[subseq[r]].mcount++; } else for (n = first_n(i); n <= n_max; n = next_n(i,n)) { r = n % Q; set_bit(tmp[subseq[r]].M,n/Q-tmp_low); tmp[subseq[r]].mcount++; } if (list) { free(SEQ[i].N); SEQ[i].N = NULL; SEQ[i].nsize = 0; SEQ[i].type = seq_bitmap; #ifndef NDEBUG old_count += SEQ[i].ncount; #endif } else { free(SUBSEQ[i].M); SUBSEQ[i].M = NULL; #ifndef NDEBUG old_count += SUBSEQ[i].mcount; #endif } } assert (s == s_count); free(subseq); free(nmin); free(g); if (Q > 1) report("Split %"PRIu32" base %"PRIu32" sequence%s into %" PRIu32" base %"PRIu32"^%"PRIu32" subsequence%s.", seq_count, base, plural(seq_count), s_count, base, Q, plural(s_count)); subseq_Q = Q; m_low = tmp_low; m_high = tmp_high; if (!list) free(SUBSEQ); SUBSEQ = tmp; subseq_count = s_count; for (i = 0, t_count = 0; i < subseq_count; i++) t_count += SUBSEQ[i].mcount; assert(t_count == old_count); return t_count; }