Skip to content

Commit

Permalink
feat(vectors): add more strided vec ops, refactor templates
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 29, 2019
1 parent 65b29f4 commit ca91fa9
Show file tree
Hide file tree
Showing 31 changed files with 463 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/vectors/src/addm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MultiVecOpVVV, VecOpVVV } from "./api";
import { ARGS_VVV, defOp } from "./internal/codegen";
import { ADDM } from "./internal/templates";
import { MATH2 } from "./internal/templates";

/**
* Returns `out = (a + b) * c`.
Expand All @@ -9,6 +9,6 @@ import { ADDM } from "./internal/templates";
* @see subm
*/
export const [addm, addm2, addm3, addm4] = defOp<MultiVecOpVVV, VecOpVVV>(
ADDM,
MATH2("+", "*"),
ARGS_VVV
);
4 changes: 2 additions & 2 deletions packages/vectors/src/addmn.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { MultiVecOpVVN, VecOpVVN } from "./api";
import { ARGS_VVN, defOp } from "./internal/codegen";
import { ADDM_N } from "./internal/templates";
import { MATH2_N } from "./internal/templates";

/**
* Returns `out = (a + b) * n`.
*/
export const [addmN, addmN2, addmN3, addmN4] = defOp<MultiVecOpVVN, VecOpVVN>(
ADDM_N,
MATH2_N("+", "*"),
ARGS_VVN
);
9 changes: 9 additions & 0 deletions packages/vectors/src/addms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { VecOpSVVV } from "./api";
import { ARGS_VVV, defOpS, SARGS_VVV } from "./internal/codegen";
import { MATH2 } from "./internal/templates";

export const [addmS2, addmS3, addmS4] = defOpS<VecOpSVVV>(
MATH2("+", "*"),
`${ARGS_VVV},${SARGS_VVV}`,
ARGS_VVV
);
8 changes: 8 additions & 0 deletions packages/vectors/src/addmsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VecOpSVNV } from "./api";
import { ARGS_VVN, defOpS, SARGS_VV } from "./internal/codegen";
import { MATH2_N } from "./internal/templates";

export const [addmSN2, addmSN3, addmSN4] = defOpS<VecOpSVNV>(
MATH2_N("+", "*"),
`${ARGS_VVN},${SARGS_VV}`
);
14 changes: 14 additions & 0 deletions packages/vectors/src/addsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { VecOpSVN } from "./api";
import {
ARGS_V,
ARGS_VN,
defOpS,
SARGS_V
} from "./internal/codegen";
import { MATH_N } from "./internal/templates";

export const [addSN2, addSN3, addSN4] = defOpS<VecOpSVN>(
MATH_N("+"),
`${ARGS_VN},${SARGS_V}`,
ARGS_V
);
65 changes: 65 additions & 0 deletions packages/vectors/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ export type VecOpSV = (
so?: number,
sa?: number
) => Vec;

export type VecOpSVN = (
out: Vec | null,
a: ReadonlyVec,
n: number,
io?: number,
ia?: number,
so?: number,
sa?: number
) => Vec;

