[libc++] Support int8_t and uint8_t in integer distributions as an extension
authorLouis Dionne <ldionne.2@gmail.com>
Wed, 1 Jun 2022 19:25:14 +0000 (15:25 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Fri, 22 Jul 2022 12:33:01 +0000 (08:33 -0400)
commit07e984bc52014a8565033be10f7e80982e974b99
treeb9a7df6bb755b85cdebf76605b6372bb95a5160f
parent908054df4f15d44a5e1e72d95aa2bcf92ddb6e1a
[libc++] Support int8_t and uint8_t in integer distributions as an extension

In D125283, we ensured that integer distributions would not compile when
used with arbitrary unsupported types. This effectively enforced what
the Standard mentions here: http://eel.is/c++draft/rand#req.genl-1.5.

However, this also had the effect of breaking some users that were
using integer distributions with unsupported types like int8_t. Since we
already support using __int128_t in those distributions, it is reasonable
to also support smaller types like int8_t and its unsigned variant. This
commit implements that, adds tests and documents the extension. Note that
we voluntarily don't add support for instantiating these distributions
with bool and char, since those are not integer types. However, it is
trivial to replace uses of these random distributions on char using int8_t.

It is also interesting to note that in the process of adding tests
for smaller types, I discovered that our distributions sometimes don't
provide as faithful a distribution when instantiated with smaller types,
so I had to relax a couple of tests. In particular, we do a really bad
job at implementing the negative binomial, geometric and poisson distributions
for small types. I think this all boils down to the algorithm we use in
std::poisson_distribution, however I am running out of time to investigate
that and changing the algorithm would be an ABI break (which might be
reasonable).

As part of this patch, I also added a mitigation for a very likely
integer overflow bug we were hitting in our tests in negative_binomial_distribution.
I also filed http://llvm.org/PR56656 to track fixing the problematic
distributions with int8_t and uint8_t.

Supersedes D125283.

Differential Revision: https://reviews.llvm.org/D126823
16 files changed:
libcxx/docs/ReleaseNotes.rst
libcxx/docs/UsingLibcxx.rst
libcxx/include/__random/binomial_distribution.h
libcxx/include/__random/discrete_distribution.h
libcxx/include/__random/geometric_distribution.h
libcxx/include/__random/is_valid.h
libcxx/include/__random/negative_binomial_distribution.h
libcxx/include/__random/poisson_distribution.h
libcxx/include/__random/uniform_int_distribution.h
libcxx/test/libcxx/numerics/rand/rand.req.urng/valid_int_type.verify.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.bern/rand.dist.bern.bin/eval.pass.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.bern/rand.dist.bern.geo/eval.pass.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.bern/rand.dist.bern.negbin/eval.pass.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.pois/rand.dist.pois.poisson/eval.pass.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.samp/rand.dist.samp.discrete/eval.pass.cpp
libcxx/test/std/numerics/rand/rand.dist/rand.dist.uni/rand.dist.uni.int/eval.pass.cpp