Hi all,

I have recently run into a non-portable c-torture test  
(gcc.c-torture/execute/20030128-1.c) and would like to suggest an update for 
it.

The test performs division of unsigned char by (signed) short:

unsigned char x = 50;
volatile short y = -5;

int main ()
{
  x /= y;

and checks that signed division operation is performed:
if (x != (unsigned char) -10)
  abort ();

But according to C99 Standard (see Usual arithmetic conversions, 6.3.1.8):
1) "if the operand that has unsigned integer type has rank greater or equal to 
the rank of the type of the other operand, then the operand with signed 
integer type is converted to the type of the operand with unsigned integer 
type."
2) "Otherwise, if the type of the operand with signed integer type can 
represent all of the values of the type of the operand with unsigned integer 
type, then the operand with unsigned integer type is converted to the type of 
the operand with signed integer type."

so signed division is performed only if SHRT_MAX > UCHAR_MAX (otherwise 
division is unsigned).

I suggest to change the test to

if( SHRT_MAX > UCHAR_MAX && x != (unsigned char) -10)
  abort ();

(see the attached patch).

If I am correct, could someone commit the patch? I am not a gcc developer.

-- 
Best regards,
Yuri
*** gcc/gcc/testsuite/gcc.c-torture/execute/20030128-1.c	2010-01-11 18:59:41.000000000 +0300
--- 20030128-1.c	2010-01-18 14:16:13.000000000 +0300
***************
*** 1,10 ****
  unsigned char x = 50;
  volatile short y = -5;
  
  int main ()
  {
    x /= y;
!   if (x != (unsigned char) -10)
      abort ();
    exit (0);
  }
--- 1,12 ----
+ #include <limits.h>
+ 
  unsigned char x = 50;
  volatile short y = -5;
  
  int main ()
  {
    x /= y;
!   if (SHRT_MAX > UCHAR_MAX && x != (unsigned char) -10)
      abort ();
    exit (0);
  }

Reply via email to