From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 4E0FB3850872; Wed, 8 Jun 2022 15:52:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E0FB3850872 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-8464] libstdc++: Fix narrowing conversions for 16-bit size_t [PR105681] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 64f5d58d8151324be111ba6e13c50b4b642c41c2 X-Git-Newrev: 6666ca1ab44ad8a61e2b916cf4173fe452b78ed6 Message-Id: <20220608155258.4E0FB3850872@sourceware.org> Date: Wed, 8 Jun 2022 15:52:58 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 15:52:58 -0000 https://gcc.gnu.org/g:6666ca1ab44ad8a61e2b916cf4173fe452b78ed6 commit r12-8464-g6666ca1ab44ad8a61e2b916cf4173fe452b78ed6 Author: Jonathan Wakely Date: Thu May 26 21:32:55 2022 +0100 libstdc++: Fix narrowing conversions for 16-bit size_t [PR105681] On a 16-bit target such as msp430 we get errors about narrowing long values to size_t, which is only 16-bit. When --enable-libstdcxx-pch is used the header breaks the build because of these narrowing errors. libstdc++-v3/ChangeLog: PR libstdc++/105681 * include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp: Limit ga_sizes array to values that fit in size_t. * include/ext/random [__SIZE_WIDTH < 32] (sfmt86243) (sfmt86243_64, sfmt132049, sfmt132049_64, sfmt216091) (sfmt216091_64): Do not declare. (cherry picked from commit 367740bf6d3a6627798b3955e5d85efc7549ef50) Diff: --- .../resize_policy/hash_prime_size_policy_imp.hpp | 18 +++++++++++++++--- libstdc++-v3/include/ext/random | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp b/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp index d53392c8211..43d72768206 100644 --- a/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp +++ b/libstdc++-v3/include/ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp @@ -46,15 +46,23 @@ namespace detail { enum { + num_distinct_sizes_16_bit = 14, num_distinct_sizes_32_bit = 30, num_distinct_sizes_64_bit = 62, - num_distinct_sizes = sizeof(std::size_t) != 8 ? - num_distinct_sizes_32_bit : num_distinct_sizes_64_bit, + // The number of values is limited by the width of size_t. + // Maybe we could just use (__SIZE_WIDTH__ - 2) here. +#if __SIZE_WIDTH__ >= 64 + num_distinct_sizes = num_distinct_sizes_64_bit +#elif __SIZE_WIDTH__ >= 32 + num_distinct_sizes = num_distinct_sizes_32_bit +#else + num_distinct_sizes = num_distinct_sizes_16_bit +#endif }; // Originally taken from the SGI implementation; acknowledged in the docs. // Further modified (for 64 bits) from tr1's hashtable. - static const std::size_t g_a_sizes[num_distinct_sizes_64_bit] = + static const std::size_t g_a_sizes[num_distinct_sizes] = { /* 0 */ 5ul, /* 1 */ 11ul, @@ -70,6 +78,7 @@ namespace detail /* 11 */ 14033ul, /* 12 */ 28411ul, /* 13 */ 57557ul, +#if __SIZE_WIDTH__ >= 32 /* 14 */ 116731ul, /* 15 */ 236897ul, /* 16 */ 480881ul, @@ -86,6 +95,7 @@ namespace detail /* 27 */ 1164186217ul, /* 28 */ 2364114217ul, /* 29 */ 4294967291ul, +#if __SIZE_WIDTH__ >= 64 /* 30 */ (std::size_t)8589934583ull, /* 31 */ (std::size_t)17179869143ull, /* 32 */ (std::size_t)34359738337ull, @@ -118,6 +128,8 @@ namespace detail /* 59 */ (std::size_t)4611686018427387847ull, /* 60 */ (std::size_t)9223372036854775783ull, /* 61 */ (std::size_t)18446744073709551557ull, +#endif +#endif }; } // namespace detail diff --git a/libstdc++-v3/include/ext/random b/libstdc++-v3/include/ext/random index b7155767b02..50505b876cc 100644 --- a/libstdc++-v3/include/ext/random +++ b/libstdc++-v3/include/ext/random @@ -346,6 +346,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 0xa3ac4000U, 0xecc1327aU> sfmt44497_64; +#if __SIZE_WIDTH__ >= 32 typedef simd_fast_mersenne_twister_engine sfmt216091_64; +#endif // __SIZE_WIDTH__ >= 32 #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__