Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-01-20 15:11:26


I've done some tests with gcc, which has an option to show the time
taken by each subprocess in the compilation sequence. Results and test
program are below. The file log2_impl.hpp contained either the trivial
implementation I've posted before, or Vesa's one, or a simple #include
"boost/static_log2.hpp". In the case of Vesa's implementation I
avoided inclusion of <climits> by simple replacing

    sizeof(unsigned long) * CHAR_BIT / 2

with the literal 16.

Results are:

- trivial

$ g++ -I"...boost_1_29_0" -time log.cpp
# cc1plus 0.49 0.61
# as 0.04 0.09
# collect2 0.36 1.24

-vesa

$ g++ -I"...boost_1_29_0" -time log.cpp
# cc1plus 0.71 0.71
# as 0.05 0.07
# collect2 0.37 1.43

- boost::static_log2

$ g++ -I"...boost_1_29_0" -time log.cpp
# cc1plus 3.14 0.91
# as 0.05 0.07
# collect2 0.41 1.29

------ Test program

#include "log2_impl.hpp"
#include "boost/static_assert.hpp"

//#define log2 boost::static_log2

#define BOOST_INT_CONST_LOG2_TEST(val, result) \
           BOOST_STATIC_ASSERT( log2<val>::value == result)

#ifdef BOOST_INT_CONST_LOG2_TEST_FOR_ZERO
  enum { dummy = log2<0>::value }; // should give an error
#endif

