[LLVMbugs] [Bug 24090] New: Division of signed ints incorrectly optimizes to right bitshift
bugzilla-daemon at llvm.org
bugzilla-daemon at llvm.org
Fri Jul 10 22:29:53 PDT 2015
https://llvm.org/bugs/show_bug.cgi?id=24090
Bug ID: 24090
Summary: Division of signed ints incorrectly optimizes to right
bitshift
Product: clang
Version: unspecified
Hardware: PC
OS: All
Status: NEW
Severity: normal
Priority: P
Component: -New Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: fraggle+llvm at gmail.com
CC: llvmbugs at cs.uiuc.edu
Classification: Unclassified
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
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150711/384a9575/attachment.html>
More information about the llvm-bugs
mailing list