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 / ale-local.vimrc
Last active September 14, 2020 12:11
Local ALE setup for C++ projects
" Lint .h files as C++, not C
let g:ale_pattern_options_enabled = 1
let g:ale_pattern_options = { '\.h$': { 'ale_linters': { 'cpp' : ['cc', 'gcc', 'clang'] } } }
" Set flags for gcc/clang
let opts = '-std=c++17 -Wall -Wextra'
let g:ale_linters = { 'cpp': ['cc', 'gcc', 'clang'] }
let g:ale_cpp_cc_options = opts
let g:ale_cpp_gcc_options = opts
let g:ale_cpp_clang_options = opts
@madmann91
madmann91 / morton.c
Last active February 27, 2020 15:25
Fast, vectorized morton code encoding. Completely portable and only relies on compiler optimizations.
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
static inline uint64_t split(uint64_t x, int log_bits) {
uint64_t mask = (UINT64_C(1) << (1 << log_bits)) - 1;
for (int i = log_bits, n = 1 << log_bits; i > 0; --i, n >>= 1) {
mask = (mask | (mask << n)) & ~(mask << (n / 2));
x = (x | (x << n)) & mask;
@madmann91
madmann91 / morton_encoder_generator.c
Last active February 27, 2020 15:24
Morton code encoder generator. Generates C code that encodes the X dimension of a 3D morton code. Other dimensions can be obtained by shifting by 1 or 2.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <limits.h>
#include <assert.h>
typedef struct op_s op_t;
@madmann91
madmann91 / sort.cpp
Last active February 21, 2020 15:15
Sorting routines for C99 (with testbed in C++11)
#include <memory>
#include <iostream>
#include <algorithm>
#include <climits>
#define SWAP(T, x, y) \
{ \
T z = x; \
(x) = y; \
(y) = z; \
@madmann91
madmann91 / ir.cpp
Last active July 23, 2019 13:34
Current plan to make Thorin an extensible IR
// g++ -std=c++17 ir.cpp -pedantic -Wall -Wextra -g -o ir
#include <type_traits>
#include <unordered_set>
#include <cassert>
#include <cstdint>
#include <iostream>
// Def is just exposing the data, nothing more
struct Def {
uint32_t gid;
@madmann91
madmann91 / Simplify.hs
Last active April 29, 2019 16:52
Simplification of a simple boolean + Comparison language
-- Compile and run with:
-- ghc Simplify.hs
-- ./Simplify
import Data.Bits
import Data.List
import Data.Maybe
import Data.Function
import System.Random
import Control.Monad
import Debug.Trace
@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
@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 / 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 / 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 {