Skip to content

Instantly share code, notes, and snippets.

View bitonic's full-sized avatar
🍥
bottomless pit

Francesco Mazzoli bitonic

🍥
bottomless pit
View GitHub Profile
@bitonic
bitonic / vectorized-atan2f.cpp
Last active April 16, 2024 12:17
Vectorized & branchless atan2f
// Copyright (c) 2021 Francesco Mazzoli <f@mazzo.li>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
@bitonic
bitonic / configuration.nix
Last active March 2, 2024 12:58
NixOS configuration for a remote ZFS server on Hetzner
# Full NixOS configuration for a ZFS server with full disk encryption hosted on Hetzner.
# See <https://mazzo.li/posts/hetzner-zfs.html> for more information.
{ config, pkgs, ... }:
let
# Deployment-specific parameters -- you need to fill these in where the ... are
hostName = "...";
publicKey = "...";
# From `ls -lh /dev/disk/by-id`
@bitonic
bitonic / server.cpp
Last active January 22, 2024 10:35
Stopping linux threads example
// See <https://mazzo.li/posts/stopping-linux-threads.html>
// for blog post.
//
// Spawns a thread with a server listening on 55555 UDP, and
// then terminates it after 1 minute.
//
// I compile and run with
//
// clang++ -Wall -std=c++20 server.cpp -lpthread -o server && ./server
//
// See <https://gist.github.com/pervognsen/b58108d134824e61caedffcc01004e03> for
// Per Vognsen gist on value speculation.
//
// I compile this on linux with
//
// $ clang --version
// clang version 12.0.0
// Target: x86_64-unknown-linux-gnu
// $ clang -static -Wall -O3 value-speculation-linux.c -o value-speculation-linux
//
# From <https://github.com/clefru/nur-packages/tree/f633986f5e31afdd130595b6dc0dea6824d69ef9/pkgs/ib-tws>
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
twsWrap = builtins.toFile "tws-wrap.sh" ''
#!/bin/sh
export INSTALL4J_JAVA_HOME_OVERRIDE='__JAVAHOME__'
mkdir -p $HOME/.tws
VMOPTIONS=$HOME/.tws/tws.vmoptions
@bitonic
bitonic / realpath_test.py
Last active September 26, 2022 14:19
race in `realpath`
# If TEST_RACE = False, no exception is thrown. If it's True, it is. The only difference is
# a sleep in _joinrealpath.
TEST_RACE = False
import os
import time
import threading
import pathlib
def realpath(filename):
{ lib
, stdenv
, coreutils
, callPackage
, makeWrapper
, requireFile
, python3
, writeTextFile
, binutils
, patchelf
/*
Benchmark for Eigen change, see <https://gitlab.com/libeigen/eigen/-/merge_requests/734#note_743674873>
Comment reported here for posterity:
Here's a synthetic "benchmark" which I _believe_ shows the difference: https://gist.github.com/bitonic/2d09df858ba2233b7f472f5f8c0512b4 .
I say that I believe that it exhibits the difference because it shows the runtime differences that I'd expect, with some caveats (see comments on number of instructions below).
However, I have not inspected the assembly manually to check that the code varies in the way I'd expect, which would be a requirement to ensure that things change in the way we expect. That is a bit more labor intensive, and while I might do it, I don't have time to do it right now.
@bitonic
bitonic / TrigApprox.h
Last active November 25, 2021 11:04
Vectorized acos and atan2
// Vectorized (SSE and AVX, through Eigen) atan2 and acos.
//
// Care has been taken to ensure that the vectorized results are identical to the non-vectorized
// ones.
//
// Original functions from <https://developer.download.nvidia.com/cg/index_stdlib.html>, adapted
// to work in bulk. Both seem to be very accurate (only tested that error < 0.01
// degrees, which is what I need).
#include <Eigen/Core>
@bitonic
bitonic / reverse.hs
Last active March 22, 2021 12:20
Simple reverse AD
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -Wall #-}
import Data.IORef