Skip to content

Instantly share code, notes, and snippets.

@minoki
minoki / adder_nowx.c
Last active October 19, 2023 09:38
Code generation on x86_64
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h> // mmap, mprotect, munmap
#include <unistd.h>
typeof(int (*)(int)) adder(int y)
{
void *mem = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (mem == MAP_FAILED) {
@minoki
minoki / multi-prompt.lua
Last active November 27, 2022 12:32
one-shot delimited continuations in Lua
function newPromptTag()
return {}
end
local sk_meta = {}
local function runWithTag(tag, co, ...)
local status, a, b, c = coroutine.resume(co, ...)
if status then
if a == "return" then
return b
elseif a == "capture" then
@minoki
minoki / fib.py
Created September 8, 2022 10:02
Fibonacci in Python and Ruby
import timeit
from typing import Tuple
def naive_fib(n: int) -> int:
if n <= 1: # n == 0 or n == 1
return n
else:
return naive_fib(n - 1) + naive_fib(n - 2)
print(naive_fib(20))
@minoki
minoki / ghc-ieee754.md
Last active February 20, 2022 05:27
IEEE 754 conformance of GHC primitives

Literal

-0.0## under NegativeLiterals+MagicHash yields positive 0.0##.

Optimization

Constant folding

Fragile to negative zero and infinity and NaN (LitFloat/LitDouble cannot express them). cf. #9811, #18897.

@minoki
minoki / expr-test.tex
Created December 15, 2021 03:40
Standard ML on LuaTeX: math formula
\documentclass{article}
\usepackage{amsmath}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
local M = require "expr"
function diff(expr, var)
local result = M.parse(expr)
if result.tag == "Ok" then
local e = M.simplify(M.diff(var)(result.payload))
@minoki
minoki / seki-bernoulli.lua
Last active December 15, 2021 03:12
Standard ML on LuaTeX: Bernoulli numbers
local assert = assert
local error = error
local getmetatable = getmetatable
local pairs = pairs
local pcall = pcall
local setmetatable = setmetatable
local math = math
local math_abs = math.abs
local math_type = math.type
local math_maxinteger = math.maxinteger
@minoki
minoki / test_cblas.c
Created January 27, 2021 11:51
BLASなんもわからん
#include <cblas_openblas.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
double A[3][3] = {
{1.0, 2.0, 3.0},
{2.0, 3.0, 4.0},
{4.0, 5.0, 6.0},
@minoki
minoki / timer-remote.ino
Created December 17, 2020 06:53
M5Stackで作るタイマーリモコン
#include <M5Stack.h>
#include <utility/M5Timer.h>
const int SHUTTER_PIN = 12;
const int FOCUS_PIN = 5;
const int LEVER_UP = 13;
const int LEVER_PUSH = 0;
const int LEVER_DOWN = 34;
@minoki
minoki / Sing.hs
Last active December 10, 2020 04:24
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeOperators, NoStarIsType #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}
{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
module Sing where
import qualified Data.Singletons.Prelude as S
@minoki
minoki / sin-test.c
Created November 6, 2020 10:36
libmのsinの実装が x=2^n (-1000≤n≤1000)の形の入力に対して「正しく丸められた値」からどのぐらいずれているか検査するやつ
// Written by @mod_poppo
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <mpfr.h>
#include <gmp.h>
static void to_normalized_hex(char *buf, mpfr_t a, int mant)
{
// assume a is positive