Subject: strength-reduce
To: None <port-i386@NetBSD.ORG>
From: Hiroyuki Ito <hiroy@netcom.com>
List: port-i386
Date: 05/03/1996 16:02:35
------------------------ quote on ----------------------------
From: albert@krakatoa.ccs.neu.edu (Albert Cahalan)
Newsgroups: comp.os.linux.announce
Subject: gcc bug reminder
Followup-To: comp.os.linux.development.system
Date: Sun, 07 Jan 96 19:17:47 GMT
Organization: Northeastern University, College of Computer Science
Lines: 96
Approved: linux-announce@news.ornl.gov (Lars Wirzenius)
Message-ID: <cola-liw-821042267-8219-1@oravannahka.helsinki.fi>
NNTP-Posting-Host: kruuna.helsinki.fi
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

-----BEGIN PGP SIGNED MESSAGE-----

/* sr_bug.c
 *
 * This program tests for a gcc bug.
 * To compile this test program:  gcc -O2 sr_bug.c
 *
 * Sometimes gcc for Intel CPUs generates bad code at optimization
 * level 2.  The bad code is 'almost right' and stay hidden inside
 * a program that seems to work - including the Linux kernel.
 * The bug is very old and has been reported.  As of 19-Dec-95,
 * the bug has not been fixed.
 *
 * If you change this code you should test it, because even a
 * tiny change will hide this elusive bug.  If you think you
 * fixed the bug, please run the original test just to make
 * sure. You can find the original test below, after the #if 0.
 * I wrote this version of the test to be user-friendly, and
 * it may not be as solid as the original.
 *
 * Some people who know more than I do:
 * davis@space.mit.edu (John E. Davis)
 * anlauf@crunch.ikp.physik.th-darmstadt.de (Harald Anlauf)
 * craigs@iii2.iii.net (Craig Shrimpton)
 *
 * User-friendly version by Albert Cahalan
 *
 */
 
#include <stdio.h>

int gcc_sr_bug(void){
  static int Array[3]; /* must be static (or global) */
  unsigned int B = 3;  /* must be unsigned 32-bit */
  int i;
  for(i=0; i<B; i++) Array[i] = i - 3;
  for(i=0; i<B; i++) printf(" %d,%d", i, Array[i]);
  return !Array[1];
}

int main(){
  printf("Testing for gcc bug...");
  if(gcc_sr_bug()){
    printf("\n\nBad code! Your compiler generates bad output.\n\n");
    printf("Add -fno-strength-reduce to your gcc command line\n");
    printf("or put it into your gcc config file, such as in\n");
    printf("/usr/lib/gcc-lib/i486-linux/2.7.0/specs.\n");
    exit(1);
  }else{
    printf("\nOK, no problem.\n");
    exit(0);
  }
}

#if 0
/*********  original code + whitespace  ***********/

#include <stdio.h>

int A[3];
unsigned int B = 3;

void printit(void){
  int i;
  for(i = 0; i < B; i++) fprintf(stdout, "A[%d] = %d\n", i, A[i]);
}

int main(){
  int i;
  for(i = 0; i < B; i++) A[i] = i - 3;
  printit();
  return 0;
}

#endif
- --

Albert Cahalan
albert@ccs.neu.edu


-----BEGIN PGP SIGNATURE-----
Version: 2.6.2i

iQCVAwUBMPAaUoQRll5MupLRAQEHTwQA2Z2Mo09bkTwtBi2OA8sYOeXovKVh4VCN
DkRkOovLg5MzmTjxdd275fhKYiReNl5W0OEj2mbjXjJpmN6lFvDI3MMTE3O0W9fc
L+WxrH062leCcNUXzc1SFEr3AhnU3PRMb7gMao6zerl6vlQVYvNGOmCxG+GUZOHt
HYq1Q8Wro0c=
=hzty
-----END PGP SIGNATURE-----

-- 
This article has been digitally signed by the moderator, using PGP.
Finger wirzeniu@kruuna.helsinki.fi for PGP key needed for validating signature.
Send submissions for comp.os.linux.announce to: linux-announce@news.ornl.gov
PLEASE remember a short description of the software and the LOCATION.

------------------------ quote off ---------------------------

% uname -srm
NetBSD 1.1B i386
% cc -O2 sr_bug.c
% ./a.out
Testing for gcc bug... 0,-3 1,0 2,0

Bad code! Your compiler generates bad output.

Add -fno-strength-reduce to your gcc command line
or put it into your gcc config file, such as in
/usr/lib/gcc-lib/i486-linux/2.7.0/specs.
% cc -O2 -fno-strength-reduce sr_bug.c
% ./a.out
Testing for gcc bug... 0,-3 1,-2 2,-1
OK, no problem.
% cc -S -O2 -o 1.s sr_bug.c
% cc -S -O2 -fno-strength-reduce -o 2.s sr_bug.c
% diff -c 1.s 2.s
*** 1.s	Sat May  4 07:22:13 1996
--- 2.s	Sat May  4 07:22:45 1996
***************
*** 14,26 ****
  	pushl %esi
  	pushl %ebx
  	movl $3,%esi
! 	movl $-3,%eax
! 	xorl %edx,%edx
  	.align 2,0x90
  L9:
! 	movl %eax,_Array.4+12(,%eax,4)
! 	incl %eax
! 	cmpl %edx,%eax
  	jb L9
  	xorl %ebx,%ebx
  	cmpl %esi,%ebx
--- 14,26 ----
  	pushl %esi
  	pushl %ebx
  	movl $3,%esi
! 	xorl %ebx,%ebx
  	.align 2,0x90
  L9:
! 	leal -3(%ebx),%edx
! 	movl %edx,_Array.4(,%ebx,4)
! 	incl %ebx
! 	cmpl %esi,%ebx
  	jb L9
  	xorl %ebx,%ebx
  	cmpl %esi,%ebx
% 

  Q: Why "0,-3 1,0 2,0"?
  A: Because: edx (== 0) < eax (== 0xfffffffe)

Hiroyuki Ito