Skip to content

Instantly share code, notes, and snippets.

@williamyang98
williamyang98 / calculate_horner_polynomial_gradient_descent.cpp
Last active May 29, 2024 00:42
Calculate chebyshev polynomial that will approximate f(x) = sin(kx). where k = π/root. Uses gradient descent.
// Compiling with clang or gcc
// clang main.cpp -o main -march=native -O3 -ffast-math -Wextra -Werror -Wunused-parameter -lm
// Use "-march=native -ffast-math -O3" for faster gradient descent
// Use "-lm" if linking to glibc math libraries on Linux
// Remove "-W*" warning flags if you don't need checks
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <cmath>
#include <vector>
@williamyang98
williamyang98 / aligned_allocator.hpp
Created February 25, 2024 07:34
Aligned allocator for C++17 STL
#pragma once
#include <assert.h>
#include <cstddef>
#include <new>
#include <type_traits>
// C++17 aligned allocator
// Sources: https://en.cppreference.com/w/cpp/named_req/Allocator
// https://en.cppreference.com/w/cpp/memory/allocator_traits
@williamyang98
williamyang98 / benchmark_fast_inverse_square_root.cpp
Last active June 5, 2024 18:27
Fast inverse square root explanation
#include <cmath>
#include <stdint.h>
#include <stdio.h>
// | idx | mae | description |
// |-----|----------|-------------------|
// | 0 | 1.008427 | Naiive with k0=0 |
// | 1 | 0.144398 | Original Quake |
// | 2 | 0.099314 | Our grad descent |
// | 3 | 0.060105 | Jan Kadlec (wiki) |