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
+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.
/**
* @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>
__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>