By: ananon (whhhyy.delete@this.trashcan8765.com), August 25, 2022 12:56 am
Room: Moderated Discussions
Mark Roulo (nothanks.delete@this.xxx.com) on August 24, 2022 4:25 pm wrote:
> 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?
> >
> > Mind risc-v don't offer BPU, there's extensions but not in the requirements.
>
> How do you write code to do one thing if a condition holds and another thing if the condition doesn't hold?
>
> Example:
>
>
How you write code is extremely important, because it has to be read and understood by both humans and compilers. Aiming for clean and straightforward code whenever possible should be your priority, not to try doing micro-optimizations while writing functional code.
> 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?
> >
> > Mind risc-v don't offer BPU, there's extensions but not in the requirements.
>
> How do you write code to do one thing if a condition holds and another thing if the condition doesn't hold?
>
> Example:
> I'll note that the conditional operator (?) is a branch.
> if (b)
> printf("Hello");
> else
> printf("Goodbye");
>
>
>
Theoretically you could program anything without branches, but it's not worth doing so in practice with regards to hassle and performance. Especially not because in general you are better off letting the compiler do the obfuscating optimization, and decide when it'd help performance. You only need to actively avoid branches when you'd like your code to be vectorized by the compiler (though you can't use above method).
funct_ptr action[2] = {function1, function2};
action[condition == x]();
How you write code is extremely important, because it has to be read and understood by both humans and compilers. Aiming for clean and straightforward code whenever possible should be your priority, not to try doing micro-optimizations while writing functional code.