Computer Chess Club Archives


Search

Terms

Messages

Subject: curiosity killed the cat... hi/lo bit C verses Assembly

Author: Dann Corbit

Date: 10:42:07 07/17/03


The assembly versions will surely be faster in a test harness.
But when you poke them into actual code and turn on the maximal optimizer
settings, do the assembly high and low bit functions actually beat this C code?

I ask because it seems that the optimizer is somehow smarter when dealing with C
with some experiments that I have run.

What happens when you try it?

#ifdef _MSC_VER
typedef unsigned __int64 Bitboard;
#else
typedef unsigned long long Bitboard;
#endif

/* Returns top bit of x, or 0 if x = 0. */
/* This algorithm is due to Robert Harley */
Bitboard   hi_bit(Bitboard x)
{
    x |= x >> 1;
    x |= x >> 2;
    x |= x >> 4;
    x |= x >> 8;
    x |= x >> 16;
    x |= x >> 32;
    return x ^ x >> 1;
}                               /* end function top_bit */


//
// Hard to say which of these will be faster on your system.
//

Bitboard   lo_bit0(Bitboard n)
{
    return ((n & (n - 1)) ^ n);
}

Bitboard   lo_bit1(Bitboard n)
{
    return n & -n;
}



This page took 0.01 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.