Skip to content

A collection of copy pasta branch-less and/or high-performance bit hacks because I always find myself writing them from scratch when I need them. Maybe other people find them useful too...

Notifications You must be signed in to change notification settings

frederik-hoeft/branchless-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

Signed ints

Max function

int max(int a, int b)
{
    const int BITSIZE_INT = ((sizeof(int) << 3) - 1));
    return a - ((a - b) & (a - b) >> BITSIZE_INT);
}

Min function

int min(int a, int b)
{
    const int BITSIZE_INT = ((sizeof(int) << 3) - 1));
    return b + ((a - b) & (a - b) >> BITSIZE_INT);
}

Abs function

int abs(int x)
{
    const int BITSIZE_INT = ((sizeof(int) << 3) - 1));
    return x + ((x * (x >> BITSIZE_INT)) << 1);
}

Swap function

void swap(int* x, int* y)
{
    if (*x != *y)
    {
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
    }
}

Unsigned ints

About

A collection of copy pasta branch-less and/or high-performance bit hacks because I always find myself writing them from scratch when I need them. Maybe other people find them useful too...

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published