comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@acm.nospam.org>
Subject: Re: sub-optimal code for packed boolean arrays -- bug or inherent limitation
Date: Wed, 04 Jul 2007 01:12:45 GMT
Date: 2007-07-04T01:12:45+00:00	[thread overview]
Message-ID: <huCii.12793$Fc.1922@attbi_s21> (raw)
In-Reply-To: <1183491750.177186.154490@k29g2000hsd.googlegroups.com>

Alinabi wrote:
> The bitboard representation does not to save space. In fact it is
> quite wasteful, which is why it is not used in embedded devices, like
> cell phones. If you wonder how can this save time, here is an example:
> suppose that you have a white pawn on c4 and you want to determine if
> it is a passed pawn. That means that the pawn can no longer be blocked
> or attacked by a black pawn, and therefore it threatens to advance and
> get promoted. You would have to check if there are any black pawns at
> b5, b6, b7, c5, c6, c7, d5, d6, d7. In the bitboard representation,
> you would have a bitboard called BlackPawns which has a one for every
> square occupied by a black pawn and than you would keep an array of
> bitboard masks BlackBlockers : array(a1..h8) of Bitboard, such that,
> for example, BlackBlockers(c4) has 1's at b5, b6, b7, c5, c6, c7, d5,
> d6, d7, and 0's everywhere else. Then the white pawn at c4 is passed
> if BlackPawns and BlackBlockers(c4) = 0, which is two machine
> instruction, one for indexing and one for the logical operator.

Zero is not an array of Boolean. You'd have to compare against (others 
=> False). That the compiler recognizes this as 0 might be something 
else you'll want to check.

> As for the proof that the hand coded version is faster, here it is:
> 
> Hand coded: 2 MOV, 1 SHL, 1 XOR
> Compiler:   2 MOV, 1 SHL, 1 XOR, 1 SHR, 1 ROL, 2 AND, 1 OR
> ---------------------
> 5 extraneous instructions, Q.E.D.

This demonstrates only that the hand-coded version has fewer 
instructions. To prove that it's faster, you have to time them both.

However, granting that the hand-coded version is faster, you haven't 
demonstrated that it makes an actual difference.

> As I mentioned in a previous post, I already implemented all these
> bitboard operations the usual way, using Unsigned_64, shifts, masks
> and even some inline assembly. I am not opposed to that. I was just
> wondering if I could use packed arrays of booleans to make the code
> more legible and delegate the low level work to the compiler. I did
> not expect the compiler to perform as well as it did. Since it only
> failed far off the mark in one case, I was wondering if that would be
> considered a bug. That question was answered in the negative. You are
> getting way to defensive about this. I don't have a gnat bashing
> agenda.

My reaction is due to an allergy to premature optimization, the root of 
all evil.

-- 
Jeff Carter
"Gentlemen, you can't fight in here. This is the War Room!"
Dr. Strangelove
30



  reply	other threads:[~2007-07-04  1:12 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-02 19:34 sub-optimal code for packed boolean arrays -- bug or inherent limitation Alinabi
2007-07-02 20:08 ` Ludovic Brenta
2007-07-03  1:01 ` Jeffrey R. Carter
2007-07-03  7:22   ` Harald Korneliussen
2007-07-03  8:37     ` Georg Bauhaus
2007-07-03  7:59 ` gautier_niouzes
2007-07-03  9:25 ` Stefan Lucks
2007-07-03 12:40   ` Stefan Lucks
2007-07-03 15:42 ` Adam Beneschan
2007-07-03 18:04 ` Alinabi
2007-07-03 18:09   ` Alinabi
2007-07-03 18:17     ` Alinabi
2007-07-10  2:06       ` Randy Brukardt
2007-07-03 18:36   ` Jeffrey R. Carter
2007-07-03 19:42     ` Alinabi
2007-07-04  1:12       ` Jeffrey R. Carter [this message]
2007-07-04 10:15         ` Jeffrey Creem
2007-07-04 18:28           ` Jeffrey R. Carter
2007-07-04  3:22       ` Steve
2007-07-04  6:31         ` Harald Korneliussen
2007-07-08 22:53     ` Robert A Duff
2007-07-09  6:09       ` tmoran
2007-07-04  9:00   ` Jean-Pierre Rosen
2007-07-04 18:27     ` tmoran
2007-07-04 19:16       ` Pascal Obry
2007-07-05  1:45         ` tmoran
2007-07-05  4:53       ` Jeffrey R. Carter
2007-07-04 18:51     ` tmoran
2007-07-08 22:58 ` Robert A Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox