Skip to content

Commit

Permalink
Add WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
ashayp22 committed Feb 8, 2024
1 parent ef957b1 commit 98d4318
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "simd_vol",
"version": "0.1.0",
"files": [
"simd_vol_bg.wasm",
"simd_vol.js",
"simd_vol.d.ts"
],
"module": "simd_vol.js",
"types": "simd_vol.d.ts",
"sideEffects": [
"./snippets/*"
]
}
53 changes: 53 additions & 0 deletions pkg/simd_vol.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {OptionDir} option_dir
* @param {Float32Array} price
* @param {Float32Array} spot
* @param {Float32Array} strike
* @param {Float32Array} risk_free_rate
* @param {Float32Array} dividend_yield
* @param {Float32Array} years_to_expiry
* @param {number} max_iterations
* @param {number} threshold
* @returns {Float32Array}
*/
export function implied_vol(option_dir: OptionDir, price: Float32Array, spot: Float32Array, strike: Float32Array, risk_free_rate: Float32Array, dividend_yield: Float32Array, years_to_expiry: Float32Array, max_iterations: number, threshold: number): Float32Array;
/**
* Specify whether an option is put or call
*/
export enum OptionDir {
CALL = 2,
PUT = 1,
}

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly implied_vol: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
/**
* Instantiates the given `module`, which can either be bytes or
* a precompiled `WebAssembly.Module`.
*
* @param {SyncInitInput} module
*
* @returns {InitOutput}
*/
export function initSync(module: SyncInitInput): InitOutput;

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
export default function __wbg_init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
187 changes: 187 additions & 0 deletions pkg/simd_vol.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
let wasm;

const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );

if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };

let cachedUint8Memory0 = null;

function getUint8Memory0() {
if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

let cachedFloat32Memory0 = null;

function getFloat32Memory0() {
if (cachedFloat32Memory0 === null || cachedFloat32Memory0.byteLength === 0) {
cachedFloat32Memory0 = new Float32Array(wasm.memory.buffer);
}
return cachedFloat32Memory0;
}

let WASM_VECTOR_LEN = 0;

function passArrayF32ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 4, 4) >>> 0;
getFloat32Memory0().set(arg, ptr / 4);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

let cachedInt32Memory0 = null;

function getInt32Memory0() {
if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}

function getArrayF32FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getFloat32Memory0().subarray(ptr / 4, ptr / 4 + len);
}
/**
* @param {OptionDir} option_dir
* @param {Float32Array} price
* @param {Float32Array} spot
* @param {Float32Array} strike
* @param {Float32Array} risk_free_rate
* @param {Float32Array} dividend_yield
* @param {Float32Array} years_to_expiry
* @param {number} max_iterations
* @param {number} threshold
* @returns {Float32Array}
*/
export function implied_vol(option_dir, price, spot, strike, risk_free_rate, dividend_yield, years_to_expiry, max_iterations, threshold) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArrayF32ToWasm0(price, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
const ptr1 = passArrayF32ToWasm0(spot, wasm.__wbindgen_malloc);
const len1 = WASM_VECTOR_LEN;
const ptr2 = passArrayF32ToWasm0(strike, wasm.__wbindgen_malloc);
const len2 = WASM_VECTOR_LEN;
const ptr3 = passArrayF32ToWasm0(risk_free_rate, wasm.__wbindgen_malloc);
const len3 = WASM_VECTOR_LEN;
const ptr4 = passArrayF32ToWasm0(dividend_yield, wasm.__wbindgen_malloc);
const len4 = WASM_VECTOR_LEN;
const ptr5 = passArrayF32ToWasm0(years_to_expiry, wasm.__wbindgen_malloc);
const len5 = WASM_VECTOR_LEN;
wasm.implied_vol(retptr, option_dir, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, ptr5, len5, max_iterations, threshold);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v7 = getArrayF32FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4, 4);
return v7;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* Specify whether an option is put or call
*/
export const OptionDir = Object.freeze({ CALL:2,"2":"CALL",PUT:1,"1":"PUT", });

async function __wbg_load(module, imports) {
if (typeof Response === 'function' && module instanceof Response) {
if (typeof WebAssembly.instantiateStreaming === 'function') {
try {
return await WebAssembly.instantiateStreaming(module, imports);

} catch (e) {
if (module.headers.get('Content-Type') != 'application/wasm') {
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

} else {
throw e;
}
}
}

const bytes = await module.arrayBuffer();
return await WebAssembly.instantiate(bytes, imports);

} else {
const instance = await WebAssembly.instantiate(module, imports);

if (instance instanceof WebAssembly.Instance) {
return { instance, module };

} else {
return instance;
}
}
}

function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

return imports;
}

function __wbg_init_memory(imports, maybe_memory) {

}

function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;
cachedFloat32Memory0 = null;
cachedInt32Memory0 = null;
cachedUint8Memory0 = null;


return wasm;
}

function initSync(module) {
if (wasm !== undefined) return wasm;

const imports = __wbg_get_imports();

__wbg_init_memory(imports);

if (!(module instanceof WebAssembly.Module)) {
module = new WebAssembly.Module(module);
}

const instance = new WebAssembly.Instance(module, imports);

return __wbg_finalize_init(instance, module);
}

async function __wbg_init(input) {
if (wasm !== undefined) return wasm;

if (typeof input === 'undefined') {
input = new URL('simd_vol_bg.wasm', import.meta.url);
}
const imports = __wbg_get_imports();

if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
input = fetch(input);
}

__wbg_init_memory(imports);

const { instance, module } = await __wbg_load(await input, imports);

return __wbg_finalize_init(instance, module);
}

export { initSync }
export default __wbg_init;
Binary file added pkg/simd_vol_bg.wasm
Binary file not shown.
7 changes: 7 additions & 0 deletions pkg/simd_vol_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function implied_vol(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number, k: number, l: number, m: number, n: number, o: number, p: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;

0 comments on commit 98d4318

Please sign in to comment.