<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 --- - a==-b and -a==b runs at different speed"
href="https://llvm.org/bugs/show_bug.cgi?id=23016">23016</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>a==-b and -a==b runs at different speed
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</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>LLVM Codegen
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>191919@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>The following C code is a 8-queen puzzler solver for educational purpose.
#include <stdio.h>
#include <stdlib.h>
static inline int validate(const int* a, const int d)
{
for (int i = 0; i < d; ++i)
{
for (int j = i + 1, x = 1; j < d; ++j, ++x)
{
const int p = a[i] - a[j];
///////////// !!!!!!!!!!
if (p == 0 || p == x || p == -x) return 0;
}
}
return 1;
}
static inline int solve(int d)
{
int r = 0;
int* a = (int*) calloc(sizeof(int), d+1);
int p = d - 1;
for (;;)
{
a[p]++;
if (a[p] > d-1)
{
int bp = p - 1;
while (bp >= 0)
{
a[bp]++;
if (a[bp] <= d-1) break;
a[bp] = 0;
--bp;
}
if (bp < 0)
break;
a[p] = 0;
}
if (validate(a, d))
{
++r;
}
}
free(a);
return r;
}
int main(int argc, char** argv)
{
if (argc != 2) return -1;
int r = solve((int) strtol(argv[1], NULL, 10));
printf("%d solutions\n", r);
}
In function `validate`, there is a line:
if (p == 0 || p == x || p == -x) return 0;
If I compile the source without any modifications and run it:
$ /opt/bin/clang -v
clang version 3.7.0 (trunk 233173)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
$ /opt/bin/clang -O3 -o 8 8.c
$ time ./8 9
352 solutions
./9 9 1.96s user 0.00s system 99% cpu 1.966 total
Now I change that line to:
if (p == 0 || p == x || -p == x) return 0;
then compile with the same command line and run it again:
$ /opt/bin/clang -O3 -o 8 8.c
$ time ./8 9
352 solutions
./8 9 1.84s user 0.00s system 99% cpu 1.845 total
As you see, there I got a 7% speed boost by just swapping the left and right
side of an equation.
The weirder thing is if I compile the same source code with clang-3.5 which
comes with Xcode 6.1.3 or 6.2, the result flipped:
$ clang -v
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
# original
$ clang -O3 -o 8 8.c # p == x || p == -x
$ time ./8 9
352 solutions
./8 9 1.98s user 0.00s system 99% cpu 1.982 total
$ clang -O3 -o 8 8.c # p == x || -p == x
$ time ./8 9
352 solutions
./9 9 1.66s user 0.00s system 99% cpu 1.658 total
The speed difference is nearly 17%.
I am really confused how the optimizer works and can't stop to worry if I have
to manually try to swap all the equations of my codes to get faster codes.</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>