This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc does not unroll loops && does not skip unnecessary if-blocks??????


Hi!
egcs-2.91.66
Linux dittrich 2.2.12-20 #9 Wed Dec 22 15:27:47 CET 1999 i686 unknown (Red Hat)


1.) gcc does not seem to be able to unroll a simple loop!
                        for(int i=0; i< dim; i++){
                                a[i] = b[i]*3.123;
                        }    
should become
                        a[0] = b[0]*3.123;
for a const int dim = 1;
The compiler does not perform this simple optimization.
Turning on -funroll-loops does not help either.
2.) The following situation is not detected by the compiler
const int dim = 1
if(dim == 1){
    doSomething();
}
else
{
    doWhatever();
}
This should become:
    doSomething();
Why not?
The TestCode is included in this mail.


Manual:
Try to uncomment either line 49 or 50 in Test.cpp.
case 1:
    line 49 is uncommented, i.e.
g++ -funroll-loops -O2 Test.cpp -otest
ls -l test
-rwxr-xr-x   1 jd       users       13538 Jan 21 11:56 test*
runtime:
0.344 sec


case 2:
    line 50 is uncommented, i.e.
g++ -funroll-loops -O2 Test.cpp -otest
ls -l test
-rwxr-xr-x   1 jd       users       13498 Jan 21 11:58 test*
THE SIZE OF THE OUTPUT-FILE DIFFERS!!!
runtime:
0.325 sec
How is that possible?
It would be very helpul if you could clarify these strange effects.
Thank you!


Sincerely


Jens Dittrich




-- 
Jens-Peter Dittrich                          jens@d1ttr1ch.de, d1ttr1ch.de

Database Research Group, Prof. Dr. Seeger    www.mathematik.uni-marburg.de/DBS
Marburg University, Germany                  www.uni-marburg.de
 
#include <iostream.h>
#include "TimeMeasure2.h"

const int dim = 1;

class Test{
public:
	double a[dim];

	//case 1:
	void f1(double* b){
		if(dim==1){	
			a[0] = b[0]*3.123;
		}
		else
		{
			for(int i=0; i< dim; i++){
				a[i] = b[i]*3.123;
			}
		}/**/
	}

	//case 2:
	void f2(double* b){
//		if(dim==1){	
			a[0] = b[0]*3.123; //only this line included in the code
/*		}
		else
		{
			for(int i=0; i< dim; i++){
				a[i] = b[i]*3.123;
			}
		}/**/
	}

};


void main(){
	double b[2];
	Test t;
		
	MyTimeval time;
	TimeMeasure2 timer;
	timer.start();
	for(double a=0.0; a<1000;a+=0.0001){
		b[0] = a * 63.14;
		b[1] = a * 13.546;
		//t.f1(b);	//case 1
		t.f2(b);	//case 2	
	}
	timer.stopABS(time);
	time.show();
	cout << endl;
}
#ifndef _TimeMeasure2
#define _TimeMeasure2

/*
	Messen von Zeiten in [Sekunden].[Millisekunden]
*/

#include <iostream.h>
#include <sys/time.h>
#include <unistd.h>
#include "types.h"

class MyTimeval: public timeval{
public:
	inline void reset(){tv_sec=tv_usec=0;}

	MyTimeval(){reset();}

	inline friend MyTimeval operator+ (const MyTimeval& a, 
					   const MyTimeval& b){
		MyTimeval ret;

		ret.tv_sec = a.tv_sec + b.tv_sec;
		ret.tv_usec = a.tv_usec + b.tv_usec;
		
		if(ret.tv_usec >= (ulong)1E6){
			ret.tv_usec-= (ulong)1E6;
			ret.tv_sec++;
		}
		return ret;
	}

	inline friend MyTimeval operator+= (MyTimeval& a, MyTimeval& b){
		a.tv_sec  += b.tv_sec;
		a.tv_usec += b.tv_usec;
		if(a.tv_usec >=    (ulong)1E6){
			a.tv_usec-= (ulong)1E6;
			a.tv_sec++;
		}
		return a;
	}

	inline friend MyTimeval operator- (const MyTimeval& a, 
					   const MyTimeval& b){
		if(a<b){ cout << "Fehler: MyTimeval operator-: a<b"<<endl;
			return a;}
		MyTimeval ret;
		ret.tv_sec = a.tv_sec - b.tv_sec;
		long temp = a.tv_usec - b.tv_usec;
		if(temp>=0)
			ret.tv_usec = temp;
		else
		{
			ret.tv_usec = temp + (ulong)1E6;
			ret.tv_sec--;
		}

		return ret;
	}

	inline friend double operator/ (MyTimeval& a, MyTimeval& b){

	    return
		(a.tv_sec+ (a.tv_usec/ double(1E6)))
			/
		(b.tv_sec+ (b.tv_usec/ double(1E6)));
	}

	inline friend int operator< (const MyTimeval& a, const MyTimeval& b){
		if( (a.tv_sec < b.tv_sec)||
		
			((a.tv_sec == b.tv_sec)&&(a.tv_usec < b.tv_usec)))
			return true;
		else
			return false;
	}

	inline void show(){
		cout.precision(6);
		cout.setf(ios::fixed);
		cout.width(9);
		cout << tv_sec+ (tv_usec/ double(1E6));
	}
};



class TimeMeasure2{
	MyTimeval vor;
	MyTimeval nach;
public:
	inline long start(){return gettimeofday(&vor,NULL);}

	inline long stop(MyTimeval& delta){
		long ret = gettimeofday(&nach,NULL);
		if(nach.tv_sec == vor.tv_sec){
		  delta.tv_usec += nach.tv_usec - vor.tv_usec;
		  //delta.tv_sec = 0;
		}
		else // d.h. sec2 > sec1
		{
		  delta.tv_sec += nach.tv_sec - vor.tv_sec;
		  delta.tv_usec += nach.tv_usec - vor.tv_usec;
		  if(nach.tv_usec < vor.tv_usec){
			delta.tv_sec--;
			delta.tv_usec+= (ulong)1E6;
		  }
		}//else	

		return ret;
	}//stop

	inline long stopABS(MyTimeval& delta){
		long ret = gettimeofday(&nach,NULL);
		if(nach.tv_sec == vor.tv_sec){
		  delta.tv_usec = nach.tv_usec - vor.tv_usec;
		  delta.tv_sec = 0;
		}
		else // d.h. sec2 > sec1
		{
		  delta.tv_sec = nach.tv_sec - vor.tv_sec;
		  delta.tv_usec = nach.tv_usec - vor.tv_usec;
		  if(nach.tv_usec < vor.tv_usec){
			delta.tv_sec--;
			delta.tv_usec+= (ulong)1E6;
		  }
		}//else	

		return ret;
	}//stop

};


#endif // _TimeMeasure2

#ifndef _Types
#define _Types

/*
	zentrale Typ-Definitionen fuer das gesamte Projekt
*/

typedef unsigned char byte;
typedef unsigned short ushort;
typedef unsigned long ulong;

enum JOINTYPE 		{ JT_UNDEFINED, JT_S3J, JT_S3J_WITH_JUMP, JT_PBSM, 
			  JT_NxN };
enum RFC_MAPPING  	{ HILBERT, PEANO, SIMPLE_RECURSIVE };

enum DATASET 		{ LEFT, RIGHT, BOTH };

#endif // _Types

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]