Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - kernel/none - ratlift.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19619-c9e92b8) Lines: 87 94 92.6 %
Date: 2016-09-29 05:54:10 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/ratlift.c"
       2             : /* Copyright (C) 2003  The PARI group.
       3             : 
       4             : This file is part of the PARI/GP package.
       5             : 
       6             : PARI/GP is free software; you can redistribute it and/or modify it under the
       7             : terms of the GNU General Public License as published by the Free Software
       8             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /*==========================================================
      16             :  * Fp_ratlift(GEN x, GEN m, GEN *a, GEN *b, GEN amax, GEN bmax)
      17             :  *==========================================================
      18             :  * Reconstruct rational number from its residue x mod m
      19             :  *    Given t_INT x, m, amax>=0, bmax>0 such that
      20             :  *         0 <= x < m;  2*amax*bmax < m
      21             :  *    attempts to find t_INT a, b such that
      22             :  *         (1) a = b*x (mod m)
      23             :  *         (2) |a| <= amax, 0 < b <= bmax
      24             :  *         (3) gcd(m, b) = gcd(a, b)
      25             :  *    If unsuccessful, it will return 0 and leave a,b unchanged  (and
      26             :  *    caller may deduce no such a,b exist).  If successful, sets a,b
      27             :  *    and returns 1.  If there exist a,b satisfying (1), (2), and
      28             :  *         (3') gcd(m, b) = 1
      29             :  *    then they are uniquely determined subject to (1),(2) and
      30             :  *         (3'') gcd(a, b) = 1,
      31             :  *    and will be returned by the routine.  (The caller may wish to
      32             :  *    check gcd(a,b)==1, either directly or based on known prime
      33             :  *    divisors of m, depending on the application.)
      34             :  * Reference:
      35             :  @article {MR97c:11116,
      36             :      AUTHOR = {Collins, George E. and Encarnaci{\'o}n, Mark J.},
      37             :       TITLE = {Efficient rational number reconstruction},
      38             :     JOURNAL = {J. Symbolic Comput.},
      39             :      VOLUME = {20},
      40             :        YEAR = {1995},
      41             :      NUMBER = {3},
      42             :       PAGES = {287--297},
      43             :  }
      44             :  * Preprint available from:
      45             :  * ftp://ftp.risc.uni-linz.ac.at/pub/techreports/1994/94-64.ps.gz */
      46             : static ulong
      47     3728961 : get_vmax(GEN r, long lb, long lbb)
      48             : {
      49     3728961 :   long lr = lb - lgefint(r);
      50             :   ulong vmax;
      51     3728961 :   if (lr > 1)        /* still more than a word's worth to go */
      52      868332 :     vmax = ULONG_MAX;        /* (cannot in fact happen) */
      53             :   else
      54             :   { /* take difference of bit lengths */
      55     2860629 :     long lbr = bfffo(*int_MSW(r));
      56     2860629 :     lr = lr*BITS_IN_LONG - lbb + lbr;
      57     2860629 :     if ((ulong)lr > BITS_IN_LONG)
      58      439643 :       vmax = ULONG_MAX;
      59     2420986 :     else if (lr == 0)
      60       53356 :       vmax = 1UL;
      61             :     else
      62     2367630 :       vmax = 1UL << (lr-1); /* pessimistic but faster than a division */
      63             :   }
      64     3728961 :   return vmax;
      65             : }
      66             : 
      67             : /* Assume x,m,amax >= 0,bmax > 0 are t_INTs, 0 <= x < m, 2 amax * bmax < m */
      68             : int
      69     2323056 : Fp_ratlift(GEN x, GEN m, GEN amax, GEN bmax, GEN *a, GEN *b)
      70             : {
      71             :   GEN d, d1, v, v1, q, r;
      72     2323056 :   pari_sp av = avma, av1;
      73             :   long lb, lbb, s, s0;
      74             :   ulong vmax;
      75             :   ulong xu, xu1, xv, xv1; /* Lehmer stage recurrence matrix */
      76             :   int lhmres;             /* Lehmer stage return value */
      77             : 
      78             :   /* special cases x=0 and/or amax=0 */
      79     2323056 :   if (!signe(x)) { *a = gen_0; *b = gen_1; return 1; }
      80     1636887 :   if (!signe(amax)) return 0;
      81             :   /* assert: m > x > 0, amax > 0 */
      82             : 
      83             :   /* check whether a=x, b=1 is a solution */
      84     1636887 :   if (cmpii(x,amax) <= 0) { *a = icopy(x); *b = gen_1; return 1; }
      85             : 
      86             :   /* There is no special case for single-word numbers since this is
      87             :    * mainly meant to be used with large moduli. */
      88     1601331 :   (void)new_chunk(lgefint(bmax) + lgefint(amax)); /* room for a,b */
      89     1601331 :   d = m; d1 = x;
      90     1601331 :   v = gen_0; v1 = gen_1;
      91             :   /* assert d1 > amax, v1 <= bmax here */
      92     1601331 :   lb = lgefint(bmax);
      93     1601331 :   lbb = bfffo(*int_MSW(bmax));
      94     1601331 :   s = 1;
      95     1601331 :   av1 = avma;
      96             : 
      97             :   /* General case: Euclidean division chain starting with m div x, and
      98             :    * with bounds on the sequence of convergents' denoms v_j.
      99             :    * Just to be different from what invmod and bezout are doing, we work
     100             :    * here with the all-nonnegative matrices [u,u1;v,v1]=prod_j([0,1;1,q_j]).
     101             :    * Loop invariants:
     102             :    * (a) (sign)*[-v,v1]*x = [d,d1] (mod m)  (componentwise)
     103             :    * (sign initially +1, changes with each Euclidean step)
     104             :    * so [a,b] will be obtained in the form [-+d,v] or [+-d1,v1];
     105             :    * this congruence is a consequence of
     106             :    *
     107             :    * (b) [x,m]~ = [u,u1;v,v1]*[d1,d]~,
     108             :    * where u,u1 is the usual numerator sequence starting with 1,0
     109             :    * instead of 0,1  (just multiply the eqn on the left by the inverse
     110             :    * matrix, which is det*[v1,-u1;-v,u], where "det" is the same as the
     111             :    * "(sign)" in (a)).  From m = v*d1 + v1*d and
     112             :    *
     113             :    * (c) d > d1 >= 0, 0 <= v < v1,
     114             :    * we have d >= m/(2*v1), so while v1 remains smaller than m/(2*amax),
     115             :    * the pair [-(sign)*d,v] satisfies (1) but violates (2) (d > amax).
     116             :    * Conversely, v1 > bmax indicates that no further solutions will be
     117             :    * forthcoming;  [-(sign)*d,v] will be the last, and first, candidate.
     118             :    * Thus there's at most one point in the chain division where a solution
     119             :    * can live:  v < bmax, v1 >= m/(2*amax) > bmax,  and this is acceptable
     120             :    * iff in fact d <= amax  (e.g. m=221, x=34 or 35, amax=bmax=10 fail on
     121             :    * this count while x=32,33,36,37 succeed).  However, a division may leave
     122             :    * a zero residue before we ever reach this point  (consider m=210, x=35,
     123             :    * amax=bmax=10),  and our caller may find that gcd(d,v) > 1  (Examples:
     124             :    * keep m=210 and consider any of x=29,31,32,33,34,36,37,38,39,40,41).
     125             :    * Furthermore, at the start of the loop body we have in fact
     126             :    *
     127             :    * (c') 0 <= v < v1 <= bmax, d > d1 > amax >= 0,
     128             :    * (and are never done already).
     129             :    *
     130             :    * Main loop is similar to those of invmod() and bezout(), except for
     131             :    * having to determine appropriate vmax bounds, and checking termination
     132             :    * conditions.  The signe(d1) condition is only for paranoia */
     133     5240547 :   while (lgefint(d) > 3 && signe(d1))
     134             :   {
     135             :     /* determine vmax for lgcdii so as to ensure v won't overshoot.
     136             :      * If v+v1 > bmax, the next step would take v1 beyond the limit, so
     137             :      * since [+-d1,v1] is not a solution, we give up.  Otherwise if v+v1
     138             :      * is way shorter than bmax, use vmax=MAXULUNG.  Otherwise, set vmax
     139             :      * to a crude lower approximation of bmax/(v+v1), or to 1, which will
     140             :      * allow the inner loop to do one step */
     141     2915157 :     r = addii(v,v1);
     142     2915157 :     if (cmpii(r,bmax) > 0) { avma = av; return 0; } /* done, not found */
     143     2914590 :     vmax = get_vmax(r, lb, lbb);
     144             :     /* do a Lehmer-Jebelean round */
     145     2914590 :     lhmres = lgcdii((ulong *)d, (ulong *)d1, &xu, &xu1, &xv, &xv1, vmax);
     146     2914590 :     if (lhmres) /* check progress */
     147             :     { /* apply matrix */
     148     2656913 :       if (lhmres == 1 || lhmres == -1)
     149             :       {
     150      813576 :         s = -s;
     151     1627152 :         if (xv1 == 1)
     152             :         { /* re-use v+v1 computed above */
     153      422052 :           v = v1; v1 = r;
     154      422052 :           r = subii(d,d1); d = d1; d1 = r;
     155             :         }
     156             :         else
     157             :         {
     158      391524 :           r = subii(d, mului(xv1,d1)); d = d1; d1 = r;
     159      391524 :           r = addii(v, mului(xv1,v1)); v = v1; v1 = r;
     160             :         }
     161             :       }
     162             :       else
     163             :       {
     164     1843337 :         r  = subii(muliu(d,xu),  muliu(d1,xv));
     165     1843337 :         d1 = subii(muliu(d,xu1), muliu(d1,xv1)); d = r;
     166     1843337 :         r  = addii(muliu(v,xu),  muliu(v1,xv));
     167     1843337 :         v1 = addii(muliu(v,xu1), muliu(v1,xv1)); v = r;
     168     1843337 :         if (lhmres&1) { togglesign(d); s = -s; } else togglesign(d1);
     169             :       }
     170             :       /* check whether we're done.  Assert v <= bmax here.  Examine v1:
     171             :        * if v1 > bmax, check d and return 0 or 1 depending on the outcome;
     172             :        * if v1 <= bmax, check d1 and return 1 if d1 <= amax, otherwise proceed*/
     173     2656913 :       if (cmpii(v1,bmax) > 0)
     174             :       {
     175       74685 :         avma = av;
     176       74685 :         if (cmpii(d,amax) > 0) return 0; /* done, not found */
     177             :         /* done, found */
     178       59451 :         *a = icopy(d); setsigne(*a,-s);
     179       59451 :         *b = icopy(v); return 1;
     180             :       }
     181     2582228 :       if (cmpii(d1,amax) <= 0)
     182             :       { /* done, found */
     183      672637 :         avma = av;
     184      672637 :         if (signe(d1)) { *a = icopy(d1); setsigne(*a,s); } else *a = gen_0;
     185      672637 :         *b = icopy(v1); return 1;
     186             :       }
     187             :     } /* lhmres != 0 */
     188             : 
     189     2167268 :     if (lhmres <= 0 && signe(d1))
     190             :     {
     191      257752 :       q = dvmdii(d,d1,&r);
     192      257752 :       d = d1; d1 = r;
     193      257752 :       r = addii(v, mulii(q,v1));
     194      257752 :       v = v1; v1 = r;
     195      257752 :       s = -s;
     196             :       /* check whether we are done now.  Since we weren't before the div, it
     197             :        * suffices to examine v1 and d1 -- the new d (former d1) cannot cut it */
     198      257752 :       if (cmpii(v1,bmax) > 0) { avma = av; return 0; } /* done, not found */
     199      257750 :       if (cmpii(d1,amax) <= 0) /* done, found */
     200             :       {
     201      129381 :         avma = av;
     202      129381 :         if (signe(d1)) { *a = icopy(d1); setsigne(*a,s); } else *a = gen_0;
     203      129381 :         *b = icopy(v1); return 1;
     204             :       }
     205             :     }
     206             : 
     207     2037885 :     if (gc_needed(av,1))
     208             :     {
     209           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"ratlift");
     210           0 :       gerepileall(av1, 4, &d, &d1, &v, &v1);
     211             :     }
     212             :   } /* end while */
     213             : 
     214             :   /* Postprocessing - final sprint.  Since we usually underestimate vmax,
     215             :    * this function needs a loop here instead of a simple conditional.
     216             :    * Note we can only get here when amax fits into one word  (which will
     217             :    * typically not be the case!).  The condition is bogus -- d1 is never
     218             :    * zero at the start of the loop.  There will be at most a few iterations,
     219             :    * so we don't bother collecting garbage */
     220     1542263 :   while (signe(d1))
     221             :   {
     222             :     /* Assertions: lgefint(d)==lgefint(d1)==3.
     223             :      * Moreover, we aren't done already, or we would have returned by now.
     224             :      * Recompute vmax */
     225      818204 :     r = addii(v,v1);
     226      818204 :     if (cmpii(r,bmax) > 0) { avma = av; return 0; } /* done, not found */
     227      814371 :     vmax = get_vmax(r, lb, lbb);
     228             :     /* single-word "Lehmer", discarding the gcd or whatever it returns */
     229      814371 :     (void)rgcduu((ulong)*int_MSW(d), (ulong)*int_MSW(d1), vmax, &xu, &xu1, &xv, &xv1, &s0);
     230      814371 :     if (xv1 == 1) /* avoid multiplications */
     231             :     { /* re-use r = v+v1 computed above */
     232           0 :       v = v1; v1 = r;
     233           0 :       r = subii(d,d1); d = d1; d1 = r;
     234           0 :       s = -s;
     235             :     }
     236      814371 :     else if (xu == 0) /* and xv==1, xu1==1, xv1 > 1 */
     237             :     {
     238       19743 :       r = subii(d, mului(xv1,d1)); d = d1; d1 = r;
     239       19743 :       r = addii(v, mului(xv1,v1)); v = v1; v1 = r;
     240       19743 :       s = -s;
     241             :     }
     242             :     else
     243             :     {
     244      794628 :       r  = subii(muliu(d,xu),  muliu(d1,xv));
     245      794628 :       d1 = subii(muliu(d,xu1), muliu(d1,xv1)); d = r;
     246      794628 :       r  = addii(muliu(v,xu),  muliu(v1,xv));
     247      794628 :       v1 = addii(muliu(v,xu1), muliu(v1,xv1)); v = r;
     248      794628 :       if (s0 < 0) { togglesign(d); s = -s; } else togglesign(d1);
     249             :     }
     250             :     /* check whether we're done, as above.  Assert v <= bmax.
     251             :      * if v1 > bmax, check d and return 0 or 1 depending on the outcome;
     252             :      * if v1 <= bmax, check d1 and return 1 if d1 <= amax, otherwise proceed.
     253             :      */
     254      814371 :     if (cmpii(v1,bmax) > 0)
     255             :     {
     256      593138 :       avma = av;
     257      593138 :       if (cmpii(d,amax) > 0) return 0; /* done, not found */
     258             :       /* done, found */
     259      586057 :       *a = icopy(d); setsigne(*a,-s);
     260      586057 :       *b = icopy(v); return 1;
     261             :     }
     262      221233 :     if (cmpii(d1,amax) <= 0)
     263             :     { /* done, found */
     264      127088 :       avma = av;
     265      127088 :       if (signe(d1)) { *a = icopy(d1); setsigne(*a,s); } else *a = gen_0;
     266      127088 :       *b = icopy(v1); return 1;
     267             :     }
     268             :   } /* while */
     269             : 
     270             :   /* We have run into d1 == 0 before returning. This cannot happen */
     271           0 :   pari_err_BUG("ratlift failed to catch d1 == 0");
     272           0 :   return 0; /* NOTREACHED */
     273             : }

Generated by: LCOV version 1.11