> ## Documentation Index
> Fetch the complete documentation index at: https://klyne-research.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Swizzle

> Bank-conflict-free shared memory access via XOR address remapping.

Enigma provides CuTe-style swizzle for threadgroup memory bank conflict avoidance.

## `enigma.Swizzle(bits, base, shift)`

XOR-based address remapping. Apple Silicon threadgroup memory has 32 banks at 4-byte granularity.

| Parameter | Type | Description                                                   |
| --------- | ---- | ------------------------------------------------------------- |
| `bits`    | int  | Number of XOR bits (B). Controls pattern period. Typical: 1-3 |
| `base`    | int  | Bit position of lowest XOR target bit (M)                     |
| `shift`   | int  | Bit position of lowest source bit (S)                         |

Transformation: extracts `bits` bits starting at position `shift`, XORs them into position `base`.

```python theme={null}
sw = enigma.Swizzle(bits=3, base=3, shift=6)
sw(0)    # row 0 -> offset 0
sw(128)  # row 1 -> XOR remapped to avoid bank collision
```

## `enigma.swizzle(layout, bits, base, shift)`

Compose a `Layout` with a `Swizzle`, returning a `SwizzledLayout`.

```python theme={null}
tile = enigma.Layout((16, 32), (32, 1))   # 16x32 row-major
swizzled = enigma.swizzle(tile, bits=3, base=2, shift=5)
offset = swizzled((row, col))  # bank-conflict-free offset
```

## `enigma.SwizzledLayout`

Behaves like a `Layout` but applies XOR remapping after coordinate-to-offset computation. Has the same `shape`, `stride`, `size()`, `rank()` properties as `Layout`.
