* fnstcw in simple math functions?
@ 2000-03-15 14:40 Michael Vance
2000-03-15 15:00 ` Zack Weinberg
0 siblings, 1 reply; 2+ messages in thread
From: Michael Vance @ 2000-03-15 14:40 UTC (permalink / raw)
To: gcc
Why is gcc emitting fnstcw instructions for this simple code? This
seems like a hopelessly conservative way of maintaining state. I tried
-ffast-math and got the same results.
int enclosed_mul( int x, float y )
{
x = static_cast<int>( x * y );
return x;
}
produces:
.size shortcut_mul__Fif,.Lfe2-shortcut_mul__Fif
.align 4
.globl enclosed_mul__Fif
.type enclosed_mul__Fif,@function
enclosed_mul__Fif:
pushl %ebp
movl %esp,%ebp
subl $24,%esp
fildl 8(%ebp)
fmuls 12(%ebp)
fnstcw -6(%ebp)
movw -6(%ebp),%dx
orw $3072,%dx
movw %dx,-8(%ebp)
fldcw -8(%ebp)
fistpl -4(%ebp)
movl -4(%ebp),%eax
fldcw -6(%ebp)
movl %ebp,%esp
popl %ebp
ret
This is using gcc-2.95.2 compiled as:
$ /usr/local/gcc-2.95.2/bin/g++ -O6 -fno-rtti -fno-exceptions -S
casting.cpp -o casting.s
m.
--
Programmer "Ha ha." "Ha ha." "What are you laughing at?"
Loki Software "Just the horror of being alive."
http://lokigames.com/~briareos/ - Tony Millionaire
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: fnstcw in simple math functions?
2000-03-15 14:40 fnstcw in simple math functions? Michael Vance
@ 2000-03-15 15:00 ` Zack Weinberg
0 siblings, 0 replies; 2+ messages in thread
From: Zack Weinberg @ 2000-03-15 15:00 UTC (permalink / raw)
To: Michael Vance; +Cc: gcc
On Wed, Mar 15, 2000 at 02:39:51PM -0800, Michael Vance wrote:
> Why is gcc emitting fnstcw instructions for this simple code? This
> seems like a hopelessly conservative way of maintaining state. I tried
> -ffast-math and got the same results.
>
> int enclosed_mul( int x, float y )
> {
> x = static_cast<int>( x * y );
> return x;
> }
...
This is because conversion from float to int is required to truncate,
but all other math operations are supposed to do round-to-nearest.
It might be interesting to see what kind of code we got if we
pretended that there was no way to put the 387 in truncate mode. We'd
have to do more math, but fldcw/fnstcw is so slow that it'd probably
come out faster anyway. Paging Jan Hubicka?
[I see glibc changes the control word in floor(). Bleah.]
zw
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-03-15 15:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-15 14:40 fnstcw in simple math functions? Michael Vance
2000-03-15 15:00 ` Zack Weinberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).