Skip to content

Instantly share code, notes, and snippets.

@MehdiNS
MehdiNS / main.cpp
Last active June 15, 2016 02:08
Generic function timer - could be more fancy but it is good enough
// -First idea was to do it thanks to RAII. Problem is it requires a lot of brackets
// -Then i wanted to go full variadic generic template function,
// but i got stuck because i would have to pass a variadic function
// to a function which doesn't make any sense, and was overkill
// - This solution seems the best for now... Client code is okay-ish...
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
@MehdiNS
MehdiNS / euler.cpp
Last active July 24, 2016 12:19
Euler19 - 10min to lose...
#include <iostream>
#include <string>
#include <vector>
enum yearTypeT
{
COMMON = 0, LEAP
};
using yearT = unsigned int;
@MehdiNS
MehdiNS / latency.txt
Created August 2, 2016 17:07 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@MehdiNS
MehdiNS / ordered_dithering.txt
Last active October 30, 2023 17:15
Ordered dithering explanation
(Written as a way to stop forgetting these things.
May be wrong sometimes, as it's just the result of my research here and there.
If it helps you, give me a shout!)
Ordered dithering is a technique used to reduce - deliberately! - the precision of an image.
Motivation : artistic (mainly ?), color quantization --> reduce the number of color in an image
----------------------------------------------------------------
INTRODUCTION
@MehdiNS
MehdiNS / bayer_matrix.cpp
Created October 31, 2016 13:49
Generate the bayer matrix of order n (of dimension 2^n x 2^n) iteratively
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using uint = unsigned int;
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
@MehdiNS
MehdiNS / halton.cpp
Created April 26, 2017 13:13
Compile time generation of Halton sequences
// Works with C++14 compiler
// Had to use my own array instead of std::array but with a C++17 compiler, it "should work" (tm) without it.
// Also, could use some static_assert here and there....
#include <iostream>
#include <cmath>
using namespace std;
constexpr float halton(int i, int base)
{
@MehdiNS
MehdiNS / montecarlo_pi.cpp
Created April 28, 2017 22:47
Value of Pi computed using Monte Carlo
#include <iostream>
#include <vector>
#include <random>
#include <string>
#include <cmath>
#include <chrono>
using namespace std;
using namespace std::chrono;
@MehdiNS
MehdiNS / Glsl_include.cpp
Last active May 4, 2019 08:27
Support #include in GLSL
// Credits to "Mon Chic Prof" Pat for the Lines trick.
// This contains clever things, and other not so much.
// But it does the job, and we sometimes need ugliness in order to appreciate the beauty of things.
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <fstream>
#include <string>
@MehdiNS
MehdiNS / endianness.cpp
Created July 5, 2017 01:19
Endianness at compile-time
// """"Should"""" work with any C+14 compiler
// (And yes, there is a beautiful UB in this code)
#include <iostream>
struct Endianness
{
constexpr static bool isBig()
{
union
@MehdiNS
MehdiNS / slotmap.cpp
Created July 23, 2017 14:05
Implementation of a slotmap
#include <iostream>
#include <algorithm>
#include <string>
#include <memory>
#include <vector>
using namespace std;
using u32 = unsigned int;
using s32 = int;