Skip to content

Instantly share code, notes, and snippets.

@Badel2
Badel2 / data_race.rs
Created December 5, 2020 17:52
Using miri to detect data races
//! Using miri to detect data races.
//!
//! A data race ocurrs when a variable is shared by multiple threads
//! without any synchronization primitives, and at least one of the threads may
//! mutate the variable. Reading or writing while the variable is being updated
//! is undefined behavior.
//!
//! Note that miri will only detect data races that are ocurring during the
//! current execution. It will not formally prove that your program is free of
//! data races.
@Badel2
Badel2 / hello_world.py
Created November 21, 2018 21:15
Hello world in python
#!/usr/bin/env python3
import random
random.seed(0x230f9ab296eb91053876d5fdcd15b7b989a12fd4451927196eafd39502b4487571ab839da616c9caea2089dd52e5219b809b5f735aadcec7dde80a96c2714b36b5f192fd1cdb3732994d0c5385ebb51a828d31205678b8eac7ab75a18c1b259ee18c986336fb38fb313e37b8*16**3168+1)
print("".join([chr(32+int(random.random()*96)) for i in range(13)]))
@Badel2
Badel2 / codegolf_173977.rs
Last active October 19, 2018 14:55
Describe a 2D Bitmap Using Filled Rectangle Mapping
// https://codegolf.stackexchange.com/questions/173977/describe-a-2d-bitmap-using-filled-rectangle-mapping#173977
// Badel2
extern crate rand;
use rand::{Rng, thread_rng};
#[derive(Copy, Clone, Debug)]
struct Rect {
x0: usize,
y0: usize,
@Badel2
Badel2 / prime_circles.py
Created August 19, 2018 23:16
Prime circles
#!/usr/bin/env python3
from pprint import pprint
from itertools import compress
from math import sqrt, inf, gcd
from random import randrange
import sys
# prime_circles.py: Find primes that form circles
# https://math.stackexchange.com/questions/2886024/a-conjecture-involving-prime-numbers-and-circles
# https://www.reddit.com/r/math/comments/98ie5z/a_conjecture_involving_prime_numbers_and_circles/
#[derive(Debug, Copy, Clone, PartialEq)]
enum Bit {
L, // Low, false, 0
H, // High, true, 1
X, // Undefined
}
trait Component: std::fmt::Debug {
fn update(&mut self, input: &[Bit]) -> Vec<Bit>;
fn num_inputs(&self) -> usize;
@Badel2
Badel2 / nand_comp.rs
Last active February 14, 2018 20:58
Implementing an OR gate from NAND gates https://badel2.github.io/comphdl-01
#![allow(dead_code)]
#[derive(Debug, Copy, Clone, PartialEq)]
enum Bit {
L, // Low, false, 0
H, // High, true, 1
}
trait Component {
fn update(&mut self, input: &[Bit]) -> Vec<Bit>;
@Badel2
Badel2 / spectre.c
Last active March 12, 2023 00:18
Spectre attack example implementation
/* https://spectreattack.com/spectre.pdf */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif