Skip to content

Instantly share code, notes, and snippets.

@socantre
socantre / regions.lua
Last active September 30, 2017 20:12
regions.lua composing bitmaps
local module = {}
function module.circle(cx, cy, radius)
return function(x, y) return (x - cx)^2 + (y - cy)^2 < radius^2 end
end
function module.halfspace(rise, run, x0, y0)
return function(x, y) return run*(y - y0) < rise*(x - x0) end
end
@socantre
socantre / scope_exit.h
Created July 20, 2017 17:47
simple scope exit
template <typename Func> struct scope_exit
{
Func cleanup;
scope_exit(Func &&f) : cleanup(std::forward<Func>(f)) {}
~scope_exit() { cleanup(); }
};
template <typename Func> scope_exit<Func> make_scope_exit(Func &&f)
{
@socantre
socantre / require.hpp
Created June 1, 2017 19:24
require() with REQUIRE() macro to deal with source locations, until we get a real std::source_location type
#include <stdexcept>
struct require_error : std::runtime_error {
using std::runtime_error::runtime_error;
};
void require(bool b, char const *message) {
if (!b) {
throw require_error(message);
}
@socantre
socantre / main.js
Created March 24, 2017 00:24
screeps room distance transform
function distanceTransform(roomName) {
let vis = new RoomVisual(roomName);
let topDownPass = new PathFinder.CostMatrix();
for (let y = 0; y < 50; ++y) {
for (let x = 0; x < 50; ++x) {
if (Game.map.getTerrainAt(x, y, roomName) == 'wall') {
topDownPass.set(x, y, 0);
}
else {
@socantre
socantre / demangle.cpp
Last active September 26, 2016 16:39
Example use of the Itanium ABI demangling API.
#include <cxxabi.h>
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <memory>
struct clib_free {
template<typename T>
@socantre
socantre / music.sh
Created March 12, 2016 21:42
random note generator
# based on http://blog.robertelder.org/bash-one-liner-compose-music/
# with overflow, channels, timing, etc. bugs fixed
< /dev/urandom hexdump -v -e '/1 "%u\n"' |
gawk 'BEGIN { split("0,2,4,5,7,9,11,12",a,",")}
{ for (i = 0; i < 0.3125; i += 0.0000625)
printf("%02X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' |
sed -e 's/.*\(..\)$/\1/g' |
xxd -r -p |
sox -traw -c 1 -r16000 -b8 -e signed-integer - -tcoreaudio
@socantre
socantre / fenv.hpp
Created October 29, 2015 20:17
playing with system_error and error codes
#include <system_error>
#include <cfenv>
#include <cassert>
namespace fe {
enum class rounding_mode : int {
downward = FE_DOWNWARD,
to_nearest = FE_TONEAREST,
toward_zero = FE_TOWARDZERO,
@socantre
socantre / foil_optimizer.cpp
Last active August 11, 2019 17:36
CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!" functions to disable compiler optimization
static void escape(void *p) {
asm volatile("" : : "g"(p) : "memory");
}
static void clobber() {
asm volatile("" : : : "memory");
}
@socantre
socantre / seed_sequence.c
Created October 1, 2015 18:41
Find seeds that produce sequential states in rand(). e.g. find the seed that produces the same state as `srand(1); rand();`. In other words, find `x` such that the following prints `1`: `srand(1); rand(); int a = rand(); srand(x); int b = rand(); printf("%i", a == b);
#include <stdlib.h>
#include <stdio.h>
unsigned find_following_seed(unsigned seed) {
srand(seed);
(void)rand();
int arr[] = {rand(), rand(), rand(), rand()};
for (long long next_seed = 0; next_seed <= UINT_MAX; ++next_seed) {
srand(next_seed);
@socantre
socantre / find_sequence_length.c
Created October 1, 2015 18:30
find how many times one can call rand() in sequence before the values start repeating.
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(1);
int arr[] = {rand(), rand(), rand(), rand()};
long long n = sizeof(arr) / sizeof(*arr);
while (true) {
if (rand() != arr[0]) n += 1;
else if (rand() != arr[1]) n += 2;