google / google/xls

Dynamic versions of stdlib functions that expect constexpr bitwidths as parametrics

Open
#2,409 0 comments 0 reactions 0 assignees View on GitHub
dslx enhancement
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

### What's hard to do? (limit 100 words)

The standard library has many functions for operating on bits, but a number of these require constexpr parametrics that aren't compatible with dynamic bitwidths.

In certain cases, data may be encoded with flexible bitwidths that are only known at runtime, which means these functions become unusable. It would be nice to provide dynamic versions of these functions so users don't need to write the non-parametric version of these.

For example:

* `unsigned_min_value`/ `signed_min_value`
* `unsigned_max_value`/ `signed_max_value`
* `mask_bits`
* `extract_bits` / `vslice`

### Current best alternative workaround (limit 100 words)

You can implement the bit math yourself (similar to the stdlib version without parametrics).

### Your view of the "best case XLS enhancement" (limit 100 words)

The stdlib could provide the dynamic versions of these functions. The main catch is that you still need some constexpr bitwidth to statically specific the result type. In practice, this is not really a problem as this is true in the hardware as well. The result can just be padded to a fixed size to cover the expected dynamic bitwidths.

So for example:

```
pub fn signed_min_value(n: u32) -> sN[MAX_N]
```

would provide the most negative value represented by"`sN[n]`" if `n` were a compile time constant .

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.