* axp/gcc the double cast gets trashed on return
@ 1997-10-06 14:49 Uncle George
0 siblings, 0 replies; 4+ messages in thread
From: Uncle George @ 1997-10-06 14:49 UTC (permalink / raw)
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2531 bytes --]
--------------719205A674A3765CD026A878
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,tell me why the double value returned from go_convert() is not 7.0???
If u step through ( via gdb ) the val l.dd = 7, x.dd = 7,
BUT d.dd is some bogus number. d.dd should have 7.0
Am I doing something wrong ( u cannot change the code, unless there is a "C"
violation somewhere!!!!)
GAT
PS, thanks for the SIGNAL/FPE syscall. u should note ( although I havent verified )
that a sigfpe is still possible on a double conversion to long.
*********************************************************
typedef long int64_t;
double go_convert ( int64_t );
main()
{
union {
long ll;
double dd;
} l, d;
l.ll = 0x401c000000000000;
d.dd = go_convert ( l.ll );
}
double
go_convert( int64_t v )
{
union {
long ll;
double dd ;
} x;
x.ll = v;
return(x.dd);
}
> --
> To unsubscribe: send e-mail to axp-list-request@redhat.com with
> 'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
--------------719205A674A3765CD026A878
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,tell me why the double value returned from go_convert() is not 7.0???
If u step through ( via gdb ) the val l.dd = 7, x.dd = 7,
BUT d.dd is some bogus number. d.dd
should have 7.0
Am I doing something wrong ( u cannot change the code, unless there
is a "C"
violation somewhere!!!!)
GAT
PS, thanks for the SIGNAL/FPE syscall. u should note ( although I havent
verified )
that a sigfpe is still possible on a double conversion to long.
*********************************************************
typedef long int64_t;
double go_convert ( int64_t );
main()
{
union {
long ll;
double dd;
} l, d;
l.ll = 0x401c000000000000;
d.dd = go_convert ( l.ll
);
}
double
go_convert( int64_t v )
{
union {
long ll;
double dd ;
} x;
x.ll = v;
return(x.dd);
}
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
--------------719205A674A3765CD026A878--
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: axp/gcc the double cast gets trashed on return
@ 1997-10-06 18:35 Uncle George
0 siblings, 0 replies; 4+ messages in thread
From: Uncle George @ 1997-10-06 18:35 UTC (permalink / raw)
Richard Henderson wrote:
> On Mon, Oct 06, 1997 at 10:49:36AM -0400, Uncle George wrote:
> >
> If you compile this with optimization, the compiler will elide the
> store to d.dd since (1) it is a local variable, thus there are no
> side effects once the function is complete and (2) the value is not
> used.
>
> Make d global or static and you'll see it gets the right value.
done that, no avail. This excerpt was taken from another pgm ( the java jvm )
when the double test failed.
looked at the assembler, but dont see the prob. maybe u like to try it - maybe u
can tellme how to look at float regs
in gdb.
gat
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 4+ messages in thread
[parent not found: <3438FA7E.E8FD1D28@voicenet.com>]
* Re: axp/gcc the double cast gets trashed on return
[not found] <3438FA7E.E8FD1D28@voicenet.com>
@ 1997-10-06 17:03 ` Richard Henderson
1997-10-07 0:08 ` Volker Barthelmann
1 sibling, 0 replies; 4+ messages in thread
From: Richard Henderson @ 1997-10-06 17:03 UTC (permalink / raw)
On Mon, Oct 06, 1997 at 10:49:36AM -0400, Uncle George wrote:
> Hi,tell me why the double value returned from go_convert() is not 7.0???
> If u step through ( via gdb ) the val l.dd = 7, x.dd = 7,
>
> BUT d.dd is some bogus number. d.dd should have 7.0
>
> Am I doing something wrong (u cannot change the code, unless there is a "C"
> violation somewhere!!!!)
[...]
> l.ll = 0x401c000000000000;
> d.dd = go_convert ( l.ll );
If you compile this with optimization, the compiler will elide the
store to d.dd since (1) it is a local variable, thus there are no
side effects once the function is complete and (2) the value is not
used.
Make d global or static and you'll see it gets the right value.
r~
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: axp/gcc the double cast gets trashed on return
[not found] <3438FA7E.E8FD1D28@voicenet.com>
1997-10-06 17:03 ` Richard Henderson
@ 1997-10-07 0:08 ` Volker Barthelmann
1 sibling, 0 replies; 4+ messages in thread
From: Volker Barthelmann @ 1997-10-07 0:08 UTC (permalink / raw)
On Mon, 6 Oct 1997, Uncle George wrote:
> Hi,tell me why the double value returned from go_convert() is not 7.0???
> If u step through ( via gdb ) the val l.dd = 7, x.dd = 7,
>
> BUT d.dd is some bogus number. d.dd should have 7.0
I added a printf() and got 7.0 here. BUT...
> Am I doing something wrong ( u cannot change the code, unless there is a "C"
> violation somewhere!!!!)
The code is basically broken. Reading another member of a union than the
one which was last written causes implementation defined behaviour (IMHO
it should even be undefined behaviour).
And I don't know if gcc documents its behaviour here. You really shouldn't
do such things because there are compilers that will break such code.
The only clean way to do that is to use a bytewise copy, e.g. with
memcpy(). Things like *((double*)&l.ll) will also cause trouble.
Volker
--
To unsubscribe: send e-mail to axp-list-request@redhat.com with
'unsubscribe' as the subject. Do not send it to axp-list@redhat.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1997-10-07 0:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-10-06 14:49 axp/gcc the double cast gets trashed on return Uncle George
-- strict thread matches above, loose matches on Subject: below --
1997-10-06 18:35 Uncle George
[not found] <3438FA7E.E8FD1D28@voicenet.com>
1997-10-06 17:03 ` Richard Henderson
1997-10-07 0:08 ` Volker Barthelmann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox