Skip to content

Instantly share code, notes, and snippets.

@aprell
aprell / deps.mk
Created October 8, 2012 14:12
Makefile dependency generation
# Dependency generation
# Project must define SRCS
DEPS = $(addprefix deps/,$(SRCS:.c=.d))
deps/%.d: %.c
$(CC) -MM $(CFLAGS) $(CPPFLAGS) -o $@ $<
sed -i "s/\($*.o\)/\1 deps\/$(notdir $@)/g" $@
# Prevent make from generating dependencies when running 'make clean'
@aprell
aprell / rules.mk
Created October 9, 2012 12:14
Makefile rule generation
# Rule generation
# Project must define PROGS, SRCS, and *_SRCS to specify targets and prerequisites
OBJS = $(SRCS:.c=.o)
define RULE_template
$(1): $$($(subst -,_,$(1))_SRCS:.c=.o) $$($(subst -,_,$(1))_PREREQS)
$$(CC) -o $$@ $$(filter %.o,$$^) $$(LDFLAGS) $$(IMPORTS) $$($(subst -,_,$(1))_LIBS:%=-l%)
endef
@aprell
aprell / newproject.sh
Created October 12, 2012 09:57
Project templates
#!/bin/bash
usage="Usage: $(basename "$0") <name>"
if [ $# -ne 1 ]; then
echo $usage
exit 0
fi
proj=$1
@aprell
aprell / channel_range.h
Created October 23, 2012 10:43
Range: Iterate through all values received on a channel
#include "channel.h"
// Example use:
// channel_range(chan, &val) {
// do sth with val
// }
#define unique_name_paste(id, n) id ##_## n
#define unique_name(id, n) unique_name_paste(id, n)
@aprell
aprell / affinity.h
Last active May 8, 2017 10:48
Processor affinity: convenience functions for binding threads to cores (nonstandard)
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <sched.h>
#include "overload_set_thread_affinity.h"
static inline void set_thread_affinity(pthread_t t, int cpu)
@aprell
aprell / stddev
Created December 13, 2012 16:39
Simple script to calculate the standard deviation of a series of numbers (Bash, Io)
#!/bin/bash
# Calculate the standard deviation of a series of numbers
cat > stddev.io <<IOCODE
#!/usr/bin/env io
l := List clone
IOCODE
chmod +x stddev.io
@aprell
aprell / double.asm
Created December 17, 2012 14:55
MIPS: making sure the stack pointer is double-word aligned
.data
array: .double 1.0 2.0 3.0
.text
.globl main
main:
andi $sp, 0xfffffff8
addi $sp, $sp, -24
@aprell
aprell / rt.sh
Last active December 11, 2015 07:18
Throw-away script for runtime experiments
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 program"
exit 0
fi
# Task granularity
taskgran="small"
nruns=10
@aprell
aprell / egypt.io
Created February 3, 2013 19:35
Fun with Io: Egyptian fractions
#!/usr/bin/env io
# Egyptian fractions
# Load code from utest.io
doFile("utest.io")
# egypt(f) = 1 / ceil(y/x) + egypt((-y mod x) / (y * ceil(y/x)))
egypt := method(x, y, l,
r := (y / x) ceil
l append("1/#{r}" interpolate)
@aprell
aprell / signals.c
Created March 22, 2013 15:09
Sending signals to threads with pthread_kill
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <signal.h>
#include <errno.h>
static pthread_t threads[2];
static int IDs[2] = { 1, 2 };