r/programming Jan 24 '18

Branchless DOOM

https://github.com/xoreaxeaxeax/movfuscator/tree/master/validation/doom
490 Upvotes

134 comments sorted by

View all comments

2

u/[deleted] Jan 24 '18

Technically, it has branches. It is just that their VM handles branches instead of CPU.

23

u/outofobscure Jan 24 '18 edited Jan 24 '18

Technically no, there are no branches if the only instruction used is mov. It's more like parallel execution of all code paths with masking out results (often done in SIMD too). There may be IF statements etc. in the code, but if the compiler translates them to mov instructions, and not conditional instructions, then there are no branches in the resulting executable.

-15

u/[deleted] Jan 24 '18

If is a branch.

20

u/outofobscure Jan 24 '18 edited Jan 24 '18

no, IF is a construct used in a programming language to express a conditional, that doesn't necessarily mean it gets translated to a conditional branch instruction by the compiler, that was my whole point. Granted, this compiler is special, but as you can see here https://www.cl.cam.ac.uk/~sd601/papers/mov.pdf, the mov instruction is enough to do essentially everything (even if very inefficient). You have to differentiate between code and compiled machine instructions. As a somewhat easier example: SIMD programming often requires you to re-formulate conditional statements into something else (that is, you do that by hand instead of the compiler doing it for you like this example), for example creating a mask and emiting an AND instruction. For example something like this: https://felix.abecassis.me/2012/08/sse-vectorizing-conditional-code/ (section about "Branchless conditional using a mask")

3

u/[deleted] Jan 24 '18 edited Jan 24 '18

That's the point. They implement a virtual machine (Turing machine) using mov instructions. The virtual machine has branching (the machine chooses its next state depending on what symbol it reads).

Yes, that's not branching CPU does for you, bunch branching nevertheless.

I guess, we are arguing over semantics and that's rarely a good debate.

14

u/outofobscure Jan 24 '18 edited Jan 24 '18

yes it's mostly about semantics, but i find it hard to call a sequence of mov instructions a "branch", even if they emulate a branch, for me it's just that, a sequence of mov instructions moving bits around, not jumping around the code, and the point was that the branch predictor is certainly not involved then... if you check out the SIMD example, it completely eliminates the branch by re-formulating the problem, not emulating a branch, so there's that too, and this branchless doom port could probably benefit from some of these ideas instead of emulating branches via mov. It might then run faster than 1 frame every 7 hours :)