Skip to content

Instantly share code, notes, and snippets.

@OrangeTide
OrangeTide / bitfield.h
Created April 7, 2011 17:47
bitfield macros
/* bitfield.h - bitfield macros */
/*
* PUBLIC DOMAIN - NO COPYRIGHT CLAIMED - Jonathan Mayo - July 2005
*
* Updated January 2010
*/
#ifndef BITFIELD_H
#define BITFIELD_H
#include <limits.h>
@OrangeTide
OrangeTide / showif.c
Created April 8, 2011 02:59
example to list network interfaces on linux/bsd/osx/etc
/* showif.c : PUBLIC DOMAIN - Jon Mayo - August 22, 2006
* - You may remove any comments you wish, modify this code any way you wish,
* and distribute any way you wish.*/
/* finds all network interfaces and shows a little information about them.
* some operating systems list interfaces multiple times because of different
* flags, modes, etc. if you want to use this code you should be aware that
* duplicate interfaces is a possibility */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@OrangeTide
OrangeTide / ipow.c
Created April 14, 2011 19:39
integer pow() using Montgomery's ladder
/* Montgomery's Ladder Technique
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.5.4956&rep=rep1&type=pdf
*/
#include <stdio.h>
#include <limits.h>
static unsigned long long ipow(unsigned g, unsigned k)
{
unsigned long long r0, r1;
unsigned j;
@OrangeTide
OrangeTide / ini.c
Created April 28, 2011 19:11
.ini file parser
/* This software is public domain. No copyright is claimed.
* Jon Mayo April 2011
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Load an .ini format file
* filename - path to a file
* report - callback can return non-zero to stop, the callback error code is
@OrangeTide
OrangeTide / copyfd.c
Created June 1, 2011 16:49
fast copy of select's fd_set
#define _XOPEN_SOURCE 700
#include <sys/select.h>
static void copyfdset(fd_set *d, fd_set *s, unsigned fd_max) {
unsigned i;
unsigned n = (fd_max + 1 + __NFDBITS - 1) / __NFDBITS;
for(i = 0; i < n; i++) {
d->fds_bits[i] = s->fds_bits[i];
}
}
@OrangeTide
OrangeTide / debug.h
Created June 4, 2011 06:42
some portable vararg debug macros based on an old example I saw
/* a complicated set of macros to support empty __VA_ARGS__ macros.
* example:
* pr_dbg("Check\n");
* pr_dbg("Check %d\n", 12);
* pr_dbg("Check %d %s %d\n", 42, "Hello", 99);
*
* pr_info() and pr_err() macros don't use __func__ and __LINE__
*/
#ifndef DEBUG_H
#define DEBUG_H
@OrangeTide
OrangeTide / otadd0.c
Created June 27, 2011 21:27
add without using +,++ or -,--
/* otadd0.c : add without using +,++ or -,-- */
/* clean:
int add(int a, int b)
{
int r,n,c;
for(r=c=0,n=1;n;n<<=1) {
c = (a & b) ^ (c & (a ^ b));
c <<= 1;
r |= (a ^ b ^ c) & n;
}
@OrangeTide
OrangeTide / bench.c
Created November 21, 2011 22:30
benchmark of a simple 2D pixel pattern
/* bench.c : XOR pattern without any display/output to get a baseline benchmark */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#define BGRX8888(r, g, b) \
((uint32_t)((unsigned char)(b)) << 24 | \
(uint32_t)((unsigned char)(g)) << 16 | \
@OrangeTide
OrangeTide / toylang.leg
Created July 22, 2013 20:02
Toy scripting language with C-like syntax
Program = _ (DeclareVar | Function { printf("good\n"); }
| . { printf("bad\n"); exit(1); }
)*
| !. { printf("done\n"); exit(0); }
DeclareVar = type VarInit ( "," _ VarInit )* ";" _
VarInit = identifier ( "=" _ Expression )?
Function = t:type i:identifier "(" _ Parameters? ")" _ Compound {
printf("Function:\n");
value_print(t);
@OrangeTide
OrangeTide / cube_vbo.c
Created April 25, 2015 04:56
Drawing a cube in OpenGL using VBOs. (required SDL2)
/* cube_vbo.c - demo of drawing a cube in GL using VBOs.
*
* PUBLIC DOMAIN - Jon Mayo - April 24, 2015
*
* Requires SDL2
*
* To build:
* gcc -Wall -W -o cube_vbo cube_vbo.c `pkg-config --libs --cflags sdl2 gl`
*
*/