Skip to content

Instantly share code, notes, and snippets.

@socantre
socantre / regions.lua
Last active Sep 30, 2017
regions.lua composing bitmaps
View regions.lua
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 Jul 20, 2017
simple scope exit
View scope_exit.h
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 Jun 1, 2017
require() with REQUIRE() macro to deal with source locations, until we get a real std::source_location type
View require.hpp
#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 Mar 24, 2017
screeps room distance transform
View main.js
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 Sep 26, 2016
Example use of the Itanium ABI demangling API.
View demangle.cpp
#include <cxxabi.h>
#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <memory>
struct clib_free {
template<typename T>
@socantre
socantre / music.sh
Created Mar 12, 2016
random note generator
View music.sh
# 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 Oct 29, 2015
playing with system_error and error codes
View fenv.hpp
#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 Aug 11, 2019
CppCon 2015: Chandler Carruth "Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!" functions to disable compiler optimization
View foil_optimizer.cpp
static void escape(void *p) {
asm volatile("" : : "g"(p) : "memory");
}
static void clobber() {
asm volatile("" : : : "memory");
}
@socantre
socantre / seed_sequence.c
Created Oct 1, 2015
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);
View seed_sequence.c
#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 Oct 1, 2015
find how many times one can call rand() in sequence before the values start repeating.
View find_sequence_length.c
#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;
You can’t perform that action at this time.