Skip to content

Instantly share code, notes, and snippets.

@pkhuong
pkhuong / yannakakis.md
Last active April 13, 2024 14:36
A minimal version of Yannakakis's algorithm for mostly plain Python
#!/usr/bin/env sed -re s|^|\x20\x20\x20\x20| -e s|^\x20{4}\x23\x23{(.*)$|<details><summary>\1</summary>\n| -e s|^\x20{4}\x23\x23}$|\n</details>| -e s|^\x20{4}\x23\x23\x20?|| -e s|\x0c|\x20|
license, imports
# Yannakakis.py by Paul Khuong
#
# To the extent possible under law, the person who associated CC0 with
# Yannakakis.py has waived all copyright and related or neighboring rights
# to Yannakakis.py.

@pkhuong
pkhuong / README.md
Created May 3, 2021 15:39 — forked from parsley42/README.md
Chromebook ssh-agent setup

Setting up ssh-agent in Linux/Crostini on Chromebook

  • Create your user systemd directory:
$ mkdir -p .config/systemd/user
  • Edit the unit file .config/systemd/user/ssh-agent.service:
[Unit]
Description=SSH key agent
@pkhuong
pkhuong / 00RECORD_STREAM.md
Last active January 11, 2021 17:09
Word-stuffed record stream

Backtrace's log record framing format

We use this self-synchronising format to store key metadata for our server-side embedded crash database. See this blog post for more details and explanations.

We released the source under the MIT license, but the scheme is simple enough that coding an interface tuned to one's specific needs probably makes sense.

@pkhuong
pkhuong / kmv_ci.py
Last active September 25, 2020 21:13
Numerical implementation of Beyer et al's confidence intervals for k min values
"""Numerical implementation of Beyer et al's confidence intervals for k min values
[On Synopses for Distinct-Value Estimation Under Multiset Operations](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.230.7008&rep=rep1&type=pdf#page=6), Section 4.3.
ci_probability(k, eps) computes the probability that the unbiased
cardinality estimate
\hat{D}_k^{UB} = (k - 1) / U(k),
where k is the number of minimum values we keep, and U(k) is the
@pkhuong
pkhuong / bridgetc_digit_update_1.inc
Last active May 24, 2020 00:27
BridgeTC: a bridge from C types to C singleton values
/* -*- mode: C -*- */
#if BRIDGETC_IMPL_DIGIT_1 == 0
# undef BRIDGETC_IMPL_DIGIT_1
# define BRIDGETC_IMPL_DIGIT_1 1
# undef BRIDGETC_IMPL_SEARCH_1
# define BRIDGETC_IMPL_SEARCH_1 BRIDGETC_IMPL_SEARCH_1_1
#elif BRIDGETC_IMPL_DIGIT_1 == 1
# undef BRIDGETC_IMPL_DIGIT_1
# define BRIDGETC_IMPL_DIGIT_1 2
# undef BRIDGETC_IMPL_SEARCH_1
"""Too many ways to generate pairs of two integers in ascending
order.
"""
from hypothesis import assume, given
import hypothesis.strategies as st
import unittest
class TestAscending(unittest.TestCase):
@pkhuong
pkhuong / LICENCE
Last active December 15, 2021 23:25
MIT-licensed support code for coverage-guided Hypothesis testing
Copyright 2020 Paul Khuong, Backtrace I/O, Inc.
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 the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWAR
@pkhuong
pkhuong / signal-on-instruction-count.c
Created February 18, 2020 02:21
Trigger a unix signal based on the HW instruction counter PMC
#define RUN_ME /*
exec cc -g -ggdb -O2 -W -Wall -std=c99 $0 -o "$(basename $0 .c)"
*/
/*
* Copyright 2020 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 / multiplicative-inverse.lisp
Created December 30, 2019 04:01
Multiplicative inverse mod 2^w, without Euclid or Bezout.
(defun multiplicative-inverse (x bitwidth)
"Iteratively finds the multiplicative inverse of x mod 2^bitwidth.
The induction step uses the fact that, for a given inverse x^-1
with (x * x^-1) mod 2^w == 1, (x * x^-1) mod 2^{w - 1} == 1 as
well. We start with the trivial inverse mod 2, and increase the
bitwidth iteratively: given inv_{w - 1} = x^-1 mod 2^{w - 1},
x^-1 mod 2^w is either inv_{w - 1}, or inv_{w - 1} + 2^{w - 1}."
(assert (oddp x))
(let ((inverse 1))
@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;
}