From 1fcd0a2c503b9fd16cf52df248c92c387d0086f9 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Thu, 25 Oct 2007 10:11:58 +0000 Subject: [PATCH] stl_algo.h (__lg<>(_Size)): Slightly tweak. 2007-10-25 Paolo Carlini * include/bits/stl_algo.h (__lg<>(_Size)): Slightly tweak. (__lg(int), __lg(long), __lg(long long)): Add, overloads exploiting __builtin_clz*. From-SVN: r129624 --- libstdc++-v3/ChangeLog | 6 ++++++ libstdc++-v3/include/bits/stl_algo.h | 18 +++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 3616d367d6a..fabbe8ace7a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,9 @@ +2007-10-25 Paolo Carlini + + * include/bits/stl_algo.h (__lg<>(_Size)): Slightly tweak. + (__lg(int), __lg(long), __lg(long long)): Add, overloads + exploiting __builtin_clz*. + 2007-10-24 Paolo Carlini * include/tr1_impl/array (_M_instance): Align naturally. diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index dd6d7f338c4..d254ec6dc6e 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -2085,7 +2085,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std) /** * @if maint - * This is a helper function for the sort routines. + * This is a helper function for the sort routines. Precondition: __n > 0. * @endif */ template @@ -2093,11 +2093,23 @@ _GLIBCXX_BEGIN_NAMESPACE(std) __lg(_Size __n) { _Size __k; - for (__k = 0; __n != 1; __n >>= 1) + for (__k = 0; __n != 0; __n >>= 1) ++__k; - return __k; + return __k - 1; } + inline int + __lg(int __n) + { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); } + + inline long + __lg(long __n) + { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); } + + inline long long + __lg(long long __n) + { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); } + // sort template -- 2.30.2