0.6.119 • Published 7 days ago

@thi.ng/simd v0.6.119

Weekly downloads
69
License
Apache-2.0
Repository
github
Last release
7 days ago

simd

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

WebAssembly SIMD vector operations for array/batch processing, written in AssemblyScript. These functions use the CPU's vector instructions to process 128bit words at once, which is the equivalent width of a 4D vector with 4x 32bit components. Several of the provided functions can also be used to process 2D vectors.

Available functions

See /assembly for sources:

  • abs4_f32
  • add4_f32
  • addn4_f32
  • clamp4_f32
  • clampn4_f32
  • div4_f32
  • divn4_f32
  • dot2_f32_aos (2)
  • dot4_f32_aos
  • dot4_f32_soa
  • invsqrt4_f32
  • madd4_f32
  • maddn4_f32
  • mag2_f32_aos
  • mag4_f32_aos
  • magsq2_f32_aos
  • magsq4_f32_aos
  • max4_f32
  • min4_f32
  • mix4_f32
  • mixn4_f32
  • msub4_f32
  • msubn4_f32
  • mul4_f32
  • muln4_f32
  • mul_m22v2_aos (2)
  • mul_m23v2_aos (2)
  • mul_m44v4_aos
  • neg4_f32
  • normalize2_f32_aos (2)
  • normalize4_f32_aos
  • sqrt4_f32
  • sub4_f32
  • subn4_f32
  • sum4_f32
  • swizzle4_32 (f32 and u32)

(2) 2x vec2 per iteration

Also see src/api.ts for documentation about the exposed TS/JS API...

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

The WebAssembly SIMD spec is still WIP and (at the time of writing) only partially implemented and hidden behind feature flags. Currently only fully tested (& testable for me) on Node 14.6+.

BREAKING CHANGES

0.4.0

Due to the opcode renumbering of SIMD operations proposed in April 2020, the WASM module will only work on engines released after 2020-05-21 when that change was committed to the WASM spec. For NodeJS this means only v14.6.0 or newer will be supported. This was an external change and outside our control...

Related packages

  • @thi.ng/malloc - ArrayBuffer based malloc() impl for hybrid JS/WASM use cases, based on thi.ng/tinyalloc
  • @thi.ng/soa - SOA & AOS memory mapped structured views with optional & extensible serialization
  • @thi.ng/vectors - Optimized 2d/3d/4d and arbitrary length vector operations
  • @thi.ng/vector-pools - Data structures for managing & working with strided, memory mapped vectors

Installation

yarn add @thi.ng/simd

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/simd"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const simd = await import("@thi.ng/simd");

Package sizes (gzipped, pre-treeshake): ESM: 2.44 KB

Dependencies

API

Generated API docs

import { init } from "@thi.ng/simd";

// the WASM module doesn't specify any own memory and it must be provided by user
// the returned object contains all available vector functions & memory views
// (an error will be thrown if WASM isn't available or SIMD unsupported)
const simd = init(new WebAssembly.Memory({ initial: 1 }));

// input data: 3x vec4 buffers
const a = simd.f32.subarray(0, 4);
const b = simd.f32.subarray(4, 16);
const out = simd.f32.subarray(16, 18);

a.set([1, 2, 3, 4])
b.set([10, 20, 30, 40,  40, 30, 20, 10]);

// compute dot products: dot(A[i], B[i])
// by using 0 as stride for A, all dot products are using the same vec
simd.dot4_f32_aos(
    out.byteOffset, // output addr / pointer
    a.byteOffset,   // vector A addr
    b.byteOffset,   // vector B addr
    2,              // number of vectors to process
    1,              // output stride (floats)
    0,              // A stride (floats)
    4               // B stride (floats)
);

// results for [dot(a0, b0), dot(a0, b1)]
out
// [300, 200]

// mat4 * vec4 matrix-vector multiplies
const mat = simd.f32.subarray(0, 16);
const points = simd.f32.subarray(16, 24);

// mat4 (col major)
mat.set([
    10, 0, 0, 0,
    0, 20, 0, 0,
    0, 0, 30, 0,
    100, 200, 300, 1
]);

// vec4 array
points.set([
    1, 2, 3, 1,
    4, 5, 6, 1,
]);

simd.mul_m44v4_aos(
    points.byteOffset, // output addr / pointer
    mat.byteOffset,    // mat4 addr
    points.byteOffset, // vec4 addr
    2,                 // number of vectors to process
    4,                 // output stride (float)
    4                  // vec stride (float)
);

