Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: How to reverse a Reverse BitBoard!

Author: Dann Corbit

Date: 14:03:33 01/21/03

Go up one level in this thread


On January 21, 2003 at 15:48:57, Sander de Zoete wrote:

>The following instruction I found for the new architecture of Intel Chips
>
>BSWAP—Byte Swap
>
>Description
>Reverses the byte order of a 32-bit (destination) register:
>
>Operation
>TEMP ¬ DEST
>DEST(7..0) ¬ TEMP(31..24)
>DEST(15..8) ¬ TEMP(23..16)
>DEST(23..16) ¬ TEMP(15..8)
>DEST(31..24) ¬ TEMP(7..0)
>Flags Affected
>None.
>Opcode Instruction Description
>0F C8+ rd BSWAP r32 Reverses the byte order of a 32-bit register.
>
>It is only valid for 486 architecture.
>
>If this instruction can be used, it should be very easy to reverse Reverse
>BitBoards into Forward Boards again. Saving a lot of updating in make and unmake
>move and also be much faster using the bitboards for generating attack
>board for evaluation purposes, incheck checks etc.
>
>The thing left to do is to reverse the bits per byte.
>
>I use the following trick, but it doesn't seem to work voor the value 9. Let me
>show you what I mean: (and ofcourse, can you help me?)
>
>In C code, it looks like this:
>
>typedef unsigned __int64 BITBOARD;
>
>void ReverseBitsPerByte(BITBOARD bitboard)
>{
>//  1.  Load the constant, k = 5555555555555555h
>BITBOARD k = 0x5555555555555555;
>
>//  2.  x = [(x shl 1) and k] or [(x and k) shr 1] result is: EFCDAB8967452301
>bitboard = ((bitboard<<1) & k) | ((bitboard & k)>>1 );
>}
>
>Initial bitboard:
>11111110
>11011100
>10111010
>10011000
>01110110
>01010100
>00110010
>00010000
>
>Result of ReverseBitsPerByte(bitboard):
>01111111
>00111011
>01011101
>00011000<--what the ^*&* goes wrong here? Should be 00011001.
>01101110
>00101010
>01001100
>00001000
>
>Thanks for any help or suggestions.

typedef unsigned __int64 Bitboard;

Bitboard        breverse0(Bitboard n)
{
    Bitboard        r = 0,
                    j = 1,
                    m = n;
    while (r = 2 * r + m % 2, j *= 2)
        m /= 2;
    return r;
}

Bitboard        breverse1(Bitboard n)
{
    Bitboard        r = 0,
                    j = 1;
    while (r = r * 2 | n & 1, j *= 2)
        n >>= 1;
    return r;
}

Bitboard        breverse2(Bitboard n)
{
    Bitboard        r = 0,
                    j = 1;
    while (r = 2 * r | (n & j) / j, j *= 2);
    return r;
}

Bitboard        breverse3(Bitboard v)
{
    Bitboard        r = 0,
                    q = -1;
    while (q) {
        r = (r << 1) | (v & 1);
        v >>= 1;
        q >>= 1;
    } return r;
}

Bitboard        breverse4(Bitboard v)
{
    Bitboard        r = 0,
                    q = -1;
    while (q) {
        r = (r << 1) | (v & 1);
        v >>= 1;
        q >>= 1;
    } return r;
}

Bitboard        breverse5(Bitboard n)
{
    int             i = 64;
    Bitboard        m = -1;
    while (i /= 2)
        m ^= m << i, n = n >> i & m | (n & m) << i;
    return n;
}

#include <cstdio>
int             main(void)
{
    Bitboard        a,
                    b;
    b = 0xffffffffffffffffui64;
    a = breverse0(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse0 fails.");
    a = breverse1(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse1 fails.");
    a = breverse2(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse2 fails.");
    a = breverse3(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse3 fails.");
    a = breverse4(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse4 fails.");
    a = breverse5(b);
    if (a != b || a != 0xffffffffffffffffui64)
        puts("breverse5 fails.");

    b = 0xfafafafafafafafaui64;
    a = breverse0(b);
    b = breverse0(a);
    if (a != breverse0(b) || b != 0xfafafafafafafafaui64)
        puts("breverse0 fails.");
    b = 0xfafafafafafafafaui64;
    a = breverse1(b);
    b = breverse1(a);
    if (a != breverse1(b) || b != 0xfafafafafafafafaui64)
        puts("breverse1 fails.");
    b = 0xfafafafafafafafaui64;
    a = breverse2(b);
    b = breverse2(a);
    if (a != breverse2(b) || b != 0xfafafafafafafafaui64)
        puts("breverse2 fails.");
    b = 0xfafafafafafafafaui64;
    a = breverse3(b);
    b = breverse3(a);
    if (a != breverse3(b) || b != 0xfafafafafafafafaui64)
        puts("breverse3 fails.");
    b = 0xfafafafafafafafaui64;
    a = breverse4(b);
    b = breverse4(a);
    if (a != breverse4(b) || b != 0xfafafafafafafafaui64)
        puts("breverse4 fails.");
    b = 0xfafafafafafafafaui64;
    a = breverse5(b);
    b = breverse5(a);
    if (a != breverse5(b) || b != 0xfafafafafafafafaui64)
        puts("breverse5 fails.");
    return 0;
}




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.