Skip to content

Instantly share code, notes, and snippets.

Created May 17, 2013 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5601991 to your computer and use it in GitHub Desktop.
Save anonymous/5601991 to your computer and use it in GitHub Desktop.
// cl /O2 wtf.cpp
#include <stdio.h>
#include <windows.h>
static inline int LLR(int a, int b)
{
if (a>0)
return (b>0) ? __min(a,b) : -__min(a,-b);
else
return (b>0) ? -__min(-a,b) : __min(-a,-b);
}
static inline int LLR_2(int a, int b)
{
int sign, numbers[2];
sign = 1-2*((unsigned)a^b)>>31;
a *= 1-2*((unsigned)a)>>31;
b *= 1-2*((unsigned)b)>>31;
numbers[0] = b;
numbers[1] = a;
a -= b;
return sign*numbers[((unsigned)a)>>31];
}
static unsigned int mask[] = {0,(unsigned)-1};
static unsigned int constant[] = {0, 1};
static inline int LLR_3(int a, int b)
{
int sign,c, numbers[2];
sign = (_rotl(a^b,1) & 1);
c = ((_rotl(a,1) & 1));
a = a^mask[c]+constant[c];
c = ((_rotl(b,1) & 1));
a = a^mask[c]+constant[c];
numbers[0] = b;
numbers[1] = a;
c = ((_rotl(a-b,1) & 1));
return numbers[c]^mask[sign]+constant[sign];
}
void main()
{
SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
int a[1024], b[1024];
srand(0);
for ( int i=0; i<1024; i++ )
{
a[i] = rand() - RAND_MAX / 2;
b[i] = rand() - RAND_MAX / 2;
}
int x;
__int64 t1,t2;
// bench reference version
x = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&t1);
for (int i=0;i<100*1000*1000;i++)
{
register int j = i & 1023;
x += LLR(a[j],b[j]);
}
QueryPerformanceCounter((LARGE_INTEGER*)&t2);
t2 -= t1;
QueryPerformanceFrequency((LARGE_INTEGER*)&t1);
printf("%f sec, x=%d\n",t2/(t1*1.), x);
// bench v2
x = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&t1);
for (int i=0;i<100*1000*1000;i++)
{
register int j = i & 1023;
x += LLR_2(a[j],b[j]);
}
QueryPerformanceCounter((LARGE_INTEGER*)&t2);
t2 -= t1;
QueryPerformanceFrequency((LARGE_INTEGER*)&t1);
printf("%f sec, x=%d\n",t2/(t1*1.), x);
// bench v3
x = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&t1);
for (int i=0;i<100*1000*1000;i++)
{
register int j = i & 1023;
x += LLR_3(a[j],b[j]);
}
QueryPerformanceCounter((LARGE_INTEGER*)&t2);
t2 -= t1;
QueryPerformanceFrequency((LARGE_INTEGER*)&t1);
printf("%f sec, x=%d\n",t2/(t1*1.), x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment