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 to exceed 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 - halfgcd.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.0 lcov report (development 28181-34a890c4ae) Lines: 169 173 97.7 %
Date: 2022-11-30 07:28:11 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #line 2 "../src/kernel/none/halfgcd.c"
       2             : /* Copyright (C) 2019  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; either version 2 of the License, or (at your option) any later
       9             : version. It is distributed in the hope that it will be useful, but WITHOUT
      10             : ANY WARRANTY WHATSOEVER.
      11             : 
      12             : Check the License for details. You should have received a copy of it, along
      13             : with the package; see the file 'COPYING'. If not, write to the Free Software
      14             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      15             : 
      16             : GEN
      17     6064921 : ZM2_mul(GEN A, GEN B)
      18             : {
      19     6064921 :   const long t = 52;
      20     6064921 :   GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
      21     6064921 :   GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
      22     6064921 :   if (lgefint(A11) < t || lgefint(B11) < t || lgefint(A22) < t || lgefint(B22) < t
      23       25069 :    || lgefint(A12) < t || lgefint(B12) < t || lgefint(A21) < t || lgefint(B21) < t)
      24             :   {
      25     6039853 :     GEN a = mulii(A11, B11), b = mulii(A12, B21);
      26     6038880 :     GEN c = mulii(A11, B12), d = mulii(A12, B22);
      27     6038872 :     GEN e = mulii(A21, B11), f = mulii(A22, B21);
      28     6039008 :     GEN g = mulii(A21, B12), h = mulii(A22, B22);
      29     6038814 :     retmkmat2(mkcol2(addii(a,b), addii(e,f)), mkcol2(addii(c,d), addii(g,h)));
      30             :   } else
      31             :   {
      32       25068 :     GEN M1 = mulii(addii(A11,A22), addii(B11,B22));
      33       25068 :     GEN M2 = mulii(addii(A21,A22), B11);
      34       25068 :     GEN M3 = mulii(A11, subii(B12,B22));
      35       25068 :     GEN M4 = mulii(A22, subii(B21,B11));
      36       25068 :     GEN M5 = mulii(addii(A11,A12), B22);
      37       25068 :     GEN M6 = mulii(subii(A21,A11), addii(B11,B12));
      38       25068 :     GEN M7 = mulii(subii(A12,A22), addii(B21,B22));
      39       25068 :     GEN T1 = addii(M1,M4), T2 = subii(M7,M5);
      40       25068 :     GEN T3 = subii(M1,M2), T4 = addii(M3,M6);
      41       25068 :     retmkmat2(mkcol2(addii(T1,T2), addii(M2,M4)),
      42             :               mkcol2(addii(M3,M5), addii(T3,T4)));
      43             :   }
      44             : }
      45             : 
      46             : static GEN
      47         162 : matid2(void)
      48             : {
      49         162 :     retmkmat2(mkcol2(gen_1,gen_0),
      50             :               mkcol2(gen_0,gen_1));
      51             : }
      52             : 
      53             : /* Return M*[q,1;1,0] */
      54             : static GEN
      55     2259145 : mulq(GEN M, GEN q)
      56             : {
      57     2259145 :   GEN u, v, res = cgetg(3, t_MAT);
      58     2259169 :   u = addii(mulii(gcoeff(M,1,1), q), gcoeff(M,1,2));
      59     2259155 :   v = addii(mulii(gcoeff(M,2,1), q), gcoeff(M,2,2));
      60     2259153 :   gel(res,1) = mkcol2(u, v);
      61     2259155 :   gel(res,2) = gel(M,1);
      62     2259155 :   return res;
      63             : }
      64             : static GEN
      65         535 : mulqab(GEN M, GEN q, GEN *ap, GEN *bp)
      66             : {
      67         535 :   GEN b = subii(*ap, mulii(*bp, q));
      68         535 :   *ap = *bp; *bp = b;
      69         535 :   return mulq(M,q);
      70             : }
      71             : 
      72             : /* Return M*[q,1;1,0]^-1 */
      73             : 
      74             : static GEN
      75        9767 : mulqi(GEN M, GEN q, GEN *ap, GEN *bp)
      76             : {
      77             :   GEN u, v, res, a;
      78        9767 :   a = addii(mulii(*ap, q), *bp);
      79        9767 :   *bp = *ap; *ap = a;
      80        9767 :   res = cgetg(3, t_MAT);
      81        9767 :   u = subii(gcoeff(M,1,1),mulii(gcoeff(M,1,2), q));
      82        9767 :   v = subii(gcoeff(M,2,1),mulii(gcoeff(M,2,2), q));
      83        9767 :   gel(res,1) = gel(M,2);
      84        9767 :   gel(res,2) = mkcol2(u,v);
      85        9767 :   return res;
      86             : }
      87             : 
      88             : static long
      89     3092634 : uexpi(GEN a)
      90     3092634 : { return expi(subiu(shifti(a,1),1)); }
      91             : 
      92             : static GEN
      93      777697 : FIXUP0(GEN M, GEN *a, GEN *b, long m)
      94             : {
      95      777697 :   long cnt=0;
      96     1014717 :   while (expi(*b) >= m)
      97             :   {
      98      237020 :     GEN r, q = dvmdii(*a, *b, &r);
      99      237020 :     *a = *b; *b = r;
     100      237020 :     M = mulq(M, q);
     101      237020 :     cnt++;
     102             :   };
     103      777697 :   if (cnt>6) pari_err_BUG("FIXUP0");
     104      777697 :   return M;
     105             : }
     106             : 
     107             : static long
     108     1904383 : signdet(GEN Q)
     109             : {
     110     1904383 :   long a = Mod4(gcoeff(Q,1,1)), b = Mod4(gcoeff(Q,1,2));
     111     1904442 :   long c = Mod4(gcoeff(Q,2,1)), d = Mod4(gcoeff(Q,2,2));
     112     1904471 :   return ((a*d-b*c)&3)==1 ? 1 : -1;
     113             : }
     114             : 
     115             : static GEN
     116     1115065 : ZM_inv2(GEN M)
     117             : {
     118     1115065 :   long e = signdet(M);
     119     1675873 :   if (e==1) return mkmat22(gcoeff(M,2,2),negi(gcoeff(M,1,2)),
     120      560745 :                           negi(gcoeff(M,2,1)),gcoeff(M,1,1));
     121      554386 :   else      return mkmat22(negi(gcoeff(M,2,2)),gcoeff(M,1,2),
     122      554394 :                            gcoeff(M,2,1),negi(gcoeff(M,1,1)));
     123             : }
     124             : 
     125             : static GEN
     126        9767 : lastq(GEN Q)
     127             : {
     128        9767 :   GEN p = gcoeff(Q,1,1), q = gcoeff(Q,1,2), s = gcoeff(Q,2,2);
     129        9767 :   if (signe(q)==0) pari_err_BUG("halfgcd");
     130        9767 :   if (signe(s)==0) return p;
     131        9767 :   if (equali1(q))  return subiu(p,1);
     132        9767 :   return divii(p, q);
     133             : }
     134             : 
     135             : static GEN
     136       18918 : mulT(GEN Q, GEN *ap, GEN *bp)
     137             : {
     138       18918 :   *ap = addii(*ap, *bp);
     139       18918 :   *bp = negi(*bp);
     140       18918 :   return mkmat2(gel(Q,1),
     141       18918 :            mkcol2(subii(gcoeff(Q,1,1), gcoeff(Q,1,2))
     142       18918 :                 , subii(gcoeff(Q,2,1), gcoeff(Q,2,2))));
     143             : }
     144             : 
     145             : static GEN
     146      789317 : FIXUP1(GEN M, GEN a, GEN b, long m, long t, GEN *ap, GEN *bp)
     147             : {
     148      789317 :   GEN Q = gel(M,1), a0 = gel(M,2), b0 = gel(M,3);
     149      789317 :   GEN q, am = remi2n(a, m), bm = remi2n(b, m);
     150      789317 :   if (signdet(Q)==-1)
     151             :   {
     152      396677 :     *ap = subii(mulii(bm, gcoeff(Q,1,2)),mulii(am, gcoeff(Q,2,2)));
     153      396677 :     *bp = subii(mulii(am, gcoeff(Q,2,1)),mulii(bm, gcoeff(Q,1,1)));
     154      396677 :     *ap = addii(*ap, shifti(addii(a0, gcoeff(Q,2,2)), m));
     155      396677 :     *bp = addii(*bp, shifti(subii(b0, gcoeff(Q,2,1)), m));
     156      396677 :     if (signe(*bp) >= 0)
     157      377149 :       return Q;
     158       19528 :     if (expi(addii(*ap,*bp)) >= m+t)
     159       18918 :       return mulT(Q, ap ,bp);
     160         610 :     q = lastq(Q);
     161         610 :     Q = mulqi(Q, q, ap, bp);
     162         610 :     if (cmpiu(q, 2)>=0)
     163         535 :       return mulqab(Q, subiu(q,1), ap, bp);
     164             :     else
     165          75 :       return mulqi(Q, lastq(Q), ap, bp);
     166             :   }
     167             :   else
     168             :   {
     169      392640 :     *ap = subii(mulii(am, gcoeff(Q,2,2)),mulii(bm, gcoeff(Q,1,2)));
     170      392640 :     *bp = subii(mulii(bm, gcoeff(Q,1,1)),mulii(am, gcoeff(Q,2,1)));
     171      392640 :     *ap = addii(*ap, shifti(subii(a0, gcoeff(Q,2,2)), m));
     172      392640 :     *bp = addii(*bp, shifti(addii(b0, gcoeff(Q,2,1)), m));
     173      392640 :     if (expi(*ap) >= m+t)
     174      383558 :       return FIXUP0(Q, ap, bp, m+t);
     175             :     else
     176        9082 :       return signe(gcoeff(Q,1,2))==0? Q: mulqi(Q, lastq(Q), ap, bp);
     177             :   }
     178             : }
     179             : 
     180             : static long
     181     2698512 : magic_threshold(GEN a)
     182     2698512 : { return (3+uexpi(a))>>1; }
     183             : 
     184             : static GEN
     185     1511470 : HGCD_basecase(GEN a, GEN b)
     186             : {
     187     1511470 :   pari_sp av=avma;
     188     1511470 :   long m = magic_threshold(a);
     189             :   GEN u,u1,v,v1;
     190     1511432 :   u1 = v = gen_0;
     191     1511432 :   u = v1 = gen_1;
     192    37566280 :   while (expi(b) >= m)
     193             :   {
     194    36055246 :     GEN r, q = dvmdii(a,b, &r);
     195    36054586 :     a = b; b = r; swap(u,u1); swap(v,v1);
     196    36054586 :     u = addii(mulii(u1, q), u);
     197    36054735 :     v = addii(mulii(v1, q), v);
     198    36054833 :    if (gc_needed(av,2))
     199             :     {
     200           0 :       if (DEBUGMEM>1) pari_warn(warnmem,"halfgcd (d = %ld)",lgefint(b));
     201           0 :       gerepileall(av,6, &a,&b,&u1,&v1,&u,&v);
     202             :     }
     203             :   }
     204     1511133 :   return gerepilecopy(av, mkvec3(mkmat22(u,u1,v,v1), a, b));
     205             : }
     206             : 
     207             : static GEN HGCD(GEN x, GEN y);
     208             : 
     209             : /*
     210             : Based on
     211             : Klaus Thull and Chee K. Yap,
     212             : A unified approach to HGCD algorithms for polynomials andintegers,
     213             : 1990, Manuscript.
     214             : URL: http://cs.nyu.edu/cs/faculty/yap/papers.
     215             : */
     216             : 
     217             : static GEN
     218      395358 : HGCD_split(GEN a, GEN b)
     219             : {
     220      395358 :   pari_sp av = avma;
     221      395358 :   long m = magic_threshold(a), t, l, k, tp;
     222             :   GEN a0, b0, ap, bp, c, d, c0, d0, cp, dp, R, S, T, q, r;
     223      395358 :   if (signe(b) < 0  || cmpii(a,b)<0) pari_err_BUG("HGCD_split");
     224      395358 :   if (expi(b) < m)
     225         162 :     return gerepilecopy(av, mkvec3(matid2(), a, b));
     226      395196 :   a0 = addiu(shifti(a, -m), 1);
     227      395196 :   if (cmpiu(a0,7) <= 0)
     228             :   {
     229           0 :     R = FIXUP0(matid2(), &a, &b, m);
     230           0 :     return gerepilecopy(av, mkvec3(R, a, b));
     231             :   }
     232      395196 :   b0 = shifti(b,-m);
     233      395196 :   t = magic_threshold(a0);
     234      395196 :   R = FIXUP1(HGCD(a0,b0),a, b, m, t, &ap, &bp);
     235      395196 :   if (expi(bp) < m)
     236        1057 :     return gerepilecopy(av, mkvec3(R, ap, bp));
     237      394139 :   q = dvmdii(ap, bp, &r);
     238      394139 :   c = bp; d = r;
     239      394139 :   if (cmpiu(shifti(c,-m),6) <= 0)
     240             :   {
     241          18 :     R = FIXUP0(mulq(R, q), &c, &d, m);
     242          18 :     return gerepilecopy(av, mkvec3(R, c, d));
     243             :   }
     244      394121 :   l = uexpi(c);
     245      394121 :   k = 2*m-l-1; if (k<0) pari_err_BUG("halfgcd");
     246      394121 :   c0 = addiu(shifti(c, -k), 1); if (cmpiu(c0,8)<0) pari_err_BUG("halfgcd");
     247      394121 :   d0 = shifti(d, -k);
     248      394121 :   tp = magic_threshold(c0);
     249      394121 :   S = FIXUP1(HGCD(c0,d0), c, d, k, tp, &cp, &dp);
     250      394121 :   if (!(expi(cp)>=m+1 && m+1 > expi(dp))) pari_err_BUG("halfgcd");
     251      394121 :   T = FIXUP0(ZM2_mul(mulq(R, q), S), &cp, &dp, m);
     252      394121 :   return gerepilecopy(av, mkvec3(T, cp, dp));
     253             : }
     254             : 
     255             : static GEN
     256     1906830 : HGCD(GEN x, GEN y)
     257             : {
     258     1906830 :   if (lgefint(y)-2 < HALFGCD_LIMIT)
     259     1511472 :     return HGCD_basecase(x, y);
     260             :   else
     261      395358 :     return HGCD_split(x, y);
     262             : }
     263             : 
     264             : static GEN
     265     1119125 : HGCD0(GEN x, GEN y)
     266             : {
     267     1119125 :   if (signe(y) >= 0 && cmpii(x, y) >= 0)
     268     1117452 :     return HGCD(x, y);
     269        1692 :   if (cmpii(x, y) < 0)
     270             :   {
     271        1359 :     GEN M = HGCD0(y, x), Q = gel(M,1);
     272        1359 :     return mkvec3(mkmat22(gcoeff(Q,2,1),gcoeff(Q,2,2),gcoeff(Q,1,1),gcoeff(Q,1,2)),
     273        1359 :         gel(M,2),gel(M,3));
     274             :   } /* Now y <= x*/
     275         333 :   if (signe(x) <= 0)
     276             :   { /* y <= x <=0 */
     277          60 :     GEN M = HGCD(negi(y), negi(x)), Q = gel(M,1);
     278          60 :     return mkvec3(mkmat22(negi(gcoeff(Q,2,1)),negi(gcoeff(Q,2,2)),
     279          60 :                           negi(gcoeff(Q,1,1)),negi(gcoeff(Q,1,2))),
     280          60 :         gel(M,2),gel(M,3));
     281             :   }
     282             :   else /* y <= 0 <=x */
     283             :   {
     284         273 :     GEN M = HGCD0(x, negi(y)), Q = gel(M,1);
     285         273 :     return mkvec3(mkmat22(gcoeff(Q,1,1),gcoeff(Q,1,2),negi(gcoeff(Q,2,1)),negi(gcoeff(Q,2,2))),
     286         273 :         gel(M,2),gel(M,3));
     287             :   }
     288             : }
     289             : 
     290             : GEN
     291     1114912 : halfgcdii(GEN A, GEN B)
     292             : {
     293     1114912 :   pari_sp av = avma;
     294     1114912 :   GEN M, Q, a, b, m = absi_shallow(abscmpii(A, B)>0 ? A: B);
     295     1114947 :   M = HGCD0(A,B); Q = gel(M,1); a = gel(M,2); b = gel(M,3);
     296     2742306 :   while (signe(b) && cmpii(sqri(b), m) >= 0)
     297             :   {
     298     1627352 :     GEN r, q = dvmdii(a, b, &r);
     299     1627317 :     a = b; b = r;
     300     1627317 :     Q = mulq(Q, q);
     301             :   }
     302     1114867 :   return gerepilecopy(av, mkvec2(ZM_inv2(Q),mkcol2(a,b)));
     303             : }

Generated by: LCOV version 1.14