// transformed points
points
// [110, 240, 390, 1, 140, 300, 480, 1]

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-simd,
  title = "@thi.ng/simd",
  author = "Karsten Schmidt",
  note = "https://thi.ng/simd",
  year = 2019
}

License

© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0

0.6.119

7 days ago

0.6.118

9 days ago

0.6.117

12 days ago

0.6.116

21 days ago

0.6.115

24 days ago

0.6.114

1 month ago

0.6.113

1 month ago

0.6.112

1 month ago

0.6.111

1 month ago

0.6.110

1 month ago

0.6.109

2 months ago

0.6.108

2 months ago

0.6.107

2 months ago

0.6.106

2 months ago

0.6.105

2 months ago

0.6.104

2 months ago

0.6.103

2 months ago

0.6.102

2 months ago

0.6.99

2 months ago

0.6.101

2 months ago

0.6.100

2 months ago

0.6.98

2 months ago

0.6.97

2 months ago

0.6.96

3 months ago

0.6.95

3 months ago

0.6.94

3 months ago

0.6.93

3 months ago

0.6.90

3 months ago

0.6.92

3 months ago

0.6.91

3 months ago

0.6.89

3 months ago

0.6.88

3 months ago

0.6.87

4 months ago

0.6.86

4 months ago

0.6.83

5 months ago

0.6.85

5 months ago

0.6.84

5 months ago

0.6.82

5 months ago

0.6.81

5 months ago

0.6.65

8 months ago

0.6.64

8 months ago

0.6.67

7 months ago

0.6.66

7 months ago

0.6.61

8 months ago

0.6.60

8 months ago

0.6.63

8 months ago

0.6.62

8 months ago

0.6.69

7 months ago

0.6.68

7 months ago

0.6.54

9 months ago

0.6.56

9 months ago

0.6.55

9 months ago

0.6.52

9 months ago

0.6.51

10 months ago

0.6.58

8 months ago

0.6.57

9 months ago

0.6.59

8 months ago

0.6.80

5 months ago

0.6.75

6 months ago

0.6.78

6 months ago

0.6.77

6 months ago

0.6.72

6 months ago

0.6.71

6 months ago

0.6.74

6 months ago

0.6.73

6 months ago

0.6.79

5 months ago

0.6.70

6 months ago

0.6.50

11 months ago

0.6.49

12 months ago

0.6.47

1 year ago

0.6.48

1 year ago

0.6.43

1 year ago

0.6.42

1 year ago

0.6.45

1 year ago

0.6.44

1 year ago

0.6.41

1 year ago

0.6.46

1 year ago

0.6.40

1 year ago

0.6.39

1 year ago

0.6.36

1 year ago

0.6.38

1 year ago

0.6.32

1 year ago

0.6.31

1 year ago

0.6.34

1 year ago

0.6.33

1 year ago

0.6.35

1 year ago

0.6.30

1 year ago

0.6.21

2 years ago

0.6.23

2 years ago

0.6.22

2 years ago

0.6.29

1 year ago

0.6.28

1 year ago

0.6.25

2 years ago

0.6.24

2 years ago

0.6.27

2 years ago

0.6.26

2 years ago

0.6.20

2 years ago

0.6.19

2 years ago

0.6.18

2 years ago

0.6.12

2 years ago

0.6.11

2 years ago

0.6.17

2 years ago

0.6.14

2 years ago

0.6.13

2 years ago

0.6.16

2 years ago

0.6.15

2 years ago

0.6.7

2 years ago

0.6.9

2 years ago

0.6.8

2 years ago

0.6.10

2 years ago

0.6.6

2 years ago

0.6.5

2 years ago

0.5.8

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.4

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.7

2 years ago

0.5.4

3 years ago

0.5.6

3 years ago

0.5.3

3 years ago

0.5.0

3 years ago

0.5.1

3 years ago

0.4.41

3 years ago

0.4.40

3 years ago

0.4.39

3 years ago

0.4.38

3 years ago

0.4.37

3 years ago

0.4.36

3 years ago

0.4.35

3 years ago

0.4.34

3 years ago

0.4.33

3 years ago

0.4.32

3 years ago

0.4.31

3 years ago

0.4.30

3 years ago

0.4.29

3 years ago

0.4.28

3 years ago

0.4.27

3 years ago

0.4.26

3 years ago

0.4.25

3 years ago

0.4.24

3 years ago

0.4.23

3 years ago

0.4.22

3 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

1.0.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.3.1

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.12

4 years ago

0.1.11

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago