From 3ca55d9c414541026dfea39bff809fc65e94cc87 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Sat, 1 Nov 2014 22:25:23 +0000 Subject: [PATCH] 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 --- clang/lib/Headers/lzcntintrin.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 -- 2.7.4