Skip to content

Commit

Permalink
Add tests for aorsrsh
Browse files Browse the repository at this point in the history
  • Loading branch information
Albin Ahlbäck committed Dec 2, 2024
1 parent 2daf43a commit 96786cd
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/mpn_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ mp_limb_t mpn_rsh1sub_n(mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
#if FLINT_HAVE_ASSEMBLY_x86_64_adx
# define FLINT_MPN_AORSRSH_FUNC_TAB_WIDTH 17

# define FLINT_HAVE_AORS_FUNC(n) ((n) < FLINT_MPN_AORS_FUNC_TAB_WIDTH)
# define FLINT_HAVE_AORSRSH_FUNC(n) ((n) < FLINT_MPN_AORSRSH_FUNC_TAB_WIDTH)

# define FLINT_MPN_ADDRSH_HARD(rp, xp, yp, n, cnt) (flint_mpn_addrsh_func_tab[n](rp, xp, yp, cnt))
# define FLINT_MPN_SUBRSH_HARD(rp, xp, yp, n, cnt) (flint_mpn_subrsh_func_tab[n](rp, xp, yp, cnt))
Expand All @@ -485,6 +485,7 @@ MPN_EXTRAS_INLINE
mp_limb_t flint_mpn_addrsh_n(mp_ptr rp, mp_srcptr xp, mp_srcptr yp, mp_size_t n, unsigned int cnt)
{
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(1 <= cnt && cnt < FLINT_BITS);

if (FLINT_HAVE_AORSRSH_FUNC(n))
return FLINT_MPN_ADDRSH_HARD(rp, xp, yp, n, cnt);
Expand All @@ -499,12 +500,12 @@ MPN_EXTRAS_INLINE
mp_limb_t flint_mpn_subrsh_n(mp_ptr rp, mp_srcptr xp, mp_srcptr yp, mp_size_t n, unsigned int cnt)
{
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(1 <= cnt && cnt < FLINT_BITS);

if (FLINT_HAVE_AORSRSH_FUNC(n))
return FLINT_MPN_SUBRSH_HARD(rp, xp, yp, n, cnt);
else
{
/* r = x - 2^c y */
mpn_rshift(rp, yp, n, cnt);
return mpn_sub_n(rp, xp, rp, n);
}
Expand Down
2 changes: 2 additions & 0 deletions src/mpn_extras/test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "t-2add_n_inplace.c"
#include "t-aors_n.c"
#include "t-aorsrsh_n.c"
#include "t-divides.c"
#include "t-divrem_preinv1.c"
#include "t-divrem_preinvn.c"
Expand Down Expand Up @@ -40,6 +41,7 @@ test_struct tests[] =
{
TEST_FUNCTION(flint_mpn_2add_n_inplace),
TEST_FUNCTION(flint_mpn_aors_n),
TEST_FUNCTION(flint_mpn_aorsrsh_n),
TEST_FUNCTION(flint_mpn_divides),
TEST_FUNCTION(flint_mpn_divrem_preinv1),
TEST_FUNCTION(flint_mpn_divrem_preinvn),
Expand Down
35 changes: 32 additions & 3 deletions src/mpn_extras/test/t-aors_n.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ TEST_FUNCTION_START(flint_mpn_aors_n, state)
{
int result;
int type;
int aliasing;
mp_limb_t cf, cg;
mp_size_t n;
mp_ptr fp, gp, xp, yp;
Expand All @@ -34,6 +35,11 @@ TEST_FUNCTION_START(flint_mpn_aors_n, state)
if (n_randint(state, 1 << 10) == UWORD(0))
n += N_STOR;

/* 0: No aliasing
* 1: fp = xp
* 2: fp = yp */
aliasing = n_randint(state, 3);

fp = flint_malloc(sizeof(mp_limb_t) * n);
gp = flint_malloc(sizeof(mp_limb_t) * n);
xp = flint_malloc(sizeof(mp_limb_t) * n);
Expand All @@ -46,27 +52,50 @@ TEST_FUNCTION_START(flint_mpn_aors_n, state)

if (type == 0)
{
cf = flint_mpn_add_n(fp, xp, yp, n);
if (aliasing == 0)
cf = flint_mpn_add_n(fp, xp, yp, n);
else if (aliasing == 1)
{
flint_mpn_copyi(fp, xp, n);
cf = flint_mpn_add_n(fp, fp, yp, n);
}
else
{
flint_mpn_copyi(fp, yp, n);
cf = flint_mpn_add_n(fp, xp, fp, n);
}
cg = mpn_add_n(gp, xp, yp, n);
}
else
{
cf = flint_mpn_sub_n(fp, xp, yp, n);
if (aliasing == 0)
cf = flint_mpn_sub_n(fp, xp, yp, n);
else if (aliasing == 1)
{
flint_mpn_copyi(fp, xp, n);
cf = flint_mpn_sub_n(fp, fp, yp, n);
}
else
{
flint_mpn_copyi(fp, yp, n);
cf = flint_mpn_sub_n(fp, xp, fp, n);
}
cg = mpn_sub_n(gp, xp, yp, n);
}

result = (cf == cg && mpn_cmp(fp, gp, n) == 0);
if (!result)
TEST_FUNCTION_FAIL(
"%s:\n"
"aliasing: %d\n"
"ix = %wd\n"
"n = %wd\n"
"xp = %{ulong*}\n"
"yp = %{ulong*}\n"
"FLINT (cy = %wu): %{ulong*}\n"
"GMP (cy = %wu): %{ulong*}\n",
type == 0 ? "flint_mpn_add_n" : "flint_mpn_sub_n",
ix, n, xp, n, yp, n, cf, fp, n, cg, gp, n + 1);
aliasing, ix, n, xp, n, yp, n, cf, fp, n, cg, gp, n + 1);

flint_free(fp);
flint_free(gp);
Expand Down
125 changes: 125 additions & 0 deletions src/mpn_extras/test/t-aorsrsh_n.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright (C) 2024 Albin Ahlbäck
Copyright (C) 2024 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/

#include "test_helpers.h"
#include "mpn_extras.h"

#define N_MIN 1
#define N_MAX (FLINT_MPN_AORSRSH_FUNC_TAB_WIDTH - 1)
#define N_STOR (FLINT_MPN_AORSRSH_FUNC_TAB_WIDTH + 10)

static mp_limb_t mpn_addrsh_n(mp_ptr rp, mp_srcptr xp, mp_srcptr yp, mp_size_t n, unsigned int cnt)
{
mpn_rshift(rp, yp, n, cnt);
return mpn_add_n(rp, rp, xp, n);
}

static mp_limb_t mpn_subrsh_n(mp_ptr rp, mp_srcptr xp, mp_srcptr yp, mp_size_t n, unsigned int cnt)
{
mpn_rshift(rp, yp, n, cnt);
return mpn_sub_n(rp, xp, rp, n);
}

TEST_FUNCTION_START(flint_mpn_aorsrsh_n, state)
{
#if FLINT_USE_AORSRSH_FUNC_TAB
slong ix;

for (ix = 0; ix < 10000 * flint_test_multiplier(); ix++)
{
int result;
int type;
int aliasing;
unsigned int cnt;
mp_limb_t cf, cg;
mp_size_t n;
mp_ptr fp, gp, xp, yp;

n = N_MIN + n_randint(state, N_MAX - N_MIN + 1);
if (n_randint(state, 1 << 10) == UWORD(0))
n += N_STOR;

/* 0: No aliasing
* 1: fp = xp
* 2: fp = yp */
aliasing = n_randint(state, 3);

fp = flint_malloc(sizeof(mp_limb_t) * n);
gp = flint_malloc(sizeof(mp_limb_t) * n);
xp = flint_malloc(sizeof(mp_limb_t) * n);
yp = flint_malloc(sizeof(mp_limb_t) * n);

flint_mpn_rrandom(xp, state, n);
flint_mpn_rrandom(yp, state, n);
cnt = 1 + n_randint(state, FLINT_BITS - 1);

type = n_randint(state, 2);

if (type == 0)
{
if (aliasing == 0)
cf = flint_mpn_addrsh_n(fp, xp, yp, n, cnt);
else if (aliasing == 1)
{
flint_mpn_copyi(fp, xp, n);
cf = flint_mpn_addrsh_n(fp, fp, yp, n, cnt);
}
else
{
flint_mpn_copyi(fp, yp, n);
cf = flint_mpn_addrsh_n(fp, xp, fp, n, cnt);
}
cg = mpn_addrsh_n(gp, xp, yp, n, cnt);
}
else
{
if (aliasing == 0)
cf = flint_mpn_subrsh_n(fp, xp, yp, n, cnt);
else if (aliasing == 1)
{
flint_mpn_copyi(fp, xp, n);
cf = flint_mpn_subrsh_n(fp, fp, yp, n, cnt);
}
else
{
flint_mpn_copyi(fp, yp, n);
cf = flint_mpn_subrsh_n(fp, xp, fp, n, cnt);
}
cg = mpn_subrsh_n(gp, xp, yp, n, cnt);
}

result = (cf == cg && mpn_cmp(fp, gp, n) == 0);
if (!result)
TEST_FUNCTION_FAIL(
"%s:\n"
"aliasing: %d\n"
"ix = %wd\n"
"n = %u\n"
"cnt = %wd\n"
"xp = %{ulong*}\n"
"yp = %{ulong*}\n"
"FLINT (cy = %wu): %{ulong*}\n"
"GMP (cy = %wu): %{ulong*}\n",
type == 0 ? "flint_mpn_add_n" : "flint_mpn_sub_n",
aliasing, ix, n, cnt, xp, n, yp, n, cf, fp, n, cg, gp, n + 1);

flint_free(fp);
flint_free(gp);
flint_free(xp);
flint_free(yp);
}

TEST_FUNCTION_END(state);
#else
TEST_FUNCTION_END_SKIPPED(state);
#endif
}

0 comments on commit 96786cd

Please sign in to comment.