Skip to content

Instantly share code, notes, and snippets.

Avatar

Zach Wegner zwegner

View GitHub Profile
@zwegner
zwegner / gist:5079097
Last active Dec 14, 2015
A simple program that counts the number of monotonic, dependent, and distinct functions of 4 boolean variables. I wrote this for a little mini-contest for my Digital Logic Design class in 2010. There was only one other student who solved it, and his Java code was wayyy longer and uglier. As I thought this was a fairly elegant little program, I'm…
View gist:5079097
#include <stdio.h>
const int mask[4] = { 0x5555, 0x3333, 0x0F0F, 0x00FF };
int count_table(int *table, char *lbl) {
int count, x;
for (count = x = 0; x < 1<<16; x++)
count += table[x];
@zwegner
zwegner / gist:6841688fa897aef64a11016967c36f2d
Created Jan 13, 2020
Ternary logic multiplication (0, 1, unknown)
View gist:6841688fa897aef64a11016967c36f2d
N_BITS = 8
MASK = (1 << N_BITS) - 1
class Ternary:
def __init__(self, ones, unknowns):
self.ones = ones & MASK
self.unknowns = unknowns & MASK
def __add__(self, other):
x = self.ones + other.ones
u = self.unknowns | other.unknowns | (x ^ (x + self.unknowns + other.unknowns))
@zwegner
zwegner / gist:0202c60b9410d029b5cfc5c5643e3374
Last active Jan 13, 2020 — forked from dougallj/gist:9211fd24c3759f7f340dede28929c659
Ternary logic multiplication (0, 1, unknown)
View gist:0202c60b9410d029b5cfc5c5643e3374
import itertools
N_BITS = 8
MASK = (1 << N_BITS) - 1
# Bit class, which stores values as a truth table of up to two variables
class Bit:
def __init__(self, op, bit_a=None, bit_b=None):
self.op = op
# Remove variables when the truth table doesn't depend on them
@zwegner
zwegner / utf8_prob.py
Created Jun 30, 2020
Calculate probability of N-byte sequence being part of a valid UTF-8 sequence
View utf8_prob.py
from fractions import Fraction
ascii = range(0, 0x80)
cont = range(0x80, 0xC0)
cont_2_0 = range(0xA0, 0xC0)
cont_2_2 = range(0x80, 0xA0)
cont_3_0 = range(0x90, 0xC0)
cont_3_2 = range(0x80, 0x90)
leader2 = range(0xC2, 0xE0)
leader3_0 = range(0xE0, 0xE1)
@zwegner
zwegner / wordcount.py
Created Jul 8, 2020
Assembly generator script in Python, for making a fast wc -w
View wordcount.py
import collections
import contextlib
import sys
# Register class, for GPRs and vector registers
_Reg = collections.namedtuple('Reg', 't v')
class Reg(_Reg):
def __str__(self):
names = ['ax', 'cx', 'dx', 'bx', 'sp', 'bp', 'si', 'di']
if self.t == 'r' and self.v < 8:
@zwegner
zwegner / flopsort.py
Created Jul 27, 2020
Quick+dumb way to sort an array to try and maximize Hamming distance between adjacent elements
View flopsort.py
import random
def hamming(a, b):
x = a ^ b
c = 0
while x:
x &= x - 1
c += 1
return c
@zwegner
zwegner / p1_p0156.py
Last active Sep 2, 2020
Find uops that execute on p1 pre-ICL and on p0156 on ICL (x86-info-term example query code)
View p1_p0156.py
import re
from x86_info_term import get_cache
args, cache = get_cache()
uops_info = cache['datasets']['uops_info']['data']
for inst in uops_info:
for form in uops_info[inst]['forms']:
if 'ICL' in form['arch'] and 'SKX' in form['arch']:
@zwegner
zwegner / measure-store-buffer-false-dependencies.py
Last active Sep 7, 2020
A tool for measuring the performance of false dependencies in store forwarding
View measure-store-buffer-false-dependencies.py
# Test address matching in store forwarding
#
# This tests a mechanism discussed in these references:
#
# * US Patent 2008/0082765 A1
# https://patentimages.storage.googleapis.com/73/b9/bf/b258f3e3985f47/US20080082765A1.pdf
# * Fallout: Leaking Data on Meltdown-resistant CPUs
# https://mdsattacks.com/files/fallout.pdf
# * Stack Overflow: What are the microarchitectural details behind MSBDS (Fallout)?
# https://stackoverflow.com/a/56213609
You can’t perform that action at this time.