
// clamping is bad, slow because of branching
// we are assuming unsigned values (else need to clamp to 0 too)
#define clamp(x) \
        ((x)>255)?255:(x)

int a,b,y;

// non-branching code versions of clamp
#define fastClampIdea1(x) \
	b=(0x100-((x>>8)!=0))&0xFF;\
	y=(x&0xFF)-(x&b)+b;

#define fastClampIdea2(x) \
	b=0x100-((x>>8)!=0);\
	y=((x-(x&b))+b)&0xFF;

#define fastClampIdea3(x) \
	b=0-((x&~0xFF)==0);\
	y=(x&b)|(~b&0xFF);

#define fastClampIdea(x) \
	b=0-((x&~0xFF)==0);\
	y=(x&b)|(~b&0xFF);


#include <stdio.h>
#include <time.h>


int main()
{
    for ( int i = 0; i < 65536; i++ ) {
	int p = i;
	int q = i;
	p = clamp(p);
	fastClampIdea(q);
	q = y;
	if (p!=q) 
	    printf("%i:  p != q   %i %i\n", i, p, q);
    }

    puts("go clamp");
    long clk = clock();
    for ( int i = -65536; i < 10000*65536; i++ ) {
	int p = clamp(i);
    }
    long time = clock() - clk;
    printf("done clamp %li\n", time);
    
    puts("go fastClamp");
    clk = clock();
    for ( int i = -65536; i < 10000*65536; i++ ) {
	int q;
	fastClampIdea(i);
	q = y;
    }
    time = clock() - clk;
    printf("done fastClamp %li\n", time);

}



