Please rid me of this goto

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 23 10:34:17 PDT 2016


On Thu, Jun 23, 2016 at 01:22:55PM -0400, Andrei Alexandrescu via Digitalmars-d wrote:
> So I was looking for an efficient exponentiation implementation, and
> http://www.stepanovpapers.com/PAM.pdf has a nice discussion starting
> at page 54. Stepanov's solution, however, duplicates code, so I
> eliminated it:
> 
> https://dpaste.dzfl.pl/e53acb41885a
> 
> The cost is the use of one goto. Can the code be restructured to not
> need it?
[...]

I don't understand why that goto is necessary. Or, indeed, why a nested
loop is needed at all. Why can't it be written like this?:

	outer: for (;;)
	{
		if (e % 2 != 0)
		{
			r = mul(r, b, overflow);
			if (e == 1) return r;
		}

		b = mul(b, b, overflow);
		e /= 2;
	}

AFAICT, the two possible code paths through the loops are the same.  Am
I missing something??


T

-- 
Winners never quit, quitters never win. But those who never quit AND never win are idiots.


More information about the Digitalmars-d mailing list