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 / hilbert3d.c
Created May 18, 2024 09:52
Hilbert curve in 3D
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <assert.h>
enum dim : uint8_t {
X, Y, Z, DIM_COUNT
};
@madmann91
madmann91 / poly.c
Last active May 15, 2024 13:06
Solver for quartic and cubic polynomials
#include <stdio.h>
#include <tgmath.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#define PI 3.141592653589793f
#define SEARCH_RADIUS 1.e-5f // Initial search radius for the local search procedure
@madmann91
madmann91 / bvh.glsl
Last active June 22, 2022 22:03
Basic BVH traversal with early exit & traversal order optimizations
#version 440
#define STACK_SIZE 32
#define INVALID_HIT_ID uint(-1)
// The primitive data be changed to the desired primitive type
// (including using BVHs as primitives for top-level BVH traversal).
#define PrimitiveData Triangle
#define HitData vec2 // Barycentric coordinates of a hit on a triangle (can be changed to anything)
#define intersect_ray_primitive(ray, prim, hit, tnear, tfar) \
@madmann91
madmann91 / dx.cpp
Created February 15, 2022 21:32
Automatic differentiation
#include <cmath>
#include <iostream>
template <typename T>
struct dFloat {
T x, dx;
dFloat(T x, T dx = 0) : x(x), dx(dx) {}
};
template <typename T>
@madmann91
madmann91 / gen_primes.c
Created September 20, 2021 09:32
Prime number generator for hash tables
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
static bool is_prime(size_t i) {
if (i <= 1)
return false;
if (i <= 3)
return true;
if (i % 3 == 0 || i % 2 == 0)
@madmann91
madmann91 / spherical_tri.py
Created June 29, 2021 13:57
Spherical triangle sampling according to [Arvo 95]
#!/bin/env python3
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
import math
def vnorm(v):
return v / np.linalg.norm(v)
def draw_unit_sphere(ax):
@madmann91
madmann91 / dist.c
Last active June 14, 2021 21:53
Sampling from a discrete probability distribution
#include <tgmath.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
// Discrete probability distribution sampling according to the method named
// "Squared Histogram" in "Fast Generation of Discrete Random Variables",
@madmann91
madmann91 / audio_fixes.sh
Created February 15, 2021 16:02
Small fixes for audio issues on Linux
#!/bin/sh
# Disables notification sounds from gnome
dconf write /org/gnome/desktop/sound/event-sounds "false"
# Disables PulseAudio's suspend-on-idle to avoid cracks and pops
echo "
.include /etc/pulse/default.pa
.nofail
unload-module module-suspend-on-idle
@madmann91
madmann91 / vm.c
Created February 14, 2021 14:26
Small VM example
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
#define REG_COUNT 64
#define OPCODE_LIST(f) \
@madmann91
madmann91 / bench_renderline.cpp
Last active December 9, 2020 14:49
Benchmark for devilutionX's RenderLine function
// g++ bench_renderline.cpp -std=c++17 -O3 -march=native -o bench_renderline
// The benchmarked functions (RenderLine, ...) where taken from
// diasurgical/devilutionX
// Copyrights to their respective authors, license is "The Unlicense"
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <climits>