From a52e0d7cc03306d3a89d891c1c5f4d6fee2cac1f Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Sat, 1 Nov 2014 22:50:54 +0000 Subject: [PATCH] Avoid undefined behavior in the x86 bmi header file by explicitly checking for 0 before calling __builtin_ctz. 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.cttz with defined zero behavior. llvm-svn: 221065 --- clang/lib/Headers/bmiintrin.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Headers/bmiintrin.h b/clang/lib/Headers/bmiintrin.h index 43c4a5e..0e5fd55 100644 --- a/clang/lib/Headers/bmiintrin.h +++ b/clang/lib/Headers/bmiintrin.h @@ -43,7 +43,7 @@ static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__)) __tzcnt_u16(unsigned short __X) { - return __builtin_ctzs(__X); + return __X ? __builtin_ctzs(__X) : 16; } static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) @@ -87,7 +87,7 @@ __blsr_u32(unsigned int __X) static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__)) __tzcnt_u32(unsigned int __X) { - return __builtin_ctz(__X); + return __X ? __builtin_ctz(__X) : 32; } #ifdef __x86_64__ @@ -140,7 +140,7 @@ __blsr_u64(unsigned long long __X) static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__)) __tzcnt_u64(unsigned long long __X) { - return __builtin_ctzll(__X); + return __X ? __builtin_ctzll(__X) : 64; } #endif /* __x86_64__ */ -- 2.7.4