Skip to content

Instantly share code, notes, and snippets.

View dy's full-sized avatar

Dmitry Iv. dy

View GitHub Profile
@dy
dy / xyz2rgb.js
Created February 3, 2025 03:58
WASM vs JS – rgb to xyz transform
// test some performance parts
// wasm is surprizingly not as fast, even in full version
import t from 'tst'
import watr from 'watr'
const f64pow = `(func $f64pow (param f64 f64)(result f64)(local f64 i64 i64 i64 f64 f64 f64 f64 f64 f64)(local.set 2(f64.const 0x1p+0))(block(br_if 0(f64.eq(local.get 1)(f64.const 0x0p+0)))(local.set 3(i64.const 0))(block(br_if 0(i64.gt_s(i64.reinterpret_f64(local.get 0))(i64.const -1)))(br_if 0(f64.ne(f64.nearest(local.get 1))(local.get 1)))(local.set 3(i64.shl(i64.extend_i32_u(f64.ne(f64.nearest(local.tee 2(f64.mul(local.get 1)(f64.const 0x1p-1))))(local.get 2)))(i64.const 63)))(local.set 0(f64.neg(local.get 0))))(local.set 2(f64.const 0x1p+0))(block(br_if 0(f64.eq(local.get 0)(f64.const 0x1p+0)))(block(br_if 0(f64.ne(local.get 0)(f64.const 0x0p+0)))(local.set 2(select(f64.const inf)(f64.const 0x0p+0)(i64.lt_s(i64.reinterpret_f64(local.get 1))(i64.const 0))))(br 1))(block(br_if 0(f64.ne(f64.abs(local.get 0))(f64.const inf)))(local.set 2(select(f64.const 0x0p+0)(f64.const inf)
@dy
dy / float64.js
Last active December 27, 2024 18:09
Construct float from sign, exponent, significand ints
function constructFloat64(sign, exp, sig) {
// Constants
const EXPONENT_BIAS = 1023n;
const EXPONENT_MASK = 0x7FFn;
const SIGNIFICAND_MASK = 0xFFFFFFFFFFFFFn;
console.log(sign, exp, sig, BigInt(sig) & SIGNIFICAND_MASK)
// Mask and adjust the components
sign = sign < 0 ? 1n : 0n;
@dy
dy / floathex.js
Last active December 27, 2024 18:08
Float hex - create 0x1.abcp123 form from float number
// based on https://observablehq.com/@jrus/hexfloat
const flt_arr = new Float64Array([1]);
const int_arr = new Int32Array(flt_arr.buffer);
const SIGN_MASK = 0x7fffffff; // 0111 1111 1111 1111 1111 1111 1111 1111
const EXP_MASK = 0x7ff00000; // 0111 1111 1111 0000 0000 0000 0000 0000
// HI = 1 on little endian platforms, 0 on big-endian platforms.
const HI = 1, LO = 0;
const floathex = (x) => {
@dy
dy / name-transforms.md
Created October 3, 2024 03:44
Word/name transforms for npm

Here’s a minimal and organized markdown structure, focusing on word transformations with examples, excluding duplicates:

Word Transformations

1. Vowel Shifts and Substitutions

  • Changing vowels to create variation while keeping the basic consonant structure.
    • Examples:
      • Spray → Sprae, Sprea, Sprao, Sprai
      • Tone → Toon, Teun, Tyn, Tun
@dy
dy / signal-polyfill-preact.js
Created April 13, 2024 13:36
signal polyfill preact
// @preact/signals compatible wrapper for signal proposal
import { Signal } from 'signal-polyfill'
const { untrack, Watcher } = Signal.subtle
export const signal = v => wrap(new Signal.State(v))
export const computed = fn => wrap(new Signal.Computed(fn))
export { untrack as untracked }
@dy
dy / signals.md
Created February 22, 2024 20:19
Signals gotchas
@dy
dy / WeakishMap.js
Created November 18, 2023 19:53
WeakishMap
// based on https://github.com/WebReflection/not-so-weak/
const refs = new WeakMap;
const set = value => {
const ref = new WeakRef(value);
refs.set(value, ref);
return ref;
};
@dy
dy / c-to-wasm.md
Last active August 18, 2023 03:05
C to WASM
@dy
dy / wasm-gotchas.md
Last active June 22, 2023 17:57
WASM gotchas

Params order

First taken from stack as block, then augmented with inline param.

(func $x (param i32 i32 i32))
...
(i32.const 0)(i32.const 1) (call $x (i32.const 3))

;; converts to
@dy
dy / web-codec-decode.js
Created January 26, 2023 14:33
Web Codec API
// web codec API (one day, hopefully)
const codecConfig = {
codec: 'mp3',
sampleRate: 48000,
numberOfChannels: 2,
description: new ArrayBuffer
}
let chunk = new EncodedAudioChunk({
type: 'key',