0

I have long wondered why boost is not using built-in functions. Even if we are concerned with purity, I would expect it to be an option at least.

For example, the lowest_bit calculation from

https://www.boost.org/doc/libs/1_70_0/boost/dynamic_bitset/detail/lowest_bit.hpp

    template <typename T>
    int lowest_bit(T x) {

        BOOST_ASSERT(x >= 1); // PRE

        // clear all bits on except the rightmost one,
        // then calculate the logarithm base 2
        //
        return boost::integer_log2<T>( x - ( x & (x-1) ) );

    }

involves a loop inside integer_log2 function, while it will be enough to call an appropriate version of __builtin_ctz() ???

Update: I found stackoverflow question with decoded __builtin_ctz Where is the source code of gcc builtins?

  xor eax, eax
  rep bsf eax, edi
  ret
uuu777
  • 765
  • 4
  • 21
  • 6
    `__builtin_ctz` does not exist on every platform that boost supports – Caleth Nov 16 '22 at 15:45
  • boost isn't a compiler it is a cross platform, cross compiler library. It is usually fairly optimized but not to the point where it wouldn't work accross a large number of configurations. – Pepijn Kramer Nov 16 '22 at 15:47
  • plus, in your example, I'd really check whether the builtin and the loop don't compile to the same machine code with a modern compiler in the highest optimization setting. – Marcus Müller Nov 16 '22 at 15:49
  • 3
    `T` is not always `unsigned int` – Caleth Nov 16 '22 at 15:49
  • 1
    I would expect it to compile in a few (if not one) machine instructions (see update to the question). I do not see matching the right version of the built-in function to T will be complicated. – uuu777 Nov 16 '22 at 16:36

0 Answers0