Skip to content

Instantly share code, notes, and snippets.

View enjoylife's full-sized avatar
📖

Matthew Clemens enjoylife

📖
View GitHub Profile
@enjoylife
enjoylife / array_test.c
Created October 29, 2012 18:46
Flexible array test case
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#define CEILING(x,y) (((x) + (y) - 1) / (y))
#define LEADINGBIT(r) ((sizeof(unsigned long int)*8) - __builtin_clzl(r))
#define BITLAST(k,n) ((k) & ((1<<(n))-1)) // for geting subset of bits
#define BITSUBSET(k,m,n) BITLAST((k)>>(m),((n)-(m)))
@enjoylife
enjoylife / gist:3983893
Created October 30, 2012 23:53
funny snip
if [ `date +%w` -eq 6 ]; then
echo "Enjoy your life. Do not work on Sunday!"
exit 1
fi
exit 0
@enjoylife
enjoylife / gist:3983903
Created October 30, 2012 23:55
runs esvalidate before
files=$(git diff-index --name-only HEAD | grep -P '\.js$')
for file in $files; do
esvalidate $file
if [ $? -eq 1 ]; then
echo "Syntax error: $file"
exit 1
fi
done
@enjoylife
enjoylife / ballVol.c
Created November 5, 2012 18:26
n-sphere volume
#include <stdio.h>
#include <math.h>
#ifndef M_PI
# define M_PI 3.1415926535
#endif
#ifndef ABS
# define ABS(a) ((a) > 0 ? (a) : -(a))
#endif
/* Stack-based Douglas Peucker line simplification routine
returned is a reduced GLatLng array
After code by Dr. Gary J. Robinson,
Environmental Systems Science Centre,
University of Reading, Reading, UK
*/
function GDouglasPeucker (source, kink)
/* source[] Input coordinates in GLatLngs */
/* kink in metres, kinks above this depth kept */
@enjoylife
enjoylife / ssh-id_rsa.pub
Created November 8, 2012 22:33
Password-less SSH
1. Generate keys on local machine: `ssh-keygen`
2. Create `~/.ssh` directory on remote machine
3. Copy `local:~/.ssh/id_rsa.pub` to `user@remote:~/.ssh/authorized_keys`
method a) `ssh-copy-id -i ~/.ssh/id_rsa.pub user@host`
method b) cat .ssh/id_rsa.pub | ssh user@host 'cat >> .ssh/authorized_keys'
@enjoylife
enjoylife / swaptrick
Created November 16, 2012 00:28
swap without temp
a=a|b, b=a^b, a=a^b
//aka
a=a+b, b=a-b, a=a-b
@enjoylife
enjoylife / branch.c
Created November 16, 2012 00:31
gcc branch predict macro
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
@enjoylife
enjoylife / pop64.c
Created November 16, 2012 23:24
64 bit pop count
static inline uint64_t kbi_popcount64(uint64_t y) // standard popcount; from wikipedia
{
y -= ((y >> 1) & 0x5555555555555555ull);
y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull);
return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
}
@enjoylife
enjoylife / clockit.c
Created November 17, 2012 19:35
clock C code
#include <time.h>
int main()
{
clock_t tic = clock();
my_expensive_function_which_can_spawn_threads();
clock_t toc = clock();