<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=http://email.email.llvm.org/c/eJzFVE1v2zAM_TXKhahhy_mwDz4kTQLs0KHYBnQ7DbLM2FplKZPkpO2vHxUn61YU27AdFtiORFLk4yOp2jaP1V2nNMJRBNkp08IWG-vgVt0PHoLQ92AN1E4Y2Wn0HvbOtk70fTTdOdsDm63kfi-t4SnP2GzNeNGFsPcsXzK-ped4PCaPdghDjYm0fZTEWCzfHli-bq_ubrtP7zbbL_eMl_CG8cUBQaNwBhsInQhgMHToQGpBMQ2Ba6UEuw-qV09IJgg7q7U9Rkj4IPo9AU1YumbpMktgM0oiztY2tdXhNZBnVWJdS7snemfd14fCFobW5eiMzdPxoXzPEp4rI_XQkPv8WvrQaFWzfHPWnr7KBOiFMhTy2dNiNS6Afg7D4AzQ4YgmXxLXzWgM0VW-hpTlq8vB9QssP4bif5_th1m2-bqYu9v_mu11zPbd8u36883y4x8mvbKhA7uLfeARpPDowXd20A3UCHvrvaqJkGDj9tI1Tdx7RVQ9vnAtfD9KenuIeAHFA-PXkI1Sgv8rLCjFQCiEP4-Fwx06NBJfqwKa5EeTy3DQGynnWzP06JSkVSTppBzZKiktItoDTdXPTALjfGTYg4C9x6GxV-NpoBNIo6vhIPSAxEY4IhpgG86KlBWr9PsKoqdLGSBW53c2576I2BKCMGmqvCnzUkzEEDrrquXNe55NBqerF42oQjfU58y1Plz-ruia-YIy0FZ5P6CPA5lPZ3zSVSLfFbKU01lRZNM6nYu6FOlMYj1NBRfZdKJFjdpXVAGCYvAIJxe0piJM_h2Bquim42nGF1nB05QniwzLXV7MRZruZlKUbJoizYBOop84YxNXnVzWQ-tJqZUP_lkpqENbg3gCTAiDChqrG4pKbXpuWBEUXcM7uvvG4lFJW4xd_8ocTU54qxPYbwMzxZE>53452</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization for value range of std::rand()
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AMS21
</td>
</tr>
</table>
<pre>
While watching Fedor Pikus talk on branchless programming from [cppcon2021](https://www.youtube.com/watch?v=g-WPhYREFjk) I've learned that nether clang nor gcc optimize the following examples.
1. Example [godbolt](https://godbolt.org/z/5hqx8o8nz)
```cpp
#include <cstdlib>
int main()
{
return std::rand() >= 0;
}
```
2. Example [godbolt](https://godbolt.org/z/T51Eq76rP)
```cpp
#include <cstdlib>
int main()
{
return std::rand() <= RAND_MAX;
}
```
Both of these cases should be possible to be optimized to simply
```asm
mov eax, 1
ret
```
Because as [cppreference](https://en.cppreference.com/w/cpp/numeric/random/rand) points out std::rand "returns a pseudo-random integral value between 0 and RAND_MAX (0 and RAND_MAX included)."
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVMFu2zAM_RrlQtSw5TmJDz4kTQLs0KHoBmw7DbJM21plKZPkpO3Xj4qTdSuKbdhlge1IJEU-PpKqbfNYfeyVRjiKIHtlOthhYx3cqvvRQxD6HqyB2gkje43ew97ZzolhiKatswOwYi33e2kNT3nGig3jyz6EvWf5ivEdPcfjMXm0YxhrTKQdoiTGYvnuwPJNd_Xxtv98t919vWe8hLeMLw4IGoUz2EDoRQCDoUcHUguKaQhcJyXYfVCDekIyQWit1vYYIeGDGPYENGHphqWrLIHtJIk4O9vUVofXQJ5ViXUd7Z7oLfpvD0u7NLQuJ2dsnk4P5XuW8FwZqceG3OfX0odGq5rl27P29FUmwCCUoZDPnhbraQH0cxhGZ4AORzT5irhuJmOIrvINpCxfXw5uXmD5ORT_92w_FNn222Lubv9rttcx27vVu82Xm9Wnv0x6bUMPto194BGk8OjB93bUDdQIe-u9qomQYOP20jVN3HtFVD2-cC38MEkGe4h4AcUD49eQTVKC_zssKMVIKIQ_j4XDFh0aia9VAU3ys8llOOiNlPOdGQd0StIqknRSTmyVlBYR7YGm6lcmgXE-MexBwN7j2Nir6TTQCaTR1XAQekRiIxwRDbAtZ8uULdfpjxVET5cyQKzOn2zOfRGxJQRh1lR5U-almAUVNFY3ynvi_My-CIrulJYGeUJC-DqMJXylKWaj09WL5lWhH-szW1ofLn9XdDV9RRloS-FG9HGI8zcFn_VVVtdp0wpRy2whynmLBfKyaPJCLrGUvJ1pUaP2FVWN4Bs8wskFralwM1XR3cbTjC-yJU9TniwyLNt8ORdp2hZSlOxNitT1Ook44lTNXHWCVI-dJ6VWPvhnpaCe7AziKRz5F2PoratWN-95NjsFrk7AvwNXarDy">