Skip to content

Commit

Permalink
feat(vectors): add generic strided dot product
Browse files Browse the repository at this point in the history
- add `dotS()` for arbitrary sized strided vectors
  • Loading branch information
postspectacular committed Aug 29, 2021
1 parent 2bbb54e commit 9c34793
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/vectors/src/dots.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import type { VecOpSRoVV } from "./api";
import type { ReadonlyVec, VecOpSRoVV } from "./api";
import { compile, SARGS_V } from "./internal/codegen";
import { DOT } from "./internal/templates";

export const dotS = (
a: ReadonlyVec,
b: ReadonlyVec,
n: number,
ia = 0,
ib = 0,
sa = 1,
sb = 1
) => {
let sum = 0;
for (; --n >= 0; ia += sa, ib += sb) sum += a[ia] * b[ib];
return sum;
};

const $ = (dim: number): VecOpSRoVV<number> =>
compile(dim, DOT, `o,a,${SARGS_V}`, "o,a", "", "+", "return ", ";", true);

Expand Down

0 comments on commit 9c34793

Please sign in to comment.