From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26492 invoked by alias); 6 Aug 2008 12:10:55 -0000 Received: (qmail 26481 invoked by uid 22791); 6 Aug 2008 12:10:54 -0000 X-Spam-Check-By: sourceware.org Received: from exprod6og107.obsmtp.com (HELO exprod6og107.obsmtp.com) (64.18.1.208) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 06 Aug 2008 12:10:13 +0000 Received: from source ([192.150.11.134]) by exprod6ob107.postini.com ([64.18.5.12]) with SMTP; Wed, 06 Aug 2008 05:10:10 PDT Received: from inner-relay-3.eur.adobe.com ([192.150.8.236]) by outbound-smtp-1.corp.adobe.com (8.12.10/8.12.10) with ESMTP id m76C6UG3025220; Wed, 6 Aug 2008 05:06:30 -0700 (PDT) Received: from fe1.corp.adobe.com (fe1.corp.adobe.com [10.8.192.70]) by inner-relay-3.eur.adobe.com (8.12.10/8.12.9) with ESMTP id m76CA6qJ013526; Wed, 6 Aug 2008 05:10:07 -0700 (PDT) Received: from namailgen.corp.adobe.com ([10.8.192.91]) by fe1.corp.adobe.com with Microsoft SMTPSVC(6.0.3790.1830); Wed, 6 Aug 2008 05:10:06 -0700 Received: from 10.7.234.18 ([10.7.234.18]) by namailgen.corp.adobe.com ([10.8.192.91]) via Exchange Front-End Server namail.corp.adobe.com ([10.8.189.97]) with Microsoft Exchange Server HTTP-DAV ; Wed, 6 Aug 2008 12:10:05 +0000 User-Agent: Microsoft-Entourage/12.11.0.080522 Date: Wed, 06 Aug 2008 12:10:00 -0000 Subject: Re: Undesired automatic cast, workarounds? From: Eljay Love-Jensen To: CC: GCC-help Message-ID: In-Reply-To: <200808051555.28989.palvarado@ietec.org> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-08/txt/msg00060.txt.bz2 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