public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Eljay Love-Jensen <eljay@adobe.com>
To: <palvarado@ietec.org>
Cc: GCC-help <gcc-help@gcc.gnu.org>
Subject: Re: Undesired automatic cast, workarounds?
Date: Wed, 06 Aug 2008 12:10:00 -0000	[thread overview]
Message-ID: <C4BEFEA9.7AAF%eljay@adobe.com> (raw)
In-Reply-To: <200808051555.28989.palvarado@ietec.org>

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

       reply	other threads:[~2008-08-06 12:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <200808051555.28989.palvarado@ietec.org>
2008-08-06 12:10 ` Eljay Love-Jensen [this message]
2008-08-05 14:27 Pablo Alvarado
2008-08-05 18:43 ` John Love-Jensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=C4BEFEA9.7AAF%eljay@adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=palvarado@ietec.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).