public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
[parent not found: <3085.22076015851$1208722372@news.gmane.org>]
[parent not found: <23793.5913996354$1208708537@news.gmane.org>]
* Integral conversions in C/C++
@ 2008-04-20  6:11 Christian Böhme
  0 siblings, 0 replies; 25+ messages in thread
From: Christian Böhme @ 2008-04-20  6:11 UTC (permalink / raw)
  To: gcc-help

Hi all,

I ran into a problem with an expression that I imagined is too
trivial to even needing a second thought in the first place.
However, on a 32 bit machine, the code

--->8---

#include <stdint.h>

// ...

int64_t a;
uint32_t b = 8;

a = -(b * 2u);

--->8---

assigns a the value 0xfffffff0 while no warnings were issued even
when compiling with all warnings (-Wall) switched on.  Strangely
enough and for reasons escaping me

--->8---

#include <stdint.h>

// ...

int64_t a;
uint32_t b = 8;

a = -((int64_t )(b * 2u));

--->8---

produces the expected value of 0xfffffffffffffff0 (or -16).

The destination type (ie, a's type) is the same in both versions
which is large enough to hold the result of the RHS of a = -(x)
with x being a temporary variable for the result of the sub
expression (b * 2u).  In order to represent the latter's result,
its destination type clearly cannot be that of its operands and
must be converted to a larger type.  Since a's type satisfies
these requirements already, it seems logical to assume that x
is defined with this type also which clearly is not the case
with the first version.  Having 4.7 of the ISO C++ standard
in mind which deals with integral _conversions_ (as opposed
to integral _promotions_) at which point may I be missing
something here ?  Where's the logic in having to insert a
type cast for a temporary when it can be unambiguously inferred
from the destination type of the expression ?  Is this an
``implementation detail'' ?


Cheers,
Christian

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: Integral conversions in C/C++
@ 2008-04-20  4:58 Tom St Denis
  0 siblings, 0 replies; 25+ messages in thread
From: Tom St Denis @ 2008-04-20  4:58 UTC (permalink / raw)
  To: gcc-help



On Sat 19/04/08  9:57 PM , Christian Böhme monodhs@gmx.de sent:
> Hi all,
> 
> 
> 
> I ran into a problem with an expression that I imagined is too
> 
> trivial to even needing a second thought in the first place.
> 
> However, on a 32 bit machine, the code
> 
> 
> 
> --->8---
> 
> 
> 
> #include 
> 
> 
> 
> // ...
> 
> 
> 
> int64_t a;
> 
> uint32_t b = 8;
> 
> 
> 
> a = -(b * 2u);
> 
> 
> 
> --->8---
> 
> 
> 
> assigns a the value 0xfffffff0 while no warnings were issued even
> 
> when compiling with all warnings (-Wall) switched on.  Strangely
> 
> enough and for reasons escaping me

What type is the expression -(b * 2u)?  And why would the number 2^32 - 16 be
sign extended when stored in a 64-bit signed int?  2^32 - 16 is a valid positive
number for the 64-bit type.

Consider

int main(void)
{
   long long a;
   unsigned long b;

   b = 8;
   a = (int)(-(b * 2U));
   printf("a == %lld\n", a);
   return 0;
}

Why does this work when your example does not?

Tom

^ permalink raw reply	[flat|nested] 25+ messages in thread
* Integral conversions in C/C++
@ 2008-04-20  4:49 Christian Böhme
  0 siblings, 0 replies; 25+ messages in thread
From: Christian Böhme @ 2008-04-20  4:49 UTC (permalink / raw)
  To: gcc-help

Hi all,

I ran into a problem with an expression that I imagined is too
trivial to even needing a second thought in the first place.
However, on a 32 bit machine, the code

--->8---

#include <stdint.h>

// ...

int64_t a;
uint32_t b = 8;

a = -(b * 2u);

--->8---

assigns a the value 0xfffffff0 while no warnings were issued even
when compiling with all warnings (-Wall) switched on.  Strangely
enough and for reasons escaping me

--->8---

#include <stdint.h>

// ...

int64_t a;
uint32_t b = 8;

a = -((int64_t )(b * 2u));

--->8---

produces the expected value of 0xfffffffffffffff0 (or -16).

The destination type (ie, a's type) is the same in both versions
which is large enough to hold the result of the RHS of a = -(x)
with x being a temporary variable for the result of the sub
expression (b * 2u).  In order to represent the latter's result,
its destination type clearly cannot be that of its operands and
must be converted to a larger type.  Since a's type satisfies
these requirements already, it seems logical to assume that x
is defined with this type also which clearly is not the case
with the first version.  Having 4.7 of the ISO C++ standard
in mind which deals with integral _conversions_ (as opposed
to integral _promotions_) at which point may I be missing
something here ?  Where's the logic in having to insert a
type cast for a temporary when it can be unambiguously inferred
from the destination type of the expression ?  Is this an
``implementation detail'' ?


Cheers,
Christian

^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2008-04-23 11:22 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <10044.4877906286$1208659138@news.gmane.org>
2008-04-20 17:57 ` Integral conversions in C/C++ Christian Böhme
2008-04-20 18:16   ` Andrew Haley
2008-04-20 23:07     ` Christian Böhme
2008-04-21 11:48       ` Andrew Haley
2008-04-21 14:18         ` Christian Böhme
2008-04-21 15:55           ` Tom St Denis
2008-04-21 23:51           ` Andrew Haley
2008-04-22 12:33             ` Christian Böhme
2008-04-22 13:30               ` Andrew Haley
2008-04-22 14:11                 ` John Fine
2008-04-22 20:48                   ` Andrew Haley
2008-04-22 21:05                     ` John Love-Jensen
2008-04-23  8:45                       ` Christian Böhme
2008-04-23  2:30                   ` Christian Böhme
2008-04-23 11:22                     ` Andrew Haley
2008-04-24 11:30                       ` John Love-Jensen
2008-04-22 22:42                 ` Christian Böhme
2008-04-23 11:11                   ` Andrew Haley
     [not found] <3085.22076015851$1208722372@news.gmane.org>
2008-04-21  3:23 ` Christian Böhme
2008-04-21  8:28   ` Segher Boessenkool
2008-04-21  9:00     ` Christian Böhme
     [not found] <23793.5913996354$1208708537@news.gmane.org>
2008-04-21  0:36 ` Christian Böhme
2008-04-20  6:11 Christian Böhme
  -- strict thread matches above, loose matches on Subject: below --
2008-04-20  4:58 Tom St Denis
2008-04-20  4:49 Christian Böhme

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).