(root)/
gmp-6.3.0/
tests/
mpn/
t-toom2-sqr.c
       1  #define mpn_toomN_sqr mpn_toom2_sqr
       2  #define mpn_toomN_sqr_itch mpn_toom2_sqr_itch
       3  #define MIN_AN MPN_TOOM2_SQR_MINSIZE
       4  #define MAX_AN SQR_TOOM3_THRESHOLD
       5  
       6  #define MORE_SQR_TESTS explore_unlikely_branch
       7  #include "toom-sqr-shared.h"
       8  
       9  void
      10  explore_unlikely_branch (gmp_randstate_ptr rands)
      11  {
      12    mp_ptr ap, refp, pp, scratch;
      13    mp_size_t an;
      14    mp_bitcnt_t bit;
      15    TMP_DECL;
      16    TMP_MARK;
      17  
      18    ap = TMP_ALLOC_LIMBS (MAX_AN);
      19    refp = TMP_ALLOC_LIMBS (MAX_AN * 2);
      20    pp = 1 + TMP_ALLOC_LIMBS (MAX_AN * 2 + 2);
      21    scratch
      22      = 1+TMP_ALLOC_LIMBS (mpn_toomN_sqr_itch (MAX_AN) + 2);
      23  
      24    for (an = MIN_AN + (MIN_AN & 1); an < MAX_AN; an+=2)
      25      {
      26        mp_size_t itch;
      27        mp_limb_t p_before, p_after, s_before, s_after;
      28  
      29        bit = an / 2 * GMP_NUMB_BITS
      30  	+ gmp_urandomm_ui (rands, an / 2 * GMP_NUMB_BITS - 1);
      31  
      32        mpn_zero (ap, an);
      33        mpn_zero (pp, an * 2);
      34        pp [an - 1] |= GMP_NUMB_HIGHBIT;
      35        pp [bit / GMP_NUMB_BITS] |= CNST_LIMB (1) << (bit % GMP_NUMB_BITS);
      36        mpn_sqrtrem (ap, NULL, pp, an);
      37        /* We need {ap, an} such that {ap + an/2, an/2} is zero and
      38  	 the result {pp, 2*an} is such that the sum
      39  	 {pp, an/2} + {pp + an/2, an/2} gives a carry. */
      40        mpn_random2 (pp-1, an * 2 + 2);
      41        p_before = pp[-1];
      42        p_after = pp[an * 2];
      43  
      44        itch = mpn_toomN_sqr_itch (an);
      45        ASSERT_ALWAYS (itch <= mpn_toomN_sqr_itch (MAX_AN));
      46        mpn_random2 (scratch-1, itch+2);
      47        s_before = scratch[-1];
      48        s_after = scratch[itch];
      49  
      50        mpn_toomN_sqr (pp, ap, an, scratch);
      51        refmpn_mul (refp, ap, an, ap, an);
      52        if (pp[-1] != p_before || pp[an * 2] != p_after
      53  	  || scratch[-1] != s_before || scratch[itch] != s_after
      54  	  || mpn_cmp (refp, pp, an * 2) != 0)
      55  	{
      56  	  printf ("ERROR with bit %lu, an = %d\n",
      57  		  (unsigned long) bit, (int) an);
      58  	  if (pp[-1] != p_before)
      59  	    {
      60  	      printf ("before pp:"); mpn_dump (pp -1, 1);
      61  	      printf ("keep:   "); mpn_dump (&p_before, 1);
      62  	    }
      63  	  if (pp[an * 2] != p_after)
      64  	    {
      65  	      printf ("after pp:"); mpn_dump (pp + an * 2, 1);
      66  	      printf ("keep:   "); mpn_dump (&p_after, 1);
      67  	    }
      68  	  if (scratch[-1] != s_before)
      69  	    {
      70  	      printf ("before scratch:"); mpn_dump (scratch-1, 1);
      71  	      printf ("keep:   "); mpn_dump (&s_before, 1);
      72  	    }
      73  	  if (scratch[itch] != s_after)
      74  	    {
      75  	      printf ("after scratch:"); mpn_dump (scratch + itch, 1);
      76  	      printf ("keep:   "); mpn_dump (&s_after, 1);
      77  	    }
      78  	  mpn_dump (ap, an);
      79  	  mpn_dump (pp, an * 2);
      80  	  mpn_dump (refp, an * 2);
      81  
      82  	  abort();
      83  	}
      84      }
      85    TMP_FREE;
      86  }