<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 --- - Division of signed ints incorrectly optimizes to right bitshift" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D24090&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=KIInaFozd5KeI06CP8UR18kks9wJJgdLmdIUfvkSINI&s=eDXQRcwQstoiNfMEO4NmqJdrGf46BTNv6BuLoyEKHxk&e=">24090</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Division of signed ints incorrectly optimizes to right bitshift
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</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>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>fraggle+llvm@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Hi,
I've encountered what looks like a bug in Clang where dividing a 'signed int'
with a negative value by a denominator that is a power of two mistakenly gives
positive values. The following is an example that demonstrates the bug:
#include <stdio.h>
#include <stdlib.h>
#define ANG45 0x20000000
#define ANGLETOFINESHIFT 19
int main(int argc, char *argv[])
{
short angle;
signed int an;
angle = atoi("315");
an = (ANG45 * ((signed int) angle / 45));
printf("an %i -> %i\n", an, an / (1 << ANGLETOFINESHIFT));
return 0;
}
This program works correctly when compiled with -O0, giving this output:
an -536870912 -> -1024
but when compiled with -O2, it gives this incorrect result:
an -536870912 -> 7168
I don't know much about Clang's internals, but it's common to optimize a
division by a constant power of two into a right bitshift. If the printf line
looked like this:
printf("an %i -> %i\n", an, an >> ANGLETOFINESHIFT);
Then the right bitshift of a negative value would be implementation-defined
behavior, and this would be a valid result. But the code is dividing, not
shifting.
Clang version:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix</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>