Skip to content

Instantly share code, notes, and snippets.

@bellbind
bellbind / dct.js
Last active February 27, 2025 04:08
[ecmascript] Simple DCT(discrete cosine transform) implementation
// 1D-DCT(Discreet Cosine Transform)
function dct(xs) {
const pn = Math.PI / xs.length, f = Math.sqrt(2 / xs.length);
return xs.map((_, k) => {
const c = pn * k;
return f * xs.reduce((r, x, n) => r + x * Math.cos(c * (n + 0.5)), 0);
});
}
function idct(xs) {
const xs_ = [xs[0] / 2].concat(xs.slice(1));
@bellbind
bellbind / avxvec.c
Last active February 21, 2025 04:41
[c][avx][simd]example for intel SSE/AVX intrinsic api
// clang -std=c11 -mavx avxvec.c -o avxvec
#include <x86intrin.h>
#include <stdio.h>
// see https://software.intel.com/sites/landingpage/IntrinsicsGuide/#techs=AVX
int main() {
double mem[4] = {1.0, 2.0, 3.0, 4.0};
//__m256d v = _mm256_load_pd(mem);
__m256d v = _mm256_set_pd(1, 2, 3, 4); // (e3, e2, e1, e1)
__m256d sq = _mm256_mul_pd(v, v);
@bellbind
bellbind / Shaders.metal
Last active January 24, 2025 23:45
[swift][osx] Metal programming with commandline (Xcode less)
//-*- mode: c++ -*-
// build:
// xcrun -sdk macosx metal -std=osx-metal1.1 Shaders.metal -o shaders.air
// xcrun -sdk macosx metal-ar rcs Shaders.metalar Shaders.air
// xcrun -sdk macosx metallib -o Shaders.metallib Shaders.metalar
#include <metal_stdlib>
kernel void square(const device float* input [[buffer(0)]],
device float* output [[buffer(1)]],
metal::uint id [[thread_position_in_grid]]) {
@bellbind
bellbind / index.html
Created July 29, 2022 14:13
[JavaScript][HTML] run es modules in iframe context
<!doctype html>
<html>
<head>
<script type="module">
document.querySelector("#run").addEventListener("click", async ev => {
const modUrl = new URL("./module.js", location.href).href;
//console.log(modUrl);
const iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.append(iframe);
@bellbind
bellbind / index.html
Last active November 2, 2024 00:19
[react][redux]Reversi Game with React/Redux and React-Redux
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://unpkg.com/react@15.3.1/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.3.1/dist/react-dom.js"
></script>
<script src="https://unpkg.com/redux@3.6.0/dist/redux.js"></script>
<script src="https://unpkg.com/react-redux@4.4.5/dist/react-redux.js"
></script>
@bellbind
bellbind / tetris-svg.html
Created November 20, 2012 09:22
[javascript][svg]tetris model with svg ui (arrow key control)
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<script src="tetris.js"></script>
<script src="tetris-svg.js"></script>
</head>
<body>
<div><svg id="stage"></svg></div>
@bellbind
bellbind / sizeof.c
Last active October 25, 2024 02:49
[C][emscripten]sizeof number types
// clang -Wall -Wextra -std=c11 sizeof.c -o sizeof
// emcc -Wall -Wextra -std=c11 sizeof.c -o sizeof.js
#include <stdlib.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <wchar.h>
#include <time.h>
@bellbind
bellbind / ecc.py
Created December 1, 2011 08:08
[python]basics of elliptic curve cryptography
# Basics of Elliptic Curve Cryptography implementation on Python
import collections
def inv(n, q):
"""div on PN modulo a/b mod q as a * inv(b, q) mod q
>>> assert n * inv(n, q) % q == 1
"""
for i in range(q):
if (n * i) % q == 1:
@bellbind
bellbind / pem.js
Last active September 17, 2024 20:19
[nodejs]Example of RSA usages with node-forge
// RSA with node-forge
"use strict";
// npm install node-forge
const forge = require("node-forge");
new Promise((f, r) => forge.pki.rsa.generateKeyPair(
2048, (err, pair) => err ? r(err) : f(pair)))
.then(keypair => {
const priv = keypair.privateKey;
@bellbind
bellbind / Dockerfile
Last active August 27, 2024 12:32
[docker] ruby-1.8.7 with rails-2.3.18 image
# docker build -t ruby-1.8.7 .
# docker run -it --rm ruby-1.8.7
FROM ubuntu:16.04
WORKDIR /root
RUN apt update
RUN apt upgrade -y
RUN apt install -y ruby-build autoconf subversion bison
RUN apt install -y mecab mecab-ipadic-utf8 wget