Skip to content

Commit

Permalink
cppcheck fixes for libsrc/*.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCremona committed Apr 22, 2024
1 parent 3970c75 commit 38c03e5
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 100 deletions.
26 changes: 14 additions & 12 deletions libsrc/GetOpt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef __GNUG__
#pragma implementation
#endif
/* AIX requires the alloca decl to be the first thing in the file. */
#ifdef __GNUC__
#define alloca __builtin_alloca
#elif defined(sparc)
#include <alloca.h>
extern "C" void *__builtin_alloca(...);
#elif defined(_AIX)
#pragma alloca
#else
char *alloca ();
#endif
// /* AIX requires the alloca decl to be the first thing in the file. */
// #ifdef __GNUC__
// #define alloca __builtin_alloca
// #elif defined(sparc)
// #include <alloca.h>
// extern "C" void *__builtin_alloca(...);
// #elif defined(_AIX)
// #pragma alloca
// #else
// char *alloca ();
// #endif
#include <eclib/GetOpt.h>

char* GetOpt::nextchar = 0;
Expand Down Expand Up @@ -62,7 +62,8 @@ GetOpt::exchange (char **argv) const
{
int nonopts_size
= (last_nonopt - first_nonopt) * sizeof (char *);
char **temp = (char **) alloca (nonopts_size);
char **temp = new char*[nonopts_size];
// char **temp = (char **) alloca (nonopts_size);

/* Interchange the two blocks of data in argv. */

Expand All @@ -76,6 +77,7 @@ GetOpt::exchange (char **argv) const

first_nonopt += (optind - last_nonopt);
last_nonopt = optind;
delete[] temp;
}