export type VecOpSVV = (
out: Vec | null,
a: ReadonlyVec,
Expand All @@ -132,6 +143,50 @@ export type VecOpSVV = (
sa?: number,
sb?: number
) => Vec;

export type VecOpSVNV = (
out: Vec | null,
a: ReadonlyVec,
n: number,
b: ReadonlyVec,
io?: number,
ia?: number,
ib?: number,
so?: number,
sa?: number,
sb?: number
) => Vec;

export type VecOpSVVN = (
out: Vec | null,
a: ReadonlyVec,
b: ReadonlyVec,
n: number,
io?: number,
ia?: number,
ib?: number,
so?: number,
sa?: number,
sb?: number
) => Vec;

export type VecOpSVVV = (
out: Vec | null,
a: ReadonlyVec,
b: ReadonlyVec,
c: ReadonlyVec,
io?: number,
ia?: number,
ib?: number,
ic?: number,
so?: number,
sa?: number,
sb?: number,
sc?: number
) => Vec;

export type VecOpSRoV<T> = (a: ReadonlyVec, ia?: number, sa?: number) => T;

export type VecOpSRoVV<T> = (
a: ReadonlyVec,
b: ReadonlyVec,
Expand All @@ -141,6 +196,16 @@ export type VecOpSRoVV<T> = (
sb?: number
) => T;

export type VecOpSVO<T> = (
out: Vec | null,
a: ReadonlyVec,
b?: T,
io?: number,
ia?: number,
so?: number,
sa?: number
) => Vec;

export interface MultiVecOpV extends VecOpV, MultiVecOp<VecOpV> {}
export interface MultiVecOpN extends VecOpN, MultiVecOp<VecOpN> {}
export interface MultiVecOpVV extends VecOpVV, MultiVecOp<VecOpVV> {}
Expand Down
31 changes: 31 additions & 0 deletions packages/vectors/src/crosss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ReadonlyVec, Vec } from "./api";
import { setSC3 } from "./setsc";

export const crossS2 = (
a: ReadonlyVec,
b: ReadonlyVec,
ia = 0,
ib = 0,
sa = 1,
sb = 1
) => a[ia] * b[ib + sb] - a[ia + sa] * b[ib];

export const crossS3 = (
out: Vec | null,
a: ReadonlyVec,
b: ReadonlyVec,
io = 0,
ia = 0,
ib = 0,
so = 1,
sa = 1,
sb = 1
) =>
setSC3(
out || a,
a[ia + sa] * b[ib + 2 * sb] - a[ia + 2 * sa] * b[ib + sb],
a[ia + 2 * sa] * b[ib] - a[ia] * b[ib + 2 * sb],
a[ia] * b[ib + sb] - a[ia + sa] * b[ib],
io,
so
);
14 changes: 14 additions & 0 deletions packages/vectors/src/divsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { VecOpSVN } from "./api";
import {
ARGS_V,
ARGS_VN,
defOpS,
SARGS_V
} from "./internal/codegen";
import { MATH_N } from "./internal/templates";

export const [divSN2, divSN3, divSN4] = defOpS<VecOpSVN>(
MATH_N("/"),
`${ARGS_VN},${SARGS_V}`,
ARGS_V
);
21 changes: 21 additions & 0 deletions packages/vectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export * from "./add";
export * from "./addi";
export * from "./addm";
export * from "./addmn";
export * from "./addms";
export * from "./addmsn";
export * from "./addn";
export * from "./adds";
export * from "./addsn";
export * from "./angle-between";
export * from "./asin";
export * from "./atan";
Expand All @@ -37,6 +40,7 @@ export * from "./copy";
export * from "./cos";
export * from "./cosh";
export * from "./cross";
export * from "./crosss";
export * from "./degrees";
export * from "./direction";
export * from "./dist";
Expand All @@ -47,6 +51,7 @@ export * from "./div";
export * from "./divi";
export * from "./divn";
export * from "./divs";
export * from "./divsn";
export * from "./dot";
export * from "./dotc";
export * from "./dots";
Expand Down Expand Up @@ -81,8 +86,12 @@ export * from "./logic-or";
export * from "./lshift";
export * from "./madd";
export * from "./maddn";
export * from "./madds";
export * from "./maddsn";
export * from "./mag";
export * from "./mags";
export * from "./magsq";
export * from "./magsqs";
export * from "./major";
export * from "./map";
export * from "./max";
Expand All @@ -93,15 +102,23 @@ export * from "./mix-cubic";
export * from "./mix-quadratic";
export * from "./mix";
export * from "./mixn";
export * from "./mixs";
export * from "./mixsn";
export * from "./mod";
export * from "./modn";
export * from "./msub";
export * from "./msubn";
export * from "./msubs";
export * from "./msubsn";
export * from "./mul";
export * from "./muli";
export * from "./muln";
export * from "./muls";
export * from "./mulsn";
export * from "./neg";
export * from "./normal";
export * from "./normalize";
export * from "./normalizes";
export * from "./ortho-normal";
export * from "./perpendicular";
export * from "./polar";
Expand All @@ -121,6 +138,7 @@ export * from "./set";
export * from "./setc";
export * from "./setn";
export * from "./sets";
export * from "./setsc";
export * from "./setsn";
export * from "./setvn";
export * from "./setvv";
Expand All @@ -136,8 +154,11 @@ export * from "./sub";
export * from "./subi";
export * from "./subm";
export * from "./submn";
export * from "./subms";
export * from "./submsn";
export * from "./subn";
export * from "./subs";
export * from "./subsn";
export * from "./sum";
export * from "./swizzle";
export * from "./tan";
Expand Down
12 changes: 6 additions & 6 deletions packages/vectors/src/internal/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export const MATH = (op: string): Template => ([o, a, b]) => `${o}=${a}${op}${b}
// prettier-ignore
export const MATH_N = (op: string): Template => ([o, a]) => `${o}=${a}${op}n;`;
// prettier-ignore
export const MATH2 = (op1: string, op2: string): Template => ([o, a, b, c]) => `${o}=(${a}${op1}${b})${op2}${c};`;
// prettier-ignore
export const MATH2_N = (op1: string, op2: string): Template => ([o, a, b]) => `${o}=(${a}${op1}${b})${op2}n;`;
// prettier-ignore
export const MATH2A_N = (op1: string, op2: string): Template => ([o, a, b]) => `${o}=(${a}${op1}n)${op2}${b};`;
// prettier-ignore
export const SIGNED = (op: string): Template => ([o, a, b]) => `${o}=(${a}${op}${b})|0;`;
// prettier-ignore
export const UNSIGNED = (op: string): Template => ([o, a, b]) => `${o}=(${a}${op}${b})>>>0;`;
Expand All @@ -28,11 +34,5 @@ export const DOT_G: Template = ([a, b]) => `s+=${a}*${b};`;
export const SET: Template = ([o, a]) => `${o}=${a};`;
export const SET_N: Template = ([a]) => `${a}=n;`;

export const ADDM: Template = ([o, a, b, c]) => `${o}=(${a}+${b})*${c};`;
export const ADDM_N: Template = ([o, a, b]) => `${o}=(${a}+${b})*n;`;
export const MADD: Template = ([o, a, b, c]) => `${o}=${a}*${b}+${c};`;
export const MADD_N: Template = ([o, a, b]) => `${o}=${a}*n+${b};`;
export const MIX: Template = ([o, a, b, c]) => `${o}=${a}+(${b}-${a})*${c};`;
export const MIX_N: Template = ([o, a, b]) => `${o}=${a}+(${b}-${a})*n;`;
export const SUBM: Template = ([o, a, b, c]) => `${o}=(${a}-${b})*${c};`;
export const SUBM_N: Template = ([o, a, b]) => `${o}=(${a}-${b})*n;`;
4 changes: 2 additions & 2 deletions packages/vectors/src/madd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MultiVecOpVVV, VecOpVVV } from "./api";
import { ARGS_VVV, defOp } from "./internal/codegen";
import { MADD } from "./internal/templates";
import { MATH2 } from "./internal/templates";

/**
* Returns `out = a * b + c`.
Expand All @@ -14,6 +14,6 @@ import { MADD } from "./internal/templates";
* @param c
*/
export const [madd, madd2, madd3, madd4] = defOp<MultiVecOpVVV, VecOpVVV>(
MADD,
MATH2("*", "+"),
ARGS_VVV
);
4 changes: 2 additions & 2 deletions packages/vectors/src/maddn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MultiVecOpVNV, VecOpVNV } from "./api";
import { ARGS_VNV, ARGS_VV, defOp } from "./internal/codegen";
import { MADD_N } from "./internal/templates";
import { MATH2A_N } from "./internal/templates";

/**
* Returns `out = a * n + b`.
Expand All @@ -11,7 +11,7 @@ import { MADD_N } from "./internal/templates";
* @param b vec
*/
export const [maddN, maddN2, maddN3, maddN4] = defOp<MultiVecOpVNV, VecOpVNV>(
MADD_N,
MATH2A_N("*", "+"),
ARGS_VNV,
ARGS_VV
);
9 changes: 9 additions & 0 deletions packages/vectors/src/madds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { VecOpSVVV } from "./api";
import { ARGS_VVV, defOpS, SARGS_VVV } from "./internal/codegen";
import { MATH2 } from "./internal/templates";

export const [maddS2, maddS3, maddS4] = defOpS<VecOpSVVV>(
MATH2("*", "+"),
`${ARGS_VVV},${SARGS_VVV}`,
ARGS_VVV
);
8 changes: 8 additions & 0 deletions packages/vectors/src/maddsn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VecOpSVNV } from "./api";
import { ARGS_VNV, defOpS, SARGS_VV } from "./internal/codegen";
import { MATH2A_N } from "./internal/templates";

export const [maddSN2, maddSN3, maddSN4] = defOpS<VecOpSVNV>(
MATH2A_N("*", "+"),
`${ARGS_VNV},${SARGS_VV}`
);
11 changes: 11 additions & 0 deletions packages/vectors/src/mags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VecOpSRoV } from "./api";
import { dotS2, dotS3, dotS4 } from "./dots";

export const magS2: VecOpSRoV<number> = (a, ia, sa) =>
Math.sqrt(dotS2(a, a, ia, ia, sa, sa));

export const magS3: VecOpSRoV<number> = (a, ia, sa) =>
Math.sqrt(dotS3(a, a, ia, ia, sa, sa));

export const magS4: VecOpSRoV<number> = (a, ia, sa) =>
Math.sqrt(dotS4(a, a, ia, ia, sa, sa));
11 changes: 11 additions & 0 deletions packages/vectors/src/magsqs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VecOpSRoV } from "./api";
import { dotS2, dotS3, dotS4 } from "./dots";

export const magSqS2: VecOpSRoV<number> = (a, ia, sa) =>
dotS2(a, a, ia, ia, sa, sa);

export const magSqS3: VecOpSRoV<number> = (a, ia, sa) =>
dotS3(a, a, ia, ia, sa, sa);

export const magSqS4: VecOpSRoV<number> = (a, ia, sa) =>
dotS4(a, a, ia, ia, sa, sa);
9 changes: 9 additions & 0 deletions packages/vectors/src/mixs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { VecOpSVVV } from "./api";
import { ARGS_VVV, defOpS, SARGS_VVV } from "./internal/codegen";
import { MIX } from "./internal/templates";

export const [mixS2, mixS3, mixS4] = defOpS<VecOpSVVV>(
MIX,
`${ARGS_VVV},${SARGS_VVV}`,
ARGS_VVV
);
Loading

0 comments on commit ca91fa9

Please sign in to comment.