Subject: Re: Assming char == signed char?
To: None <tech-ports@NetBSD.ORG>
From: Christos Zoulas <christos@deshaw.com>
List: tech-ports
Date: 06/19/1995 13:56:07
In article <UjtAWfn02QU8M9TUlB@highland.com.au> cagney@highland.com.au (Andrew Cagney) writes:
>Briefly,
>
>Has the NetBSD source files ever been built on a machine where `char' is
>really `unsigned char'?  I have the feeling that it it hasn't been.  I
>ask because I've stumbled across code of the form:
>
>		if (tty->a_member_of_type_char == -1)
>
>which, if char is unsigned, is a little pointless :-) (this was in the
>kernel, exactly where is at home :-( ).

IMHO:

>
>As an aside is the correct fix:
>
>		(signed char)tty->a_member_of_type_char == -1

This is not a desirable fix, because it assumes that one has access to
an ansi compiler.

>or
>		tty->a_member_of_type_char == (char)(-1)
>
>			Andrew

This is much better.

I would assume that on machines where the default character type is
unsigned, this is done because signed char operations are more expensive
than unsigned char operations. This is another reason to prefer the
second method over the first.


The real problem comes when you have code that compares:

	int x = -1;
	char y = -1;

	x == y

I guess then the only portable solution is to use ugly SIGN_EXTEND_CHAR()
macros... If you look in <sys/cdefs.h> it has something like

if !__STDC__
#define signed
#define const
#endif

So signed will become a noop.

christos