Fix char signedness
PATCH 1/1 Fix char signedness
If you want to put a negative number in a char you must use a signed char.
Andreas.
Index: fpu/softfloat-native.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.c,v retrieving revision 1.5
softfloat-native.c +6 -6
@@ -149,7 +149,7 @@ float32 float32_sqrt( float32 a STATUS_P {return sqrtf (a); } -char float32_compare( float32 a, float32 b STATUS_PARAM ) +signed char float32_compare( float32 a, float32 b STATUS_PARAM ) {if (a < b) {return -1 ;
@@ -161,7 +161,7 @@ char float32_compare( float32 a, float32return 2 ; } } -char float32_compare_quiet( float32 a, float32 b STATUS_PARAM ) +signed char float32_compare_quiet( float32 a, float32 b STATUS_PARAM ) {if (isless (a, b)) {return -1 ;
@@ -258,7 +258,7 @@ float64 float64_sqrt( float64 a STATUS_P {return sqrt (a); } -char float64_compare( float64 a, float64 b STATUS_PARAM ) +signed char float64_compare( float64 a, float64 b STATUS_PARAM ) {if (a < b) {return -1 ;
@@ -270,7 +270,7 @@ char float64_compare( float64 a, float64return 2 ; } } -char float64_compare_quiet( float64 a, float64 b STATUS_PARAM ) +signed char float64_compare_quiet( float64 a, float64 b STATUS_PARAM ) {if (isless (a, b)) {return -1 ;
@@ -350,7 +350,7 @@ floatx80 floatx80_sqrt( floatx80 a STATU {return sqrtl (a); } -char floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM ) +signed char floatx80_compare( floatx80 a, floatx80 b STATUS_PARAM ) {if (a < b) {return -1 ;
@@ -362,7 +362,7 @@ char floatx80_compare( floatx80 a, floatreturn 2 ; } } -char floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM ) +signed char floatx80_compare_quiet( floatx80 a, floatx80 b STATUS_PARAM ) {if (isless (a, b)) {return -1 ;
Index: fpu/softfloat-native.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat-native.h,v retrieving revision 1.7
softfloat-native.h +6 -6
@@ -181,8 +181,8 @@ INLINE char float32_unordered( float32 areturn isunordered (a, b); } -char float32_compare (float32 ,float32 STATUS_PARAM ); -char float32_compare_quiet (float32 ,float32 STATUS_PARAM ); +signed char float32_compare (float32 ,float32 STATUS_PARAM ); +signed char float32_compare_quiet (float32 ,float32 STATUS_PARAM );char float32_is_signaling_nan (float32 ); INLINE float32 float32_abs(float32 a)
@@ -263,8 +263,8 @@ INLINE char float64_unordered( float64 areturn isunordered (a, b); } -char float64_compare (float64 ,float64 STATUS_PARAM ); -char float64_compare_quiet (float64 ,float64 STATUS_PARAM ); +signed char float64_compare (float64 ,float64 STATUS_PARAM ); +signed char float64_compare_quiet (float64 ,float64 STATUS_PARAM );char float64_is_signaling_nan (float64 );flag float64_is_nan (float64 );
@@ -345,8 +345,8 @@ INLINE char floatx80_unordered( floatx80return isunordered (a, b); } -char floatx80_compare (floatx80 ,floatx80 STATUS_PARAM ); -char floatx80_compare_quiet (floatx80 ,floatx80 STATUS_PARAM ); +signed char floatx80_compare (floatx80 ,floatx80 STATUS_PARAM ); +signed char floatx80_compare_quiet (floatx80 ,floatx80 STATUS_PARAM );char floatx80_is_signaling_nan (floatx80 ); INLINE floatx80 floatx80_abs(floatx80 a)
Index: fpu/softfloat.c
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.c,v
retrieving revision 1.3
softfloat.c +3 -3
@@ -5283,7 +5283,7 @@ unsigned int float64_to_uint32_round_to_ }#define COMPARE (s, nan_exp) \ -INLINE char float ## s ## _compare_internal( float ## s a, float ## s b, \ +INLINE signed char float ## s ## _compare_internal( float ## s a, float ## s b, \ int is_quiet STATUS_PARAM ) \ { \ flag aSign, bSign; \
@@ -5317,12 +5317,12 @@ INLINE char float ## s ## _compare_inter } \ } \ \ -char float ## s ## _compare( float ## s a, float ## s b STATUS_PARAM ) \ +signed char float ## s ## _compare( float ## s a, float ## s b STATUS_PARAM ) \ { \ return float ## s ## _compare_internal(a, b, 0 STATUS_VAR); \ } \ \ -char float ## s ## _compare_quiet( float ## s a, float ## s b STATUS_PARAM ) \ +signed char float ## s ## _compare_quiet( float ## s a, float ## s b STATUS_PARAM ) \ { \ return float ## s ## _compare_internal(a, b, 1 STATUS_VAR); \ }
Index: fpu/softfloat.h
===================================================================
RCS file: /sources/qemu/qemu/fpu/softfloat.h,v
retrieving revision 1.4
softfloat.h +4 -4
@@ -234,8 +234,8 @@ char float32_lt( float32, float32 STATUSchar float32_eq_signaling (float32 ,float32 STATUS_PARAM );char float32_le_quiet (float32 ,float32 STATUS_PARAM );char float32_lt_quiet (float32 ,float32 STATUS_PARAM ); -char float32_compare (float32 ,float32 STATUS_PARAM ); -char float32_compare_quiet (float32 ,float32 STATUS_PARAM ); +signed char float32_compare (float32 ,float32 STATUS_PARAM ); +signed char float32_compare_quiet (float32 ,float32 STATUS_PARAM );char float32_is_signaling_nan (float32 );flag float64_is_nan (float64 a );
@@ -283,8 +283,8 @@ char float64_lt( float64, float64 STATUSchar float64_eq_signaling (float64 ,float64 STATUS_PARAM );char float64_le_quiet (float64 ,float64 STATUS_PARAM );char float64_lt_quiet (float64 ,float64 STATUS_PARAM ); -char float64_compare (float64 ,float64 STATUS_PARAM ); -char float64_compare_quiet (float64 ,float64 STATUS_PARAM ); +signed char float64_compare (float64 ,float64 STATUS_PARAM ); +signed char float64_compare_quiet (float64 ,float64 STATUS_PARAM );char float64_is_signaling_nan (float64 ); INLINE float64 float64_abs(float64 a)
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Hi,
Quoted 1 line · Andreas Schwab
If you want to put a negative number in a char you must use a signed char.
It has been a really long time I have been working on a broken system that did not default to "signed". Just out of curiosity: what system did you see the breakage on?
Ciao,
Dscho
Quoted 4 lines · Johannes Schindelin
On Fri, 27 Oct 2006, Andreas Schwab wrote:
It has been a really long time I have been working on a broken system that did not default to "signed". Just out of curiosity: what system did you see the breakage on?
AFAIK, it's not a broken system. The default signedness of char is undefined and I remember reading something on arm.linux.org.uk about ARM compilers (including gcc) using the opposite default to the rest of the archs.
Regards,
Quoted 2 lines · Johannes Schindelin
It has been a really long time I have been working on a broken system that did not default to "signed".
The only thing that is broken is your knowlege of C.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
In message: <[email protected]>
Andreas Schwab <[email protected]> writes:
: Johannes Schindelin <[email protected]> writes:
:
: > It has been a really long time I have been working on a broken system that : > did not default to "signed".
:
: The only thing that is broken is your knowlege of C.
gcc on ARM systems default to unsigned. The C standard specifically states that char is either signed or unsigned at the whim of the implementor:
Section 6.2.5 para 15:
[#15] The three types char, signed char, and unsigned char
are collectively called the character types. The
implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.30)
...
30)CHAR_MIN, defined in <limits.h>, will have one of the
values 0 or SCHAR_MIN, and this can be used to
distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.
...
I just confirmed that gcc on arm does indeed default to unsigned chars:
% cat xxx.c
signed char cs = -1;
unsigned char cu = -1;
char c = -1;
void foo()
{
int i = 0;
if (cs < 0) i++;
if (cu < 0) i++;
if (c < 0) i++;
}% gcc -v
Using built-in specs.
Configured with: FreeBSD/arm system compiler
Thread model: posix
gcc version 3.4.4 [FreeBSD] 20050518
% gcc -W -c xxx.cxxx.c: In function `foo':
xxx.c:9: warning: comparison is always false due to limited range of data type xxx.c:10: warning: comparison is always false due to limited range of data type %
Note: line 10 is the naked char.
Warner
Hi,
Quoted 6 lines · Andreas Schwab
Quoted 2 lines · Johannes Schindelin
It has been a really long time I have been working on a broken system that did not default to "signed".
The only thing that is broken is your knowlege of C.
Okay.
And what system did you encounter this behaviour on?
Ciao,
Dscho
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
It has been a really long time I have been working on a broken system that did not default to "signed".
The only thing that is broken is your knowlege of C.
Okay.
And what system did you encounter this behaviour on?
Common arm, ppc, s390 and mips systems all have default char as unsigned. As do a few other less common embedded systems.
Paul
In message: <Pine.LNX.4.63.0610280057240.26682@wbgn013.biozentrum.uni-wuerzburg.de>
Johannes Schindelin <[email protected]> writes:
: Hi,
:
: On Fri, 27 Oct 2006, Andreas Schwab wrote:
:
: > Johannes Schindelin <[email protected]> writes:
: >
: > > It has been a really long time I have been working on a broken system that : > > did not default to "signed".
: >
: > The only thing that is broken is your knowlege of C.
:
: Okay.
:
: And what system did you encounter this behaviour on?
It is the default for arm with gcc, for one.
The behavior is standards conforming, per section 6.2.5, para 15 of ANSI-C (1989) standard. Your characterization of it as 'broken' is wrong.
Warner
Quoted 6 lines · Johannes Schindelin
Hi,
Quoted 2 lines · Andreas Schwab
Johannes Schindelin <[email protected]> writes:
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
It has been a really long time I have been working on a broken system that did not default to "signed".
The only thing that is broken is your knowlege of C.
Okay.
And what system did you encounter this behaviour on?
AFAIR powerpc and ARM are the usual suspects for unsigned chars.
Thiemo
Quoted 2 lines · Johannes Schindelin
And what system did you encounter this behaviour on?
Do a:
grep -wr DEFAULT_SIGNED_CHAR gcc/config
in a gcc source tree and you will get a list for gcc targets :)
Laurent
Quoted 3 lines
gcc on ARM systems default to unsigned. The C standard specifically states that char is either signed or unsigned at the whim of the implementor
Or, more to the point, at the behest of the machine architecture. Having to generate code to sign-extend the hard way every time you do char-integer promotion if the hardware doesn't do it automatically would be long and inefficient, specially since it happens all the time.
This has been a problem since the "All the world's a VAX" days, the classic boob being
char c;
while ((c = getchar()) != EOF) {
...
}of course, 255 always != -1 so it loops forever.
Check how many of your C primers get this wrong!
The flipside of that boob is that on a signed char architecture, the loop will exit prematurely when it meets a 255 character.
These days instead, all the world's a 386 and anything different is broken...
24-bit integers, anyone? :)
M
Quoted 11 lines · Paul Brook
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
It has been a really long time I have been working on a broken system that did not default to "signed".
The only thing that is broken is your knowlege of C.
Okay.
And what system did you encounter this behaviour on?
Common arm, ppc, s390 and mips systems all have default char as unsigned. As do a few other less common embedded systems.
BusyBox added "-funsigned-char" to our build to get consistent behavior. (I picked unsigned because that way we were automatically 8-bit clean in things like sed.)
(I note that tcc doesn't work right if built with -funsigned-char, so I wouldn't be too surprised if qemu had the same problem.) In both cases, specifying -fsigned-char in the build should fix it.
Quoted 1 line
Paul
Rob
Signature · 2 lines
"Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away." - Antoine de Saint-Exupery
Quoted 9 lines · Martin Guy
Quoted 3 lines
gcc on ARM systems default to unsigned. The C standard specifically states that char is either signed or unsigned at the whim of the implementor
Or, more to the point, at the behest of the machine architecture. Having to generate code to sign-extend the hard way every time you do char-integer promotion if the hardware doesn't do it automatically would be long and inefficient, specially since it happens all the time.
I'd rather have inefficient than broken. And in general, using "char" as a small "int" is going to _suck_ on arm. The extra code size more than makes up for the data size you save if you only have one or two instances of your data at a time.
Rob
Signature · 2 lines
"Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away." - Antoine de Saint-Exupery