<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - Integer divide by constant close to UINT*_MAX can be compare rather than divide"
href="https://llvm.org/bugs/show_bug.cgi?id=28672">28672</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Integer divide by constant close to UINT*_MAX can be compare rather than divide
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>simon.hosie@arm.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>When doing unsigned division by an integer which is greater than `UINT_MAX / 2`
(or another range limit in place of UINT_MAX, as appropriate), the result can
be only 0 or 1, and this can be evaluated with a compare rather than a
reciprocal-multiply (or divide).
For example:
uint32_t f(uint32_t x) { return (x + 1) % 4000569407; }
is equivalent to:
uint32_t f(uint32_t x) { x++; return x < 4000569407 ? x : x - 4000569407; }
Clang gives me:
clang --target=arm-linux-gnueabihf -O3 -S -o- p1modk.c
ldr r1, .LCPI0_0 // 316062335
add r0, r0, #1
umull r1, r2, r0, r1
sub r1, r0, r2
add r1, r2, r1, lsr #1
ldr r2, .LCPI0_1 // 4000569407
lsr r1, r1, #31
mul r1, r1, r2
sub r0, r0, r1
I expect something more like:
ldr r1, =4000569407
add r0, r0, #1
cmp r0, r1
subhs r0, r0, r1
Note that this applicable in loops containing the `x = (x + 1) % k;` idiom if x
can be shown to be never greater than k (even if k is variable, I think).</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>