Skip to content

Instantly share code, notes, and snippets.

View madmann91's full-sized avatar

Arsène Pérard-Gayot madmann91

View GitHub Profile
@madmann91
madmann91 / gen_sort.py
Last active May 31, 2016 16:28
Creates a sequence of compare-swap operations that perform a sort.
#!/usr/bin/python3
import sys
# Returns a list of compare-swap tuples that merges two lists
def gen_merge(a, b):
return [(x, y) for x in a for y in b]
# Returns a list of compare-swap tuples that performs a merge sort
# Generates n^2/2 - n/2 = O(n^2) compare-swaps
def gen_sort(l):
@madmann91
madmann91 / ieee.c
Last active July 22, 2016 13:56
Manipulation of the IEEE floating point representation.
#include <stdio.h>
int as_int(float f) {
union { float f; int i; } u;
u.f = f;
return u.i;
}
float as_float(int i) {
union { float f; int i; } u;
@madmann91
madmann91 / setupenv.sh
Last active March 29, 2024 12:06
Setup environment (requires fish + git + curl + Fira Code)
#!/bin/sh
# Install vim-plug and create vim config
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
echo "syntax enable
"\"" Avoid problems when pressing Q by error (Dvorak layout)
map Q <Nop>
"\"" Allow executing local .vimrc files
set exrc
set secure
@madmann91
madmann91 / fastdiv.c
Created March 8, 2018 10:44
Fast division using precomputed multiplicative inverse
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#define PAGE_SIZE 4096
typedef struct asmbuf_s asmbuf_t;
struct asmbuf_s {
@madmann91
madmann91 / huff.c
Created March 15, 2018 13:03
Simple huffman byte encoding/decoding
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
typedef struct symb_s symb_t;
typedef struct queue_s queue_t;
typedef struct dict_s dict_t;
@madmann91
madmann91 / clz.cpp
Created September 13, 2018 14:22
Count leading zeros in a portable manner
#include <iostream>
#include <limits>
#include <algorithm>
#include <cstdint>
int clz(uint32_t u) {
uint32_t a = 0;
uint32_t b = 32;
uint32_t all = 0xFFFFFFFF;
#pragma unroll
@madmann91
madmann91 / filter.cpp
Last active October 26, 2018 14:58
Basic Monte Carlo noise remover
#include <iostream>
#include <fstream>
#include <memory>
#include <string>
#include <algorithm>
#include <cmath>
#include <png.h>
struct Image {
@madmann91
madmann91 / enum_parts.py
Last active October 31, 2018 20:00
Enumerate possible partitions of a set with M objects into N bins.
def insert(p, i, m, n):
q = []
for j in range(0, n):
if j == i:
s = set(p[j])
s.add(m)
q.append(s)
else:
q.append(p[j])
return q
@madmann91
madmann91 / skip_list.c
Created February 18, 2019 16:04
Skip list implementation in C99
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
static inline uint8_t first_bit_set(uint32_t bits) {
@madmann91
madmann91 / Chebyshev.hs
Created March 15, 2019 16:19
Chebyshev approximation of arbitrary functions
import Math.Polynomial
import Math.Polynomial.Chebyshev
coeff f n j = (2 / nf) * (sum [f(xk) * yk | (xk, yk) <- zip x y])
where
x = map ((\x -> cos (pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1]
y = map ((\x -> cos (jf * pi * (x + 0.5) / nf)) . fromIntegral) [0..n-1]
nf = fromIntegral n
jf = fromIntegral j