All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Schwab <schwab@suse.de>
To: linux-ia64@vger.kernel.org
Subject: Re: [PATCH] Altix system controller event handling
Date: Thu, 10 Mar 2005 10:57:08 +0000	[thread overview]
Message-ID: <je64zzu5tn.fsf@sykes.suse.de> (raw)
In-Reply-To: <Pine.SGI.4.58.0503090818080.99386@gallifrey.americas.sgi.com>

Christoph Hellwig <hch@infradead.org> writes:

>> +static unsigned int
>> +scdrv_buffer_to_int(char *buf)
>> +{
>> +	int i;
>> +	unsigned int n = 0;
>> +	for( i = 0; i < sizeof(n); i++ ) {
>> +		n |= ((unsigned)(*(unsigned char *)buf++)
>> +		      << (8 * ((sizeof(n) - i) - 1)));
>
> urgg.  the (*(unsigned char *)buf++) should be just *(buf++), no?
> address arithmetics on signed and unsigned char are the same.

But not value promotion.  The latter gives you a different value when *buf
is negative.  For example if *buf = -1 then (unsigned)*buf = 0xffffffff,
but (unsigned char)*buf = 0xff.

> So
> 	for (i = 0; i < sizeof(n); i++)
> 		n |= ((unsigned int)buf[i] << (8 * (sizeof(n) - i - 1)));
>
> should do the same, no?

This should be better:

 	for (i = 0; i < sizeof(n); i++)
 		n |= ((unsigned char)buf[i] << (8 * (sizeof(n) - i - 1)));

Or even better: replace scdrv_buffer_to_int by be32_to_cpup.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

  parent reply	other threads:[~2005-03-10 10:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
2005-03-09 22:31 ` Christoph Hellwig
2005-03-10 10:57 ` Andreas Schwab [this message]
2005-03-10 16:48 ` Greg Howard
2005-03-10 17:02 ` Andreas Schwab
2005-03-10 18:02 ` Greg Howard
2005-03-10 22:19 ` Christoph Hellwig
2005-03-10 22:55 ` Greg Howard

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=je64zzu5tn.fsf@sykes.suse.de \
    --to=schwab@suse.de \
    --cc=linux-ia64@vger.kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.