# Module /simd

Portable 128-bit SIMD vector types (F32x4, F64x2, I32x4, I64x2, U8x16) with lane-parallel arithmetic.

```tangu
(use /simd)
```

## Functions & symbols

### F32x4

`(F32x4 x y z w) -> Simd128`

Constructs a 4x32-bit floating-point 128-bit SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `lanes` | `Float` | no | Four Float scalars, a single broadcast scalar, a Float-Array of length 4, or empty |

```tangu
(use /simd)
(let v (F32x4 1.0 2.0 3.0 4.0))
```

### F64x2

`(F64x2 x y) -> Simd128`

Constructs a 2x64-bit floating-point 128-bit SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `lanes` | `Float` | no | Two Float scalars, a single broadcast scalar, a Float-Array of length 2, or empty |

```tangu
(use /simd)
(let v (F64x2 1.5 2.5))
```

### I32x4

`(I32x4 x y z w) -> Simd128`

Constructs a 4x32-bit signed integer 128-bit SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `lanes` | `Int` | no | Four Int scalars, a broadcast scalar, an Int-Array of length 4, or empty |

### I64x2

`(I64x2 x y) -> Simd128`

Constructs a 2x64-bit signed integer 128-bit SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `lanes` | `Int` | no | Two Int scalars, a broadcast scalar, an Int-Array of length 2, or empty |

### U8x16

`(U8x16 byte...) -> Simd128`

Constructs a 16x8-bit unsigned byte 128-bit SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `lanes` | `U8` | no | Sixteen bytes, a single broadcast byte/int, or a 16-byte Slab/array |

### abs

`(simd/abs vec) -> Simd-Vec`

Computes lane-wise absolute values of `vec`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `F32x4 | F64x2 | I32x4` | no | SIMD vector |

### add-float-arrays

`(add-float-arrays a b) -> Array Float`

Adds two float arrays elementwise using SIMD vector instructions.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Array Float` | no | First float array |
| `b` | `Array Float` | no | Second float array |

### all-true?

`(simd/all-true? mask) -> Bool`

Returns `true` if every lane in `mask` is active.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `mask` | `Simd-Mask` | no | SIMD mask vector |

### any-true?

`(simd/any-true? mask) -> Bool`

Returns `true` if any lane in `mask` is active.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `mask` | `Simd-Mask` | no | SIMD mask vector |

### clamp

`(simd/clamp v lo hi) -> Simd-Vec`

Clamps every lane of `v` into `[lo, hi]`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `v` | `Simd-Vec` | no | Input vector |
| `lo` | `Simd-Vec | Float | Int` | no | Lower bound |
| `hi` | `Simd-Vec | Float | Int` | no | Upper bound |

### dot

`(dot a b) -> Float`

Computes the dot product of two SIMD vectors.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd128` | no | First SIMD vector |
| `b` | `Simd128` | no | Second SIMD vector |

### dot-float-arrays

`(dot-float-arrays a b) -> Float`

Computes the dot product of two float arrays using SIMD vector multiplication and accumulation.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Array Float` | no | First float array |
| `b` | `Array Float` | no | Second float array |

### dot-int-arrays

`(dot-int-arrays a b) -> Int`

Computes the integer dot product of two integer arrays.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Array Int` | no | First int array |
| `b` | `Array Int` | no | Second int array |

### fast-inv

`(simd/fast-inv vec) -> F32x4`

Computes fast reciprocal `1.0 / x` across all lanes.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `F32x4` | no | Float SIMD vector |

### fast-inv-sqrt

`(simd/fast-inv-sqrt vec) -> F32x4`

Computes fast inverse square root `1.0 / sqrt(x)` across lanes.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `F32x4` | no | Float SIMD vector |

### fma

`(fma a b c) -> Simd128`

Computes fused multiply-add `(a * b) + c` across floating-point SIMD lanes.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd128` | no | Multiplicand vector |
| `b` | `Simd128` | no | Multiplier vector |
| `c` | `Simd128` | no | Addend vector |

### lane-at

`(lane-at vec idx) -> Number`

Extracts the scalar value at lane `idx` from a SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd128` | no | SIMD vector |
| `idx` | `Int` | no | Zero-based lane index |

### max

`(simd/max a b) -> Simd-Vec`

Computes lane-wise maximum across two SIMD vectors.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | First vector |
| `b` | `Simd-Vec` | no | Second vector |

### min

