Optimal for loop on the 68020

Burkhard Neidecker-Lutz neideck at 49.1009.enet
Thu Jun 8 16:42:38 AEST 1989


Freshly off a DecStation 3100 with a MIPS 1.31 compiler:
 
#define COUNT 127
int main()
{
  int i;
     
  for ( i = 0; i < COUNT; ++i );
  	move	v1,zero
  	addiu	v1,v1,1
L1:  	slti	at,v1,127		# 3 cycle loop
  	bne	at,zero,L1
  	addiu	v1,v1,1
  for ( i = 0; ++i <= COUNT; );
  	li	v1,1
  	addiu	v1,v1,1
L2:  	slti	at,v1,128		# 3 cycle loop
  	bne	at,zero,L2
  	addiu	v1,v1,1
  for ( i = 0; i++ < COUNT; );
  	li	v1,1
L3:	slti	v0,v1,127		# 3 cycle loop
  	bne	v0,zero,L3
  	addiu	v1,v1,1
  for ( i = COUNT; i > 0; i-- );
  	li	v1,127
  	addiu	v1,v1,-1
L4:  	bgtz	v1,L4			# 2 cycle loop
  	addiu	v1,v1,-1
  for ( i = COUNT; i > 0; --i );
  	li	v1,127
  	addiu	v1,v1,-1
L5:  	bgtz	v1,L5			# 2 cycle loop
  	addiu	v1,v1,-1
  for ( i = COUNT; --i >= 0; );
  	li	v1,126
  	addiu	v1,v1,-1
L6:  	bgez	v1,L6			# 2 cycle loop
  	addiu	v1,v1,-1
  for ( i = COUNT; i-- > 0; );
  	li	v1,126
  	move	v0,v1
L7:	bgtz	v0,L7			# 2 cycle loop
  	addiu	v1,v1,-1
  return(i);
  	jr	ra
  	move	v0,v1
}
 
As usual, counting towards zero is more efficient than the upward direction,
but we knew that already, don't we ?
 
 
	Burkhard Neidecker-Lutz, Digital CEC Karlsruhe, Project NESTOR
 
Disclaimer: I don't speak for Digital, etc. ...



More information about the Comp.unix.wizards mailing list