int main()
{

    // Test on 2**n and 2**n - 1

    BOOST_INT_CONST_LOG2_TEST(1, 0);
    BOOST_INT_CONST_LOG2_TEST(2, 1);

    BOOST_INT_CONST_LOG2_TEST(3, 1);
    BOOST_INT_CONST_LOG2_TEST(4, 2);

    BOOST_INT_CONST_LOG2_TEST(7, 2);
    BOOST_INT_CONST_LOG2_TEST(8, 3);

    BOOST_INT_CONST_LOG2_TEST(15, 3);
    BOOST_INT_CONST_LOG2_TEST(16, 4);

    BOOST_INT_CONST_LOG2_TEST(31, 4);
    BOOST_INT_CONST_LOG2_TEST(32, 5);

    BOOST_INT_CONST_LOG2_TEST(63, 5);
    BOOST_INT_CONST_LOG2_TEST(64, 6);

    BOOST_INT_CONST_LOG2_TEST(127, 6);
    BOOST_INT_CONST_LOG2_TEST(128, 7);

    BOOST_INT_CONST_LOG2_TEST(255, 7);
    BOOST_INT_CONST_LOG2_TEST(256, 8);

    BOOST_INT_CONST_LOG2_TEST(511, 8);
    BOOST_INT_CONST_LOG2_TEST(512, 9);

    BOOST_INT_CONST_LOG2_TEST(1023, 9);
    BOOST_INT_CONST_LOG2_TEST(1024, 10);

    BOOST_INT_CONST_LOG2_TEST(2047, 10);
    BOOST_INT_CONST_LOG2_TEST(2048, 11);

    BOOST_INT_CONST_LOG2_TEST(4095, 11);
    BOOST_INT_CONST_LOG2_TEST(4096, 12);

    BOOST_INT_CONST_LOG2_TEST(8191, 12);
    BOOST_INT_CONST_LOG2_TEST(8192, 13);

    BOOST_INT_CONST_LOG2_TEST(16383, 13);
    BOOST_INT_CONST_LOG2_TEST(16384, 14);

    BOOST_INT_CONST_LOG2_TEST(32767, 14);
    BOOST_INT_CONST_LOG2_TEST(32768, 15);

    BOOST_INT_CONST_LOG2_TEST(65535, 15);
    BOOST_INT_CONST_LOG2_TEST(65536, 16);

    BOOST_INT_CONST_LOG2_TEST(131071, 16);
    BOOST_INT_CONST_LOG2_TEST(131072, 17);

    BOOST_INT_CONST_LOG2_TEST(262143, 17);
    BOOST_INT_CONST_LOG2_TEST(262144, 18);

    BOOST_INT_CONST_LOG2_TEST(524287, 18);
    BOOST_INT_CONST_LOG2_TEST(524288, 19);

    BOOST_INT_CONST_LOG2_TEST(1048575, 19);
    BOOST_INT_CONST_LOG2_TEST(1048576, 20);

    BOOST_INT_CONST_LOG2_TEST(2097151, 20);
    BOOST_INT_CONST_LOG2_TEST(2097152, 21);

    BOOST_INT_CONST_LOG2_TEST(4194303, 21);
    BOOST_INT_CONST_LOG2_TEST(4194304, 22);

    BOOST_INT_CONST_LOG2_TEST(8388607, 22);
    BOOST_INT_CONST_LOG2_TEST(8388608, 23);

    BOOST_INT_CONST_LOG2_TEST(16777215, 23);
    BOOST_INT_CONST_LOG2_TEST(16777216, 24);

    BOOST_INT_CONST_LOG2_TEST(33554431, 24);
    BOOST_INT_CONST_LOG2_TEST(33554432, 25);

    BOOST_INT_CONST_LOG2_TEST(67108863, 25);
    BOOST_INT_CONST_LOG2_TEST(67108864, 26);

    BOOST_INT_CONST_LOG2_TEST(134217727, 26);
    BOOST_INT_CONST_LOG2_TEST(134217728, 27);

    BOOST_INT_CONST_LOG2_TEST(268435455, 27);
    BOOST_INT_CONST_LOG2_TEST(268435456, 28);

    BOOST_INT_CONST_LOG2_TEST(536870911, 28);
    BOOST_INT_CONST_LOG2_TEST(536870912, 29);

    BOOST_INT_CONST_LOG2_TEST(1073741823, 29);
    BOOST_INT_CONST_LOG2_TEST(1073741824, 30);

    BOOST_INT_CONST_LOG2_TEST(2147483647, 30);
    BOOST_INT_CONST_LOG2_TEST(2147483648u, 31);

    BOOST_INT_CONST_LOG2_TEST(4294967295u, 31);

    // Ok, now test again

    BOOST_INT_CONST_LOG2_TEST(1, 0);
    BOOST_INT_CONST_LOG2_TEST(2, 1);

    BOOST_INT_CONST_LOG2_TEST(3, 1);
    BOOST_INT_CONST_LOG2_TEST(4, 2);

    BOOST_INT_CONST_LOG2_TEST(7, 2);
    BOOST_INT_CONST_LOG2_TEST(8, 3);

    BOOST_INT_CONST_LOG2_TEST(15, 3);
    BOOST_INT_CONST_LOG2_TEST(16, 4);

    BOOST_INT_CONST_LOG2_TEST(31, 4);
    BOOST_INT_CONST_LOG2_TEST(32, 5);

    BOOST_INT_CONST_LOG2_TEST(63, 5);
    BOOST_INT_CONST_LOG2_TEST(64, 6);

    BOOST_INT_CONST_LOG2_TEST(127, 6);
    BOOST_INT_CONST_LOG2_TEST(128, 7);

    BOOST_INT_CONST_LOG2_TEST(255, 7);
    BOOST_INT_CONST_LOG2_TEST(256, 8);

    BOOST_INT_CONST_LOG2_TEST(511, 8);
    BOOST_INT_CONST_LOG2_TEST(512, 9);

    BOOST_INT_CONST_LOG2_TEST(1023, 9);
    BOOST_INT_CONST_LOG2_TEST(1024, 10);

    BOOST_INT_CONST_LOG2_TEST(2047, 10);
    BOOST_INT_CONST_LOG2_TEST(2048, 11);

    BOOST_INT_CONST_LOG2_TEST(4095, 11);
    BOOST_INT_CONST_LOG2_TEST(4096, 12);

    BOOST_INT_CONST_LOG2_TEST(8191, 12);
    BOOST_INT_CONST_LOG2_TEST(8192, 13);

    BOOST_INT_CONST_LOG2_TEST(16383, 13);
    BOOST_INT_CONST_LOG2_TEST(16384, 14);

    BOOST_INT_CONST_LOG2_TEST(32767, 14);
    BOOST_INT_CONST_LOG2_TEST(32768, 15);

    BOOST_INT_CONST_LOG2_TEST(65535, 15);
    BOOST_INT_CONST_LOG2_TEST(65536, 16);

    BOOST_INT_CONST_LOG2_TEST(131071, 16);
    BOOST_INT_CONST_LOG2_TEST(131072, 17);

    BOOST_INT_CONST_LOG2_TEST(262143, 17);
    BOOST_INT_CONST_LOG2_TEST(262144, 18);

    BOOST_INT_CONST_LOG2_TEST(524287, 18);
    BOOST_INT_CONST_LOG2_TEST(524288, 19);

    BOOST_INT_CONST_LOG2_TEST(1048575, 19);
    BOOST_INT_CONST_LOG2_TEST(1048576, 20);

    BOOST_INT_CONST_LOG2_TEST(2097151, 20);
    BOOST_INT_CONST_LOG2_TEST(2097152, 21);

    BOOST_INT_CONST_LOG2_TEST(4194303, 21);
    BOOST_INT_CONST_LOG2_TEST(4194304, 22);

    BOOST_INT_CONST_LOG2_TEST(8388607, 22);
    BOOST_INT_CONST_LOG2_TEST(8388608, 23);

    BOOST_INT_CONST_LOG2_TEST(16777215, 23);
    BOOST_INT_CONST_LOG2_TEST(16777216, 24);

    BOOST_INT_CONST_LOG2_TEST(33554431, 24);
    BOOST_INT_CONST_LOG2_TEST(33554432, 25);

    BOOST_INT_CONST_LOG2_TEST(67108863, 25);
    BOOST_INT_CONST_LOG2_TEST(67108864, 26);

    BOOST_INT_CONST_LOG2_TEST(134217727, 26);
    BOOST_INT_CONST_LOG2_TEST(134217728, 27);

    BOOST_INT_CONST_LOG2_TEST(268435455, 27);
    BOOST_INT_CONST_LOG2_TEST(268435456, 28);

    BOOST_INT_CONST_LOG2_TEST(536870911, 28);
    BOOST_INT_CONST_LOG2_TEST(536870912, 29);

    BOOST_INT_CONST_LOG2_TEST(1073741823, 29);
    BOOST_INT_CONST_LOG2_TEST(1073741824, 30);

    BOOST_INT_CONST_LOG2_TEST(2147483647, 30);
    BOOST_INT_CONST_LOG2_TEST(2147483648u, 31);

    BOOST_INT_CONST_LOG2_TEST(4294967295u, 31);
        
}
---------------------

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk