Skip to content

Instantly share code, notes, and snippets.

(define-vop (bignum-mult-and-add-3-arg)
(:translate sb!bignum:%multiply-and-add)
(:policy :fast-safe)
(:args (x :scs (unsigned-reg) :to :result)
(y :scs (unsigned-reg) :to :result)
(carry-in :scs (unsigned-reg) :target lo))
(:arg-types unsigned-num unsigned-num unsigned-num)
(:results (hi :scs (unsigned-reg) :from :eval)
(lo :scs (unsigned-reg) :from (:argument 2)))
(:result-types unsigned-num unsigned-num)
@pkhuong
pkhuong / B-tree.cpp
Last active August 29, 2015 14:20
Array searches: Morin's code is probably representative of what most people use in industry... i.e., not that good :x
// b-tree
// Before
template<unsigned B, typename T, typename I>
I btree_array<B, T,I>::search(const T &x) {
I j = n;
I i = 0;
while (i < n) {
I lo = i;
I hi = std::min(i+B, n);
while (lo < hi) {
@pkhuong
pkhuong / sb-complex-puns
Created July 6, 2011 05:27
Pun double-float vectors as complex double-float vectors
(defpackage "COMPLEX-PUNS"
(:use "CL")
(:export "%CDREF" "CDREF" "%CDSET" "CDSET"))
(in-package "COMPLEX-PUNS")
(deftype index ()
`(mod ,array-dimension-limit))
(sb-c:defknown (%cdref cdref) ((simple-array double-float 1) index) (complex double-float)
(sb-c:flushable sb-c:movable sb-c:foldable))
Entity component system for multi-threaded systems with a large memory footprint?
I spend most of my development time at work on systems that could be described as very read-optimised index engines. We do things
like work with sorted arrays because the density for reads is more important than linear time inserts.
The systems are also single writer, multi reader, and we optimise for read latency and throughput at the expense of writes.
In a way, I'm trying to bring the design of an ad hoc in-memory object graph closer to normal data bases, and one of the ways I'm
considering is to use something like ECS for data representation. We're moving to 1GB pages, and that really simplifies the design
tradeoffs: we only really want to make sure we use cache lines fully, but the rest (make sure that related data are mostly in the
(defun foo (&rest arguments)
;; this should all be gensymmed
(let (a-value a-p b-value b-p allow-other-keys allow-other-keys-p other-key-p)
(loop while arguments do
(let ((key (pop arguments))
(value (if (null arguments)
(error "Odd number of keyword arguments")
(pop arguments))))
(case key
(:a
@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>
@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; */
@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 / gist:926068
Created April 18, 2011 20:13
An interpreter for a tiny lexically-scoped language with (global) functions
#||
Let's see if an example works better to explain how lexical scoping works
A naive way to implement lexical scoping threads an environment across
all the operations. It could, for example, be implemented as an association
list: a list of (variable . value) conses. In CL, a second environment
is needed for functions (and GO tags, and ...), but it's just more of the
same thing. An interpreter for a very restricted language with similar
scoping rules, could look like:
||#
@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>