By: Brendan (btrotter.delete@this.gmail.com), August 25, 2022 7:54 am
Room: Moderated Discussions
Hi,
Kara (karaardalan.delete@this.gmail.com) on August 24, 2022 2:07 pm wrote:
>
> Since like my second year programming I stopped writing branched code totally,
> few tens of thousands lines of code in, I haven't encounter anything that
> intrinsically requires speculative branch prediction, like logically.
>
> But I write only numes.
>
> Is there any program that can't be written without a branching that can't
> be rephrased (literally, all that is is rephrasing) into branchless?
Is it possible for a good and useful program to exist without at least one branch?
Just passing control from executable loader to your program involves an indirect branch, function pointers are indirect branches, function returns are just another type of indirect branch (pop a return address from stack and branch to it); "switch()" is (conditional or indirect) branches; all loops that can't be fully unrolled require branches; and things like "break;" and "continue;" are unconditional branches.
There's only 2 ways I can think of to avoid (all types of) branches:
a) Use self modifying code that continually appends pieces to linear/branchless sequence of instructions (like a train that lays train tracks in front of itself)
b) Lie. Make the programming language so high level and/or so specialized that the programmer doesn't notice that their code has branches (e.g. pixel shaders, where the outer "for each group of pixels" loop is implied).
- Brendan
Kara (karaardalan.delete@this.gmail.com) on August 24, 2022 2:07 pm wrote:
>
> Since like my second year programming I stopped writing branched code totally,
> few tens of thousands lines of code in, I haven't encounter anything that
> intrinsically requires speculative branch prediction, like logically.
>
> But I write only numes.
>
> Is there any program that can't be written without a branching that can't
> be rephrased (literally, all that is is rephrasing) into branchless?
Is it possible for a good and useful program to exist without at least one branch?
Just passing control from executable loader to your program involves an indirect branch, function pointers are indirect branches, function returns are just another type of indirect branch (pop a return address from stack and branch to it); "switch()" is (conditional or indirect) branches; all loops that can't be fully unrolled require branches; and things like "break;" and "continue;" are unconditional branches.
There's only 2 ways I can think of to avoid (all types of) branches:
a) Use self modifying code that continually appends pieces to linear/branchless sequence of instructions (like a train that lays train tracks in front of itself)
b) Lie. Make the programming language so high level and/or so specialized that the programmer doesn't notice that their code has branches (e.g. pixel shaders, where the outer "for each group of pixels" loop is implied).
- Brendan