Code Snippets Wiki

Hello World[]

#include <stdio.h>
int main(int argc, char** argv){
  printf("Hello World\n");
  return 0;
}

strcpy[]

void strcpy(char *s, const char *t)
{
    while (*s++ = *t++);
}

IEEE floating-point fastsqrt[]

float fastsqrt(float val) {
    int tmp = *(int *)&val;
    tmp -= 1<<23; /* Remove last bit to not let it go to mantissa */
    /* tmp is now an approximation to logbase2(val) */
    tmp >>= 1;    /* divide by 2 */
    tmp += 1<<29; /* add 64 to exponent: (e+127)/2 =(e/2)+63, */
                  /* that represents (e/2)-64 but we want e/2 */
    return *(float *)&tmp;
}

xor_swap[]

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

IEEE floating-point invsqrt[]

float invSqrt(float x)
{
    float xhalf = 0.5f*x;
    int i = *(int *)&x;
    i = 0x5f3759df - (i >> 1);
    x = *(float *)&i;
    x = x * (1.5f - xhalf * x * x);
    return x;
}