https://github.com/infinitensor/digit-layout
Define memory layout of digital types as `[sign|exponent|mantissa; N]`
https://github.com/infinitensor/digit-layout
Last synced: 9 months ago
JSON representation
Define memory layout of digital types as `[sign|exponent|mantissa; N]`
- Host: GitHub
- URL: https://github.com/infinitensor/digit-layout
- Owner: InfiniTensor
- License: mit
- Created: 2024-06-21T02:23:47.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-05-15T02:35:08.000Z (about 1 year ago)
- Last Synced: 2025-05-15T03:38:16.209Z (about 1 year ago)
- Language: Rust
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# digit-layout
[](https://github.com/InfiniTensor/digit-layout/actions)
[](https://crates.io/crates/digit-layout)
[](https://docs.rs/digit-layout)
[](https://mit-license.org/)
[](https://codecov.io/gh/InfiniTensor/digit-layout)


[](https://github.com/InfiniTensor/digit-layout/issues)
[](https://github.com/InfiniTensor/digit-layout/pulls)


这个库提供了一个统一的数据类型定义,可以高效地编码类型信息,避免重复定义数据类型。
## 特性
- 支持无符号整数类型;
- 支持浮点数类型;
- 支持自定义命名类型;
- 紧凑的内存布局;
- 无标准库依赖(no_std);
- 可选的 half 浮点数支持;
## 使用示例
### 基本用法
```rust
use digit_layout::{DigitLayout, LayoutContent};
// 创建无符号整数类型布局
let u8_layout = DigitLayout::unsigned(8, 1);
assert_eq!(u8_layout.to_string(), "u8");
// 创建浮点数类型布局
let f32_layout = DigitLayout::real(8, 23, 1);
assert_eq!(f32_layout.to_string(), "f32_e8m23");
// 创建自定义类型布局
let custom_layout = DigitLayout::named("custom", 1, 4);
assert_eq!(custom_layout.to_string(), "custom");
```
### 数组类型
```rust
use digit_layout::DigitLayout;
// 创建无符号整数数组布局
let u8_array = DigitLayout::unsigned(8, 4);
assert_eq!(u8_array.to_string(), "[u8; 4]");
// 创建浮点数数组布局
let f32_array = DigitLayout::real(8, 23, 4);
assert_eq!(f32_array.to_string(), "[f32_e8m23; 4]");
```
### 解码布局
```rust
use digit_layout::{DigitLayout, LayoutContent};
// 解码无符号整数布局
let u8_layout = DigitLayout::unsigned(8, 1);
match u8_layout.decode() {
LayoutContent::Unsigned { width } => {
assert_eq!(width, 8);
}
_ => panic!("Expected unsigned layout"),
}
// 解码浮点数布局
let f32_layout = DigitLayout::real(8, 23, 1);
match f32_layout.decode() {
LayoutContent::Real { exponent, mantissa } => {
assert_eq!(exponent, 8);
assert_eq!(mantissa, 23);
}
_ => panic!("Expected real layout"),
}
```
## 性能测试
项目包含一个性能测试示例,可以测量各种操作的执行时间。运行性能测试:
```bash
cargo run --example benchmark
```
性能测试会测量以下操作的执行时间:
1. 创建布局
- 创建无符号整数布局;
- 创建浮点数布局;
- 创建自定义布局;
2. 解码布局
- 解码无符号整数布局;
- 解码浮点数布局;
- 解码自定义布局;
测试结果会显示每个操作的平均执行时间。
## 文档
完整的 API 文档可以在 [docs.rs](https://docs.rs/digit-layout) 上找到。
## 贡献
欢迎提交 Issue 和 Pull Request!
## 许可证
本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件。