`(simd/min a b) -> Simd-Vec`

Computes lane-wise minimum across two SIMD vectors.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | First vector |
| `b` | `Simd-Vec` | no | Second vector |

### product

`(product vec) -> Number`

Multiplies all lanes of a SIMD vector together.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd128` | no | SIMD vector to multiply across lanes |

### reduce-max

`(simd/reduce-max vec) -> Float | Int`

Returns the largest scalar lane value in `vec`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd-Vec` | no | Input SIMD vector |

### reduce-min

`(simd/reduce-min vec) -> Float | Int`

Returns the smallest scalar lane value in `vec`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd-Vec` | no | Input SIMD vector |

### select

`(simd/select mask on-true on-false) -> Simd-Vec`

Branchless lane-wise blend selecting `on-true` where mask bits are set, else `on-false`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `mask` | `Simd-Mask` | no | Comparison mask vector |
| `on_true` | `Simd-Vec` | no | True branch vector |
| `on_false` | `Simd-Vec` | no | False branch vector |

### simd-eq

`(simd/simd-eq a b) -> Simd-Mask`

Lane-wise equality comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### simd-ge

`(simd/simd-ge a b) -> Simd-Mask`

Lane-wise `>=` comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### simd-gt

`(simd/simd-gt a b) -> Simd-Mask`

Lane-wise `>` comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### simd-le

`(simd/simd-le a b) -> Simd-Mask`

Lane-wise `<=` comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### simd-lt

`(simd/simd-lt a b) -> Simd-Mask`

Lane-wise `<` comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### simd-ne

`(simd/simd-ne a b) -> Simd-Mask`

Lane-wise inequality comparison returning a SIMD mask vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `a` | `Simd-Vec` | no | Left vector |
| `b` | `Simd-Vec` | no | Right vector |

### splat

`(splat Type val) -> Simd128`

Broadcasts a scalar number into all lanes of the specified 128-bit SIMD vector type.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `type` | `Type` | no | SIMD vector type (`F32x4`, `F64x2`, `I32x4`, `I64x2`, or `U8x16`) |
| `val` | `Number` | no | Scalar value to broadcast across all lanes |

```tangu
(use /simd)
(let v (splat F32x4 1.0))
```

### sqrt

`(simd/sqrt vec) -> F32x4 | F64x2`

Computes hardware lane-wise square root.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `F32x4 | F64x2` | no | Float SIMD vector |

### sum

`(sum vec) -> Number`

Performs horizontal sum across all lanes of a SIMD vector.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd128` | no | SIMD vector to horizontally sum |

### sum-float-array

`(sum-float-array arr) -> Float`

Sums all elements of a float array using SIMD vector accumulation.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `arr` | `Array Float` | no | Float array or Float-Array buffer |

```tangu
(use /simd)
(sum-float-array [1.0 2.0 3.0 4.0])
```

### sum-int-array

`(sum-int-array arr) -> Int`

Sums all elements of an integer array using 128-bit SIMD vector addition.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `arr` | `Array Int` | no | Int array or Int-Array buffer |

### swizzle

`(simd/swizzle vec indices) -> Simd-Vec`

Permutes SIMD vector lanes according to `indices`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd-Vec` | no | Source vector |
| `indices` | `Array Int` | no | Lane permutation indices |

### to-array

`(simd/to-array vec) -> Array`

Unpacks all SIMD lanes into a standard Tangu `Array`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd-Vec` | no | SIMD vector |

### to-bitmask

`(simd/to-bitmask mask) -> Int`

Packs active mask lanes into a compact integer bitmask.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `mask` | `Simd-Mask` | no | SIMD mask vector |

### to-float-array

`(simd/to-float-array vec) -> Float-Array`

Extracts float SIMD lanes into an unboxed `Float-Array`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `F32x4 | F64x2` | no | Float SIMD vector |

### with-lane

`(with-lane vec idx val) -> Simd128`

Returns a copy of `vec` with lane `idx` replaced by `val`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `vec` | `Simd128` | no | Source SIMD vector |
| `idx` | `Int` | no | Zero-based lane index to replace |
| `val` | `Number` | no | Replacement scalar lane value |

### zero

`(zero Type) -> Simd128`

Creates a zero-initialized 128-bit SIMD vector of the given type.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `type` | `Type` | no | SIMD vector type (`F32x4`, `F64x2`, `I32x4`, `I64x2`, or `U8x16`) |
