Skip to content

Instantly share code, notes, and snippets.

View Yaffle's full-sized avatar

Viktor Yaffle

View GitHub Profile
@Yaffle
Yaffle / convertPointFromPageToNode.js
Last active April 30, 2024 03:50
function to get the MouseEvent coordinates for an element that has CSS3 Transforms
/*jslint plusplus: true, vars: true, indent: 2 */
/*
convertPointFromPageToNode(element, event.pageX, event.pageY) -> {x, y}
returns coordinate in element's local coordinate system (works properly with css transforms without perspective projection)
convertPointFromNodeToPage(element, offsetX, offsetY) -> {x, y}
returns coordinate in window's coordinate system (works properly with css transforms without perspective projection)
*/
@Yaffle
Yaffle / Math.fma.js
Last active April 27, 2024 04:21
Math.fma - fused multiply–add (FMA) or fused multiply–accumulate (FMAC) in JavaScript
// a * b + c
// Math.fma - fused multiply–add (FMA) or fused multiply–accumulate (FMAC) in JavaScript
// For implementation details, see "The Handbook of Applied Cryptography"
// http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
// and
// https://github.com/JuliaLang/openlibm/blob/master/src/s_fma.c
//!? BUGGY
(function () {
@Yaffle
Yaffle / gist:1287361
Last active April 27, 2024 04:19
crc 32 in javascript
function crc32(s/*, polynomial = 0x04C11DB7, initialValue = 0xFFFFFFFF, finalXORValue = 0xFFFFFFFF*/) {
s = String(s);
var polynomial = arguments.length < 2 ? 0x04C11DB7 : (arguments[1] >>> 0);
var initialValue = arguments.length < 3 ? 0xFFFFFFFF : (arguments[2] >>> 0);
var finalXORValue = arguments.length < 4 ? 0xFFFFFFFF : (arguments[3] >>> 0);
var table = new Array(256);
var reverse = function (x, n) {
var b = 0;
@Yaffle
Yaffle / TextEncoderTextDecoder.js
Last active February 21, 2024 18:27
TextEncoder/TextDecoder polyfills for utf-8
// TextEncoder/TextDecoder polyfills for utf-8 - an implementation of TextEncoder/TextDecoder APIs
// Written in 2013 by Viktor Mukhachev <vic99999@yandex.ru>
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Some important notes about the polyfill below:
// Native TextEncoder/TextDecoder implementation is overwritten
// String.prototype.codePointAt polyfill not included, as well as String.fromCodePoint
// TextEncoder.prototype.encode returns a regular array instead of Uint8Array
// No options (fatal of the TextDecoder constructor and stream of the TextDecoder.prototype.decode method) are supported.
@Yaffle
Yaffle / jsHelper64.js
Created March 29, 2023 08:11
jsHelper64.js
const uint64 = new BigUint64Array(1);
const uint32 = new Uint32Array(uint64.buffer);
function clz64(bigint) {
uint64[0] = bigint;
let x = Math.clz32(uint32[1]);
let y = Math.clz32(uint32[0]);
return x + (x === 32 ? y : 0);
}
@Yaffle
Yaffle / wast2wasm.js
Last active March 18, 2023 11:13
conversion of wast to wasm (WebAssembley Text Format to WebAssemlby binary-code) - no dependencies
/*jshint esversion:6, bitwise:false*/
// https://webassembly.github.io/wabt/demo/wat2wasm/index.html helps a lot
function wast2wasm(sExpression) {
function sExpressionToJSON(s) {
return JSON.parse(s.trim().replace(/\s*\)/g, ']').replace(/\(\s*/g, '[').replace(/\s+/g, ',').replaceAll('"', '').replace(/([^\[\],]+)/g, '"$1"'));
}
// CPU L2 cache size
function ASMModule1(stdlib, foreign, heap) {
"use asm";
var memory = new stdlib.Uint32Array(heap);
function loop(c) {
c = c | 0;
var k = 0;
var j = 0;
for (j = c; (j | 0) != 0; j = (j - 1) | 0) {
k = memory[k >> 2] << 2;
@Yaffle
Yaffle / gist:1900579
Created February 24, 2012 12:08
setTimeout + Page Visibility API
function setVisibleTimeout(callback, delay) {
var id = null,
t = 0,
prefix = '';
'o webkit moz ms'.replace(/\S+/g, function (p) {
if ((p + 'Hidden') in document) {
prefix = p;
}
});
function onVisibilityChange(event) {
@Yaffle
Yaffle / URLUtils.js
Last active September 5, 2022 02:19
parse URL + absolutize URL in javascript (URLUtils shim - http://url.spec.whatwg.org/#url)
/*jslint regexp: true, maxerr: 50, indent: 2 */
(function (global) {
"use strict";
function URLUtils(url, baseURL) {
var m = String(url).replace(/^\s+|\s+$/g, "").match(/^([^:\/?#]+:)?(?:\/\/(?:([^:@\/?#]*)(?::([^:@\/?#]*))?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
if (!m) {
throw new RangeError();
}
@Yaffle
Yaffle / continuedFractionForSqrt.js
Created September 6, 2021 10:53
continued fraction for sqrt(n)
function* continuedFractionForSqrt(n) {
// https://trizenx.blogspot.com/2018/10/continued-fraction-factorization-method.html
function floorDiv(a, b) {
return typeof a === "bigint" && typeof b === "bigint" ? a / b : Math.floor(a / b);
}
n = BigInt(n);
if (n < 0n) {
throw new RangeError();
}
const root = BigInt(sqrt(n));