From 80f8cfb37fbe8081f4cae591ca8189126f0fcba9 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 10 May 2017 21:30:04 +0000 Subject: [PATCH] Don't interfere with the __builtin_foo namespace under MSVC This patch follows up on feedback received in the review for D32988. Specifically that libc++ should not mess with the __builtin namespace, and that libc++ should use __popcnt to implement __pop_count under MSVC. llvm-svn: 302731 --- libcxx/include/algorithm | 125 ++++++++++++++--- .../include/support/win32/msvc_builtin_support.h | 156 --------------------- 2 files changed, 103 insertions(+), 178 deletions(-) delete mode 100644 libcxx/include/support/win32/msvc_builtin_support.h diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 896d58e..08ca23f 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -645,7 +645,7 @@ template #include "support/ibm/support.h" #endif #if defined(_LIBCPP_COMPILER_MSVC) -#include "support/win32/msvc_builtin_support.h" +#include #endif #include <__undef_min_max> @@ -783,51 +783,132 @@ struct __debug_less // Precondition: __x != 0 inline _LIBCPP_INLINE_VISIBILITY -unsigned -__ctz(unsigned __x) -{ +unsigned __ctz(unsigned __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_ctz(__x)); +#else + static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); + static_assert(sizeof(unsigned long) == 4, ""); + unsigned long where; + // Search from LSB to MSB for first set bit. + // Returns zero if no set bit is found. + if (_BitScanForward(&where, mask)) + return where; + return 32; +#endif } inline _LIBCPP_INLINE_VISIBILITY -unsigned long -__ctz(unsigned long __x) -{ +unsigned long __ctz(unsigned long __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_ctzl(__x)); +#else + static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); + return __ctz(static_cast(__x)); +#endif } inline _LIBCPP_INLINE_VISIBILITY -unsigned long long -__ctz(unsigned long long __x) -{ +unsigned long long __ctz(unsigned long long __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_ctzll(__x)); +#else + unsigned long where; +// Search from LSB to MSB for first set bit. +// Returns zero if no set bit is found. +#if defined(_LIBCPP_HAS_BITSCAN64) + (defined(_M_AMD64) || defined(__x86_64__)) + if (_BitScanForward64(&where, mask)) + return static_cast(where); +#else + // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. + // Scan the Low Word. + if (_BitScanForward(&where, static_cast(mask))) + return where; + // Scan the High Word. + if (_BitScanForward(&where, static_cast(mask >> 32))) + return where + 32; // Create a bit offset from the LSB. +#endif + return 64; +#endif // _LIBCPP_COMPILER_MSVC } // Precondition: __x != 0 inline _LIBCPP_INLINE_VISIBILITY -unsigned -__clz(unsigned __x) -{ +unsigned __clz(unsigned __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_clz(__x)); +#else + static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); + static_assert(sizeof(unsigned long) == 4, ""); + unsigned long where; + // Search from LSB to MSB for first set bit. + // Returns zero if no set bit is found. + if (_BitScanReverse(&where, mask)) + return 31 - where; + return 32; // Undefined Behavior. +#endif } inline _LIBCPP_INLINE_VISIBILITY -unsigned long -__clz(unsigned long __x) -{ +unsigned long __clz(unsigned long __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_clzl (__x)); +#else + static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); + return __clz(static_cast(__x)); +#endif } inline _LIBCPP_INLINE_VISIBILITY -unsigned long long -__clz(unsigned long long __x) -{ +unsigned long long __clz(unsigned long long __x) { +#ifndef _LIBCPP_COMPILER_MSVC return static_cast(__builtin_clzll(__x)); +#else + unsigned long where; +// BitScanReverse scans from MSB to LSB for first set bit. +// Returns 0 if no set bit is found. +#if defined(_LIBCPP_HAS_BITSCAN64) + if (_BitScanReverse64(&where, mask)) + return static_cast(63 - where); +#else + // Scan the high 32 bits. + if (_BitScanReverse(&where, static_cast(mask >> 32))) + return 63 - (where + 32); // Create a bit offset from the MSB. + // Scan the low 32 bits. + if (_BitScanReverse(&where, static_cast(mask))) + return 63 - where; +#endif + return 64; // Undefined Behavior. +#endif // _LIBCPP_COMPILER_MSVC +} + +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) { +#ifndef _LIBCPP_COMPILER_MSVC + return __builtin_popcount (__x); +#else + static_assert(sizeof(unsigned) == 4, ""); + return __popcnt(__x); +#endif +} + +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) { +#ifndef _LIBCPP_COMPILER_MSVC + return __builtin_popcountl (__x); +#else + static_assert(sizeof(unsigned long) == 4, ""); + return __popcnt(__x); +#endif } -inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) {return __builtin_popcount (__x);} -inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) {return __builtin_popcountl (__x);} -inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) {return __builtin_popcountll(__x);} +inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) { +#ifndef _LIBCPP_COMPILER_MSVC + return __builtin_popcountll(__x); +#else + static_assert(sizeof(unsigned long long) == 8, ""); + return __popcnt64(__x); +#endif +} // all_of diff --git a/libcxx/include/support/win32/msvc_builtin_support.h b/libcxx/include/support/win32/msvc_builtin_support.h deleted file mode 100644 index 0ef6320..0000000 --- a/libcxx/include/support/win32/msvc_builtin_support.h +++ /dev/null @@ -1,156 +0,0 @@ -// -*- C++ -*- -//===----------------------- support/win32/support.h ----------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_SUPPORT_H -#define _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_SUPPORT_H - -#ifndef _LIBCPP_COMPILER_MSVC -#error "This header can only be included when using Microsoft's C1XX frontend" -#endif - -#ifndef NOMINMAX -#define NOMINMAX -#endif - -// "builtins" not implemented here for Clang or GCC as they provide -// implementations. Assuming required for elsewhere else, certainly MSVC. -#include - -// Bit builtin's make these assumptions when calling _BitScanForward/Reverse -// etc. These assumptions are expected to be true for Win32/Win64 which this -// file supports. -static_assert(sizeof(unsigned long long) == 8, ""); -static_assert(sizeof(unsigned long) == 4, ""); -static_assert(sizeof(unsigned int) == 4, ""); - -_LIBCPP_ALWAYS_INLINE int __builtin_popcount(unsigned int x) -{ - // Binary: 0101... - static const unsigned int m1 = 0x55555555; - // Binary: 00110011.. - static const unsigned int m2 = 0x33333333; - // Binary: 4 zeros, 4 ones ... - static const unsigned int m4 = 0x0f0f0f0f; - // The sum of 256 to the power of 0,1,2,3... - static const unsigned int h01 = 0x01010101; - // Put count of each 2 bits into those 2 bits. - x -= (x >> 1) & m1; - // Put count of each 4 bits into those 4 bits. - x = (x & m2) + ((x >> 2) & m2); - // Put count of each 8 bits into those 8 bits. - x = (x + (x >> 4)) & m4; - // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24). - return (x * h01) >> 24; -} - -_LIBCPP_ALWAYS_INLINE int __builtin_popcountl(unsigned long x) -{ - return __builtin_popcount(static_cast(x)); -} - -_LIBCPP_ALWAYS_INLINE int __builtin_popcountll(unsigned long long x) -{ - // Binary: 0101... - static const unsigned long long m1 = 0x5555555555555555; - // Binary: 00110011.. - static const unsigned long long m2 = 0x3333333333333333; - // Binary: 4 zeros, 4 ones ... - static const unsigned long long m4 = 0x0f0f0f0f0f0f0f0f; - // The sum of 256 to the power of 0,1,2,3... - static const unsigned long long h01 = 0x0101010101010101; - // Put count of each 2 bits into those 2 bits. - x -= (x >> 1) & m1; - // Put count of each 4 bits into those 4 bits. - x = (x & m2) + ((x >> 2) & m2); - // Put count of each 8 bits into those 8 bits. - x = (x + (x >> 4)) & m4; - // Returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... - return static_cast((x * h01) >> 56); -} - -// Returns the number of trailing 0-bits in x, starting at the least significant -// bit position. If x is 0, the result is undefined. -_LIBCPP_ALWAYS_INLINE int __builtin_ctzll(unsigned long long mask) -{ - unsigned long where; -// Search from LSB to MSB for first set bit. -// Returns zero if no set bit is found. -#if defined(_LIBCPP_HAS_BITSCAN64) - (defined(_M_AMD64) || defined(__x86_64__)) - if (_BitScanForward64(&where, mask)) - return static_cast(where); -#else - // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. - // Scan the Low Word. - if (_BitScanForward(&where, static_cast(mask))) - return static_cast(where); - // Scan the High Word. - if (_BitScanForward(&where, static_cast(mask >> 32))) - return static_cast(where + 32); // Create a bit offset from the LSB. -#endif - return 64; -} - -_LIBCPP_ALWAYS_INLINE int __builtin_ctzl(unsigned long mask) -{ - unsigned long where; - // Search from LSB to MSB for first set bit. - // Returns zero if no set bit is found. - if (_BitScanForward(&where, mask)) - return static_cast(where); - return 32; -} - -_LIBCPP_ALWAYS_INLINE int __builtin_ctz(unsigned int mask) -{ - // Win32 and Win64 expectations. - static_assert(sizeof(mask) == 4, ""); - static_assert(sizeof(unsigned long) == 4, ""); - return __builtin_ctzl(static_cast(mask)); -} - -// Returns the number of leading 0-bits in x, starting at the most significant -// bit position. If x is 0, the result is undefined. -_LIBCPP_ALWAYS_INLINE int __builtin_clzll(unsigned long long mask) -{ - unsigned long where; -// BitScanReverse scans from MSB to LSB for first set bit. -// Returns 0 if no set bit is found. -#if defined(_LIBCPP_HAS_BITSCAN64) - if (_BitScanReverse64(&where, mask)) - return static_cast(63 - where); -#else - // Scan the high 32 bits. - if (_BitScanReverse(&where, static_cast(mask >> 32))) - return static_cast(63 - - (where + 32)); // Create a bit offset from the MSB. - // Scan the low 32 bits. - if (_BitScanReverse(&where, static_cast(mask))) - return static_cast(63 - where); -#endif - return 64; // Undefined Behavior. -} - -_LIBCPP_ALWAYS_INLINE int __builtin_clzl(unsigned long mask) -{ - unsigned long where; - // Search from LSB to MSB for first set bit. - // Returns zero if no set bit is found. - if (_BitScanReverse(&where, mask)) - return static_cast(31 - where); - return 32; // Undefined Behavior. -} - -_LIBCPP_ALWAYS_INLINE int __builtin_clz(unsigned int x) -{ - return __builtin_clzl(x); -} - -#endif // _LIBCPP_SUPPORT_WIN32_MSVC_BUILTIN_SUPPORT_H -- 2.7.4