Front page | perl.perl5.porters |
Postings from March 2003
Re: [PATCH] glibc _moddi3
Thread Previous
|
Thread Next
From:
Tels
Date:
March 2, 2003 09:02
Subject:
Re: [PATCH] glibc _moddi3
Message ID:
perl.perl5.porters-72636@nntp.perl.org
-----BEGIN PGP SIGNED MESSAGE-----
Moin,
On 02-Mar-03 Jarkko Hietaniemi carved into stone:
> On Sun, Mar 02, 2003 at 12:09:33PM +0200, Enache Adrian wrote:
>> On Sun, Mar 02, 2003 at 11:18:17AM +0200, Jarkko Hietaniemi wrote:
>> > Thanks for the enhancement suggestions (abs() and PL_op->op_ppaddr),
>> > the change is now in (#18797).
>>
>> My Perl has 64-bit IVs and 32-bit ints. Only llabs() (C99 only)
>> will get it right in this case.
>
> Ahhh, good point.
Do we have a macro to use in these case? I can imagine that there are some
other places using abs() that might get it wrong in this case.
>> The compiler probably does a good job and optimize right = -right,
>> left % right anyway.
>
> Another good point. I'll go back to the right = -right scheme.
Here are two test c files, both "compiled" with gcc -s -O3 and I show only
the assembly output for the main functions here:
int right = -987;
int left = 123;
int main (void)
{
/* case two */
if (right < 0)
{
right = -right;
}
left = left % right;
return 0;
}
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl right, %edx
andl $-16, %esp
testl %edx, %edx
js .L3
.L2:
movl left, %eax
movl %edx, %ecx
cltd
idivl %ecx
xorl %eax, %eax
movl %edx, left
movl %ebp, %esp
popl %ebp
ret
.p2align 4,,7
.L3:
negl %edx
movl %edx, right
jmp .L2
You see the compiler turned the jump around, so that in the normal case of
not negating, we fall through, and continue, and only if we negate, we jump
"away", negate and return to .L2. Still, awfull lot of code - it does not
optimize away the jump, nor the negation.
int right = -123;
int left = 456;
int main (void)
{
/* case 1 */
left = left % abs(right);
return 0;
}
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
movl right, %edx
andl $-16, %esp
testl %edx, %edx
js .L3
.L2:
movl left, %eax
movl %edx, %ecx
cltd
idivl %ecx
xorl %eax, %eax
movl %edx, left
movl %ebp, %esp
popl %ebp
ret
.p2align 4,,7
.L3:
negl %edx
jmp .L2
As you can see, the compiler never used abs() at all :-) And it just made a
"jump" aound like in the previous case. But it is still better, since gcc
in this case did not set the result back to right, since it knows it can
use a temporary result.
So, with gcc, left %= abs(right) is better than the if (right < 0) stuff.
YMMV, of course. However, never trust someone who says "the compiler
probably optimizes it away" and never trust the compiler, either :-P
Oh, about 64 bit vs. 32 bit: I would trust the compiler to use the "right"
abs() code, of course, e.g. if right is 64 bit, the compiler *should* get
this right. I can't try this, though.
Oh, one more thought: In the code in question, what are "right" and "left"?
ints? longs? or IVs?
Best wishes,
Tels
- --
Signed on Sun Mar 2 18:01:25 2003 with http://bloodgate.com/tels.asc
perl -MDev::Bollocks -le'print Dev::Bollocks->rand()'
preemptively seize granular developments
http://www.notcpa.org/ You have the freedom to run any code. Yet.
http://bloodgate.com/perl My current Perl projects
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.
iQEVAwUBPmI47XcLPEOTuEwVAQElxQf9GnkKlZ43yHgnzCZyi7GCgf/BSSVab3z0
UrWDuU4yMSpEg2F2OHjQHuaIlJAHIknl4SAqPEHGXHACQCmIoJwvJhcg3GURDHby
63wZUyiImfEraH7dyWgkcqv5bgoIl3kQ83w4NGoiCD20/+G+jrSNPhwOaOWhfL3w
QYGMrNvGgdpHSEMMc/dR4oSk7wjSR4ysBhkZDHoXYJLuqxiGM1Vo6FUcousp7NYU
Cjhyj8RmFe3E4PObxFwavBsJ9ATO14LycxSIUx+2hOJWaloGjae2ws8uBpLIOm+g
LCUXJ2wffASBfX3uYhPwedzVTPW+2GahRMA2Ty8j/QoLSPZdmZ/4hw==
=19KI
-----END PGP SIGNATURE-----
Thread Previous
|
Thread Next