un-inline arf_is_int, arf_is_int_2exp_si; small optimization of arf_is_int; add test code

This commit is contained in:
Fredrik Johansson 2016-02-26 13:54:10 +01:00
parent 4702cd1f74
commit 3fa073651f
4 changed files with 203 additions and 32 deletions

34
arf.h
View file

@ -676,39 +676,9 @@ arf_bot(fmpz_t e, const arf_t x)
fmpz_sub_si(e, ARF_EXPREF(x), arf_bits(x));
}
ARF_INLINE int
arf_is_int(const arf_t x)
{
if (arf_is_special(x))
return arf_is_zero(x);
else
{
fmpz_t t;
int r;
fmpz_init(t);
arf_bot(t, x);
r = fmpz_sgn(t) >= 0;
fmpz_clear(t);
return r;
}
}
int arf_is_int(const arf_t x);
ARF_INLINE int
arf_is_int_2exp_si(const arf_t x, slong e)
{
if (arf_is_special(x))
return arf_is_zero(x);
else
{
fmpz_t t;
int r;
fmpz_init(t);
arf_bot(t, x);
r = fmpz_cmp_si(t, e) >= 0;
fmpz_clear(t);
return r;
}
}
int arf_is_int_2exp_si(const arf_t x, slong e);
int arf_cmp_2exp_si(const arf_t x, slong e);

47
arf/is_int.c Normal file
View file

@ -0,0 +1,47 @@
/*=============================================================================
This file is part of ARB.
ARB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ARB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ARB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2016 Fredrik Johansson
******************************************************************************/
#include "arf.h"
int
arf_is_int(const arf_t x)
{
mp_size_t xn;
mp_srcptr xp;
slong exp, c;
exp = ARF_EXP(x);
if (ARF_IS_SPECIAL(x))
return exp == ARF_EXP_ZERO;
if (COEFF_IS_MPZ(exp))
return mpz_sgn(COEFF_TO_PTR(exp)) > 0;
ARF_GET_MPN_READONLY(xp, xn, x);
count_trailing_zeros(c, xp[0]);
return exp - xn * FLINT_BITS + c >= 0;
}

44
arf/is_int_2exp_si.c Normal file
View file

@ -0,0 +1,44 @@
/*=============================================================================
This file is part of ARB.
ARB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ARB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ARB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2016 Fredrik Johansson
******************************************************************************/
#include "arf.h"
int
arf_is_int_2exp_si(const arf_t x, slong e)
{
if (arf_is_special(x))
return arf_is_zero(x);
else
{
fmpz_t t;
int r;
fmpz_init(t);
arf_bot(t, x);
r = fmpz_cmp_si(t, e) >= 0;
fmpz_clear(t);
return r;
}
}

110
arf/test/t-is_int_2exp_si.c Normal file
View file

@ -0,0 +1,110 @@
/*=============================================================================
This file is part of ARB.
ARB is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
ARB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ARB; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2016 Fredrik Johansson
******************************************************************************/
#include "arf.h"
int main()
{
slong iter;
flint_rand_t state;
flint_printf("is_int_2exp_si....");
fflush(stdout);
flint_randinit(state);
for (iter = 0; iter < 10000; iter++)
{
arf_t x, y;
fmpz_t t;
slong e;
int res1, res2;
arf_init(x);
arf_init(y);
fmpz_init(t);
arf_randtest_special(x, state, 2000, 100);
e = n_randtest(state);
arf_mul_2exp_si(y, x, e);
res1 = arf_is_int(x);
res2 = arf_is_int_2exp_si(y, e);
if (res1 != res2)
{
flint_printf("FAIL! (1)\n");
flint_printf("x = "); arf_print(x); flint_printf("\n\n");
flint_printf("y = "); arf_print(y); flint_printf("\n\n");
flint_printf("res1 = %d, res2 = %d\n\n", res1, res2);
abort();
}
if (res1)
{
if (n_randint(state, 2))
arf_floor(y, x);
else
arf_ceil(y, x);
if (!arf_equal(x, y) || !arf_is_finite(x))
{
flint_printf("FAIL! (2)\n");
flint_printf("x = "); arf_print(x); flint_printf("\n\n");
flint_printf("y = "); arf_print(y); flint_printf("\n\n");
flint_printf("res1 = %d\n\n", res1);
abort();
}
}
if (arf_is_finite(x) && !arf_is_zero(x))
{
arf_bot(t, x);
fmpz_neg(t, t);
arf_mul_2exp_fmpz(x, x, t);
res1 = arf_is_int(x);
arf_mul_2exp_si(y, x, -1);
res2 = arf_is_int(y);
if (!arf_is_int(x) || arf_is_int(y))
{
flint_printf("FAIL! (3)\n");
flint_printf("x = "); arf_print(x); flint_printf("\n\n");
flint_printf("y = "); arf_print(y); flint_printf("\n\n");
flint_printf("res1 = %d, res2 = %d\n\n", res1, res2);
abort();
}
}
arf_clear(x);
arf_clear(y);
fmpz_clear(t);
}
flint_randclear(state);
flint_cleanup();
flint_printf("PASS\n");
return EXIT_SUCCESS;
}