/* Scan elements of ARGV (whose length is ARGC) for option characters
Expand Down
4 changes: 2 additions & 2 deletions libsrc/eclib/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ int check_transform(const bigint& a, const bigint& b, const bigint& c,
const bigint& xd, const bigint& xe);

void xshift(const bigint& alpha,
bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
const bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
unimod& m);

void zshift(const bigint& gamma,
bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
bigint& a, bigint& b, bigint& c, bigint& d, const bigint& e,
unimod& m);

void m_invert(bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
Expand Down
2 changes: 1 addition & 1 deletion libsrc/eclib/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void makeprimitive(vec& v);
void elim(const vec& a, vec& b, long pos);
void elim1(const vec& a, vec& b, long pos);
void elim2(const vec& a, vec& b, long pos, scalar lastpivot);
vec reverse(vec& order);
vec reverse(const vec& order);
vec express(const vec& v, const vec& v1, const vec& v2);
int lift(const vec& v, scalar pr, vec& ans); //lifts a mod-p vector to a rational
//and scales to a primitive vec in Z. Returns success flag
Expand Down
7 changes: 3 additions & 4 deletions libsrc/ffmod.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ FqPoly ffmodq::f2;
ffmodq::ffmodq(const curvemodq& EE)
{
E=EE; Fq=get_field(EE);
// cout<<"In ffmodq constructor"<<endl;
init_f1f2();
}

Expand All @@ -48,10 +47,10 @@ void ffmodq::init_f1f2(void)
NewGF(Fq,a4); NewGF(Fq,a6);
E.get_ai(a1,a2,a3,a4,a6);
// set f1, f2:
NewFqPoly(Fq,X);
NewFqPoly(Fq,X);
FqPolyAssignX(X);
f1 = X*(X*(X+a2)+a4)+a6;
f2 = a1*X+a3;
f2 = a1*X+a3;
}

int ffmodq::operator==(const ffmodq& b) const
Expand Down Expand Up @@ -101,7 +100,7 @@ gf_element evaluate(const FqPoly& f, const gf_element& value)

NewGF(GetField(f),result);
GFSetZ(result,0);

if (d < 0) return result;

result = PolyCoeff(f,d);
Expand Down
87 changes: 48 additions & 39 deletions libsrc/sub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,21 @@ mat expressvectors(const mat& m, const subspace& s)

//This one is used a LOT
mat restrict_mat(const mat& m, const subspace& s, int cr)
{ long i,j,k,d = dim(s), n=m.nro;
{ long d = dim(s), n=m.nro;
if(d==n) return m; // trivial special case, s is whole space
scalar dd = s.denom;
mat ans(d,d);
const mat& sb = s.basis;
scalar *ap, *a=m.entries, *b=sb.entries, *bp, *c=ans.entries, *cp, *pv=s.pivots.entries;
for(i=0; i<d; i++)
scalar *a=m.entries, *b=sb.entries, *c=ans.entries, *cp, *pv=s.pivots.entries;
for(int i=0; i<d; i++)
{
bp=b; k=n; ap=a+n*(pv[i]-1);
scalar *ap=a+n*(pv[i]-1);
scalar *bp=b;
int k=n;
while(k--)
{
cp=c; j=d;
cp=c;
int j=d;
while(j--)
{
*cp++ += *ap * *bp++;
Expand All @@ -113,24 +116,26 @@ mat restrict_mat(const mat& m, const subspace& s, int cr)
}
return ans;
}

subspace kernel(const mat& m1, int method)
{
long rank, nullity, n, r, i, j;
long rank, nullity;
scalar d;
vec pcols,npcols;
mat m = echelon(m1,pcols,npcols, rank, nullity, d, method);
int dim = m.ncols();
mat basis(dim,nullity);
for (n=1; n<=nullity; n++) basis.set(npcols[n],n,d);
for (r=1; r<=rank; r++)
{ i = pcols[r];
for (j=1; j<=nullity; j++) basis.set(i,j, -m(r,npcols[j]));
mat basis(m.ncols(),nullity);
for (int n=1; n<=nullity; n++)
basis.set(npcols[n],n,d);
for (int r=1; r<=rank; r++)
{
int i = pcols[r];
for (int j=1; j<=nullity; j++)
basis.set(i,j, -m(r,npcols[j]));
}
subspace ans(basis, npcols, d);
return ans;
}

subspace image(const mat& m, int method)
{
vec p,np;
Expand Down Expand Up @@ -166,18 +171,20 @@ subspace pcombine(const subspace& s1, const subspace& s2, scalar pr)
}

mat prestrict(const mat& m, const subspace& s, scalar pr, int cr)
{ int i,j,k,d = dim(s), n=m.nro;
{ int d = dim(s), n=m.nro;
if(d==n) return m; // trivial special case, s is whole space
scalar dd = s.denom; // will be 1 if s is a mod-p subspace
mat ans(d,d);
const mat& sb = s.basis;
scalar *ap, *a=m.entries, *b=sb.entries, *bp, *c=ans.entries, *cp, *pv=s.pivots.entries;
for(i=0; i<d; i++)
scalar *a=m.entries, *b=sb.entries, *c=ans.entries, *pv=s.pivots.entries;
for(int i=0; i<d; i++)
{
bp=b; k=n; ap=a+n*(pv[i]-1);
scalar *ap=a+n*(pv[i]-1), *bp=b, *cp;
int j, k=n;
while(k--)
{
cp=c; j=d;
cp=c;
j=d;
while(j--)
{
*cp += xmodmul(*ap , *bp++, pr);
Expand All @@ -186,7 +193,8 @@ mat prestrict(const mat& m, const subspace& s, scalar pr, int cr)
}
ap++;
}
cp=c; j=d;
cp=c;
j=d;
while(j--)
{
*cp = mod(*cp,pr);
Expand All @@ -198,25 +206,27 @@ mat prestrict(const mat& m, const subspace& s, scalar pr, int cr)
const mat& left = dd*matmulmodp(m,sb,pr);
const mat& right = matmulmodp(sb,ans,pr);
int check = (left==right);
if (!check)
if (!check)
{
cout<<"Error in prestrict: subspace not invariant!\n";
}
}
return ans;
}

subspace oldpkernel(const mat& m1, scalar pr) // using full echmodp
{
long rank, nullity, n, r, i, j;
long rank, nullity;
vec pcols,npcols;
mat m = echmodp(m1,pcols,npcols, rank, nullity, pr);
int dim = m.ncols();
mat basis(dim,nullity);
for (n=1; n<=nullity; n++) basis.set(npcols[n],n,1);
for (r=1; r<=rank; r++)
{ i = pcols[r];
for (j=1; j<=nullity; j++) basis.set(i,j, mod(-m(r,npcols[j]),pr));
mat basis(m.ncols(),nullity);
for (int n=1; n<=nullity; n++)
basis.set(npcols[n],n,1);
for (int r=1; r<=rank; r++)
{
int i = pcols[r];
for (int j=1; j<=nullity; j++)
basis.set(i,j, mod(-m(r,npcols[j]),pr));
}
subspace ans(basis, npcols, 1);
return ans;
Expand All @@ -225,21 +235,20 @@ subspace oldpkernel(const mat& m1, scalar pr) // using full echmodp
// using echmodp_uptri, with no back-substitution
subspace pkernel(const mat& m1, scalar pr)
{
long rank, nullity, i, j, jj, t, tt;
long rank, nullity;
vec pcols,npcols;
mat m = echmodp_uptri(m1,pcols,npcols, rank, nullity, pr);
int dim = m.ncols();
mat basis(dim,nullity);
for(j=nullity; j>0; j--)
mat basis(m.ncols(),nullity);
for(int j=nullity; j>0; j--)
{
jj = npcols[j];
int jj = npcols[j];
basis(jj,j) = 1;
for(i=rank; i>0; i--)
for(int i=rank; i>0; i--)
{
scalar temp = -m(i,jj);
for(t=rank; t>i; t--)
for(int t=rank; t>i; t--)
{
tt=pcols[t];
int tt=pcols[t];
temp -= xmodmul(m(i,tt),basis(tt,j),pr);
temp = xmod(temp,pr);
}
Expand All @@ -249,7 +258,7 @@ subspace pkernel(const mat& m1, scalar pr)
subspace ans(basis, npcols, 1);
return ans;
}

subspace pimage(const mat& m, scalar pr)
{
vec p,np;
Expand All @@ -258,7 +267,7 @@ subspace pimage(const mat& m, scalar pr)
subspace ans(b,p,1);
return ans;
}

subspace peigenspace(const mat& m1, scalar lambda, scalar pr)
{
const mat& m = addscalar(m1,-lambda);
Expand Down
32 changes: 18 additions & 14 deletions libsrc/svec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@
// Definitions of member operators and functions:

svec::svec(const vec& v)
:d(dim(v))
{
d = dim(v); scalar vi;
for(int i = 1; i <= d; i++ )
if((vi=v[i])) // assignment not equality!
entries[i]=vi;
{
scalar vi = v[i];
if(vi) // assignment not equality!
entries[i]=vi;
}
}

svec::svec (int dim, scalar* a) // conversion constructor
:d(dim)
{
int i=0; scalar* ai=a;
for( ; i<d; i++, ai++)
if(*ai)
entries[i]=*ai;
scalar* ai=a;
for(int i=0 ; i<d; i++)
if(*ai++)
entries[i+1]=*ai;
}

vec svec::as_vec( ) const
Expand Down Expand Up @@ -269,11 +272,10 @@ svec& svec::operator*=(scalar scal)

void svec::reduce_mod_p(const scalar& p)
{
scalar a;
auto vi = entries.begin();
while( vi != entries.end() )
{
a = mod(vi->second,p);
scalar a = mod(vi->second,p);
if(a)
{
(vi->second)=a;
Expand Down Expand Up @@ -480,11 +482,13 @@ svec& svec::operator/=(scalar scal)
int eqmodp(const svec& v1, const svec& v2, const scalar& p)
{
if(v1.d!=v2.d) return 0;
for( const auto& vi : v1.entries)
if(xmod((vi.second)-(v2.elem(vi.first)),p)!=0) return 0;
for( const auto& vi : v2.entries)
if(xmod((vi.second)-(v1.elem(vi.first)),p)!=0) return 0;
return true;
if (std::any_of(v1.entries.begin(), v1.entries.end(),
[v2,p] (const pair<int,scalar>& vi) {return xmod((vi.second)-(v2.elem(vi.first)),p)!=0;}))
return 0;
if (std::any_of(v2.entries.begin(), v2.entries.end(),
[v1,p] (const pair<int,scalar>& vi) {return xmod((vi.second)-(v1.elem(vi.first)),p)!=0;}))
return 0;
return 1;
}

int operator==(const svec& v1, const vec& v2)
Expand Down
9 changes: 5 additions & 4 deletions libsrc/timer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ int timer::count( string name ) {
* Return total time of a given timer.
*/
double timer::total( string name ) {
double total = 0;
for ( const auto& t : times_[name])
total += t;
return total;
return std::accumulate(times_[name].begin(), times_[name].end(), 0);
// double total = 0;
// for ( const auto& t : times_[name])
// total += t;
// return total;
}

