<html>
<head>
<base href="http://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 --- - unexpected behavior using result of __lzcnt64() on x86_64"
href="http://llvm.org/bugs/show_bug.cgi?id=20992">20992</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>unexpected behavior using result of __lzcnt64() on x86_64
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.4
</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>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>klberger@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>Created <span class=""><a href="attachment.cgi?id=13050" name="attach_13050" title="test case compile with: clang -Wall -pedantic -O2 -mlzcnt -o lzcnt lzcntm.c">attachment 13050</a> <a href="attachment.cgi?id=13050&action=edit" title="test case compile with: clang -Wall -pedantic -O2 -mlzcnt -o lzcnt lzcntm.c">[details]</a></span>
test case compile with: clang -Wall -pedantic -O2 -mlzcnt -o lzcnt lzcntm.c
Given the following function definition
--
#include <stdint.h>
#include <x86intrin.h>
/*
* return position [0..63] of highest bit set or -1 if val equals zero
*/
int highest_bit_set(uint64_t val)
{
return 63 - __lzcnt64(val);
}
--
If called with val==0, the function returns 127 instead of -1.
Compiled on an x86_64 platform with
clang -Wall -pedantic -O2 -mlzcnt -S lzcnt.c
the compiler generates
-- clang 3.4 --
highest_bit_set: # @highest_bit_set
.cfi_startproc
# BB#0:
lzcntq %rdi, %rax
xorl $63, %eax
# kill: EAX<def> EAX<kill> RAX<kill>
ret
--
whereas gcc generates the expected
-- gcc 4.8.3 --
highest_bit_set:
.LFB551:
.cfi_startproc
lzcntq %rdi, %rdi
movl $63, %eax
subl %edi, %eax
ret
--
Disabling optimization yields the expected result. It seems the compiler
assumes __lzcnt64() will return only values [0..63] and replaces the 63-x by
xor $63,x.
The instruction lzcnt is specified to return the operand width in bits (64) if
the source operand is 0.
I am aware that lzcnt is a "problematic" instruction because it is executed as
bsr on processors with no native lzcnt support and the result of bsr is
undefined for a zero operand. For a cpu with native lzcnt support (-mlzcnt or
implicit via -march) i'd expected the compiler to support the full range of
values returned by lzcnt.
Related intrinsics/built-ins that (may) show the same problem: __tzcnt64(),
__builtin_clzl() ...
I first used __builtin_clzl() which translates into the same code, but which is
explicitly declared to return an undefined result (gcc docs) if called with a
zero argument.</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>