Re: changes in i386 and x86_64 assembrer

"Markus Wichmann" <[email protected]> Wed, 5 Jan 2011 14:21:47 +0100 (CET)
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Felix von Leitner schrieb:
> [You didn't attribute it, but the second quote level is from Bela]
>> [and the third is from Nikola]

>> 2: save 2 bytes of code:
>
>> > 	neg	%eax
>> > #ifdef WANT_THREAD_SAFE
>> > -	movl	%eax,%ebx
>> > -	call	__errno_location
>> > -	movl	%ebx,(%eax)
>> > +	/* we cannot use anymore %ebp; it is recovered already above! */
>> > +	push	%eax
>> > +	call	__errno_location
>> > +	pop	(%eax)
>> > 	orl	$-1,%eax
>> > #else
>> > 	mov	%eax,errno
>> > 	sbb	%eax,%eax               # eax = eax - eax - CY = -1
>> > #endif
>> > .Lnoerror:
>
>> Setting %eax to return -1 can be shortened:
>
>>    	...
>>    	pop	(%eax)
>>   	stc
>>   #else
>>   	mov	%eax,errno
>>   #endif
>>   	sbb	%eax,%eax               # eax = eax - eax - CY = -1
>
> How do you know the carry flag is not set in the non-stc case?
>

Erm... Nikola is already using sbb in that case. For a better
understanding, I'll recap the whole code as Nikola wrote it, with Bela's
proposed fix: If WANT_THREAD_SAFE is set, the code is

        cmp        $-124,%eax
#                   ^^^^
# BTW: I noticed that I quoted the wrong ABI in that other posting.
# Sorry for that. The SysV ABI for i386 doesn't say anything about the
# kernel, so I guess it won't hurt to use -4095 here as well
        jb        .Lnoerror
        neg        %eax
        push       %eax
        call       __errno_location
        pop        (%eax)
        stc
        sbb        %eax, %eax
.Lnoerror:

If WANT_THREAD_SAFE is unset, the code is:

        cmp        $-124,%eax
        jb        .Lnoerror
        neg        %eax
        mov        %eax, errno
        sbb        %eax, %eax

According to the AMD docs, neg will always set CF, unless %eax is 0, and
mov will not touch the flags.

> I think this is too obfuscating for this small of a gain.
>

Didn't dietlibc set out to save every last byte it can? Plus, assembler is
always obfuscated. :-) It's a bit of a write-only language, like perl.

> Felix
>

Markus
-- 
Progress (n.): Process through which USENET evolved from smart people in
front of dumb terminals to dumb people in front of smart terminals.