Re: Strange conversion to int64

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> >with the output
> 4294967232
> -64
> -64
> -64
>
> What output were you expecting?

I was expecting to get -64 in every case. I don't understand why
(int64) = -1 (int32) * 64 (uint32)
is different from
(int64) = -1 (int32) * 64 (uint64)

Also why is
(int64) = -1 (int32) * 64 (uint32)
is different from
(int32) = -1 (int32) * 64 (uint32)

> Is your GCC on your 64-bit platform built such that (say) short int is 32-bit, int is 64-bit, and long is 128-bit?

I believe it's such that short int is 16, int is 32 and long is 64. I
don't see really how that matters as I'm using the explicit types
defined in stdint.h. AFAIK they are just aliases to what I'd expect.

> Keep in mind that integer promotion rules are char/short/int/long based, not int32/int64 based.  And the char/short/int/long is promotion behavior is sensitive (i.e., can change output behavior depending on your platform) to what bit sizes each of those types are.

I believe the issue I'm having has to do with how the compiler is
dealing with the assignment to the int64. I tried to look at the
produced assembly and it seems that the inappropriate instructions are
used (in one case it does cltq, in another not). I'm not familiar with
the assembly enough to know what the correct way of handling 32bit vs
64bit registers are.

> Sincerely,
> --Eljay

Thanks for the reply.

> From: gcc-help-owner@xxxxxxxxxxx [gcc-help-owner@xxxxxxxxxxx] On Behalf Of Tony Bernardin [sarusama@xxxxxxxxx]
> Sent: Wednesday, January 27, 2010 8:10 PM
> To: gcc-help@xxxxxxxxxxx
> Subject: Strange conversion to int64
>
> Hey, I'm having a little trouble understanding the conversion gcc is
> doing in a statement with the following types:
>
> int64 = int32 * uint32
>
> This is running on a 64bit machine with gcc (GCC) 4.4.2 20091027 (Red
> Hat 4.4.2-7)
>
> Following simple code snippet:
>
>  1 #include <stdint.h>
>  2 #include <iostream>
>  3
>  4 using namespace std;
>  5
>  6 int main(int argc, char* argv[])
>  7 {
>  8     int32_t  neg  = -1;
>  9     uint32_t mult = 64;
>  10
>  11     int64_t res  = neg * mult;
>  12     int32_t res2 = neg * mult;
>  13
>  14     cout << res << endl << res2 << endl;
>  15
>  16     uint64_t mult64 = 64;
>  17
>  18     res  = neg * mult64;
>  19     res2 = neg * mult64;
>  20
>  21     cout << res << endl << res2 << endl;
>  22
>  23     return 0;
>  24 }
>
> with the output
>
> 4294967232
> -64
> -64
> -64
>
> When compiled it gives an understandable warning
>
> main.cpp: In function ‘int main(int, char**)’:
> main.cpp:19: warning: conversion to ‘int32_t’ from ‘uint64_t’ may
> alter its value
>
> still that operation will generate the output I expect.
>
> Is there some strange casting rule that I'm not following properly? In
> particular I find lines 11 and 18 interesting as the only change
> between the type of 'mult' uint32 vs uint64.
>
> cheers,
> Tony


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux