Skip to content

Instantly share code, notes, and snippets.

View kulp's full-sized avatar
💬
ceci n'est pas un statut

Darren Kulp kulp

💬
ceci n'est pas un statut
View GitHub Profile
@kulp
kulp / nafe2png.pl
Created January 28, 2013 01:51
Convert well-behaved NAFE (nafe.sf.net) TXT output to a linear PNG
#!/usr/bin/env perl
use strict;
use GD;
my ($char_count, $char_width, $char_height);
my $im;
my ($fg, $bg);
while (<>) {
@kulp
kulp / linesplit.pl
Last active December 11, 2015 14:38
Filter stdin to stdout, using line-ranges described by binary strings.
#!/usr/bin/env perl
# Takes M parameters on the command line, binary strings of N digits.
# Filters stdin to stdout, printing only lines whose indices fall inside (or
# outside, if -invert is specified) the independent ranges specified by the
# binary string, where "" means "everything", "0" means "the first half", "01"
# means "the first quarter", "111" means "the last eighth" and so on.
use strict;
my @lines = <STDIN>;
@kulp
kulp / timers.h
Created January 4, 2013 16:38
Intel assembly timer routines for gcc inline asm
#include <stdint.h>
// from http://download.intel.com/embedded/software/IA/324264.pdf
#define rdtscp_before() \
({ uint32_t cycles_high, cycles_low; \
__asm__ volatile ("CPUID\n\t" \
"RDTSC\n\t" \
"mov %%edx, %0\n\t" \
"mov %%eax, %1\n\t": "=r" (cycles_high), "=r" (cycles_low):: \
"%rax", "%rbx", "%rcx", "%rdx"); \
@kulp
kulp / check_range.c
Created December 12, 2012 20:45
Recursively find the bounding box of mismatched bytes between two arrays
unsigned check_range(const char *bad, const char *good, size_t len, size_t bounds[2])
{
if (len < 2) {
if (len)
return *bad != *good;
return 0;
} else if (memcmp(bad, good, len)) {
size_t subbounds[2][2] = { { 0 } };
int leftbad = check_range(&bad[0 ], &good[0 ], len / 2, subbounds[0]);
int rightbad = check_range(&bad[len / 2], &good[len / 2], len - len / 2, subbounds[1]);
@kulp
kulp / buddy.c
Last active December 21, 2016 02:05
A little buddy allocator
#include <assert.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#define RANK_0_LOG 5
#define RANK_0_SIZE (1ul << RANK_0_LOG)
@kulp
kulp / test_tsearch.c
Last active August 22, 2020 20:59
tsearch() implementation in C
#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct record {
int key;
char val[16];
};
@kulp
kulp / quicksort.c
Created October 20, 2012 00:25
An in-place, unstable quicksort example in C
#include <stddef.h>
#include <string.h>
#define elem(Base, Index) \
(((char *)Base)[(Index) * width])
void swap(void *base, size_t width, size_t i0, size_t i1)
{
char temp[width];
memcpy(&temp, &elem(base,i0), width);
@kulp
kulp / gcc_hosts_ssh_config.txt
Created October 19, 2012 15:33
GCC Compile Farm hosts as an .ssh/config file as of 2012-10-19
# Taken from publicly available information at http://gcc.gnu.org/wiki/CompileFarm
# Put this in your ~/.ssh/config and s/LOGIN/username/
# AMD Opteron Magny-Cours / 64 GB RAM / Supermicro AS-1022G-BTF / Debian x86-64 -- 2TB, 2x12x1.5 GHz
Host gcc10
User LOGIN
Hostname gcc10.fsffrance.org
# GHz AMD Opteron 2212 / 4GB RAM / Dell SC1345 / Debian x86-64 -- 580G, 2x 2x2.0
Host gcc11
User LOGIN
Hostname gcc11.fsffrance.org
@kulp
kulp / trap.h
Created September 28, 2012 20:38
TF-flag management on x86 for tracing individual function calls
// clobbers %rax ; this could cause issues with variadic functions
#define trace_wrap(Ret,Func,...) \
({ \
long long temp; \
void flag_wrap(); \
/* Write the function pointer into the red zone on the stack \
* and hope that the function we call doesn't use up that much \
* stack for its arguments */ \
asm volatile("movq %0,-120(%%rsp)" : : "a"(Func)); \
flag_wrap(__VA_ARGS__); \
@kulp
kulp / jcounter.v
Created April 28, 2012 16:36
staged Johnson (?) counter
module JCounter(input clk, input ce, output tick);
parameter STAGES = 6;
parameter SHIFTS = 10;
assign tick = stage[STAGES - 1].s[SHIFTS - 1];
generate
genvar i;
for (i = 0; i < STAGES; i = i + 1) begin:stage