Skip to content

Instantly share code, notes, and snippets.

View jweinst1's full-sized avatar
🎯
Focusing

Josh Weinstein jweinst1

🎯
Focusing
View GitHub Profile
@jweinst1
jweinst1 / 1vs2bit_quantize_test.cpp
Created September 1, 2025 23:34
compares quantization of means vs 1-2 dim per bit quantize
// bucket_stats.cpp
#include <array>
#include <vector>
#include <unordered_map>
#include <random>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <numeric>
@jweinst1
jweinst1 / benchmark10m.cpp
Last active September 1, 2025 07:53
quantizer into a hash bucket test
#include <array>
#include <vector>
#include <unordered_map>
#include <random>
#include <iostream>
#include <limits>
#include <cstdint>
#include <chrono>
constexpr size_t DIM = 128;
@jweinst1
jweinst1 / 64bitpopcount.cpp
Last active August 31, 2025 02:36
an important benchmark of pop count vs jungle precomp
#include <array>
#include <cstdint>
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
constexpr size_t NUM_BYTES = 1024;
constexpr int N = 10'000'000;
@jweinst1
jweinst1 / hamming_jungle.cpp
Last active August 30, 2025 06:51
Using hamming ladders as an iterator for numbers in a bitset
#include <array>
#include <chrono>
#include <iostream>
#include <cstdint>
// --------------------------------------
// Byte-based 256-bit bitset
// --------------------------------------
struct ConstexprBitset256 {
std::array<uint8_t, 32> blocks{};
@jweinst1
jweinst1 / simd_hamming_lookup.cpp
Created August 28, 2025 08:17
simd neon version of hamming lookup
#include <arm_neon.h>
#include <array>
#include <cstdint>
#include <iostream>
#include <random>
#include <chrono>
struct ConstexprBitset256 {
uint8_t data[32];
@jweinst1
jweinst1 / bits_needed_to_not_collide.cpp
Last active August 27, 2025 23:35
Determines bits needed to not collide
#include <array>
#include <cmath>
#include <cstddef>
#include <algorithm>
#include <iostream>
// helper to compute ceiling of log2 at runtime
constexpr int ceil_log2(float x) {
return static_cast<int>(std::ceil(-std::log2(x)));
}
@jweinst1
jweinst1 / notes.txt
Last active August 26, 2025 08:36
Unrolled version of 1 bit dimension quantizer
⚡ Bit Operations Mini-Cheat Sheet
1️⃣ Integer division and modulo with powers of two
Operation Bit trick Explanation
x / 2 x >> 1 Right shift divides by 2
x / 4 x >> 2 Right shift divides by 4
x / 8 x >> 3 Right shift divides by 8
x % 2 x & 1 Bitwise AND with 2^n - 1 for modulo
x % 4 x & 3 3 = 0b11 → remainder when dividing by 4
x % 8 x & 7 7 = 0b111 → remainder when dividing by 8
@jweinst1
jweinst1 / generate_all_neighbors.cpp
Last active August 31, 2025 23:26
Uses templates and constexpr for compile time hamming lookup
#include <array>
#include <cstdint>
#include <cstddef>
// ---------- ConstexprBitset ----------
template<std::size_t N>
struct ConstexprBitset {
static constexpr std::size_t numBlocks = (N + 63) / 64;
std::array<uint64_t, numBlocks> blocks{};
@jweinst1
jweinst1 / hamming_neighbor_iter.cpp
Created August 21, 2025 08:08
hamming neighbor iterators C++
#include <cstdint>
#include <bitset>
#include <vector>
#include <iostream>
#include <algorithm>
class HammingNeighborIterator {
public:
HammingNeighborIterator(uint16_t code, int numBits)
: code(code), numBits(numBits), radius(1)
@jweinst1
jweinst1 / custom_small_Quant.cpp
Created August 21, 2025 05:21
quantize floats to small values
#include <vector>
#include <cstdint>
#include <iostream>
#include <cmath>
#include <numeric>
#include <bitset>
uint16_t quantizeVectorToBits(const std::vector<float>& vec,
int numBits,
bool useGlobalMean = false,