Skip to content

Commit 251e88d

Browse files
authored
froll: use const, internal_error; style malloc (#7301)
1 parent 5975ffd commit 251e88d

File tree

5 files changed

+71
-71
lines changed

5 files changed

+71
-71
lines changed

R/froll.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ frolladapt = function(x, n, align="right", partial=FALSE, give.names=FALSE) {
130130
}
131131
if (!length(n))
132132
stopf("'n' must be non 0 length")
133-
if (anyNA(n))
134-
stopf("'n' must not have NAs")
135133
if (!identical(align, "right"))
136134
stopf("'align' other than 'right' has not yet been implemented")
137135
if (!isTRUEorFALSE(partial))

src/data.table.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,30 +229,30 @@ typedef enum { // adding rolling functions here and in frollfunR in frollR.c
229229
PROD = 4
230230
} rollfun_t;
231231
// froll.c
232-
void frollfun(rollfun_t rfun, unsigned int algo, double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose);
233-
void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
234-
void frollmeanExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
235-
void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
236-
void frollsumExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
237-
void frollmaxFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
238-
void frollmaxExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
239-
void frollminFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
240-
void frollminExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
241-
void frollprodFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
242-
void frollprodExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
232+
void frollfun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose);
233+
void frollmeanFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
234+
void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
235+
void frollsumFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
236+
void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
237+
void frollmaxFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
238+
void frollmaxExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
239+
void frollminFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
240+
void frollminExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
241+
void frollprodFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
242+
void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose);
243243

