Skip to content

Instantly share code, notes, and snippets.

@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")
@pkhuong
pkhuong / fusion.lisp
Last active January 18, 2019 06:50
Stronger fusion types
;;; Experiments in polymorphic function type instantiation with improve Fusion types.
(defpackage #:fusion
(:use #:cl))
(require "alexandria")
(in-package #:fusion)
(defconstant v 'v
"Symbol for the implicitly constrained value in a constraint.")
@pkhuong
pkhuong / reservoir-sampling.py
Last active December 27, 2018 02:15
Logarithmic entropy usage for reservoir sampling, without entropy decoding.
"""
Asymptotically optimal entropy usage for reservoir sampling.
"""
import random
from collections import namedtuple
# Baseline implementation: split odd/even, then pick the last odd element specially, with p = 1/n
def recursive_baseline(items):
@pkhuong
pkhuong / cycle.h
Last active November 7, 2018 04:38
interrupt abuse
/*
* Copyright (c) 2003, 2007 Matteo Frigo
* Copyright (c) 2003, 2007 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
@pkhuong
pkhuong / ck_ec.c
Last active October 27, 2018 15:33
Futex-backed event count for concurrency kit
#define _GNU_SOURCE
#include "ck_ec.h"
#include <ck_limits.h>
#include <ck_stdbool.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
@pkhuong
pkhuong / u4.c
Created December 24, 2017 05:04
#include <assert.h>
#include <err.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emmintrin.h>
#include <tmmintrin.h>
@pkhuong
pkhuong / rlock.c
Last active June 6, 2017 18:53
Relaxed revocable locks
#define _GNU_SOURCE
#define RLOCK_SIGNAL_HANDLER
#include "rlock.h"
#include "rlock_process_status.h"
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
% perf stat ./tsx_per_core
Aborts: 73381
No reason: 57535
reason 0: 0
reason 1: 15833
reason 2: 15833
reason 3: 13
reason 4: 0
reason 5: 0
81597398 68933303 85476712 72654716 76819140 72119787 77728188 74167801 75244800 72119632 75951739 72606078 74035109 70998737 89185152 58165171 86098994 83338241 88201758 81982602 77964219 77861602 75642915 79207346 73514808 79929374 74429694 71508632 0 0 0 0
@pkhuong
pkhuong / brand.c
Created December 28, 2016 03:23
Track state by value with fictitious functions…
/* frama-c test.c -wp -then -report */
struct foo {
unsigned int x;
double y;
};
/*@ ghost int state_table[1UL << 30]; */
/*@ ghost unsigned long count; */