# Velurex encoding

**Five ternary weights in one byte, *decoded without division:* 1.60 bits per weight.**

A ternary weight carries log2(3), about 1.585 bits. Common 2-bit formats store four weights in a byte. Five weights fit
into one byte, because 3 to the power of 5 is 243, which is not more than 256.

## Definition

Each weight `t` in {-1, 0, +1} becomes a digit `d = t + 1` in {0, 1, 2}. Five digits form the byte `B`:

```text
B = d0 + 3*d1 + 9*d2 + 28*d3 + 84*d4
```

The first three digits use the powers of three 1, 3 and 9. The last two use 28 and 84 = 28 * 3 instead of 27 and 81.

## Why 28 and not 27

The lower three digits `L = d0 + 3*d1 + 9*d2` are at most 26. Therefore the two groups of digits separate as

```text
U = floor(B / 28)      upper two digits: U = d3 + 3*d4
L = B - 28*U           lower three digits
```

Both groups are used directly as indices into look-up tables. Vector instruction sets have no integer division, so the
quotient `U` is obtained with bit tricks.

## Dividing by 28 without division

Two shifts, one average and one more shift give `floor(B / 28)` exactly for every byte from 0 to 250.

| Step | Operation | Meaning |
|---|---|---|
| 1 | `v11 = B >> 2` | B / 4 |
| 2 | `v15 = v11 >> 3` | B / 32 |
| **3** | **`v17 = (v11 + v15 + 1) >> 1`** | **one SIMD instruction: rounded average; x86-64 `vpavgb`, Arm NEON `URHADD`** |
| 4 | `U = v17 >> 2` | floor(B / 28): digits d3, d4 |
| 5 | `L = B - (((U << 3) - U) << 2)` `// ((U << 3) - U) << 2 = 28*U` | remainder: digits d0, d1, d2 |

The scheme shows the principle. The implementation differs from architecture to architecture; see the bit tricks of
each instruction set below.

## The key step is one SIMD instruction

> **`(a + b + 1) >> 1` is exactly the rounded average of two unsigned bytes.** Processors compute it for a whole
> vector register of bytes with a single instruction, without overflow of the intermediate sum.

| Instruction set | Instruction | C intrinsic |
|---|---|---|
| x86-64 AVX2 and AVX-512 | `vpavgb` | `_mm256_avg_epu8`, `_mm512_avg_epu8` |
| Arm NEON | `URHADD` | `vrhadd_u8`, `vrhaddq_u8` |

Why it gives the quotient: `v15` is about `v11 / 8`, so the average `v17` is about `9/16 * v11` and `v17 >> 2` about
`9/64 * v11`. The factor 9/64 = 0.1406 is slightly below 1/7 = 0.1429, and the `+1` of the rounded average makes up for
the difference. Because 28 = 4 * 7, dividing `v11 = B >> 2` by 7 is dividing `B` by 28.

## Bit tricks of each instruction set

The instruction sets offer different building blocks, so each kernel takes its own shortest way.

### AVX2

- There is no shift of single bytes: 16-bit lanes are shifted and a mask removes the bits of the neighbouring byte.
- The quotient comes from the sequence above, with `vpavgb`.
- A hash replaces the remainder: `(2*B - (B >> 2) - U) & 15` no longer depends on `U` and indexes a 16-entry table
  for the digits `d0` and `d1`.
- The third digit needs no table. `u = ((B - 1) + v17 + 1) >> 1`, in 8-bit arithmetic a second use of `vpavgb`, falls
  with `u & 15` into one of three intervals (0 to 4, 5 to 9, 10 to 15). Two compares turn the interval into the weight
  -1, 0 or +1, and `vpsignb` applies it to the activation.

### Arm NEON

- Bytes can be shifted directly, so `B >> 5` gives `v15` independently of `v11`.
- The quotient comes from `URHADD`.
- One multiply-subtract (`MLS`) gives the exact remainder `L = B - 28*U`, and one two-register table lookup covers all
  27 values of `L`. Five instructions give both indices for 16 bytes.

### AVX-512 with VBMI

- Here the whole division is one table lookup: `vpermb` has 64 entries, and `B >> 2` is a 6-bit index.
- The shift needs no mask, because `vpermb` ignores the two foreign upper bits.
- A second lookup and one subtraction give the remainder, shifted by the table offset 9. Four instructions give both
  indices for 64 bytes.

## Example

```text
weights   +1   0  -1  +1  -1
digits     2   1   0   2   0

B = 2 + 3*1 + 9*0 + 28*2 + 84*0 = 61

v11 = 61 >> 2             = 15
v15 = 15 >> 3             = 1
v17 = (15 + 1 + 1) >> 1   = 8      rounded average
U   = 8 >> 2              = 2      = 2 + 3*0
L   = 61 - (((2 << 3) - 2) << 2)   // 28*2 = 56
                          = 5      = 2 + 3*1 + 9*0
```

## Properties

| Property | Value |
|---|---|
| Weights per byte | 5 |
| Bits per weight | 1.60 |
| Valid byte values | 243, between 0 and 250 |
| Never used | values with `B mod 28 = 27`, and 251 to 255 |

## Specification

The complete definition, the proof of the decoder and the comparison with other ternary formats are in the
[specification](https://github.com/velurex-labs/quintrit/blob/main/docs/velurex-encoding.md). The instruction sequences
are described in [SIMD decode](https://github.com/velurex-labs/quintrit/blob/main/docs/simd-decode.md).

## Citation

Author: Lukáš Velecký.

```bibtex
@misc{velecky2026velurex,
  author       = {Velecký, Lukáš},
  title        = {Velurex Encoding: A 1.60 Bits-Per-Weight Ternary Representation for Division-Free SIMD Inference},
  year         = {2026},
  howpublished = {\url{https://github.com/velurex-labs/quintrit}},
  note         = {Part of the Quintrit project}
}
```
