Skip to content

Commit

Permalink
feat(vectors): add new bvec ops & types, update readme
Browse files Browse the repository at this point in the history
- add `every` / `some` unary ops
- add `logicAndN` / `logicOrN`
  • Loading branch information
postspectacular committed Jul 31, 2019
1 parent 1c9906f commit 931ee43
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 8 deletions.
12 changes: 10 additions & 2 deletions packages/vectors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ fixed size versions use optimized, loop-free implementations.
| | | => `[9,-18]` |
| U | arbitrary len, unsigned int vec | `addU([], [1,2], [10,20])` |
| | | => `[11,22]` |
| I2 / I3 / I4 | fixed size signed int vec | `bitnotI3([], [10,-20,30], [7,7,7])` |
| I2 / I3 / I4 | fixed size signed int vec | `bitNotI3([], [10,-20,30])` |
| | | => `[-11,19,-31]` |
| U2 / U3 / U4 | fixed size signed int vec | `bitnotU3([], [10,20,30], [7,7,7])` |
| U2 / U3 / U4 | fixed size signed int vec | `bitNotU3([], [10,20,30])` |
| | | => `[4294967285,4294967275,4294967265]` |
| NI / NU | arbitrary len, signed int vec & scalar | `addNI([], [1,-2,3], 10)` |
| | | => `[11,8,13]` |
Expand Down Expand Up @@ -509,12 +509,20 @@ forced accordingly.
- `lshiftU` / `lshiftU4` /`lshiftU4` / `lshiftU4`
- `rshiftI` / `rshiftI2` /`rshiftI3` / `rshiftI4`
- `rshiftU` / `rshiftU2` /`rshiftU3` / `rshiftU4`
- `lshiftNI` / `lshiftNI4` /`lshiftNI4` / `lshiftNI4`
- `lshiftNU` / `lshiftNU4` /`lshiftNU4` / `lshiftNU4`
- `rshiftNI` / `rshiftNI2` /`rshiftNI3` / `rshiftNI4`
- `rshiftNU` / `rshiftNU2` /`rshiftNU3` / `rshiftNU4`

### Boolean vector logic

- `logicAnd` / `logicAnd2` / `logicAnd3` / `logicAnd4`
- `logicAndN` / `logicAndN2` / `logicAndN3` / `logicAndN4` (3rd arg is boolean)
- `logicOr` / `logicOr2` / `logicOr3` / `logicOr4`
- `logicOrN` / `logicOrN2` / `logicOrN3` / `logicOrN4` (3rd arg is boolean)
- `logicNot` / `logicNot2` / `logicNot3` / `logicNot4`
- `every` / `every2` / `every3` / `every4`
- `some` / `some2` / `some3` / `some4`

### Componentwise comparisons

Expand Down
7 changes: 7 additions & 0 deletions packages/vectors/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,20 @@ export interface MultiVecOpRoVVO<T, O>
extends VecOpRoVVO<T, O>,
MultiVecOp<VecOpRoVVO<T, O>> {}

export type BVecOpRoV<T> = Fn<ReadonlyBVec, T>;
export type BVecOpV = Fn2<BVec | null, ReadonlyBVec, BVec>;
export type BVecOpVV = Fn3<BVec | null, ReadonlyBVec, ReadonlyBVec, BVec>;
export type BVecOpVN = Fn3<BVec | null, ReadonlyBVec, boolean, BVec>;

export type CompareOp = Fn3<BVec | null, ReadonlyVec, ReadonlyVec, BVec>;

export interface MultiBVecOpV extends BVecOpV, MultiVecOp<BVecOpV> {}
export interface MultiBVecOpVV extends BVecOpVV, MultiVecOp<BVecOpVV> {}
export interface MultiBVecOpVN extends BVecOpVN, MultiVecOp<BVecOpVN> {}
export interface MultiBVecOpRoV<T>
extends BVecOpRoV<T>,
MultiVecOp<BVecOpRoV<T>> {}

export interface MultiCompareOp extends CompareOp, MultiVecOp<CompareOp> {}

const mi = -Infinity;
Expand Down
20 changes: 20 additions & 0 deletions packages/vectors/src/every.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MultiBVecOpRoV } from "./api";
import { vop } from "./internal/vop";

/**
* Returns returns true if all vector components in `v` are truthy.
*
* @param v
*/
export const every: MultiBVecOpRoV<boolean> = vop();

every.default((v) => {
for (let i = v.length; --i >= 0; ) {
if (!v[i]) return false;
}
return true;
});

export const every2 = every.add(2, (a) => a[0] && a[1]);
export const every3 = every.add(3, (a) => a[0] && a[1] && a[2]);
export const every4 = every.add(4, (a) => a[0] && a[1] && a[2] && a[3]);
2 changes: 2 additions & 0 deletions packages/vectors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export * from "./dotc";
export * from "./dots";
export * from "./empty";
export * from "./eqdelta";
export * from "./every";
export * from "./exp";
export * from "./exp_2";
export * from "./face-forward";
Expand Down Expand Up @@ -124,6 +125,7 @@ export * from "./sign";
export * from "./signed-area";
export * from "./sin";
export * from "./sinh";
export * from "./some";
export * from "./sqrt";
export * from "./step";
export * from "./smoothstep";
Expand Down
16 changes: 13 additions & 3 deletions packages/vectors/src/logic-and.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { BVecOpVV, MultiBVecOpVV } from "./api";
import { defOp } from "./internal/codegen";
import { MATH } from "./internal/templates";
import {
BVecOpVN,
BVecOpVV,
MultiBVecOpVN,
MultiBVecOpVV
} from "./api";
import { ARGS_VN, defOp } from "./internal/codegen";
import { MATH, MATH_N } from "./internal/templates";

export const [logicAnd, logicAnd2, logicAnd3, logicAnd4] = defOp<
MultiBVecOpVV,
BVecOpVV
>(MATH("&&"));

export const [logicAndN, logicAndN2, logicAndN3, logicAndN4] = defOp<
MultiBVecOpVN,
BVecOpVN
>(MATH_N("&&"), ARGS_VN);
16 changes: 13 additions & 3 deletions packages/vectors/src/logic-or.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { BVecOpVV, MultiBVecOpVV } from "./api";
import { defOp } from "./internal/codegen";
import { MATH } from "./internal/templates";
import {
BVecOpVN,
BVecOpVV,
MultiBVecOpVN,
MultiBVecOpVV
} from "./api";
import { ARGS_VN, defOp } from "./internal/codegen";
import { MATH, MATH_N } from "./internal/templates";

export const [logicOr, logicOr2, logicOr3, logicOr4] = defOp<
MultiBVecOpVV,
BVecOpVV
>(MATH("||"));

export const [logicOrN, logicOrN2, logicOrN3, logicOrN4] = defOp<
MultiBVecOpVN,
BVecOpVN
>(MATH_N("||"), ARGS_VN);
21 changes: 21 additions & 0 deletions packages/vectors/src/some.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MultiBVecOpRoV } from "./api";
import { vop } from "./internal/vop";

/**
* Returns returns true if at least one vector component in `v` is
* truthy.
*
* @param v
*/
export const some: MultiBVecOpRoV<boolean> = vop();

some.default((v) => {
for (let i = v.length; --i >= 0; ) {
if (v[i]) return true;
}
return false;
});

export const some2 = some.add(2, (a) => a[0] || a[1]);
export const some3 = some.add(3, (a) => a[0] || a[1] || a[2]);
export const some4 = some.add(4, (a) => a[0] || a[1] || a[2] || a[3]);

0 comments on commit 931ee43

Please sign in to comment.