int opEquals(Object), and other legacy ints
Walter Bright
newshound at digitalmars.com
Sat Jul 29 19:34:26 PDT 2006
Stewart Gordon wrote:
> Walter Bright wrote:
>> Stewart Gordon wrote:
>>> xs0 wrote:
>>> <snip>
>>>> Well, I'm just guessing, but I think something like
>>>>
>>>> > int opEquals(Foo foo)
>>>> > {
>>>> > return this.bar == foo.bar;
>>>> > }
>>>>
>>>> is compiled to something like
>>>>
>>>>> return this.bar-foo.bar; // 1 instruction
>>>>
>>>> but if the return type is bool, it becomes
>>>>
>>>>> return this.bar-foo.bar?1:0; // 3 instructions
>>>
>>> If it does this, then there's a serious bug in the compiler.
>>
>> What instruction sequence do expect to be generated for it?
>
> If anything resembling the above, then
>
> return this.bar-foo.bar?0:1;
? Let's look at an example:
class Foo
{
int foo, bar;
int Eq1(Foo foo)
{
return this.bar-foo.bar?0:1;
}
int Eq2(Foo foo)
{
return this.bar-foo.bar;
}
}
which generates:
Eq1:
mov EDX,4[ESP]
mov ECX,0Ch[EAX]
sub ECX,0Ch[EDX]
cmp ECX,1
sbb EAX,EAX
neg EAX
ret 4
Eq2:
mov ECX,4[ESP]
mov EAX,0Ch[EAX]
sub EAX,0Ch[ECX]
ret 4
So we have 4 instructions generated rather than 1. If there's a trick to
generate only one instruction for Eq1, I'd like to know about it.
>> I can. (a == b), where a and b are ints, can be implemented as (a -
>> b), and the result is int 0 for equality, int !=0 for inequality.
>
> How is this (a == b) rather than (a != b)?
I don't understand your question.
More information about the Digitalmars-d-bugs
mailing list