vendredi 12 mars 2021

Branchless min/max methods in c#

I've recently learned about branchless programming. I found example of branchless min method. In pesudocode it's something like this

function Min(a, b)
{
    return a * (a > b) + b * (a <= b);
}

This code works only under condition that in used language true can be casted to 1 and false to 0. In c# however it doesn't seem to work, since true and false aren't just aliases for 1 and 0, but actual logical values. Can min and max methods be implemented branchless in any other way in C#?

Aucun commentaire:

Enregistrer un commentaire