/**
Expand Down
22 changes: 10 additions & 12 deletions libsrc/transform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,24 @@ void apply_transform(bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
}

void xshift(const bigint& alpha,
bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
const bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
unimod& m)
{
e = e+alpha*(d+alpha*( c+alpha*( b+ alpha*a)));
d = d+alpha*(2*c+alpha*(3*b+4*alpha*a));
c = c+alpha*(3*b+6*alpha*a);
b = b+4*alpha*a;
//a = a;
e += alpha*(d+alpha*( c+alpha*( b+ alpha*a)));
d += alpha*(2*c+alpha*(3*b+4*alpha*a));
c += alpha*(3*b+6*alpha*a);
b += 4*alpha*a;
m.x_shift(alpha);
}

void zshift(const bigint& gamma,
bigint& a, bigint& b, bigint& c, bigint& d, bigint& e,
bigint& a, bigint& b, bigint& c, bigint& d, const bigint& e,
unimod& m)
{
a = a+gamma*(b+gamma*( c+gamma*( d+ gamma*e)));
b = b+gamma*(2*c+gamma*(3*d+4*gamma*e));
c = c+gamma*(3*d+6*gamma*e);
d = d+4*gamma*e;
//e = e;
a += gamma*(b+gamma*( c+gamma*( d+ gamma*e)));
b += gamma*(2*c+gamma*(3*d+4*gamma*e));
c += gamma*(3*d+6*gamma*e);
d += 4*gamma*e;
m.y_shift(gamma);
}

Expand Down
Loading

0 comments on commit 38c03e5

Please sign in to comment.