From: stevec@geog.ubc.ca (Steve Cumming)
Newsgroups: gnu.gcc.bug
Subject: Bug Report
Date: 25 Jan 1993 20:14:32 -0500
Organization: GNUs Not Usenet
Distribution: gnu

Bug genus:	Apparent problem with gcc -O -g when doing
		unsigned int arithmetic and compares. 
		A shar file containing stuff to tickle the bug is
		appended. The bug disappears when compiled without
		-O.

Architecture: 	Sun SPARCstation 1+

OS:		SunOS Release 4.1.2 (GENERIC)

gcc -v:		Reading specs from /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.3/specs
		gcc version 2.3.3

configure options: Unavailable to me.


To examine the bug, unshar the appended file and use make. This will create
the bitmap executable. Stepping through gdb reveals:

    (gdb)Breakpoint 1, main () at bitmap.c:276
    (gdb)n
    (gdb)s
    b_line (x1=0, y1=0, x2=100, y2=100) at bitmap.c:235
    (gdb) n 9
    (gdb) where
    #0  b_line (x1=0, y1=0, x2=16384, y2=100) at bitmap.c:260
    #1  0x283c in main () at bitmap.c:277

Note that function argument x2 gets changed without warrant from me, during
an unsigned int compare. The opposite behaviour is encountered when b_line
is called such that (dx > dy) causes the other while loop to be
executed, in which case function argument y2 gets clobbered.

I hope this is necessary and sufficient data.

Steve Cumming		 boreal mixedwood dynamics and rogue.
stevec@geog.ubc.ca		    ... how long you got left?
				        well how long do you reckon?
							

