Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about an efficient way to change the bits of a number.

Author: Ricardo Gibert

Date: 10:43:54 07/27/03

Go up one level in this thread


On July 27, 2003 at 13:14:51, Gerd Isenberg wrote:

>On July 27, 2003 at 11:39:05, Ricardo Gibert wrote:
>
>>On July 27, 2003 at 11:10:07, Ratko V Tomic wrote:
>>
>>>>To compute b = a for mask m: b = (b&~m)|(a&m)   [untested]
>>>>
>>>>That's 4 operations. 3 if m is a constant when the "~" will optimized away.
>>>
>>>A quicker way (max 3 ops):
>>>
>>>  b^=(a^b)&m;
>>
>>Neat.
>>
>>My way may gain from parallelizing operations, while yours is sequential.
>>However, I suspect mine uses an extra register and this should cause it to be
>>slower than yours depending on the context in which it is used. It would be
>>interesting to benchmark the two.
>
>Hi Ricardo,
>
>From assemblers view...
>
>I guess with constant "m" your statement is prefarable,
>due to both ands are independent.
>
>and  eax, someconst
>and  ebx, ~someconst
>or   ebx, eax

I see I was wrong about my way needing an extra register in comparison with
RVT's solution. They both use 2 with m constant. I should have realized this if
I had thought about it carefully.

>
>If "m" is variable, eg. acts like a "-1|0" mask to implement a kind of
>conditional move of some bits, i found "b ^= (a^b) & m" shorter with about the
>same quality of depencies.
>
>and  eax, ecx ; (a&m)
>not  ecx      ; ~m
>and  ebx, ecx ; D2 (b&~m)
>or   ebx, eax ; D1 (a&m)|(b&~m)
>
>xor  eax, ebx ; (a^b)
>and  eax, ecx ; D1 -> ((a^b) & m)
>xor  ebx, eax ; D2 -> b ^ ((a^b) & m)
>
>But of course, it depends on the environment, possible independent leading and
>trailing instructions.
>
>btw. which dependcy is "better"?
>
>;D1
>not  ecx
>and  ecx, ebx
>
>;D2
>not  ecx
>and  ebx, ecx
>
>Cheers,
>Gerd



This page took 0 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.