Skip to content

Instantly share code, notes, and snippets.

View CAFxX's full-sized avatar

Carlo Alberto Ferraris CAFxX

View GitHub Profile
@CAFxX
CAFxX / log10.c
Last active March 18, 2023 19:30
Fast integer log10 in C
#include <stdint.h>
/*
Fast 64bit integer log10
WARNING: calling ilog10c(0) yields undefined behaviour!
On x64 this compiles down to:
pushq %rbp
@CAFxX
CAFxX / ffscast.sh
Created March 1, 2021 02:04
ffmpeg screencast for macos
# there's a bunch of things that don't work properly:
# - can't capture computer audio output - it desyncs
# - need to change the input devices (`-i "v:a"`) based on the output of `ffmpeg -f avfoundation -list_devices true -i ""`
ffmpeg \
-thread_queue_size 150 -f avfoundation -probesize 50M -framerate 30 -pix_fmt uyvy422 -i "2:3" \
-thread_queue_size 150 -f avfoundation -probesize 50M -framerate 30 -pix_fmt uyvy422 -capture_cursor 1 -capture_mouse_clicks 1 -i "4" \
-filter_complex '
[1]fps=fps=30[scr],
[scr]scale=-1:1440[scr],
@CAFxX
CAFxX / bulk_insert.go
Last active December 31, 2022 02:39
SQL bulk insert
package bulkinsert
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
)
@CAFxX
CAFxX / load_data.go
Last active December 29, 2022 06:42
MySQL bulk data loader (via LOAD DATA LOCAL INFILE)
package mysql
import (
"bytes"
"context"
"database/sql"
"encoding/csv"
"errors"
"fmt"
"io"
@CAFxX
CAFxX / weak.go
Last active December 24, 2022 13:22
Weak references for Go
// Weak references for Go
// Copyright (C) 2021 Carlo Alberto Ferraris
//
// Based on:
// https://lab.nexedi.com/kirr/neo/blob/bb618ce122423c5ff5fa997c5aafd06819cafe40/go/zodb/internal/weak/weak.go
//
// Original copyright notice:
//
// Copyright (C) 2018 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
package xsync
import "sync"
type TryLocker interface {
sync.Locker // Lock(); Unlock()
TryLock() bool
}
// LockAndDo will acquire the lock l and execute fn.
package main
import "sync"
import "fmt"
type Work interface {
Process()
}
type WorkFunc func()
@CAFxX
CAFxX / genOptIface.go
Created November 16, 2022 08:53
Generate objects with optional methods (Golang)
package main
import "fmt"
func genOptIface(name string, opt ...string) {
if len(opt) == 0 {
panic("zero opt types")
}
if len(opt) > 10 {
panic("too many opt types")
@CAFxX
CAFxX / lookup.cpp
Created November 11, 2022 06:10
AVX-512 vectorized associative lookup
#include <immintrin.h>
#include <stdint.h>
int getIndexOf(__m512i const *values, int64_t target)
{
__m512i valuesSimd = _mm512_loadu_si512(values);
__m512i targetSplatted = _mm512_set1_epi64(target);
__mmask8 equalMask = _mm512_cmpeq_epi64_mask(valuesSimd, targetSplatted);
uint32_t equalMaskInt = _cvtmask8_u32(equalMask);
int index = _tzcnt_u32(equalMaskInt);
@CAFxX
CAFxX / Makefile
Last active July 8, 2022 02:07
aw - Write whole files atomically in Linux
CFLAGS=-O2 -flto -ffunction-sections -fdata-sections -Wl,--gc-sections
CFLAGSNATIVE=-march=native -mtune=native
CFLAGSSTATIC=-static
build: aw.c
gcc -o aw aw.c $(CFLAGS)
build-static: aw.c
gcc -o aw aw.c $(CFLAGS) $(CFLAGSSTATIC)