public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Undesired automatic cast, workarounds?
       [not found] <200808051555.28989.palvarado@ietec.org>
@ 2008-08-06 12:10 ` Eljay Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2008-08-06 12:10 UTC (permalink / raw)
  To: palvarado; +Cc: GCC-help

Hi Pablo,

> how do I then tell the compiler to keep the type?

I'm not really sure what you are trying to do.

Do you want to make sure that integral promotion is explicit in the code?

I, personally, like that approach... but I like B&D languages like Ada, so
I'm atypical.  That shouldn't impose any additional performance penalty.

Are you trying to eke every little bit of performance out of the operation?

You may want to read the book "Hacker's Delight" by Henry S. Warren.

It's not about "hacker" in the bad sense as used by the media, it's about
"hacker" in the good sense as used by programmers -- bit twiddling and
blazing performance lore all collected in one enjoyable book.

Also be very careful about alignment, since an array of bytes (char) may not
have the right kind of alignment to be treated as an array of words (int).

Also, assuming that int is 4-bytes may make for porting issues.  You may
want to use int32_t from C99's stdint.h.

I'm not sure if representing the pixels in a union would negatively impact
performance, but it could avoid alignment problems.

struct RGB
{
  byte mRed;
  byte mGreen;
  byte mBlue;
  byte mFallow; // mPad? mGarbage? mAlpha? mZero? m255?
}

union Pixel
{
  struct RGB mRGB;
  int32_t mPixel; // Be mindful of endian-ness.
};

Are you worried about promotion / slicing performance impact?

Profile the code.

Look at the assembly output of the routine compiled with -O2 or -O3.
Sometimes you can tweak the routine (usually at the "cost" of making the
routine a bit more harder to maintain) to allow the optimizer a better job
to optimize.

Do not bother to look at the assembly output of the routine compiled with
-O0 (unoptimized).

> warning: conversion to 'lti::ubyte' from 'int' may alter its value

// Assuming unsigned char as byte.  Change accordingly.
typedef unsigned char byte;
#define BYTE_MIN 0
#define BYTE_MAX 255

bool ParanoiaIntToByte(int c)
{
  return c >= BYTE_MIN && c <= BYTE_MAX;
}

int c = some_int_fn();
assert(ParanoiaIntToByte(c));
byte b0 = c; // implicit slice.  Warning?
byte b1 = byte(c); // explicit slice.  Warning?

Do you need to worry about saturation?  Such that when c>255 it ceiling's to
255, or when c<0 it floor's to 0?

HTH,
--Eljay

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

* Re: Undesired automatic cast, workarounds?
  2008-08-05 14:27 Pablo Alvarado
@ 2008-08-05 18:43 ` John Love-Jensen
  0 siblings, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2008-08-05 18:43 UTC (permalink / raw)
  To: palvarado, GCC-help

Hi Pablo,

> for some reason now the operator+ automatically converts the ubyte to int,

That's correct.  That's what the compiler should do.

> ... generating in the above code a warning.

What warnings are you enabling?  That should not be a default warning (but I
don't have GCC 4.3.1 at my disposal to confirm).

> This has been ok with all previous gcc compilers.

All previous GCC compilers promote unsigned char to int.  (I don't know of
any that promote unsigned char to unsigned int, but it could happen in some
uncommon kinds of architectures.)

> Is there any way to deactivate this?

There should be a -Wno-blah switch to turn off the warning.  But I'm
surprised that it is enabled in the first place.

The only way to deactivate promoting an unsigned char to int is to use a
different language which does not do that promotion.

Both C and C++ promote.

> I mean, not only the warning but avoiding the unnecesary casts?

To avoid the casts, use int or unsigned int instead of unsigned char.

HTH,
--Eljay

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

* Undesired automatic cast, workarounds?
@ 2008-08-05 14:27 Pablo Alvarado
  2008-08-05 18:43 ` John Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Pablo Alvarado @ 2008-08-05 14:27 UTC (permalink / raw)
  To: gcc-help

Hello all,

I tried yesterday GCC 4.3.1 for the first time, and I encountered a bad 
surprise.  If I do

typedef unsigned char ubyte;
ubyte a,b,c;
c=a+b;
c+=a;

for some reason now the operator+ automatically converts the ubyte to int, 
generating in the above code a warning.  This has been ok with all previous 
gcc compilers.  Is there any way to deactivate this?  I mean, not only the 
warning but avoiding the unnecesary casts?  This new behaviour is even worse 
in the following cases:

typedef signed char byte;
byte a,b,c;
...
a=-b;  // the operator- also produces an int !

or 

c=b/a; // the operator/ also produces an int

in the later cases the casts to are absolutelly innecessary, since the results 
are always in range (assuming a!=0 of course)!

Any hints will be very welcome,

Pablo

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

end of thread, other threads:[~2008-08-06 12:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <200808051555.28989.palvarado@ietec.org>
2008-08-06 12:10 ` Undesired automatic cast, workarounds? Eljay Love-Jensen
2008-08-05 14:27 Pablo Alvarado
2008-08-05 18:43 ` John Love-Jensen

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