viti95 / FastDoom

Doom port for DOS, optimized to be as fast as possible!
512 stars 33 forks source link

Small optimizations using xors instead of branches #61

Closed RamonUnch closed 2 years ago

RamonUnch commented 2 years ago

Well the idea is still the same that i submitted before:

if (y <= node->y)
{
    side = node->dx < 0;
}
else
{
    side = node->dx > 0;
}

is almost equivalent to side = (y <= node->y) ^ (node->dx >= 0); This applies to R_PointInSubsector() and to the crazy nested ternary operators inside P_CrossSubsector() and P_CrossBSPNode()

the only side case being when dx=0, then side can be set to true which was not possible before. I am not 100% sure this is correct though. Do you have any demo to check for regression or something.

viti95 commented 2 years ago

I've tested it and it's a bit slower, at least on PCem (486). I'll test tomorrow on real hardware to see how it behaves.

RamonUnch commented 2 years ago

This is interesting indeed, I actually have no old hardware to test this. maybe the side-case dx==0 does reduces performances. In this case a perfect branchless equivalent would be needed:

side = (!!node->dx) & ((y <= node->y) ^ (node->dx >= 0)); Or something like that. I will see if I get some time.

RamonUnch commented 2 years ago

Indeed does not seems to be a good idea in the end, I will hence close the issue then.