Hi Etienne,

thanks for narrowing this down to rhyper().  I think I found the root
cause, and I do not think we need to drop any architecture.

CUtils/c_sources/rhyper.c builds a lazy cache of exact log factorials in
afc(): the factorial itself is kept in a `long double` and only converted
to a log afterwards:

    static long double cur=3628800;
    ...
    cur *= i;
    data->al[i+1] = logl(cur);

`long double` has the same exponent range as `double` on armel/armhf (it
is `double` there) and on ppc64el (IBM double-double): about 1.8e308.
170! is still representable (7.3e306), but 171! overflows to +Inf.  So on
exactly these architectures every afc(i) with i >= 171 returns +Inf; on
amd64/i386 the x87 80-bit long double reaches 1e4932 and nothing
overflows.  That explains why only these three architectures hang.

How that freezes the test: the example data has 102 cases and 74 controls,
so rhyper(102, 74, s) is called with nn1+nn2 = 176 and afc(176) = +Inf.
In the case II (inverse transformation) branch

    w = exp(con + afc(n2) + afc(n1+n2-k) - afc(n2-k) - afc(n1+n2));

this becomes exp(-Inf) = 0 for k >= 6 (for k <= 5 it is NaN, which
terminates but does not return a proper random variate), so p starts at 0,
`u > p` is always true, ix walks up to maxjx, jumps back to L10 and the
loop repeats forever.  Case III has the same problem: the exact acceptance
test becomes Inf - Inf = NaN, so every candidate is rejected and L30 loops
forever (reproducible with rhyper(400, 400, 400)).

Reproduction without the affected hardware: take rhyper.c, replace
`long double` with `double` (same exponent range as on armel/armhf/
ppc64el), link it with mt19937ar.c and call rhyper(102,74,k).  k = 6..22
loop forever, while the amd64 build returns immediately.  With the patch
below the double build terminates for every k and gives exactly the same
sequence of results as the amd64 build.  (I did not use gcc's
-mlong-double-64 for this because on x86-64 it is not ABI-compatible with
glibc's logl.)

The fix is to keep the exact table only as long as the factorial fits into
a double and to use the existing Stirling branch beyond that, which is
what R's afc() does anyway:

--- a/CUtils/c_sources/rhyper.c
+++ b/CUtils/c_sources/rhyper.c
@@ -105,7 +105,12 @@ static double afc(int i)
       exit(1);
     } else if (i <= data.computed) {
        value = data.al[i + 1];
-    } else if (i <= 1754) {
+    /* 170! is the largest factorial that fits in a double.  On platforms
+     * where long double has the same exponent range as double (armel,
+     * armhf, ppc64el, ...), cur would overflow to +Inf from 171 on and
+     * every afc() value beyond that would be +Inf, making rhyper() loop
+     * forever.  Use Stirling's approximation there, as R does. */
+    } else if (i <= 170) {
        value = compute(i, &data);
     } else {
        di = i;

The error of the Stirling branch is around 1e-10, far below the
deltal/deltau safety margins (7.8e-3 / 3.4e-3) of the algorithm.

Unless someone objects, I will apply this as a Debian patch, upload it,
and forward it upstream, so the package can keep the full architecture
list.

Kind regards,
 Andreas

Reply via email to