244244
// frolladaptive.c
245-
void frolladaptivefun(rollfun_t rfun, unsigned int algo, double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
246-
void frolladaptivemeanFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
247-
void frolladaptivemeanExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
248-
void frolladaptivesumFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
249-
void frolladaptivesumExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
250-
//void frolladaptivemaxFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose); // does not exists as of now
251-
void frolladaptivemaxExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
252-
//void frolladaptiveminFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose); // does not exists as of now
253-
void frolladaptiveminExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
254-
void frolladaptiveprodFast(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
255-
void frolladaptiveprodExact(double *x, uint64_t nx, ans_t *ans, int *k, double fill, bool narm, int hasnf, bool verbose);
245+
void frolladaptivefun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
246+
void frolladaptivemeanFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
247+
void frolladaptivemeanExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
248+
void frolladaptivesumFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
249+
void frolladaptivesumExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
250+
//void frolladaptivemaxFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose); // does not exists as of now
251+
void frolladaptivemaxExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
252+
//void frolladaptiveminFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose); // does not exists as of now
253+
void frolladaptiveminExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
254+
void frolladaptiveprodFast(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
255+
void frolladaptiveprodExact(const double *x, uint64_t nx, ans_t *ans, const int *k, double fill, bool narm, int hasnf, bool verbose);
256256

257257
// frollR.c
258258
SEXP frollfunR(SEXP fun, SEXP xobj, SEXP kobj, SEXP fill, SEXP algo, SEXP align, SEXP narm, SEXP hasnf, SEXP adaptive);

src/froll.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if (R_FINITE(x[i-k])) { \
4242
* algo = 1: exact
4343
* recalculate whole fun for each observation, for mean roundoff correction is adjusted
4444
*/
45-
void frollfun(rollfun_t rfun, unsigned int algo, double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose) {
45+
void frollfun(rollfun_t rfun, unsigned int algo, const double *x, uint64_t nx, ans_t *ans, int k, int align, double fill, bool narm, int hasnf, bool verbose) {
4646
double tic = 0;
4747
if (verbose)
4848
tic = omp_get_wtime();
@@ -112,7 +112,7 @@ void frollfun(rollfun_t rfun, unsigned int algo, double *x, uint64_t nx, ans_t *
112112
* rollmean implemented as single pass sliding window for align="right"
113113
* if non-finite detected re-run rollmean implemented as single pass sliding window with NA support
114114
*/
115-
void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
115+
void frollmeanFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
116116
if (verbose)
117117
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmeanFast", (uint64_t)nx, k, hasnf, (int)narm);
118118
if (k == 0) {
@@ -210,7 +210,7 @@ void frollmeanFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
210210
* rollmean implemented as mean of k obs for each observation for align="right"
211211
* if non-finite detected and na.rm=TRUE then re-run NF aware rollmean
212212
*/
213-
void frollmeanExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
213+
void frollmeanExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
214214
if (verbose)
215215
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmeanExact", (uint64_t)nx, k, hasnf, (int)narm);
216216
if (k == 0) {
@@ -302,7 +302,7 @@ void frollmeanExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
302302
/* fast rolling sum - fast
303303
* same as mean fast
304304
*/
305-
void frollsumFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
305+
void frollsumFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
306306
if (verbose)
307307
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollsumFast", (uint64_t)nx, k, hasnf, (int)narm);
308308
if (k == 0) {
@@ -398,7 +398,7 @@ if (nc == 0) { \
398398
/* fast rolling sum - exact
399399
* same as mean exact
400400
*/
401-
void frollsumExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
401+
void frollsumExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
402402
if (verbose)
403403
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollsumExact", (uint64_t)nx, k, hasnf, (int)narm);
404404
if (k == 0) {
@@ -489,10 +489,10 @@ static inline void wmax(const double * restrict x, uint64_t o, int k, double * r
489489
uint64_t ii = o+i-k+1;
490490
if (ISNAN(x[ii])) {
491491
if (ISNA(x[ii])) {
492-
error("internal error: frollmax reached untested branch of code, for now use frollmax algo='exact', please provide reproducible example of your frollmax usage to data.table github issue tracker\n"); // # nocov
492+
internal_error(__func__, "frollmax reached untested branch of code, for now use frollmax algo='exact', please provide reproducible example of your frollmax usage to data.table github issue tracker"); // # nocov
493493
//iww = ii; ww = NA_REAL;
494494
} else if (ISNA(ww)) {
495-
error("internal error: frollmax reached untested branch of code, for now use frollmax algo='exact', please provide reproducible example of your frollmax usage to data.table github issue tracker\n"); // # nocov
495+
internal_error(__func__, "frollmax reached untested branch of code, for now use frollmax algo='exact', please provide reproducible example of your frollmax usage to data.table github issue tracker"); // # nocov
496496
// do nothing because w > x[i]: NA > NaN
497497
} else { // no NA in window so NaN >= than any non-NA
498498
iww = ii; ww = R_NaN;
@@ -514,7 +514,7 @@ static inline void wmax(const double * restrict x, uint64_t o, int k, double * r
514514
* new max is used to continue outer single pass as long as new max index is not leaving the running window
515515
* should scale well for bigger window size, may carry overhead for small window, needs benchmarking
516516
*/
517-
void frollmaxFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
517+
void frollmaxFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
518518
if (verbose)
519519
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmaxFast", (uint64_t)nx, k, hasnf, (int)narm);
520520
if (k == 0) {
@@ -610,7 +610,7 @@ void frollmaxFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool n
610610
* otherwise we scan for NaN/NA and run either of two loops
611611
* has.nf=FALSE can give incorrect results if NAs provided, documented to be used with care
612612
*/
613-
void frollmaxExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
613+
void frollmaxExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
614614
if (verbose)
615615
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollmaxExact", (uint64_t)nx, k, hasnf, (int)narm);
616616
if (k == 0) {
@@ -635,7 +635,7 @@ void frollmaxExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
635635
ans->dbl_v[i] = w;
636636
}
637637
} else {
638-
bool *isnan = malloc(nx*sizeof(*isnan)); // isnan lookup - we use it to reduce ISNAN calls in nested loop
638+
bool *isnan = malloc(sizeof(*isnan) * nx); // isnan lookup - we use it to reduce ISNAN calls in nested loop
639639
if (!isnan) { // # nocov start
640640
ansSetMsg(ans, 3, "%s: Unable to allocate memory for isnan", __func__); // raise error
641641
return;
@@ -699,10 +699,10 @@ static inline void wmin(const double * restrict x, uint64_t o, int k, double * r
699699
uint64_t ii = o+i-k+1;
700700
if (ISNAN(x[ii])) {
701701
if (ISNA(x[ii])) {
702-
error("internal error: frollmin reached untested branch of code, for now use frollmin algo='exact', please provide reproducible example of your frollmin usage to data.table github issue tracker\n"); // # nocov
702+
internal_error(__func__, "frollmin reached untested branch of code, for now use frollmin algo='exact', please provide reproducible example of your frollmin usage to data.table github issue tracker"); // # nocov
703703
//iww = ii; ww = NA_REAL;
704704
} else if (ISNA(ww)) {
705-
error("internal error: frollmin reached untested branch of code, for now use frollmin algo='exact', please provide reproducible example of your frollmin usage to data.table github issue tracker\n"); // # nocov
705+
internal_error(__func__, "frollmin reached untested branch of code, for now use frollmin algo='exact', please provide reproducible example of your frollmin usage to data.table github issue tracker"); // # nocov
706706
// do nothing because w > x[i]: NA > NaN
707707
} else { // no NA in window so NaN >= than any non-NA
708708
iww = ii; ww = R_NaN;
@@ -720,7 +720,7 @@ static inline void wmin(const double * restrict x, uint64_t o, int k, double * r
720720
/* fast rolling min - fast
721721
* see rolling max fast details
722722
*/
723-
void frollminFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
723+
void frollminFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
724724
if (verbose)
725725
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollminFast", (uint64_t)nx, k, hasnf, (int)narm);
726726
if (k == 0) {
@@ -813,7 +813,7 @@ void frollminFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool n
813813
/* fast rolling min - exact
814814
* see rolling max exact details
815815
*/
816-
void frollminExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
816+
void frollminExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
817817
if (verbose)
818818
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollminExact", (uint64_t)nx, k, hasnf, (int)narm);
819819
if (k == 0) {
@@ -838,7 +838,7 @@ void frollminExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
838838
ans->dbl_v[i] = w;
839839
}
840840
} else {
841-
bool *isnan = malloc(nx*sizeof(bool)); // isnan lookup - we use it to reduce ISNAN calls in nested loop
841+
bool *isnan = malloc(sizeof(*isnan) * nx); // isnan lookup - we use it to reduce ISNAN calls in nested loop
842842
if (!isnan) { // # nocov start
843843
ansSetMsg(ans, 3, "%s: Unable to allocate memory for isnan", __func__); // raise error
844844
free(isnan);
@@ -891,7 +891,7 @@ void frollminExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
891891
/* fast rolling prod - fast
892892
* same as mean fast
893893
*/
894-
void frollprodFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
894+
void frollprodFast(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
895895
if (verbose)
896896
snprintf(end(ans->message[0]), 500, _("%s: running for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollprodFast", (uint64_t)nx, k, hasnf, (int)narm);
897897
if (k == 0) {
@@ -997,7 +997,7 @@ void frollprodFast(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool
997997
/* fast rolling prod - exact
998998
* same as mean exact
999999
*/
1000-
void frollprodExact(double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
1000+
void frollprodExact(const double *x, uint64_t nx, ans_t *ans, int k, double fill, bool narm, int hasnf, bool verbose) {
10011001
if (verbose)
10021002
snprintf(end(ans->message[0]), 500, _("%s: running in parallel for input length %"PRIu64", window %d, hasnf %d, narm %d\n"), "frollprodExact", (uint64_t)nx, k, hasnf, (int)narm);
10031003
if (k == 0) {

0 commit comments

Comments
 (0)