Skip to content

Instantly share code, notes, and snippets.

View dnbaker's full-sized avatar

Daniel Baker dnbaker

View GitHub Profile
@dnbaker
dnbaker / decolor.py
Created November 11, 2021 17:53
De-color text - especially useful for R tbl output.
def decolor(x):
from re import compile
return compile(r'''
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
@dnbaker
dnbaker / alog.h
Last active December 6, 2021 19:29
Approximate log/log2
#pragma once
static inline float flog2_float(float x) {return (float)*(uint32_t *)&x * 0x1p-23f - 127.f;}
static inline double fastlog2_double(double x) {return (double)*(uint64_t *)&x * 0x1p-52 - 1023;}
// To convert this to approximate log-base-e, divide these by log(2)
#define LIBKL_ALOG_PD_MUL 1.539095918623324e-16
#define LIBKL_ALOG_PD_INC -709.0895657128241
#define LIBKL_ALOG_PS_MUL 8.2629582881927490e-8f
@dnbaker
dnbaker / timestamper.h
Created June 18, 2020 15:56
TimeStamper - runtime summarization
#ifndef TIMESTAMPER_H__
#define TIMESTAMPER_H__
#include <chrono>
#include <cstdint>
#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>
namespace timestamp {
@dnbaker
dnbaker / doge.grammar
Created February 6, 2019 20:31 — forked from somewacko/doge.grammar
A CFG definition for doge. wow.
# such grammar. very linguistics. wow.
#
# a context-free grammar of doge.
#
# much reference: the-toast.net/2014/02/06/linguist-explains-grammar-doge-wow
1 ROOT S
# ---- Sentences and doge phrases
#
@dnbaker
dnbaker / libtorch.Makefile
Last active December 25, 2018 00:31
libtorch Makefile
all: zomg
LDP=-L lib
LIBS=c10 gloo gtest clog THD protobuf caffe2 torch mkldnn c10d
LIB=$(patsubst %,-l%,$(LIBS))
FLAGS=-O3 -std=c++17 -fopenmp -D_GLIBCXX_USE_CXX11_ABI=0
INCS=include include/torch/csrc/api/include/
INCLUDE=$(patsubst %,-I%,$(INCS))
%: %.cpp
@dnbaker
dnbaker / caseshift.cpp
Last active September 3, 2018 14:22
Shift case
#include <cstdio>
#include <cstring>
#include <random>
#include <cctype>
int main(int argc, char *argv[]) {
std::FILE *ifp = argc > 1 ? std::fopen(argv[1], "rb"): stdin,
*ofp = argc > 2 ? std::fopen(argv[2], "wb"): stdout;
int c;
static constexpr int arr[]{0,32};
@dnbaker
dnbaker / caseshift.py
Created September 3, 2018 14:12
Corrupt your text
import sys
import random
if __name__ == "__main__":
for line in sys.stdin if len(sys.argv) < 2 else open(sys.argv[1]):
for char in line:
if char.isalpha():
sys.stderr.write(char if random.choice([0,1]) else chr(ord(char) ^ 32))
else:
sys.stderr.write(char)
@dnbaker
dnbaker / simdhash.h
Last active June 16, 2018 13:17
SIMD-accelerated Murmur3 Finalizer and Wang 64-bit hash function implementations
#include <cstdio>
#include <cstdint>
#ifndef _VEC_H__
# define NO_SLEEF
# define NO_BLAZE
# include "vec/vec.h" // Import vec.h, but disable blaze and sleef.
#endif // Uses https://github.com/dnbaker/vec for vectorization metaprogramming
#ifndef HAS_AVX_512
# define HAS_AVX_512 (_FEATURE_AVX512F || _FEATURE_AVX512ER || _FEATURE_AVX512PF || _FEATURE_AVX512CD || __AVX512BW__ || __AVX512CD__ || __AVX512F__)
@dnbaker
dnbaker / duff.h
Last active February 20, 2018 03:32
Easy Duff Unrolling
// Set ITER to be the code to execute at each iteration.
// Only use this when you can't guarantee your number of elements is a multiple of 8 (or some higher power of 2)
// If you want to save some time, you can initialize your accumulator with the first entry of the set and decrement len.
#define DO_DUFF(len, ITER) \
do { \
if(len) {\
std::uint64_t loop = (len + 7) >> 3;\
switch(len & 7) {\
case 0: do {\
@dnbaker
dnbaker / nuccount.cpp
Created January 21, 2018 19:09
Count nucleotides in 2-bit encoding (Slower than bitmath, but still fun.)
INLINE u32 nuccount(u64 kmer, unsigned k) {
kmer ^= XOR_MASK;
u32 counts(0);
#if 1
static const u32 lut4 [] {
67108864, 50397184, 50331904, 50331649, 50397184, 33685504, 33620224, 33619969, 50331904, 33620224, 33554944, 33554689, 50331649, 33619969, 33554689, 33554434,
50397184, 33685504, 33620224, 33619969, 33685504, 16973824, 16908544, 16908289, 33620224, 16908544, 16843264, 16843009, 33619969, 16908289, 16843009, 16842754,
50331904, 33620224, 33554944, 33554689, 33620224, 16908544, 16843264, 16843009, 33554944, 16843264, 16777984, 16777729, 33554689, 16843009, 16777729, 16777474,
50331649, 33619969, 33554689, 33554434, 33619969, 16908289, 16843009, 16842754, 33554689, 16843009, 16777729, 16777474, 33554434, 16842754, 16777474, 16777219,
50397184, 33685504, 33620224, 33619969, 33685504, 16973824, 16908544, 16908289, 33620224, 16908544, 16843264, 16843009, 33619969, 16908289, 16843009, 16842754,