Implement multiplication without using any loops, branches or function calls

1

I came up with the following in GNU C

r=0;m(a,b){x:goto*(int*[]){&&x,&&y}[r+=a&1?0:b,a>>=1,b<<=1,a<1];y:;}

One could argue there is a branch here a&1?0:b

So I also came up with this one

r=0;m(a,b){x:goto*(int*[]){&&x,&&y}[r+=(int[]){0,b}[a&1],a>>=1,b<<=1,a<1];y:;}

I'm depending on implicit int here as well.

Can you make it any smaller?

Explanation of the above code, or at least parts of how I got there can be found here http://codepad.org/L0MvnCe9

Must not use function calls (recursion, standard library functions like fma, exp, etc), loops (while, for, do), or branches (if, ?:, etc). No using the divide or multiply operator either (that's cheating.) goto is allowed for branching.

Good luck!

graphitemaster

Posted 2014-11-10T08:46:41.480

Reputation: 119

Question was closed 2014-11-10T20:16:19.717

14goto is a branch. – feersum – 2014-11-10T09:03:00.363

goto is a form of branch, yes. I was meaning branch in the more high level form where by if/?: though. – graphitemaster – 2014-11-10T09:09:00.110

1It's just an obfuscated form of if(a<1)goto x;. If an exact list of operators, keywords etc. which can't be used is provided it could be a valid code golf problem though. – feersum – 2014-11-10T09:12:09.037

It's literally impossible to do this without ANY sort of branch, you know. – Tal – 2014-11-10T10:10:34.697

2@Tal It's simple but tedious to do it without any branch, as long as the size of int is known, by calculating each of the n bits from the n^2 interactions between bits. – feersum – 2014-11-10T10:39:27.220

3Is C the only allowed language? – ProgramFOX – 2014-11-10T10:46:17.123

1Could you rearrange the information a bit, as it took me a while to see what the rules are. Also, how does the code pad link give explanation? Since it's quite small, you could move it into the question as well. – matsjoyce – 2014-11-10T19:23:22.873

No answers