aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Carlini <pcarlini@suse.de>2007-10-25 10:11:58 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2007-10-25 10:11:58 +0000
commit1fcd0a2c503b9fd16cf52df248c92c387d0086f9 (patch)
tree8efb128e5310c8415d98d1187fdc6278b1416e37
parent0bf2cf8907a382fd2223ac0975516ab99283eaa8 (diff)
downloadgcc-1fcd0a2c503b9fd16cf52df248c92c387d0086f9.zip
gcc-1fcd0a2c503b9fd16cf52df248c92c387d0086f9.tar.gz
gcc-1fcd0a2c503b9fd16cf52df248c92c387d0086f9.tar.bz2
stl_algo.h (__lg<>(_Size)): Slightly tweak.
2007-10-25 Paolo Carlini <pcarlini@suse.de> * include/bits/stl_algo.h (__lg<>(_Size)): Slightly tweak. (__lg(int), __lg(long), __lg(long long)): Add, overloads exploiting __builtin_clz*. From-SVN: r129624
-rw-r--r--libstdc++-v3/ChangeLog6
-rw-r--r--libstdc++-v3/include/bits/stl_algo.h18
2 files changed, 21 insertions, 3 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index 3616d36..fabbe8a 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,9 @@
+2007-10-25 Paolo Carlini <pcarlini@suse.de>
+
+ * 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 <pcarlini@suse.de>
* 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 dd6d7f3..d254ec6 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<typename _Size>
@@ -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<typename _RandomAccessIterator, typename _Size>