From: Craig Topper Date: Sat, 1 Nov 2014 22:25:23 +0000 (+0000) Subject: Avoid undefined behavior in the x86 lzcnt header file by explicitly checking for... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3ca55d9c414541026dfea39bff809fc65e94cc87;p=platform%2Fupstream%2Fllvm.git 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. llvm-svn: 221064 --- diff --git a/clang/lib/Headers/lzcntintrin.h b/clang/lib/Headers/lzcntintrin.h index 62ab5ca..5bb7435 100644 --- a/clang/lib/Headers/lzcntintrin.h +++ b/clang/lib/Headers/lzcntintrin.h @@ -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