Skip to content

Commit

Permalink
memcmp for complex instead of == on double
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle committed Jul 19, 2019
1 parent 2df22a3 commit dd7b24a
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/uniqlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,13 @@ SEXP rleid(SEXP l, SEXP cols) {
break;
case REALSXP : {
long long *ll = (long long *)REAL(jcol);
same = ll[i]==ll[i-1]; }
same = ll[i]==ll[i-1];
// 8 bytes of bits are identical. For real (no rounding currently) and integer64
// long long == 8 bytes checked in init.c
break;
} break;
case CPLXSXP: {
// tried to make long long complex * but got a warning that it's a GNU extension
Rcomplex *pz = COMPLEX(jcol);
same = pz[i].r == pz[i-1].r && pz[i].i == pz[i-1].i;
same = memcmp(&pz[i], &pz[i-1], sizeof(Rcomplex))==0; // compiler optimization should replace library call with best 16-byte fixed method

This comment has been minimized.

Copy link
@MichaelChirico

MichaelChirico Jul 20, 2019

Member

if memcmp for complex, why not for the other types as well?

This comment has been minimized.

Copy link
@mattdowle

mattdowle Jul 20, 2019

Author Member

I did suggest that at one point earlier in this PR. Would need testing to confirm no slow down. The difference is that applying memcmp to the more common types would have a bigger impact if I'm wrong about the compiler optimizing away the library call.

} break;
default :
error("Type '%s' not supported", type2char(TYPEOF(jcol))); // # nocov
Expand All @@ -215,32 +214,30 @@ SEXP rleid(SEXP l, SEXP cols) {
SEXP jcol = VECTOR_ELT(l, icols[0]-1);
switch (TYPEOF(jcol)) {
case INTSXP : case LGLSXP : {
int *ijcol = INTEGER(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = ijcol[i]==ijcol[i-1];
ians[i] = (grp+=!same);
}
int *ijcol = INTEGER(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = ijcol[i]==ijcol[i-1];
ians[i] = (grp+=!same);
}
break;
} break;
case STRSXP : {
for (R_xlen_t i=1; i<nrow; i++) {
bool same = STRING_ELT(jcol,i)==STRING_ELT(jcol,i-1);
ians[i] = (grp+=!same);
}
const SEXP *jd = STRING_PTR(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = jd[i]==jd[i-1];
ians[i] = (grp+=!same);
}
break;
} break;
case REALSXP : {
long long *lljcol = (long long *)REAL(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = lljcol[i]==lljcol[i-1];
ians[i] = (grp+=!same);
}
long long *lljcol = (long long *)REAL(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = lljcol[i]==lljcol[i-1];
ians[i] = (grp+=!same);
}
break;
} break;
case CPLXSXP: {
Rcomplex *pzjcol = COMPLEX(jcol);
for (R_xlen_t i=1; i<nrow; i++) {
bool same = pzjcol[i].r == pzjcol[i-1].r && pzjcol[i].i == pzjcol[i-1].i;
bool same = memcmp(&pzjcol[i], &pzjcol[i-1], sizeof(Rcomplex))==0;
ians[i] = (grp += !same);
}
} break;
Expand Down

0 comments on commit dd7b24a

Please sign in to comment.