[cfe-commits] [PATCH] [libcxx] Fix a tautologically false case for 32-bit builds

Howard Hinnant hhinnant at apple.com
Thu Dec 27 11:01:08 PST 2012


Thanks, this is much better.  Though I actually had to add a pragma to silence the warning.

Committed revision 171167.

Howard

On Dec 25, 2012, at 11:55 AM, Saleem Abdulrasool <compnerd at compnerd.org> wrote:

> When building for a system that has a 32-bit size_t, the sizeof(size_t) is 4,
> which means that the the overflow for the 64-bit version is always false.  This
> uses a template specialisation on the sizeof size_t to determine which
> specialisation to use on the build target.  Since building for a given target
> will always have a single size of size_t, a single version is instantiated now,
> and the integral_constant<size_t, sizeof(size_t) * CHAR_BIT> is no longer needed
> to differentiate the invocation.  Furthermore, because the 8 byte version is no
> longer instantiated on a 32-bit build, the tautological case no longer exists.
> 
> This was pointed out by building with GCC (4.7.2).
> 
> http://llvm-reviews.chandlerc.com/D243
> 
> Files:
>  src/hash.cpp
> 
> Index: src/hash.cpp
> ===================================================================
> --- src/hash.cpp
> +++ src/hash.cpp
> @@ -10,6 +10,7 @@
> #include "__hash_table"
> #include "algorithm"
> #include "stdexcept"
> +#include "type_traits"
> 
> _LIBCPP_BEGIN_NAMESPACE_STD
> 
> @@ -144,21 +145,21 @@
> // are fewer potential primes to search, and fewer potential primes to divide
> // against.
> 
> -inline _LIBCPP_INLINE_VISIBILITY
> -void
> -__check_for_overflow(size_t N, integral_constant<size_t, 32>)
> +template <size_t _Sz = sizeof(size_t)>
> +inline _LIBCPP_INLINE_VISIBILITY typename enable_if<_Sz == 4, void>::type
> +__check_for_overflow(size_t N)
> {
> -#ifndef _LIBCPP_NO_EXCEPTIONS 
> +#ifndef _LIBCPP_NO_EXCEPTIONS
>     if (N > 0xFFFFFFFB)
>         throw overflow_error("__next_prime overflow");
> #endif
> }
> 
> -inline _LIBCPP_INLINE_VISIBILITY
> -void
> -__check_for_overflow(size_t N, integral_constant<size_t, 64>)
> +template <size_t _Sz = sizeof(size_t)>
> +inline _LIBCPP_INLINE_VISIBILITY typename enable_if<_Sz == 8, void>::type
> +__check_for_overflow(size_t N)
> {
> -#ifndef _LIBCPP_NO_EXCEPTIONS 
> +#ifndef _LIBCPP_NO_EXCEPTIONS
>     if (N > 0xFFFFFFFFFFFFFFC5ull)
>         throw overflow_error("__next_prime overflow");
> #endif
> @@ -174,8 +175,7 @@
>         return *std::lower_bound(small_primes, small_primes + N, n);
>     // Else n > largest small_primes
>     // Check for overflow
> -    __check_for_overflow(n, integral_constant<size_t, 
> -                                                   sizeof(n) * __CHAR_BIT__>());
> +    __check_for_overflow(n);
>     // Start searching list of potential primes: L * k0 + indices[in]
>     const size_t M = sizeof(indices) / sizeof(indices[0]);
>     // Select first potential prime >= n
> <D243.1.patch>_______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list