From 5d17adaa3499c6eaf279de4a9db657cf0004e938 Mon Sep 17 00:00:00 2001 From: Roland Schulz Date: Thu, 5 Jul 2018 19:05:20 -0700 Subject: [PATCH] 3/3 of old-style casting Also remove GMX_CXX11_COMPILATION and gmx_qsort Change-Id: I3cfec7dfdb47da89d3849ff7e9cc15f0550194a9 --- src/gromacs/CMakeLists.txt | 3 +- .../correlationfunctions/gmx_lmcurve.cpp | 7 +- src/gromacs/domdec/domdec.cpp | 20 +- src/gromacs/domdec/domdec_constraints.cpp | 4 +- src/gromacs/ewald/pme.cpp | 2 +- src/gromacs/fft/fft5d.cpp | 8 +- src/gromacs/fileio/gmxfio.cpp | 8 +- src/gromacs/fileio/matio.cpp | 2 +- src/gromacs/fileio/md5.cpp | 2 +- src/gromacs/fileio/readinp.cpp | 1 - src/gromacs/fileio/tngio.cpp | 2 +- src/gromacs/gmxana/gmx_anaeig.cpp | 23 +- src/gromacs/gmxana/gmx_bar.cpp | 2 +- src/gromacs/gmxana/gmx_chi.cpp | 4 +- src/gromacs/gmxana/gmx_disre.cpp | 28 +- src/gromacs/gmxana/gmx_energy.cpp | 4 +- src/gromacs/gmxana/gmx_hbond.cpp | 4 +- src/gromacs/gmxana/gmx_wham.cpp | 22 +- src/gromacs/gmxpreprocess/pdb2gmx.cpp | 23 +- src/gromacs/gmxpreprocess/resall.cpp | 2 +- src/gromacs/gmxpreprocess/toppush.cpp | 7 +- src/gromacs/math/gmxcomplex.h | 6 +- src/gromacs/mdlib/constr.cpp | 4 +- src/gromacs/mdlib/constr.h | 2 +- src/gromacs/mdlib/lincs.cpp | 9 +- src/gromacs/mdlib/nbnxn_atomdata.cpp | 4 +- .../nbnxn_kernels/nbnxn_kernel_ref_inner.h | 4 +- src/gromacs/mdlib/ns.cpp | 6 +- src/gromacs/mdlib/qmmm.cpp | 16 +- src/gromacs/options/timeunitmanager.cpp | 4 +- src/gromacs/pulling/pull.cpp | 2 +- src/gromacs/pulling/pull_rotation.cpp | 29 +- src/gromacs/selection/compiler.cpp | 6 +- src/gromacs/selection/indexutil.cpp | 17 +- src/gromacs/selection/params.cpp | 61 +---- src/gromacs/timing/cyclecounter.h | 20 +- src/gromacs/utility/allocator.h | 2 +- src/gromacs/utility/basedefinitions.h | 22 -- src/gromacs/utility/binaryinformation.cpp | 6 +- src/gromacs/utility/bitmask.h | 8 +- src/gromacs/utility/pleasecite.cpp | 6 +- src/gromacs/utility/qsort_threadsafe.cpp | 257 ------------------ src/gromacs/utility/qsort_threadsafe.h | 109 -------- src/gromacs/utility/smalloc.h | 22 +- 44 files changed, 122 insertions(+), 678 deletions(-) delete mode 100644 src/gromacs/utility/qsort_threadsafe.cpp delete mode 100644 src/gromacs/utility/qsort_threadsafe.h diff --git a/src/gromacs/CMakeLists.txt b/src/gromacs/CMakeLists.txt index b1fba8987b..9022e5c4b6 100644 --- a/src/gromacs/CMakeLists.txt +++ b/src/gromacs/CMakeLists.txt @@ -279,7 +279,6 @@ set(IGNORED_CLANG_ALL_WARNINGS #Following ones are undecided/TODO "-Wno-disabled-macro-expansion" - "-Wno-cast-align" "-Wno-reserved-id-macro" "-Wno-global-constructors" "-Wno-exit-time-destructors" @@ -293,7 +292,6 @@ set(IGNORED_CLANG_ALL_WARNINGS "-Wno-used-but-marked-unused" "-Wno-padded" "-Wno-float-equal" - "-Wno-old-style-cast" "-Wno-conversion" "-Wno-double-promotion") string(REPLACE " " ";" IGNORED_CLANG_ALL_WARNINGS "${IGNORED_CLANG_ALL_WARNINGS}") @@ -346,6 +344,7 @@ if (GMX_CLANG_TIDY) "-readability-implicit-bool-conversion" #TODO: Remove gmx_bool "modernize-use-nullptr" "modernize-use-emplace" "modernize-make-unique" "modernize-make-shared" + "google-readability-casting" "cppcoreguidelines-*" "-cppcoreguidelines-pro-*" "-cppcoreguidelines-owning-memory" "-cppcoreguidelines-no-malloc" diff --git a/src/gromacs/correlationfunctions/gmx_lmcurve.cpp b/src/gromacs/correlationfunctions/gmx_lmcurve.cpp index c8fe2b6b30..256eb5918c 100644 --- a/src/gromacs/correlationfunctions/gmx_lmcurve.cpp +++ b/src/gromacs/correlationfunctions/gmx_lmcurve.cpp @@ -73,9 +73,8 @@ static void lmcurve_evaluate( const double* par, const int m_dat, const void* data, double* fvec, int* info) { - lmcurve_data_struct* D = (lmcurve_data_struct*)data; - int i; - for (i = 0; i < m_dat; i++) + const lmcurve_data_struct* D = reinterpret_cast(data); + for (int i = 0; i < m_dat; i++) { double dy = D->dy[i]; if (dy == 0) @@ -96,7 +95,7 @@ static void gmx_lmcurve( { lmcurve_data_struct data = { t, y, dy, f }; - lmmin(n_par, par, m_dat, nullptr, (const void*)&data, lmcurve_evaluate, + lmmin(n_par, par, m_dat, nullptr, &data, lmcurve_evaluate, control, status); } diff --git a/src/gromacs/domdec/domdec.cpp b/src/gromacs/domdec/domdec.cpp index 8226424f06..d72ccd4e9d 100644 --- a/src/gromacs/domdec/domdec.cpp +++ b/src/gromacs/domdec/domdec.cpp @@ -104,7 +104,6 @@ #include "gromacs/utility/exceptions.h" #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/gmxmpi.h" -#include "gromacs/utility/qsort_threadsafe.h" #include "gromacs/utility/real.h" #include "gromacs/utility/smalloc.h" #include "gromacs/utility/strconvert.h" @@ -5803,21 +5802,14 @@ static void set_zones_size(gmx_domdec_t *dd, } } -static int comp_cgsort(const void *a, const void *b) +static bool comp_cgsort(const gmx_cgsort_t &a, const gmx_cgsort_t &b) { - int comp; - gmx_cgsort_t *cga, *cgb; - cga = (gmx_cgsort_t *)a; - cgb = (gmx_cgsort_t *)b; - - comp = cga->nsc - cgb->nsc; - if (comp == 0) + if (a.nsc == b.nsc) { - comp = cga->ind_gl - cgb->ind_gl; + return a.ind_gl < b.ind_gl; } - - return comp; + return a.nsc < b.nsc; } /* Order data in \p dataToSort according to \p sort @@ -5908,7 +5900,7 @@ static void orderedSort(gmx::ArrayRef stationary, std::vector *sort1) { /* The new indices are not very ordered, so we qsort them */ - gmx_qsort_threadsafe(moved.data(), moved.size(), sizeof(moved[0]), comp_cgsort); + std::sort(moved.begin(), moved.end(), comp_cgsort); /* stationary is already ordered, so now we can merge the two arrays */ sort1->resize(stationary.size() + moved.size()); @@ -6000,7 +5992,7 @@ static void dd_sort_order(const gmx_domdec_t *dd, fprintf(debug, "qsort cgs: %d new home %d\n", dd->ncg_home, numCGNew); } /* Determine the order of the charge groups using qsort */ - gmx_qsort_threadsafe(cgsort.data(), dd->ncg_home, sizeof(cgsort[0]), comp_cgsort); + std::sort(cgsort.begin(), cgsort.end(), comp_cgsort); /* Remove the charge groups which are no longer at home here */ cgsort.resize(numCGNew); diff --git a/src/gromacs/domdec/domdec_constraints.cpp b/src/gromacs/domdec/domdec_constraints.cpp index 08cf72fabc..89d289ecbf 100644 --- a/src/gromacs/domdec/domdec_constraints.cpp +++ b/src/gromacs/domdec/domdec_constraints.cpp @@ -232,7 +232,7 @@ static void walk_out(int con, int con_offset, int a, int offset, int nrec, static void atoms_to_settles(gmx_domdec_t *dd, const gmx_mtop_t *mtop, const int *cginfo, - const int **at2settle_mt, + const int *const*at2settle_mt, int cg_start, int cg_end, t_ilist *ils_local, std::vector *ireq) @@ -431,7 +431,7 @@ int dd_make_local_constraints(gmx_domdec_t *dd, int at_start, t_ilist *ilc_local, *ils_local; std::vector *ireq; gmx::ArrayRef at2con_mt; - const int **at2settle_mt; + const int *const*at2settle_mt; gmx_hash_t *ga2la_specat; int at_end, i, j; t_iatom *iap; diff --git a/src/gromacs/ewald/pme.cpp b/src/gromacs/ewald/pme.cpp index 3d3f9220d4..5ca669b0bb 100644 --- a/src/gromacs/ewald/pme.cpp +++ b/src/gromacs/ewald/pme.cpp @@ -1226,7 +1226,7 @@ int gmx_pme_do(struct gmx_pme_t *pme, { fprintf(debug, "PME: number of ranks = %d, rank = %d\n", cr->nnodes, cr->nodeid); - fprintf(debug, "Grid = %p\n", (void*)grid); + fprintf(debug, "Grid = %p\n", static_cast(grid)); if (grid == nullptr) { gmx_fatal(FARGS, "No grid!"); diff --git a/src/gromacs/fft/fft5d.cpp b/src/gromacs/fft/fft5d.cpp index 12c1e010f8..fa725120ba 100644 --- a/src/gromacs/fft/fft5d.cpp +++ b/src/gromacs/fft/fft5d.cpp @@ -944,7 +944,7 @@ static void print_localdata(const t_complex* lin, const char* txt, int s, fft5d_ { for (l = 0; l < ll; l++) { - fprintf(debug, "%f ", ((real*)lin)[(z*xs[2]+y*xs[1])*2+(x*xs[0])*ll+l]); + fprintf(debug, "%f ", reinterpret_cast(lin)[(z*xs[2]+y*xs[1])*2+(x*xs[0])*ll+l]); } fprintf(debug, ","); } @@ -1399,18 +1399,18 @@ void fft5d_compare_data(const t_complex* lin, const t_complex* in, fft5d_plan pl for (l = 0; l < ll; l++) /*loop over real/complex parts*/ { real a, b; - a = ((real*)lin)[(z*xs[2]+y*xs[1])*2+x*xs[0]*ll+l]; + a = reinterpret_cast(lin)[(z*xs[2]+y*xs[1])*2+x*xs[0]*ll+l]; if (normalize) { a /= plan->rC[0]*plan->rC[1]*plan->rC[2]; } if (!bothLocal) { - b = ((real*)in)[((z+xc[2])*NG[0]*NG[1]+(y+xc[1])*NG[0])*2+(x+xc[0])*ll+l]; + b = reinterpret_cast(in)[((z+xc[2])*NG[0]*NG[1]+(y+xc[1])*NG[0])*2+(x+xc[0])*ll+l]; } else { - b = ((real*)in)[(z*xs[2]+y*xs[1])*2+x*xs[0]*ll+l]; + b = reinterpret_cast(in)[(z*xs[2]+y*xs[1])*2+x*xs[0]*ll+l]; } if (plan->flags&FFT5D_DEBUG) { diff --git a/src/gromacs/fileio/gmxfio.cpp b/src/gromacs/fileio/gmxfio.cpp index 993fe341dd..190a521c2a 100644 --- a/src/gromacs/fileio/gmxfio.cpp +++ b/src/gromacs/fileio/gmxfio.cpp @@ -119,7 +119,7 @@ static void gmx_fio_make_dummy(void) { if (!open_files) { - snew(open_files, 1); + open_files = new t_fileio {}; open_files->fp = nullptr; open_files->fn = nullptr; open_files->next = open_files; @@ -301,7 +301,7 @@ t_fileio *gmx_fio_open(const char *fn, const char *mode) strcat(newmode, "b"); } - snew(fio, 1); + fio = new t_fileio {}; tMPI_Lock_init(&(fio->mtx)); bRead = (newmode[0] == 'r' && newmode[1] != '+'); bReadWrite = (newmode[1] == '+'); @@ -386,7 +386,7 @@ int gmx_fio_close(t_fileio *fio) gmx_fio_unlock(fio); sfree(fio->fn); - sfree(fio); + delete fio; return rc; } @@ -434,7 +434,7 @@ int gmx_fio_fclose(FILE *fp) gmx_fio_remove(cur); gmx_fio_stop_getting_next(cur); sfree(cur->fn); - sfree(cur); + delete cur; break; } cur = gmx_fio_get_next(cur); diff --git a/src/gromacs/fileio/matio.cpp b/src/gromacs/fileio/matio.cpp index a9190bc359..1e824a4a5d 100644 --- a/src/gromacs/fileio/matio.cpp +++ b/src/gromacs/fileio/matio.cpp @@ -57,7 +57,7 @@ #include "gromacs/utility/smalloc.h" static const char mapper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+{}|;:',<.>/?"; -#define NMAP (long int)strlen(mapper) +#define NMAP static_cast(strlen(mapper)) #define MAX_XPM_LINELENGTH 4096 diff --git a/src/gromacs/fileio/md5.cpp b/src/gromacs/fileio/md5.cpp index ab63444073..8d985d6f1e 100644 --- a/src/gromacs/fileio/md5.cpp +++ b/src/gromacs/fileio/md5.cpp @@ -109,7 +109,7 @@ # define BYTE_ORDER 0 #endif -#define T_MASK ((md5_word_t) ~0) +#define T_MASK (static_cast(~0)) #define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87) #define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9) #define T3 0x242070db diff --git a/src/gromacs/fileio/readinp.cpp b/src/gromacs/fileio/readinp.cpp index 9d911c9b78..bc3d674192 100644 --- a/src/gromacs/fileio/readinp.cpp +++ b/src/gromacs/fileio/readinp.cpp @@ -51,7 +51,6 @@ #include "gromacs/utility/keyvaluetreebuilder.h" #include "gromacs/utility/niceheader.h" #include "gromacs/utility/programcontext.h" -#include "gromacs/utility/qsort_threadsafe.h" #include "gromacs/utility/smalloc.h" #include "gromacs/utility/stringutil.h" #include "gromacs/utility/textreader.h" diff --git a/src/gromacs/fileio/tngio.cpp b/src/gromacs/fileio/tngio.cpp index 1df209b639..d9d67fa0a5 100644 --- a/src/gromacs/fileio/tngio.cpp +++ b/src/gromacs/fileio/tngio.cpp @@ -681,7 +681,7 @@ static void add_selection_groups(gmx_tng_trajectory_t gmx_tng, } else { - res_name = (char *)""; + res_name = const_cast(""); res_id = 0; } if (tng_chain_residue_find(tng, chain, res_name, res_id, &res) diff --git a/src/gromacs/gmxana/gmx_anaeig.cpp b/src/gromacs/gmxana/gmx_anaeig.cpp index ad7e2e1048..7f12ca971a 100644 --- a/src/gromacs/gmxana/gmx_anaeig.cpp +++ b/src/gromacs/gmxana/gmx_anaeig.cpp @@ -465,7 +465,8 @@ static void project(const char *trajfile, const t_topology *top, int ePBC, matri matrix box; rvec *xread, *x; real t, inp, **inprod = nullptr; - char str[STRLEN], str2[STRLEN], **ylabel, *c; + char str[STRLEN], str2[STRLEN], *c; + const char **ylabel; real fact; gmx_rmpbc_t gpbc = nullptr; @@ -607,7 +608,7 @@ static void project(const char *trajfile, const t_topology *top, int ePBC, matri } sprintf(str, "projection on eigenvectors (%s)", proj_unit); write_xvgr_graphs(projfile, noutvec, 1, str, nullptr, output_env_get_xvgr_tlabel(oenv), - (const char **)ylabel, + ylabel, nframes, inprod[noutvec], inprod, nullptr, output_env_get_time_factor(oenv), FALSE, bSplit, oenv); } @@ -821,9 +822,10 @@ static void components(const char *outfile, int natoms, int noutvec, const int *outvec, const gmx_output_env_t *oenv) { - int g, s, v, i; - real *x, ***y; - char str[STRLEN], **ylabel; + int g, s, v, i; + real *x, ***y; + char str[STRLEN]; + const char**ylabel; fprintf(stderr, "Writing eigenvector components to %s\n", outfile); @@ -855,7 +857,7 @@ static void components(const char *outfile, int natoms, } write_xvgr_graphs(outfile, noutvec, 4, "Eigenvector components", "black: total, red: x, green: y, blue: z", - "Atom number", (const char **)ylabel, + "Atom number", ylabel, natoms, x, nullptr, y, 1, FALSE, FALSE, oenv); fprintf(stderr, "\n"); } @@ -866,9 +868,10 @@ static void rmsf(const char *outfile, int natoms, const real *sqrtm, real *eigval, int neig, const gmx_output_env_t *oenv) { - int g, v, i; - real *x, **y; - char str[STRLEN], **ylabel; + int g, v, i; + real *x, **y; + char str[STRLEN]; + const char **ylabel; for (i = 0; i < neig; i++) { @@ -903,7 +906,7 @@ static void rmsf(const char *outfile, int natoms, const real *sqrtm, } } write_xvgr_graphs(outfile, noutvec, 1, "RMS fluctuation (nm) ", nullptr, - "Atom number", (const char **)ylabel, + "Atom number", ylabel, natoms, x, y, nullptr, 1, TRUE, FALSE, oenv); fprintf(stderr, "\n"); } diff --git a/src/gromacs/gmxana/gmx_bar.cpp b/src/gromacs/gmxana/gmx_bar.cpp index f7e1db88ea..08416daea2 100644 --- a/src/gromacs/gmxana/gmx_bar.cpp +++ b/src/gromacs/gmxana/gmx_bar.cpp @@ -1041,7 +1041,7 @@ static void sim_data_histogram(sim_data_t *sd, const char *filename, bl = bl->next; } - xvgr_legend(fp, nsets, (const char**)setnames, oenv); + xvgr_legend(fp, nsets, setnames, oenv); /* now make the histograms */ diff --git a/src/gromacs/gmxana/gmx_chi.cpp b/src/gromacs/gmxana/gmx_chi.cpp index 131fb59fa3..93c2a5d235 100644 --- a/src/gromacs/gmxana/gmx_chi.cpp +++ b/src/gromacs/gmxana/gmx_chi.cpp @@ -693,7 +693,7 @@ static void histogramming(FILE *log, int nbin, gmx_residuetype_t *rt, { leg[i+NKKKPHI+NKKKPSI] = gmx_strdup(kkkchi1[i].name); } - xvgr_legend(fp, NJC, (const char**)leg, oenv); + xvgr_legend(fp, NJC, leg, oenv); fprintf(fp, "%5s ", "#Res."); for (i = 0; (i < NJC); i++) { @@ -1032,7 +1032,7 @@ static void print_transitions(const char *fn, int maxchi, int nlist, /* Print order parameters */ fp = xvgropen(fn, "Dihedral Rotamer Transitions", "Residue", "Transitions/ns", oenv); - xvgr_legend(fp, NONCHI+maxchi, (const char**)leg, oenv); + xvgr_legend(fp, NONCHI+maxchi, leg, oenv); fprintf(fp, "%5s ", "#Res."); fprintf(fp, "%10s %10s %10s ", leg[edPhi], leg[edPsi], leg[edOmega]); diff --git a/src/gromacs/gmxana/gmx_disre.cpp b/src/gromacs/gmxana/gmx_disre.cpp index e12dd7e566..7000f0ad9e 100644 --- a/src/gromacs/gmxana/gmx_disre.cpp +++ b/src/gromacs/gmxana/gmx_disre.cpp @@ -210,7 +210,7 @@ static void check_viol(FILE *log, snew(fshift, SHIFTS); interaction_function[F_DISRES].ifunc(n, &forceatoms[i], forceparams, - (const rvec*)x, f, fshift, + x, f, fshift, pbc, g, lam, &dvdl, nullptr, fcd, nullptr); sfree(fshift); viol = fcd->disres.sumviol; @@ -262,27 +262,6 @@ typedef struct { real up1, r, rT3, rT6, viol, violT3, violT6; } t_dr_stats; -static int drs_comp(const void *a, const void *b) -{ - t_dr_stats *da, *db; - - da = (t_dr_stats *)a; - db = (t_dr_stats *)b; - - if (da->viol > db->viol) - { - return -1; - } - else if (da->viol < db->viol) - { - return 1; - } - else - { - return 0; - } -} - static void dump_dump(FILE *log, int ndr, t_dr_stats drs[]) { static const char *core[] = { "All restraints", "Core restraints" }; @@ -418,7 +397,8 @@ static void dump_stats(FILE *log, int nsteps, int ndr, t_ilist *disres, dump_viol(log, ndr, drs, FALSE); fprintf(log, "+++ Sorted by linear averaged violations: +++\n"); - qsort(drs, ndr, sizeof(drs[0]), drs_comp); + std::sort(drs, drs+ndr, [](const t_dr_stats &a, const t_dr_stats &b) + {return a.viol > b.viol; }); //Reverse sort dump_viol(log, ndr, drs, TRUE); dump_dump(log, ndr, drs); @@ -794,7 +774,7 @@ int gmx_disre(int argc, char *argv[]) snew(leg[i], 12); sprintf(leg[i], "index %d", index[i]); } - xvgr_legend(xvg, isize, (const char**)leg, oenv); + xvgr_legend(xvg, isize, leg, oenv); } else { diff --git a/src/gromacs/gmxana/gmx_energy.cpp b/src/gromacs/gmxana/gmx_energy.cpp index ecbfcb78bd..6dc4c6ea4f 100644 --- a/src/gromacs/gmxana/gmx_energy.cpp +++ b/src/gromacs/gmxana/gmx_energy.cpp @@ -1831,11 +1831,11 @@ int gmx_energy(int argc, char *argv[]) if (bSum) { leg[nset] = gmx_strdup("Sum"); - xvgr_legend(out, nset+1, (const char**)leg, oenv); + xvgr_legend(out, nset+1, leg, oenv); } else { - xvgr_legend(out, nset, (const char**)leg, oenv); + xvgr_legend(out, nset, leg, oenv); } snew(bIsEner, nset); diff --git a/src/gromacs/gmxana/gmx_hbond.cpp b/src/gromacs/gmxana/gmx_hbond.cpp index 346d6c55a9..41a9f2df77 100644 --- a/src/gromacs/gmxana/gmx_hbond.cpp +++ b/src/gromacs/gmxana/gmx_hbond.cpp @@ -3216,7 +3216,7 @@ int gmx_hbond(int argc, char *argv[]) snew(leg[1], STRLEN); sprintf(leg[0], "%s", bContact ? "Contacts" : "Hydrogen bonds"); sprintf(leg[1], "Pairs within %g nm", (r2cut > 0) ? r2cut : rcut); - xvgr_legend(fp, 2, (const char**)leg, oenv); + xvgr_legend(fp, 2, leg, oenv); sfree(leg[1]); sfree(leg[0]); sfree(leg); @@ -3420,7 +3420,7 @@ int gmx_hbond(int argc, char *argv[]) { gmx_incons("number of legend entries"); } - xvgr_legend(fp, nleg, (const char**)legnames, oenv); + xvgr_legend(fp, nleg, legnames, oenv); for (i = 0; i < nframes; i++) { fprintf(fp, "%10g", hb->time[i]); diff --git a/src/gromacs/gmxana/gmx_wham.cpp b/src/gromacs/gmxana/gmx_wham.cpp index 6ca704eb3a..01606649bc 100644 --- a/src/gromacs/gmxana/gmx_wham.cpp +++ b/src/gromacs/gmxana/gmx_wham.cpp @@ -1548,26 +1548,6 @@ static void print_histograms(const char *fnhist, t_UmbrellaWindow * window, int sfree(fn); } -//! Used for qsort to sort random numbers -static int func_wham_is_larger(const void *a, const void *b) -{ - double *aa, *bb; - aa = (double*)a; - bb = (double*)b; - if (*aa < *bb) - { - return -1; - } - else if (*aa > *bb) - { - return 1; - } - else - { - return 0; - } -} - //! Make random weights for histograms for the Bayesian bootstrap of complete histograms) static void setRandomBsWeights(t_UmbrellaWindow *synthwin, int nAllPull, t_UmbrellaOptions *opt) { @@ -1582,7 +1562,7 @@ static void setRandomBsWeights(t_UmbrellaWindow *synthwin, int nAllPull, t_Umbre { r[i] = dist(opt->rng); } - qsort((void *)r, nAllPull-1, sizeof(double), &func_wham_is_larger); + std::sort(r, r+nAllPull-1); r[nAllPull-1] = 1.0*nAllPull; synthwin[0].bsWeight[0] = r[0]; diff --git a/src/gromacs/gmxpreprocess/pdb2gmx.cpp b/src/gromacs/gmxpreprocess/pdb2gmx.cpp index 49add7cf88..036e47f19a 100644 --- a/src/gromacs/gmxpreprocess/pdb2gmx.cpp +++ b/src/gromacs/gmxpreprocess/pdb2gmx.cpp @@ -43,6 +43,8 @@ #include #include +#include + #include "gromacs/commandline/pargs.h" #include "gromacs/fileio/confio.h" #include "gromacs/fileio/gmxfio.h" @@ -678,29 +680,22 @@ typedef struct { char altloc; /* alternate location indicator */ } t_pdbindex; -static int pdbicomp(const void *a, const void *b) +static bool pdbicomp(const t_pdbindex &a, const t_pdbindex &b) { - t_pdbindex *pa, *pb; - int d; - - pa = (t_pdbindex *)a; - pb = (t_pdbindex *)b; - - d = (pa->resnr - pb->resnr); + int d = (a.resnr - b.resnr); if (d == 0) { - d = (pa->j - pb->j); + d = (a.j - b.j); if (d == 0) { - d = (pa->anm1 - pb->anm1); + d = (a.anm1 - b.anm1); if (d == 0) { - d = (pa->altloc - pb->altloc); + d = (a.altloc - b.altloc); } } } - - return d; + return d < 0; } static void sort_pdbatoms(t_restp restp[], @@ -759,7 +754,7 @@ static void sort_pdbatoms(t_restp restp[], pdbi[i].anm1 = atomnm[1]; pdbi[i].altloc = pdba->pdbinfo[i].altloc; } - qsort(pdbi, natoms, static_cast(sizeof(pdbi[0])), pdbicomp); + std::sort(pdbi, pdbi+natoms, pdbicomp); /* pdba is sorted in pdbnew using the pdbi index */ snew(a, natoms); diff --git a/src/gromacs/gmxpreprocess/resall.cpp b/src/gromacs/gmxpreprocess/resall.cpp index 510525dcfa..b2bc373a74 100644 --- a/src/gromacs/gmxpreprocess/resall.cpp +++ b/src/gromacs/gmxpreprocess/resall.cpp @@ -276,7 +276,7 @@ static int get_bt(char* header) static void clear_t_restp(t_restp *rrtp) { - memset((void *)rrtp, 0, sizeof(t_restp)); + memset(rrtp, 0, sizeof(t_restp)); } /* print all the ebtsNR type numbers */ diff --git a/src/gromacs/gmxpreprocess/toppush.cpp b/src/gromacs/gmxpreprocess/toppush.cpp index 8783264b18..5f81a3d7e4 100644 --- a/src/gromacs/gmxpreprocess/toppush.cpp +++ b/src/gromacs/gmxpreprocess/toppush.cpp @@ -2492,11 +2492,6 @@ void b2_to_b(t_block2 *b2, t_blocka *b) b->index[i] = nra; } -static int icomp(const void *v1, const void *v2) -{ - return (*((int *) v1))-(*((int *) v2)); -} - void merge_excl(t_blocka *excl, t_block2 *b2, warninp_t wi) { int i, k; @@ -2525,7 +2520,7 @@ void merge_excl(t_blocka *excl, t_block2 *b2, warninp_t wi) if (b2->nra[i] > 0) { /* remove double entries */ - qsort(b2->a[i], static_cast(b2->nra[i]), static_cast(sizeof(b2->a[i][0])), icomp); + std::sort(b2->a[i], b2->a[i]+b2->nra[i]); k = 1; for (j = 1; (j < b2->nra[i]); j++) { diff --git a/src/gromacs/math/gmxcomplex.h b/src/gromacs/math/gmxcomplex.h index 67c798558e..3faa4b960b 100644 --- a/src/gromacs/math/gmxcomplex.h +++ b/src/gromacs/math/gmxcomplex.h @@ -3,7 +3,7 @@ * * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. - * Copyright (c) 2012,2014,2017, by the GROMACS development team, led by + * Copyright (c) 2012,2014,2017,2018, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -62,8 +62,8 @@ static inline t_complex rcexp(real r) { t_complex c; - c.re = (real)cos(r); - c.im = (real)sin(r); + c.re = cos(r); + c.im = sin(r); return c; } diff --git a/src/gromacs/mdlib/constr.cpp b/src/gromacs/mdlib/constr.cpp index fd90a6ce82..34977fd4f1 100644 --- a/src/gromacs/mdlib/constr.cpp +++ b/src/gromacs/mdlib/constr.cpp @@ -1113,9 +1113,9 @@ Constraints::atom2constraints_moltype() const return impl_->at2con_mt; } -const int **Constraints::atom2settle_moltype() const +int *const* Constraints::atom2settle_moltype() const { - return (const int **)impl_->at2settle_mt; + return impl_->at2settle_mt; } diff --git a/src/gromacs/mdlib/constr.h b/src/gromacs/mdlib/constr.h index d7a8b4a743..6579ba9674 100644 --- a/src/gromacs/mdlib/constr.h +++ b/src/gromacs/mdlib/constr.h @@ -178,7 +178,7 @@ class Constraints //! Getter for use by domain decomposition. const ArrayRef atom2constraints_moltype() const; //! Getter for use by domain decomposition. - const int **atom2settle_moltype() const; + int *const* atom2settle_moltype() const; /*! \brief Return the data for reduction for determining * constraint RMS relative deviations, or an empty ArrayRef diff --git a/src/gromacs/mdlib/lincs.cpp b/src/gromacs/mdlib/lincs.cpp index 557b3b9725..681cf142b4 100644 --- a/src/gromacs/mdlib/lincs.cpp +++ b/src/gromacs/mdlib/lincs.cpp @@ -1481,12 +1481,6 @@ static bool more_than_two_sequential_constraints(const t_ilist *ilist, return bMoreThanTwoSequentialConstraints; } -//! Sorting helper function to compare two integers. -static int int_comp(const void *a, const void *b) -{ - return (*(int *)a) - (*(int *)b); -} - Lincs *init_lincs(FILE *fplog, const gmx_mtop_t &mtop, int nflexcon_global, ArrayRef at2con, bool bPLINCS, int nIter, int nProjOrder) @@ -1967,8 +1961,7 @@ static void set_matrix_indices(Lincs *li, if (bSortMatrix) { /* Order the blbnb matrix to optimize memory access */ - qsort(&(li->blbnb[li->blnr[b]]), li->blnr[b + 1] - li->blnr[b], - sizeof(li->blbnb[0]), int_comp); + std::sort(&(li->blbnb[li->blnr[b]]), &(li->blbnb[li->blnr[b+1]])); } } } diff --git a/src/gromacs/mdlib/nbnxn_atomdata.cpp b/src/gromacs/mdlib/nbnxn_atomdata.cpp index 42fa2ba3dd..a79e1cb6d5 100644 --- a/src/gromacs/mdlib/nbnxn_atomdata.cpp +++ b/src/gromacs/mdlib/nbnxn_atomdata.cpp @@ -767,7 +767,7 @@ void nbnxn_atomdata_init(const gmx::MDLogger &mdlog, { GMX_LOG(mdlog.info).asParagraph().appendText("Using tree force reduction"); - snew(nbat->syncStep, nth); + nbat->syncStep = new tMPI_Atomic[nth]; } } @@ -1369,7 +1369,7 @@ static void nbnxn_atomdata_add_nbat_f_to_f_treereduce(const nbnxn_atomdata_t *nb if (sync_th < nth) /* otherwise nothing to sync index[1] will be >=nout */ { /* wait on the thread which computed input data in previous step */ - while (tMPI_Atomic_get((volatile tMPI_Atomic_t*)&(nbat->syncStep[sync_th])) < group_size/2) + while (tMPI_Atomic_get(static_cast(&(nbat->syncStep[sync_th]))) < group_size/2) { gmx_pause(); } diff --git a/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref_inner.h b/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref_inner.h index 62f5cc96a8..34088450bf 100644 --- a/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref_inner.h +++ b/src/gromacs/mdlib/nbnxn_kernels/nbnxn_kernel_ref_inner.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014,2015,2016, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015,2016,2018, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -306,7 +306,7 @@ #ifdef CALC_COUL_TAB rs = rsq*rinv*ic->tabq_scale; - ri = (int)rs; + ri = int(rs); frac = rs - ri; #if !GMX_DOUBLE /* fexcl = F_i + frac * (F_(i+1)-F_i) */ diff --git a/src/gromacs/mdlib/ns.cpp b/src/gromacs/mdlib/ns.cpp index 46b51fd357..a1b31a82ed 100644 --- a/src/gromacs/mdlib/ns.cpp +++ b/src/gromacs/mdlib/ns.cpp @@ -89,9 +89,9 @@ static gmx_bool NOTEXCL_(t_excl e[], int i, int j) return !(ISEXCL(e, i, j)); } #else -#define SETEXCL(e, i, j) (e)[((int) (j))] |= (1<<((int) (i))) -#define RMEXCL(e, i, j) (e)[((int) (j))] &= (~(1<<((int) (i)))) -#define ISEXCL(e, i, j) (gmx_bool) ((e)[((int) (j))] & (1<<((int) (i)))) +#define SETEXCL(e, i, j) (e)[int(j)] |= (1<<(int(i))) +#define RMEXCL(e, i, j) (e)[int(j)] &= (~(1<<(int(i)))) +#define ISEXCL(e, i, j) static_cast((e)[(int(j))] & (1<<(int(i)))) #define NOTEXCL(e, i, j) !(ISEXCL(e, i, j)) #endif diff --git a/src/gromacs/mdlib/qmmm.cpp b/src/gromacs/mdlib/qmmm.cpp index e6c97ebbec..e192cd4ac9 100644 --- a/src/gromacs/mdlib/qmmm.cpp +++ b/src/gromacs/mdlib/qmmm.cpp @@ -121,12 +121,10 @@ typedef struct { int shift; } t_j_particle; -static int struct_comp(const void *a, const void *b) +static bool struct_comp(const t_j_particle &a, const t_j_particle &b) { - - return (((t_j_particle *)a)->j)-(((t_j_particle *)b)->j); - -} /* struct_comp */ + return a.j < b.j; +} static real call_QMroutine(const t_commrec gmx_unused *cr, const t_forcerec gmx_unused *fr, t_QMrec gmx_unused *qm, t_MMrec gmx_unused *mm, rvec gmx_unused f[], rvec gmx_unused fshift[]) @@ -691,15 +689,11 @@ void update_QMMMrec(const t_commrec *cr, - qsort(qm_i_particles, QMMMlist->nri, - static_cast(sizeof(qm_i_particles[0])), - struct_comp); + std::sort(qm_i_particles, qm_i_particles+QMMMlist->nri, struct_comp); /* The mm_j_particles argument to qsort is not allowed to be NULL */ if (mm_nr > 0) { - qsort(mm_j_particles, mm_nr, - static_cast(sizeof(mm_j_particles[0])), - struct_comp); + std::sort(mm_j_particles, mm_j_particles+mm_nr, struct_comp); } /* remove multiples in the QM shift array, since in init_QMMM() we * went through the atom numbers from 0 to md.nr, the order sorted diff --git a/src/gromacs/options/timeunitmanager.cpp b/src/gromacs/options/timeunitmanager.cpp index 49b2c7023e..e03245e742 100644 --- a/src/gromacs/options/timeunitmanager.cpp +++ b/src/gromacs/options/timeunitmanager.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -108,7 +108,7 @@ const char *TimeUnitManager::timeUnitAsString() const double TimeUnitManager::timeScaleFactor() const { GMX_RELEASE_ASSERT(timeUnit_ >= 0 - && (size_t)timeUnit_ < sizeof(g_timeScaleFactors)/sizeof(g_timeScaleFactors[0]), + && static_cast(timeUnit_) < sizeof(g_timeScaleFactors)/sizeof(g_timeScaleFactors[0]), "Time unit index has become out-of-range"); return g_timeScaleFactors[timeUnit_]; } diff --git a/src/gromacs/pulling/pull.cpp b/src/gromacs/pulling/pull.cpp index 9ab7626302..ece8811a63 100644 --- a/src/gromacs/pulling/pull.cpp +++ b/src/gromacs/pulling/pull.cpp @@ -411,7 +411,7 @@ static FILE *open_pull_out(const char *fn, struct pull_t *pull, } if (nsets > 1) { - xvgr_legend(fp, nsets, (const char**)setname, oenv); + xvgr_legend(fp, nsets, setname, oenv); } for (int c = 0; c < nsets; c++) { diff --git a/src/gromacs/pulling/pull_rotation.cpp b/src/gromacs/pulling/pull_rotation.cpp index c0d009cc1a..1e7b3f4838 100644 --- a/src/gromacs/pulling/pull_rotation.cpp +++ b/src/gromacs/pulling/pull_rotation.cpp @@ -75,7 +75,6 @@ #include "gromacs/utility/basedefinitions.h" #include "gromacs/utility/fatalerror.h" #include "gromacs/utility/pleasecite.h" -#include "gromacs/utility/qsort_threadsafe.h" #include "gromacs/utility/smalloc.h" static char const *RotStr = {"Enforced rotation:"}; @@ -1704,7 +1703,7 @@ static inline void shift_single_coord(const matrix box, rvec x, const ivec is) /* Determine the 'home' slab of this atom which is the * slab with the highest Gaussian weight of all */ -#define round(a) (int)((a)+0.5) +#define round(a) int((a)+0.5) static inline int get_homeslab( rvec curr_x, /* The position for which the home slab shall be determined */ const rvec rotvec, /* The rotation vector */ @@ -2360,26 +2359,6 @@ static real do_flex_lowlevel( return V; } -static int projection_compare(const void *a, const void *b) -{ - auto xca = reinterpret_cast(a); - auto xcb = reinterpret_cast(b); - - if (xca->xcproj < xcb->xcproj) - { - return -1; - } - else if (xca->xcproj > xcb->xcproj) - { - return 1; - } - else - { - return 0; - } -} - - static void sort_collective_coordinates( gmx_enfrotgrp *erg, sort_along_vec_t *data) /* Buffer for sorting the positions */ @@ -2395,7 +2374,11 @@ static void sort_collective_coordinates( copy_rvec(erg->rotg->x_ref[i], data[i].x_ref); } /* Sort the 'data' structure */ - gmx_qsort(data, erg->rotg->nat, sizeof(sort_along_vec_t), projection_compare); + std::sort(data, data+erg->rotg->nat, + [](const sort_along_vec_t &a, const sort_along_vec_t &b) + { + return a.xcproj < b.xcproj; + }); /* Copy back the sorted values */ for (int i = 0; i < erg->rotg->nat; i++) diff --git a/src/gromacs/selection/compiler.cpp b/src/gromacs/selection/compiler.cpp index 25e4bff11f..1e9875240c 100644 --- a/src/gromacs/selection/compiler.cpp +++ b/src/gromacs/selection/compiler.cpp @@ -377,15 +377,15 @@ print_group_info(FILE *fp, const char *name, } else if (sel.cdata->flags & SEL_CDATA_MINMAXALLOC) { - fprintf(fp, "(%d atoms, %p)", g->isize, (void*)g); + fprintf(fp, "(%d atoms, %p)", g->isize, static_cast(g)); } else if (sel.v.type == GROUP_VALUE && g == sel.v.u.g) { - fprintf(fp, "(static, %p)", (void*)g); + fprintf(fp, "(static, %p)", static_cast(g)); } else { - fprintf(fp, "%p", (void*)g); + fprintf(fp, "%p", static_cast(g)); } } diff --git a/src/gromacs/selection/indexutil.cpp b/src/gromacs/selection/indexutil.cpp index c3f6600d66..726408045b 100644 --- a/src/gromacs/selection/indexutil.cpp +++ b/src/gromacs/selection/indexutil.cpp @@ -478,28 +478,13 @@ gmx_ana_index_check_range(gmx_ana_index_t *g, int natoms) * Set operations ********************************************************************/ -/** Helper function for gmx_ana_index_sort(). */ -static int -cmp_atomid(const void *a, const void *b) -{ - if (*(int *)a < *(int *)b) - { - return -1; - } - if (*(int *)a > *(int *)b) - { - return 1; - } - return 0; -} - /*! * \param[in,out] g Index group to be sorted. */ void gmx_ana_index_sort(gmx_ana_index_t *g) { - std::qsort(g->index, g->isize, sizeof(*g->index), cmp_atomid); + std::sort(g->index, g->index+g->isize); } void diff --git a/src/gromacs/selection/params.cpp b/src/gromacs/selection/params.cpp index d33485f90e..6e68ac8421 100644 --- a/src/gromacs/selection/params.cpp +++ b/src/gromacs/selection/params.cpp @@ -42,6 +42,7 @@ #include "gmxpre.h" #include +#include #include #include "gromacs/math/units.h" @@ -238,59 +239,19 @@ place_child(const SelectionTreeElementPointer &root, } /*! \brief - * Comparison function for sorting integer ranges. + * Comparison function for sorting ranges. * - * \param[in] a Pointer to the first range. - * \param[in] b Pointer to the second range. - * \returns -1, 0, or 1 depending on the relative order of \p a and \p b. + * \param[in] a First range. + * \param[in] b Second range. + * \returns return true if a < b * * The ranges are primarily sorted based on their starting point, and * secondarily based on length (longer ranges come first). */ -static int -cmp_int_range(const void *a, const void *b) +template +static bool cmp_range(const std::array &a, const std::array &b) { - if (((int *)a)[0] < ((int *)b)[0]) - { - return -1; - } - if (((int *)a)[0] > ((int *)b)[0]) - { - return 1; - } - if (((int *)a)[1] > ((int *)b)[1]) - { - return -1; - } - return 0; -} - -/*! \brief - * Comparison function for sorting real ranges. - * - * \param[in] a Pointer to the first range. - * \param[in] b Pointer to the second range. - * \returns -1, 0, or 1 depending on the relative order of \p a and \p b. - * - * The ranges are primarily sorted based on their starting point, and - * secondarily based on length (longer ranges come first). - */ -static int -cmp_real_range(const void *a, const void *b) -{ - if (((real *)a)[0] < ((real *)b)[0]) - { - return -1; - } - if (((real *)a)[0] > ((real *)b)[0]) - { - return 1; - } - if (((real *)a)[1] > ((real *)b)[1]) - { - return -1; - } - return 0; + return a[0] < b[0] || (a[0] == b[0] && a[1] > b[1]); } /*! \brief @@ -374,7 +335,8 @@ parse_values_range(const SelectionParserValueList &values, /* Sort the ranges and merge consequent ones */ if (param->val.type == INT_VALUE) { - qsort(idata, n, 2*sizeof(int), &cmp_int_range); + const auto range_data = reinterpret_cast*>(idata); + sort(range_data, range_data+n, cmp_range); for (i = j = 2; i < 2*n; i += 2) { if (idata[j-1]+1 >= idata[i]) @@ -394,7 +356,8 @@ parse_values_range(const SelectionParserValueList &values, } else { - qsort(rdata, n, 2*sizeof(real), &cmp_real_range); + const auto range_data = reinterpret_cast*>(rdata); + sort(range_data, range_data+n, cmp_range); for (i = j = 2; i < 2*n; i += 2) { if (rdata[j-1] >= rdata[i]) diff --git a/src/gromacs/timing/cyclecounter.h b/src/gromacs/timing/cyclecounter.h index 53652df835..2c78e68e10 100644 --- a/src/gromacs/timing/cyclecounter.h +++ b/src/gromacs/timing/cyclecounter.h @@ -55,14 +55,6 @@ #include #endif -#ifdef __cplusplus -extern "C" -{ -#endif -#if 0 -} /* fixes auto-indentation problems */ -#endif - #if ((defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__PATHSCALE__) || defined(__PGIC__)) && \ (defined(__i386__) || defined(__x86_64__))) /* x86 or x86-64 with GCC inline assembly */ @@ -210,7 +202,6 @@ static __inline__ gmx_cycles_t gmx_cycles_read(void) static __inline__ gmx_cycles_t gmx_cycles_read(void) { /* x86 with GCC inline assembly - pentium TSC register */ - gmx_cycles_t cycle; unsigned low, high; #if HAVE_RDTSCP @@ -218,10 +209,9 @@ static __inline__ gmx_cycles_t gmx_cycles_read(void) #else __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high)); #endif - - cycle = ((unsigned long long)low) | (((unsigned long long)high)<<32); - - return cycle; + const gmx_cycles_t c_low = low; + const gmx_cycles_t c_high = high; + return c_low | c_high <<32; } #elif ((defined __aarch64__) && (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__PATHSCALE__) || defined(__PGIC__))) static __inline__ gmx_cycles_t gmx_cycles_read(void) @@ -592,8 +582,4 @@ static bool gmx_cycles_have_counter(void) double gmx_cycles_calibrate(double sampletime); -#ifdef __cplusplus -} -#endif - #endif diff --git a/src/gromacs/utility/allocator.h b/src/gromacs/utility/allocator.h index c8905068df..5afb4bbf92 100644 --- a/src/gromacs/utility/allocator.h +++ b/src/gromacs/utility/allocator.h @@ -215,7 +215,7 @@ class Allocator : public AllocationPolicy */ template void - construct(pointer p, Args && ... args) { ::new((void *)p)T(std::forward(args) ...); } + construct(pointer p, Args && ... args) { ::new(p)T(std::forward(args) ...); } /*! \brief Call the destructor of object without releasing memory * diff --git a/src/gromacs/utility/basedefinitions.h b/src/gromacs/utility/basedefinitions.h index f02b2d1ed5..51faac8e9f 100644 --- a/src/gromacs/utility/basedefinitions.h +++ b/src/gromacs/utility/basedefinitions.h @@ -89,28 +89,6 @@ using index = std::ptrdiff_t; */ #define gmx_restrict __restrict -/*! \def GMX_CXX11_COMPILATION - * \brief - * Defined to 1 when compiling as C++11. - * - * While \Gromacs only supports C++11 compilation, there are some parts of the - * code that are compiled with other tools than the actual C++ compiler, and - * these may not support C++11. Most notable such case is all of CUDA code - * (with CUDA versions older than 6.5), but other types of kernels might also - * have similar limitations in the future. - * - * The define is intended for conditional compilation in low-level headers that - * need to support inclusion from such non-C++11 files, but get significant - * benefit (e.g., for correctness checking or more convenient use) from C++11. - * It should only be used for features that do not influence the ABI of the - * header; e.g., static_asserts or additional helper methods. - */ -#if defined __cplusplus && __cplusplus >= 201103L -# define GMX_CXX11_COMPILATION 1 -#else -# define GMX_CXX11_COMPILATION 0 -#endif - /*! \def gmx_unused * \brief * Attribute to suppress compiler warnings about unused function parameters. diff --git a/src/gromacs/utility/binaryinformation.cpp b/src/gromacs/utility/binaryinformation.cpp index 27be14f4b8..6609567cd9 100644 --- a/src/gromacs/utility/binaryinformation.cpp +++ b/src/gromacs/utility/binaryinformation.cpp @@ -154,14 +154,14 @@ void printCopyright(gmx::TextWriter *writer) "of the License, or (at your option) any later version." }; -#define NCONTRIBUTORS (int)asize(Contributors) -#define NCR (int)asize(CopyrightText) +#define NCONTRIBUTORS static_cast(asize(Contributors)) +#define NCR static_cast(asize(CopyrightText)) // FAH has an exception permission from LGPL to allow digital signatures in Gromacs. #ifdef GMX_FAHCORE #define NLICENSE 0 #else -#define NLICENSE (int)asize(LicenseText) +#define NLICENSE static_cast(asize(LicenseText)) #endif // TODO a centering behaviour of TextWriter could be useful here diff --git a/src/gromacs/utility/bitmask.h b/src/gromacs/utility/bitmask.h index d5eb82894f..40b897bff3 100644 --- a/src/gromacs/utility/bitmask.h +++ b/src/gromacs/utility/bitmask.h @@ -75,25 +75,25 @@ inline static void bitmask_clear(gmx_bitmask_t* m) /*! \brief Set bit at position b to 1. */ inline static void bitmask_set_bit(gmx_bitmask_t* m, int b) { - *m |= ((gmx_bitmask_t)1 << b); + *m |= gmx_bitmask_t(1) << b; } /*! \brief Initialize all bits: bit b to 1, others to 0 */ inline static void bitmask_init_bit(gmx_bitmask_t* m, int b) { - *m = ((gmx_bitmask_t)1 << b); + *m = gmx_bitmask_t(1) << b; } /*! \brief Initialize all bits: all bits below b to 1, others to 0 */ inline static void bitmask_init_low_bits(gmx_bitmask_t* m, int b) { - *m = ((gmx_bitmask_t)1 << b) - 1; + *m = (gmx_bitmask_t(1) << b) - 1; } /*! \brief Test if bit b is set */ inline static bool bitmask_is_set(gmx_bitmask_t m, int b) { - return (m & ((gmx_bitmask_t)1 << b)) != 0; + return (m & (gmx_bitmask_t(1) << b)) != 0; } /*! \brief Test if both bitmasks have no common bits enabled */ diff --git a/src/gromacs/utility/pleasecite.cpp b/src/gromacs/utility/pleasecite.cpp index 2aad0cf457..f3664822f9 100644 --- a/src/gromacs/utility/pleasecite.cpp +++ b/src/gromacs/utility/pleasecite.cpp @@ -3,7 +3,7 @@ * * Copyright (c) 1991-2000, University of Groningen, The Netherlands. * Copyright (c) 2001-2004, The GROMACS development team. - * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by + * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -382,7 +382,7 @@ void please_cite(FILE *fp, const char *key) "J. Chem. Phys.", 141, 2014, "044110" }, }; -#define NSTR (int)asize(citedb) +#define NSTR static_cast(asize(citedb)) int index; char *author; @@ -394,7 +394,7 @@ void please_cite(FILE *fp, const char *key) return; } - for (index = 0; (index < NSTR) && (strcmp(citedb[index].key, key) != 0); index++) + for (index = 0; index < NSTR && (strcmp(citedb[index].key, key) != 0); index++) { ; } diff --git a/src/gromacs/utility/qsort_threadsafe.cpp b/src/gromacs/utility/qsort_threadsafe.cpp deleted file mode 100644 index 5339b1be20..0000000000 --- a/src/gromacs/utility/qsort_threadsafe.cpp +++ /dev/null @@ -1,257 +0,0 @@ -/* - * This file is part of the GROMACS molecular simulation package. - * - * Copyright (c) 2010,2012,2014,2015,2017,2018, by the GROMACS development team, led by - * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, - * and including many others, as listed in the AUTHORS file in the - * top-level source directory and at http://www.gromacs.org. - * - * GROMACS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 - * of the License, or (at your option) any later version. - * - * GROMACS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with GROMACS; if not, see - * http://www.gnu.org/licenses, or write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * If you want to redistribute modifications to GROMACS, please - * consider that scientific software is very special. Version - * control is crucial - bugs must be traceable. We will be happy to - * consider code for inclusion in the official distribution, but - * derived work must not be called official GROMACS. Details are found - * in the README & COPYING files - if they are missing, get the - * official version at http://www.gromacs.org. - * - * To help us fund GROMACS development, we humbly ask that you cite - * the research papers on the package. Check out http://www.gromacs.org. - */ -#include "gmxpre.h" - -#include "qsort_threadsafe.h" - -#include - -static void -qsort_swapfunc(void * a, - void * b, - size_t n, - int swaptype) -{ - int * ia; - int * ib; - int itmp; - - char * ca; - char * cb; - char ctmp; - - if (swaptype <= 1) - { - ia = static_cast(a); - ib = static_cast(b); - for (; n > 0; ia += 1, ib += 1, n -= sizeof(int)) - { - itmp = *ia; - *ia = *ib; - *ib = itmp; - } - } - else - { - ca = static_cast(a); - cb = static_cast(b); - for (; n > 0; ca += 1, cb += 1, n -= 1) - { - ctmp = *ca; - *ca = *cb; - *cb = ctmp; - } - } -} - - -static void * -qsort_med3(void * a, - void * b, - void * c, - int (*compar) (const void *a, const void *b)) -{ - if (compar(a, b) < 0) - { - if (compar(b, c) < 0) - { - return b; - } - else if (compar(a, c) < 0) - { - return c; - } - else - { - return a; - } - } - else - { - if (compar(b, c) > 0) - { - return b; - } - else if (compar(a, c) > 0) - { - return c; - } - else - { - return a; - } - } -} - - -void -gmx_qsort(void * base, - size_t nmemb, - size_t size, - int (*compar)(const void *, const void *)) -{ -#define QSORT_EXCH(a, b, t) ((t) = (a), (a) = (b), (b) = (t)) -#define QSORT_SWAP(a, b) swaptype != 0 ? qsort_swapfunc(a, b, size, swaptype) : \ - (void)QSORT_EXCH(*(int *)(a), *(int *)(b), t) - - char *pa, *pb, *pc, *pd, *pl, *pm, *pn, *pv, *cbase; - int r, swaptype; - int t, v; - size_t s, st; - - if (size == 0) - { - return; - } - - cbase = static_cast(base); - - swaptype = static_cast(cbase - (char *)nullptr) % sizeof(int) || size % sizeof(int) ? 2 : size == sizeof(int) ? 0 : 1; - - if (nmemb < 7) - { - /* Insertion sort on smallest arrays */ - for (pm = cbase + size; pm < cbase + nmemb*size; pm += size) - { - for (pl = pm; (pl > cbase) && compar((void *)(pl-size), (void *) pl) > 0; pl -= size) - { - QSORT_SWAP(pl, pl-size); - } - } - return; - } - - /* Small arrays, middle element */ - pm = cbase + (nmemb/2)*size; - - if (nmemb > 7) - { - pl = cbase; - pn = cbase + (nmemb-1)*size; - if (nmemb > 40) - { - /* Big arrays, pseudomedian of 9 */ - s = (nmemb/8)*size; - pl = static_cast(qsort_med3((void *)pl, (void *)((size_t)pl+s), (void *)((size_t)pl+2*s), compar)); - pm = static_cast(qsort_med3((void *)((size_t)pm-s), (void *)pm, (void *)((size_t)pm+s), compar)); - pn = static_cast(qsort_med3((void *)((size_t)pn-2*s), (void *)((size_t)pn-s), (void *)pn, compar)); - } - /* Mid-size, med of 3 */ - pm = static_cast(qsort_med3((void *)pl, (void *)pm, (void *)pn, compar)); - } - - /* pv points to partition value */ - if (swaptype != 0) - { - pv = cbase; - QSORT_SWAP(pv, pm); - } - else - { - v = *reinterpret_cast(pm); - pv = static_cast((void*)&v); - } - - pa = pb = cbase; - pc = pd = cbase + (nmemb-1)*size; - - for (;; ) - { - while (pb <= pc && (r = compar((void *)pb, (void *) pv)) <= 0) - { - if (r == 0) - { - QSORT_SWAP(pa, pb); - pa += size; - } - pb += size; - } - while (pc >= pb && (r = compar((void *)pc, (void *) pv)) >= 0) - { - if (r == 0) - { - QSORT_SWAP(pc, pd); - pd -= size; - } - pc -= size; - } - if (pb > pc) - { - break; - } - QSORT_SWAP(pb, pc); - pb += size; - pc -= size; - } - pn = cbase + nmemb*size; - - s = pa-cbase; - st = pb-pa; - if (st < s) - { - s = st; - } - - if (s > 0) - { - qsort_swapfunc(cbase, pb-s, s, swaptype); - } - - s = pd-pc; - st = pn-pd-size; - if (st < s) - { - s = st; - } - - if (s > 0) - { - qsort_swapfunc(pb, pn-s, s, swaptype); - } - - if ((s = pb-pa) > size) - { - gmx_qsort(cbase, s/size, size, compar); - } - - if ((s = pd-pc) > size) - { - gmx_qsort(pn-s, s/size, size, compar); - } - -#undef QSORT_EXCH -#undef QSORT_SWAP - -} diff --git a/src/gromacs/utility/qsort_threadsafe.h b/src/gromacs/utility/qsort_threadsafe.h deleted file mode 100644 index b2bd1f7ec6..0000000000 --- a/src/gromacs/utility/qsort_threadsafe.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - * This file is part of the GROMACS molecular simulation package. - * - * Copyright (c) 2010,2012,2014,2016, by the GROMACS development team, led by - * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, - * and including many others, as listed in the AUTHORS file in the - * top-level source directory and at http://www.gromacs.org. - * - * GROMACS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 - * of the License, or (at your option) any later version. - * - * GROMACS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with GROMACS; if not, see - * http://www.gnu.org/licenses, or write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * If you want to redistribute modifications to GROMACS, please - * consider that scientific software is very special. Version - * control is crucial - bugs must be traceable. We will be happy to - * consider code for inclusion in the official distribution, but - * derived work must not be called official GROMACS. Details are found - * in the README & COPYING files - if they are missing, get the - * official version at http://www.gromacs.org. - * - * To help us fund GROMACS development, we humbly ask that you cite - * the research papers on the package. Check out http://www.gromacs.org. - */ -/*! \libinternal \file - * \brief - * Portable implementation of threadsafe quicksort. - * - * This module provides a \Gromacs version of the qsort() routine defined. - * It is not highly optimized, but it is threadsafe, i.e. multiple threads - * can simultaneously call gmx_qsort() with different data. - * - * The rational is that some implementations of qsort() are not threadsafe. - * For instance qsort() in glibc contains a bug which makes it not threadsafe: - * http://sources.redhat.com/bugzilla/show_bug.cgi?id=11655 - * On the other hand, system qsort() might be faster than our own. - * - * \inlibraryapi - * \ingroup module_utility - */ -#ifndef GMX_UTILITY_QSORT_THREADSAFE_H -#define GMX_UTILITY_QSORT_THREADSAFE_H - -#include - -/* For GMX_THREAD_MPI */ -#include "config.h" - -#ifdef __cplusplus -extern "C" -{ -#endif -#if 0 -} /* fixes auto-indentation problems */ -#endif - -/*! \addtogroup module_utility - * \{ - */ - -/*! \brief - * Portable threadsafe sort routine. - * - * \param base Pointer to first element in list to sort - * \param nmemb Number of elements in list - * \param size Size in bytes of each element - * \param compar Comparison function that takes two pointers to elements - * being compared as arguments. The function should return an - * integer less than, equal to, or greater than zero if the - * first argument is considered to be respectively less than, - * equal to, or greater than the second. - */ -void -gmx_qsort(void *base, - size_t nmemb, - size_t size, - int (*compar)(const void *, const void *)); - - -/*! \def gmx_qsort_threadsafe - * \brief - * Threadsafe qsort(). - * - * Expands to gmx_qsort() if Gromacs is built with threading, or system qsort() - * otherwise. - */ -#if GMX_THREAD_MPI -#define gmx_qsort_threadsafe gmx_qsort -#else -#define gmx_qsort_threadsafe qsort -#endif - -/*! \} */ - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/gromacs/utility/smalloc.h b/src/gromacs/utility/smalloc.h index 8ef0e02ca2..906da1c66c 100644 --- a/src/gromacs/utility/smalloc.h +++ b/src/gromacs/utility/smalloc.h @@ -178,9 +178,7 @@ void *save_calloc_aligned(const char *name, const char *file, int line, */ void save_free_aligned(const char *name, const char *file, int line, void *ptr); -#if GMX_CXX11_COMPILATION #include -#endif /*! \cond internal */ /*! \name Implementation templates for C++ memory allocation macros @@ -209,59 +207,47 @@ template static inline void gmx_snew_impl(const char *name, const char *file, int line, T * &ptr, size_t nelem) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value, "snew() called on C++ type"); -#endif - ptr = (T *)save_calloc(name, file, line, nelem, sizeof(T)); + ptr = static_cast(save_calloc(name, file, line, nelem, sizeof(T))); } /** C++ helper for srenew(). */ template static inline void gmx_srenew_impl(const char *name, const char *file, int line, T * &ptr, size_t nelem) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value, "srenew() called on C++ type"); -#endif - ptr = (T *)save_realloc(name, file, line, ptr, nelem, sizeof(T)); + ptr = static_cast(save_realloc(name, file, line, ptr, nelem, sizeof(T))); } /** C++ helper for smalloc(). */ template static inline void gmx_smalloc_impl(const char *name, const char *file, int line, T * &ptr, size_t size) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value, "smalloc() called on C++ type"); -#endif - ptr = (T *)save_malloc(name, file, line, size); + ptr = static_cast(save_malloc(name, file, line, size)); } /** C++ helper for snew_aligned(). */ template static inline void gmx_snew_aligned_impl(const char *name, const char *file, int line, T * &ptr, size_t nelem, size_t alignment) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value, "snew_aligned() called on C++ type"); -#endif - ptr = (T *)save_calloc_aligned(name, file, line, nelem, sizeof(T), alignment); + ptr = static_cast(save_calloc_aligned(name, file, line, nelem, sizeof(T), alignment)); } /** C++ helper for sfree(). */ template static inline void gmx_sfree_impl(const char *name, const char *file, int line, T *ptr) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value || std::is_void::value, "sfree() called on C++ type"); -#endif save_free(name, file, line, ptr); } /** C++ helper for sfree_aligned(). */ template static inline void gmx_sfree_aligned_impl(const char *name, const char *file, int line, T *ptr) { -#if GMX_CXX11_COMPILATION static_assert(std::is_pod::value || std::is_void::value, "sfree_aligned() called on C++ type"); -#endif save_free_aligned(name, file, line, ptr); } /*! \} */ -- 2.22.0