c/1061: bug in short arithmetic

matsc@sics.se matsc@sics.se
Thu Dec 14 05:46:00 GMT 2000

>Number:         1061
>Category:       c
>Synopsis:       bug in short arithmetic
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Dec 14 05:46:01 PST 2000
>Originator:     Mats Carlsson
>Release:        gcc version 2.95.2 19991024 (release)
SunOS scheutz 5.7 Generic_106541-12 sun4u sparc SUNW,Ultra-Enterprise
The attached program should not print OOPS.
The variable 'datum' seems to get the wrong value,
causing a comparison to fail.
gcc -O2 sparc_bug.c ; ./a.out

#include <stdio.h>

typedef unsigned short HALF;

# define revhalf(S) ((((S)&0xff)<<8)|(((S)&0xff00)>>8))

static HALF dest[] = {0xf801,0x0};

  HALF datum;

  datum = dest[0];
  if (datum & 1)
    datum = revhalf(datum);
  if (datum!=0x1f8)
Work-around: declare datum as unsigned int instead.

More information about the Gcc-bugs mailing list