r221064 - Avoid undefined behavior in the x86 lzcnt header file by explicitly checking for 0 before calling __builtin_clz. Without this the optimizers may take advantage of the undefined behavior and produce incorrect results. LLVM itself still needs to be taught to merge the zero check into the llvm.ctlz with defined zero behavior.

Craig Topper craig.topper at gmail.com
Sat Nov 1 15:25:23 PDT 2014


Author: ctopper
Date: Sat Nov  1 17:25:23 2014
New Revision: 221064

URL: http://llvm.org/viewvc/llvm-project?rev=221064&view=rev
Log:
Avoid undefined behavior in the x86 lzcnt header file by explicitly checking for 0 before calling __builtin_clz. Without this the optimizers may take advantage of the undefined behavior and produce incorrect results. LLVM itself still needs to be taught to merge the zero check into the llvm.ctlz with defined zero behavior.

Modified:
    cfe/trunk/lib/Headers/lzcntintrin.h

Modified: cfe/trunk/lib/Headers/lzcntintrin.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/lzcntintrin.h?rev=221064&r1=221063&r2=221064&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/lzcntintrin.h (original)
+++ cfe/trunk/lib/Headers/lzcntintrin.h Sat Nov  1 17:25:23 2014
@@ -35,20 +35,20 @@
 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
 __lzcnt16(unsigned short __X)
 {
-  return __builtin_clzs(__X);
+  return __X ? __builtin_clzs(__X) : 16;
 }
 
 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
 __lzcnt32(unsigned int __X)
 {
-  return __builtin_clz(__X);
+  return __X ? __builtin_clz(__X) : 32;
 }
 
 #ifdef __x86_64__
 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
 __lzcnt64(unsigned long long __X)
 {
-  return __builtin_clzll(__X);
+  return __X ? __builtin_clzll(__X) : 64;
 }
 #endif
 





More information about the cfe-commits mailing list