-----------------------CLIP HERE-------------------------------------
#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  Makefile
#	  bitmap.c
#	  bitmap.h
#
sed 's/^X//' << 'SHAR_EOF' > Makefile &&
XCC     = gcc # the C compiler
XCOPTS  = -g -O -DTEST#if you trust your compiler's optimizer
XLD     =$(CC) $(CFLAGS)	    # default loading command
X
X	  
Xbitmap:	  bitmap.c bitmap.h Makefile 
X	 gcc  -DTEST -DBITMAPDEBUG -g -O bitmap.c   -o bitmap -lc
SHAR_EOF
chmod 0644 Makefile || echo "restore of Makefile fails"
sed 's/^X//' << 'SHAR_EOF' > bitmap.c &&
X#ifndef lint
Xstatic char *RCSid = "$Id: bitmap.c,v 3.26 92/03/24 22:34:39 woo Exp Locker: woo $";
X#endif
X
X
X/* GNUPLOT - bitmap.c */
X/*
X * Copyright (C) 1986, 1987, 1990, 1991, 1992   Thomas Williams, Colin Kelley
X *
X * Permission to use, copy, and distribute this software and its
X * documentation for any purpose with or without fee is hereby granted, 
X * provided that the above copyright notice appear in all copies and 
X * that both that copyright notice and this permission notice appear 
X * in supporting documentation.
X *
X * Permission to modify the software is granted, but not the right to
X * distribute the modified code.  Modifications are to be distributed 
X * as patches to released version.
X *  
X * This software is provided "as is" without express or implied warranty.
X * 
X *
X * AUTHORS
X * 
X *   Original Software:
X *     Jyrki Yli-Nokari <jty@intrin.UUCP>
X *     Ronald J. Hartranft <rjh2@ns.cc.lehigh.edu>
X *     Russell Lang <rjl@monu1.cc.monash.edu.au>
X * 
X * Send your comments or suggestions to 
X *  info-gnuplot@ames.arc.nasa.gov.
X * This is a mailing list; to join it send a note to 
X *  info-gnuplot-request@ames.arc.nasa.gov.  
X * Send bug reports to
X *  bug-gnuplot@ames.arc.nasa.gov.
X */
X
X/*
X** General raster plotting routines.
X** Raster routines written and copyrighted 1987 by
X** Jyrki Yli-Nokari (jty@intrin.UUCP)
X** Intrinsic, Ltd.
X**
X** You may use this code for anything you like as long as
X** you are not selling it and the credit is given and
X** this message retained.
X**
X*/
X
X/* Bitmap plotting routines derived from above raster plotting routines
X * Russell Lang, 1990
X */
X
X
X#include <stdio.h>
X#include "bitmap.h"
X
Xbitmap *b_p = (bitmap *)NULL;	/* global pointer to bitmap */
Xunsigned int b_currx, b_curry;	/* the current coordinates */
Xunsigned int b_xsize, b_ysize;	/* the size of the bitmap */
Xunsigned int b_planes;			/* number of color planes */
Xunsigned int b_psize;			/* size of each plane */
Xunsigned int b_rastermode;		/* raster mode rotates -90deg */
Xunsigned int b_linemask = 0xffff;	/* 16 bit mask for dotted lines */
Xunsigned int b_value = 1;		/* colour of lines */
Xunsigned int b_hchar;			/* width of characters */
Xunsigned int b_hbits;			/* actual bits in char horizontally */
Xunsigned int b_vchar;			/* height of characters */
Xunsigned int b_vbits;			/* actual bits in char vertically */
Xunsigned int b_angle;			/* rotation of text */
Xunsigned int b_pattern[] = {0xffff, 0x1111,
X	0xffff, 0x5555, 0x3333, 0x7777, 0x3f3f, 0x0f0f, 0x5f5f};
Xint b_maskcount = 0;
Xunsigned int b_lastx, b_lasty;	/* last pixel set - used by b_line */
X
X#define IN(i,size)  ((unsigned)i < (unsigned)size)
X
X
X/*
X** The plotting area is defined as a huge bitmap.
X** The bitmap is stored in a dynamically allocated pixel array b_p
X**
X** The bitmap is allocated (and initialized to zero) with
X** b_makebitmap(xsize, ysize, planes)
X** and freed with b_freebitmap()
X** xsize and ysize will be rounded up to a multiple of 8.
X**
X** Valid (int) coordinates range from zero to (xsize-1,ysize-1)
X**
X** Plotting is done via b_move(x, y) and b_vector(x, y) functions,
X** where the point (x,y) is the target to go from the current point
X** To set the color use b_setvalue(value) where value is the value 
X** (0 or 1 or a color number) to be stored in every pixel.
X** To get dotted line styles, use b_setlinetype(linetype).
X**
X** Internally all plotting goes through b_setpixel(x, y, value).
X*/
X
X
X/*
X** set pixel (x, y, value) to value value (this can be 1/0 or a color number).
X*/
Xvoid
Xb_setpixel(unsigned int x,unsigned int y, unsigned int value)
X{
X  register unsigned int row;
X  register unsigned char mask;
X  int i;
X  if (b_rastermode) {
X	/* interchange so that new (x,y) is old (y,b_ysize-1-x) */
X	row = x;  /* temp storage */
X	x = y;
X	y = b_ysize-1-row;
X  }
X  if (IN(x, b_xsize) && IN(y, b_ysize))
X  {
X    row = y/8;
X    mask = 1<<(y%8);
X
X	for (i=0; i<b_planes; i++) {
X		if (value&1)
X			*((*b_p)[row]+x) |= mask;
X		else
X			*((*b_p)[row]+x) &= ~mask;
X		row += b_psize;
X		value >>= 1;
X	}
X  }
X#ifdef BITMAPDEBUG
X  else
X  {
X    if (b_rastermode)
X      fprintf(stderr, "Warning: setpixel(%d, %d, %d) out of bounds\n", 
X		b_ysize-1-y, x, value);
X    else
X      fprintf(stderr, "Warning: setpixel(%d, %d, %d) out of bounds\n",
X		x, y, value);
X  }
X#endif
X}
X
X
X/*
X** allocate the bitmap
X*/
Xvoid
Xb_makebitmap(unsigned int x, unsigned int y, unsigned int planes)
X{
X  register unsigned j;
X  unsigned rows;
X
X  x = 8 * (unsigned int)(x/8.0+0.9);	/* round up to multiple of 8 */
X  y = 8 * (unsigned int)(y/8.0+0.9);	/* round up to multiple of 8 */
X  b_psize = y/8;					/* size of each plane */
X  rows = b_psize * planes;			/* total number of rows of 8 pixels high */
X  b_xsize = x; b_ysize = y;
X  b_currx = b_curry = 0;
X  b_planes = planes;
X  b_value = 1;
X  b_angle = 0;
X  b_rastermode = 0;
X  /* allocate row pointers */
X  b_p = (bitmap *)malloc( rows * sizeof(pixels *));
X
X  bzero(b_p, rows * sizeof(pixels *));
X  for (j = 0; j < rows; j++) {
X    /* allocate bitmap buffers */
X    (*b_p)[j] = (pixels *)malloc(x * sizeof(pixels),(char *)NULL);
X    if ((*b_p)[j] == (pixels *)NULL) {
X		b_freebitmap();  /* free what we have already allocated */
X	}
X    bzero((*b_p)[j], x * sizeof(pixels));
X  }
X}
X
X/*
X** free the allocated bitmap
X*/
Xvoid
Xb_freebitmap(void)
X{
X  int j;
X  unsigned rows;
X
X  rows = b_psize * b_planes;   /* total number of rows of 8 pixels high */
X  for (j = 0; j < rows; j++)
X  {
X    (void) free((char *)(*b_p)[j]);
X  }
X  (void) free((char *)b_p);
X  b_p = (bitmap *)(NULL);
X}
X
X/*
X** set pixel at (x,y) with color b_value and dotted mask b_linemask.
X*/
X
Xvoid
Xb_setmaskpixel(unsigned int x, unsigned int y, unsigned int value)
X{
X	/* dotted line generator */
X	if ((b_linemask>>b_maskcount)&(unsigned int)(1)) {
X		b_setpixel(x,y,value);
X	}
X	b_maskcount= (b_maskcount+1) % 16;
X	b_lastx= x;  /* last pixel set with mask */
X	b_lasty= y;
X}
X
X
X
X/******   DEBUG INFO
X
X  The problem appears during the evaluation of
X  either while condition.
X
X  In gdb, the registers containing the function arguments
X  get their values changed.
X
X*******/
X
X/*
X** draw a line from (x1,y1) to (x2,y2)
X** with color b_value and dotted mask b_linemask.
X*/
Xvoid
Xb_line(unsigned int x1,unsigned int y1, unsigned int x2, unsigned int y2)
X{
Xint runcount;
Xint dx,dy;
Xint xinc,yinc;
Xunsigned int xplot,yplot;
X
X	runcount=0;
X	dx = abs((int)(x1)-(int)(x2));
X	if (x2>x1)  xinc=  1;
X	if (x2==x1) xinc=  0;
X	if (x2<x1)  xinc= -1;
X	dy = abs((int)(y1)-(int)(y2));
X	if (y2>y1)  yinc=  1;
X	if (y2==y1) yinc=  0;
X	if (y2<y1)  yinc= -1;
X	xplot=x1;
X	yplot=y1;
X	if (dx>dy) {
X		/* iterate x */
X		while (xplot!=x2) {
X			xplot+=xinc;
X			runcount+=dy;
X			if (runcount>=(dx-runcount)) {
X				yplot+=yinc;
X				runcount-=dx;
X			}
X		        b_setmaskpixel(xplot,yplot,b_value);
X		}
X	} else {
X		/* iterate y */
X		while (yplot!=y2) {
X			yplot+=yinc;
X			runcount+=dx;
X			if (runcount>=(dy-runcount)) {
X				xplot+=xinc;
X				runcount-=dy;
X			}
X			b_setmaskpixel(xplot,yplot,b_value);
X		}
X	    }
X}
X
X
X
Xmain()
X
X{
X 
X    b_makebitmap(1000,1000,3);
X    b_line(0,0,100,100);
X    return;
X}
SHAR_EOF
chmod 0644 bitmap.c || echo "restore of bitmap.c fails"
sed 's/^X//' << 'SHAR_EOF' > bitmap.h &&
X/*
X * $Id: bitmap.h,v 3.26 92/03/24 22:34:16 woo Exp Locker: woo $
X */
X
X/* bitmap.h */
X#ifdef __TURBOC__
X#define FAR far
X#else
X#define FAR
X#endif
X
X/* allow up to 16 bit width for character array */
Xtypedef unsigned int char_row;
Xtypedef char_row * FAR char_box;
X
X
Xtypedef unsigned char pixels;  /* the type of one set of 8 pixels in bitmap */
Xtypedef pixels *bitmap[];  /* the bitmap */
X
Xextern bitmap *b_p;						/* global pointer to bitmap */
Xextern unsigned int b_currx, b_curry;	/* the current coordinates */
Xextern unsigned int b_xsize, b_ysize;	/* the size of the bitmap */
Xextern unsigned int b_planes;			/* number of color planes */
Xextern unsigned int b_psize;			/* size of each plane */
Xextern unsigned int b_rastermode;		/* raster mode rotates -90deg */
Xextern unsigned int b_linemask;			/* 16 bit mask for dotted lines */
Xextern unsigned int b_value;			/* colour of lines */
Xextern unsigned int b_hchar;			/* width of characters */
Xextern unsigned int b_hbits;			/* actual bits in char horizontally */
Xextern unsigned int b_vchar;			/* height of characters */
Xextern unsigned int b_vbits;			/* actual bits in char vertically */
Xextern unsigned int b_angle;			/* rotation of text */
Xextern unsigned int b_pattern[];
Xextern int b_maskcount;
Xextern unsigned int b_lastx, b_lasty;	/* last pixel set - used by b_line */
X
X
Xextern void b_makebitmap(unsigned int,unsigned int,unsigned int);
Xextern void b_freebitmap(void);
Xextern void b_setpixel(unsigned int,unsigned int,unsigned int);
Xextern void b_line(unsigned int, unsigned int, unsigned int, unsigned int);
Xextern void b_setmaskpixel(unsigned int,unsigned int,unsigned into);
SHAR_EOF
chmod 0644 bitmap.h || echo "restore of bitmap.h fails"
exit 0