#include "profiler.h"
#include "gmpcompat.h"
#include "fmpz.h"
#define ntests 1000
void
fmpz_mul_2exp_old(fmpz_t f, const fmpz_t g, ulong exp)
{
fmpz d = *g;
if (!COEFF_IS_MPZ(d))
{
ulong dabs = FLINT_ABS(d);
ulong bits = FLINT_BIT_COUNT(dabs);
if (bits == 0)
{
fmpz_set_si(f, 0);
}
else if (bits + exp <= SMALL_FMPZ_BITCOUNT_MAX)
{
fmpz_set_si(f, d << exp);
}
else
{
mpz_ptr mf = _fmpz_promote(f);
flint_mpz_set_si(mf, d);
mpz_mul_2exp(mf, mf, exp);
}
}
else
{
mpz_ptr mf = _fmpz_promote(f);
mpz_mul_2exp(mf, COEFF_TO_PTR(d), exp);
}
}
void
sample_new(void * arg, ulong count)
{
fmpz_t res, a;
ulong ix, jx, b;
int bits = *((int *) arg);
FLINT_TEST_INIT(state);
fmpz_init(res);
fmpz_init(a);
for (ix = 0; ix < count; ix++)
{
fmpz_randtest(a, state, bits);
b = n_randint(state, 200);
prof_start();
for (jx = 0; jx < ntests; jx++)
fmpz_mul_2exp(res, a, b);
prof_stop();
}
fmpz_clear(res);
fmpz_clear(a);
FLINT_TEST_CLEAR(state);
}
void
sample_old(void * arg, ulong count)
{
fmpz_t res, a;
ulong ix, jx, b;
int bits = *((int *) arg);
FLINT_TEST_INIT(state);
fmpz_init(res);
fmpz_init(a);
for (ix = 0; ix < count; ix++)
{
fmpz_randtest(a, state, bits);
b = n_randint(state, 200);
prof_start();
for (jx = 0; jx < ntests; jx++)
fmpz_mul_2exp_old(res, a, b);
prof_stop();
}
fmpz_clear(res);
fmpz_clear(a);
FLINT_TEST_CLEAR(state);
}
int
main(void)
{
double minnew, maxnew, minold, maxold;
int bits;
for (bits = 5; bits <= 150; bits += 5)
{
prof_repeat(&minnew, &maxnew, sample_new, &bits);
prof_repeat(&minold, &maxold, sample_old, &bits);
flint_printf("%d bits: min %.2fx, max %.2fx\n",
bits, minold / minnew, maxold / maxnew);
}
return 0;
}