Z80 Divide by 10

Sean Conner spc at conman.org
Sat Jan 26 16:21:56 CST 2008


It was thus said that the Great dwight elvey once stated:
> 
> 230 clock cycles and no conditionals
> Dwight

  I came across this bit of code from http://www.hackersdelight.org/ to
divide by 10:

	unsigned int div10(unsigned int n)
	{
	  unsigned int q;
	  unsigned int r;

	  q = (n >> 1) + (n >> 2);
	  q = q + (q >> 4);
	  q = q + (q >> 8);
	  q = q + (q >> 16);
	  q = q >> 3;
	  r = n - ((q << 3) + (q << 1));
	  return (q + ((r + 6) >> 4);
	}

  On the Z80, the (q >> 8) is trivial to handle, and you can probably skip
the (q >> 16) statement all-to-gether (since you're only handling 16 bits
anyway, this reduces to 0 so the statement there can be skipped).  My Z80
skills are pretty weak (and I don't have any references at hand right now)
but this looks pretty straight forward to translate.

  -spc (In fact, the HackersDelight site is quite delightful)






More information about the cctalk mailing list