int fx_mantissa(Rational lbd, Rational ubd, Rational small) /*;mantissa*/ { Rational exact_val; int *vnum, *vden, *int_1; int power; lbd = rat_abs(lbd); ubd = rat_abs(ubd); /* find the exact # of values to be represented (aside from 0) */ if (rat_gtr(lbd, ubd)) exact_val = rat_div(lbd, small); else exact_val = rat_div(ubd, small); vnum = num(exact_val); vden = den(exact_val); int_1 = int_fri(1); /* the mantissa is calculated assuming that the bound is 'small away * from a model number, so we subtract one before computing no. of bits */ vnum = int_sub(vnum, int_1); vnum = int_quo(vnum, vden); vden = int_fri(1); power = 1; while (int_gtr(vnum, vden)) { power++; vden = int_add(int_add(vden, vden), int_1); } return power; }
int main(int argc, char *argv[]) { int a, b, c, d; a = int_add(-1, 2); b = int_sub(1, 2); c = int_mul(3, 4); d = int_div(4, -2); return (a + b + c + d) ? 0 : 1; }
void mod_inv(BIGINT *a, BIGINT *b, BIGINT *x) { BIGINT m, n, p0, p1, p2, q, r, temp, dummy; ELEMENT check; INDEX sw, i; /* initialize loop variables sw = 1; m = b; n = a; p0 = 1; p1 = m/n; q = p1; r = m % n; */ sw = 1; int_copy( b, &m); int_copy( a, &n); int_null ( &p0); p0.hw[INTMAX] = 1; int_div ( &m, &n, &p1, &r); int_copy ( &p1, &q); /* main loop, compute continued fraction intermediates */ check = 0; INTLOOP (i) check |= r.hw[i]; while (check) { sw = -sw; int_copy( &n, &m); int_copy( &r, &n); int_div( &m, &n, &q, &r); /* p2 = (q * p1 + p0) % b; core operation of routine */ int_mul( &q, &p1, &temp); int_add( &temp, &p0, &temp); int_div( &temp, b, &dummy, &p2); int_copy( &p1, &p0); int_copy( &p2, &p1); check = 0; INTLOOP (i) check |= r.hw[i]; } /* sw keeps track of sign. If sw < 0, add modulus to result */ if (sw < 0) int_sub( b, &p0, x); else int_copy( &p0, x); }
void int_gcd(BIGINT *u, BIGINT *v, BIGINT *w) { INDEX k, i, flag; ELEMENT check, carry_bit; BIGINT t, U, V; int_copy( u, &U); int_copy( v, &V); /* find common powers of 2 and eliminate them */ k = 0; /* check that both u and v are even */ while ( !(U.hw[INTMAX] & 1 || V.hw[INTMAX] & 1)) { /* increment power of 2 and divide both u and v by 2 */ k++; int_div2( &U); int_div2( &V); } /* Now both u and v have been divided by 2^k. If u is odd, set t = v and flag, otherwise t = u and clear flag */ if (U.hw[INTMAX] & 1) { int_copy( &V, &t); flag = -1; } else { int_copy( &U, &t); flag = 1; } check = 0; INTLOOP (i) check |= t.hw[i]; while (check) { /* while t is even, divide by 2 */ while ( !(t.hw[INTMAX] & 1)) int_div2( &t); /* reset u or v to t depending on sign of flag */ if (flag > 0) int_copy( &t, &U); else int_copy( &t, &V); /* t = u - v; core reduction step, gcd remains unchanged */ int_sub( &U, &V, &t); if (t.hw[0] & MSB_HW) { flag = -1; int_neg( &t); } else flag = 1; check = 0; INTLOOP (i) check |= t.hw[i]; } /* reapply common powers of 2. First do words, then do bits.*/ int_copy( &U, w); while ( k > HALFSIZE ) { for (i=0; i<INTMAX; i++) w->hw[i] = w->hw[i+1]; k -= HALFSIZE; w->hw[INTMAX] = 0; } carry_bit = 0; while ( k > 0 ) { INTLOOP (i) { w->hw[i] = (w->hw[i] << 1) | carry_bit; carry_bit = w->hw[i] & CARRY ? 1 : 0; w->hw[i] &= LOMASK; } k--; } }