Skip to content

Commit 1b74261

Browse files
author
ripley
committed
tidy up, including working around UBSAN false report
git-svn-id: https://svn.r-project.org/R/trunk@87465 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 79d40ca commit 1b74261

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/main/random.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,8 @@ static void ProbSampleReplace(int n, double *p, int *perm, int nans, int *ans)
345345
static void
346346
walker_ProbSampleReplace(int n, double *p, int *a, int nans, int *ans)
347347
{
348-
double *q, rU;
349-
int i, j, k;
350-
int *HL, *H, *L;
348+
int *HL;
349+
double *q;
351350

352351
/* Create the alias tables.
353352
The idea is that for HL[0] ... L-1 label the entries with q < 1
@@ -364,26 +363,28 @@ walker_ProbSampleReplace(int n, double *p, int *a, int nans, int *ans)
364363
HL = R_Calloc(n, int);
365364
q = R_Calloc(n, double);
366365
}
367-
H = HL - 1; L = HL + n;
368-
for (i = 0; i < n; i++) {
366+
int *H = HL, *L = HL + n;
367+
for (int i = 0; i < n; i++) {
369368
q[i] = p[i] * n;
370-
if (q[i] < 1.) *++H = i; else *--L = i;
369+
if (q[i] < 1.) *H++ = i; else *--L = i;
371370
}
372-
if (H >= HL && L < HL + n) { /* So some q[i] are >= 1 and some < 1 */
373-
for (k = 0; k < n - 1; k++) {
374-
i = HL[k];
375-
j = *L;
371+
if (H > HL && L < HL + n) { /* So some q[i] are >= 1 and some < 1 */
372+
for (int k = 0; k < n - 1; k++) {
373+
int i = HL[k];
374+
int j = *L;
376375
a[i] = j;
377-
q[j] += q[i] - 1;
376+
q[j] += q[i] - 1.;
378377
if (q[j] < 1.) L++;
379378
if(L >= HL + n) break; /* now all are >= 1 */
380379
}
381380
}
382-
for (i = 0; i < n; i++) q[i] += i;
381+
for (int i = 0; i < n; i++) q[i] += i;
383382

384383
/* generate sample */
385384
Sampletype Sample_kind = R_sample_kind();
386-
for (i = 0; i < nans; i++) {
385+
double rU;
386+
for (int i = 0; i < nans; i++) {
387+
int k;
387388
if (Sample_kind == ROUNDING) {
388389
rU = unif_rand() * n;
389390
k = (int) rU;

0 commit comments

Comments
 (0)