News for hackers. Stuff that matters.
Re:Branch-free FizzBuzz in Assembly by kmowery on Saturday January 03, 2015 @06:28PM (#8832473)
I've been writing a bunch of branch-free code recently, so I took a crack at it. In my world, data-dependent table lookups might as well be branches, so let's eliminate those as well.

Here's a fizzbuzz(char*, int) function that can accept any number up to 99999999, and will put the correct FizzBuzz result into the provided buffer (either the printed number, "Fizz ", " Buzz", or "FizzBuzz"). As promised, it's loop-free, and as a bonus it should be constant-time as well:

Assembly: http://pastebin.com/EnJEuxnp compiled from this C: http://pastebin.com/PCQQQ2cn [edit] generated from this Python: http://pastebin.com/ijr3thE2

Pastebinned since it's about 700 assembly instructions.

Unrolling the loop and printing to the screen are left as exercises...

Re:Branch-free FizzBuzz in Assembly by rertrree on Saturday January 03, 2015 @06:37PM (#8832495)
Oops: 0x00000001000015af <fizzbuzz+47>: cmp edx,0x0

                                        ^^^
Re:Branch-free FizzBuzz in Assembly by kmowery on Saturday January 03, 2015 @06:40PM (#8832503)
It does a cmp+setz, which isn't a branch (and I believe is constant-time). An x86 branch would be cmp+jne or something similar.
Re:Branch-free FizzBuzz in Assembly by drxzcl on Saturday January 03, 2015 @06:34PM (#8832488)
Do you mind sharing the python as well?
Re:Branch-free FizzBuzz in Assembly by kmowery on Saturday January 03, 2015 @06:42PM (#8832511)
Sure! It's ripped + modified from a project I'm working on, so it's a little messy:

http://pastebin.com/ijr3thE2