Skip to content

Instantly share code, notes, and snippets.

@pkhuong
pkhuong / test.c
Last active December 12, 2019 03:06
Traits in C
#include "trait.h"
static inline int
cmp_int(int x, int y)
{
if (x == y)
return 0;
return x < y ? -1 : 1;
}

Keybase proof

I hereby claim:

  • I am pkhuong on github.
  • I am pkhuong (https://keybase.io/pkhuong) on keybase.
  • I have a public key ASCqsTjZLilMzRmz9kuPxAn3nF3Z-gOZMEtG8-G3qi4o8go

To claim this, I am signing this object:

@pkhuong
pkhuong / dynamic-variance.py
Last active January 9, 2023 21:03
Fully dynamic variance for a bag of observations
import math
import struct
import unittest
import hypothesis.strategies as st
from hypothesis.stateful import Bundle, RuleBasedStateMachine, consumes, invariant, multiple, precondition, rule
class VarianceStack:
def __init__(self):
self.n = 0
@pkhuong
pkhuong / libbts.c
Last active October 18, 2022 09:01
minimal BTS tracing wrapper for linux perf
#define RUN_ME /*
exec cc -O2 -W -Wall -std=c99 -shared $0 -o "$(basename $0 .c).so" -fPIC
*/
/*
* Copyright 2019 Paul Khuong
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@pkhuong
pkhuong / Dockerfile
Created October 5, 2019 16:10
Single layer docker image with build artifacts
FROM alpine:latest AS builder
RUN echo "foo" > /foo.txt
FROM scratch
COPY --from=builder /*.txt .
@pkhuong
pkhuong / div-by-mul.lisp
Last active September 7, 2019 08:51
Quick runtime div-by-mul constants
(defstruct div-by-mul-constants
;; Implement truncated integer by `divisor` with a fixed point
;; approximation.
(divisor 0 :type sb-ext:word
:read-only t)
;; The reciprocal is roughly multipler / 2^(W + shift),
;; where W is the word width.
(multiplier 0 :type sb-ext:word
:read-only t)
(shift 0 :type sb-ext:word
@pkhuong
pkhuong / simd-pack-256.lisp.diff
Created August 11, 2019 16:26
Fix extraction sequence for element #3.
diff --git a/src/compiler/x86-64/simd-pack-256.lisp b/src/compiler/x86-64/simd-pack-256.lisp
index e6bdc560c..228f2018a 100644
--- a/src/compiler/x86-64/simd-pack-256.lisp
+++ b/src/compiler/x86-64/simd-pack-256.lisp
@@ -189,7 +189,7 @@
(:policy :fast-safe)
(:generator 3
(inst vextracti128 tmp x 1)
- (inst vpextrq dst x 1)))
+ (inst vpextrq dst tmp 1)))
;; load https://github.com/pkhuong/csm
(defun player-one ()
(* (random 1d0) (random 1d0)))
(defun player-two ()
(expt (random 1d0) 2))
(defun player-one-wins ()
(> (player-one) (player-two)))
@pkhuong
pkhuong / prefetch.lisp
Created July 3, 2019 18:51
x64-64 prefetch intrinsic for sbcl
(sb-c:defknown prefetch ((member :nta :t0 :t1 :t2)
sb-sys:system-area-pointer sb-vm:signed-word
(member 2 4 8 16)
fixnum)
(values &optional)
(sb-c:any sb-c:always-translatable) :overwrite-fndb-silently t)
(sb-c:define-vop (prefetch)
(:translate prefetch)
(:policy :fast-safe)
;;; Fractional set cover solver with adahedge for the dual surrogate multiplers.
(defpackage "FRACTIONAL-SET-COVER"
(:use "CL"))
(in-package "FRACTIONAL-SET-COVER")
(deftype dvec () `(simple-array double-float 1))
(defstruct tour
(index (error "index must be provided")