Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
Signed-off-by: YdrMaster <[email protected]>
  • Loading branch information
YdrMaster committed Jun 21, 2024
0 parents commit 18bbe60
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# rust-clippy is a tool that runs a bunch of lints to catch common
# mistakes in your Rust code and help improve your Rust code.
# More details at https://github.com/rust-lang/rust-clippy
# and https://rust-lang.github.io/rust-clippy/

name: CI

on:
push:
paths-ignore:
- '**.md'
- 'LICENSE'
pull_request:
paths:
- '**.md'
- 'LICENSE'

jobs:
rust-clippy-analyze:
name: Run rust-clippy analyzing
runs-on: ubuntu-latest
permissions:
security-events: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Check format
run: cargo fmt --check

- name: Run test
run: cargo test

- name: Install required cargo
run: cargo install clippy-sarif sarif-fmt

- name: Run rust-clippy
run:
cargo clippy
--all-features
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
continue-on-error: true

- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: rust-clippy-results.sarif
wait-for-processing: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
19 changes: 19 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "digit-layout"
description = "Define memory layout of digital types as `[sign|exponent|mantissa; N]`"
version = "0.0.0"
edition = "2021"
authors = ["YdrMaster <[email protected]>"]
repository = "https://github.com/YdrMaster/digit-layout.git"
documentation = "https://docs.rs/digit-layout"
license = "MIT"
readme = "README.md"
keywords = ["digit", "layout", "data-type"]
categories = ["no-std::no-alloc"]

[dependencies]
half_ = { version = "2.4", package = "half", optional = true }

[features]
default = ["half"]
half = ["half_"]
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright © 2024 YdrMaster

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# digit-layout

[![CI](https://github.com/YdrMaster/digit-layout/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/YdrMaster/digit-layout/actions)
[![Latest version](https://img.shields.io/crates/v/digit-layout.svg)](https://crates.io/crates/digit-layout)
[![Documentation](https://docs.rs/digit-layout/badge.svg)](https://docs.rs/digit-layout)
[![license](https://img.shields.io/github/license/YdrMaster/digit-layout)](https://mit-license.org/)
![GitHub repo size](https://img.shields.io/github/repo-size/YdrMaster/digit-layout)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/YdrMaster/digit-layout)

[![GitHub Issues](https://img.shields.io/github/issues/YdrMaster/digit-layout)](https://github.com/YdrMaster/digit-layout/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/YdrMaster/digit-layout)](https://github.com/YdrMaster/digit-layout/pulls)
![GitHub contributors](https://img.shields.io/github/contributors/YdrMaster/digit-layout)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/YdrMaster/digit-layout)

This crate provides types, traits and macros to define the memory layout of digital types as `[sign|exponent|mantissa; N]`.
119 changes: 119 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![deny(warnings, missing_docs)]

#[macro_use]
mod macros;
pub mod types;

use core::{
alloc::Layout,
mem::{align_of, transmute},
};

/// A layout of a digit data type in memory.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct DigitLayout {
signed_nbyte: u8,
packed: u8,
exponent: u8,
mantissa: u8,
}

/// A trait for types that can be represented as a digit data type.
pub trait AsDigit {
/// The layout of the digit data type.
const LAYOUT: DigitLayout;
}

/// Allow a const [`DigitLayout`] value associated with a type.
pub trait TypeOf_<const N: u32> {
/// The type associated with the layout.
type Type;
}

/// A type template for the [`TypeOf_`] trait to implement.
pub struct TypeOf<const N: u32>;

const _8: usize = u8::BITS as usize;
const _7: usize = _8 - 1;
const MAX_ALIGN: usize = align_of::<usize>();

impl DigitLayout {
/// Creates a new [`DigitLayout`] value.
#[inline]
pub const fn new(packed: usize, signed: bool, exponent: usize, mantissa: usize) -> Self {
assert!(packed <= u8::MAX as usize);
assert!(exponent <= u8::MAX as usize);
assert!(mantissa <= u8::MAX as usize);
let signed = if signed { 1 } else { 0 };

let total_bits = packed * (signed + exponent + mantissa);
let nbyte = ((total_bits + _7) / _8).next_power_of_two();
assert!(nbyte < (1 << _7));

Self {
packed: packed as _,
signed_nbyte: ((signed << _7) | nbyte) as _,
exponent: exponent as _,
mantissa: mantissa as _,
}
}

/// Converts the layout to a `u32` code.
#[inline]
pub const fn to_u32(self) -> u32 {
unsafe { transmute(self) }
}

/// Gets the packed count of the digit data type.
#[inline]
pub const fn packed(self) -> usize {
self.packed as _
}

/// Gets the signedness of the digit data type.
#[inline]
pub const fn signed(self) -> bool {
self.signed_nbyte >> _7 == 1
}

/// Gets the exponent bits of the digit data type.
#[inline]
pub const fn exponent(self) -> usize {
self.exponent as _
}

/// Gets the mantissa bits of the digit data type.
#[inline]
pub const fn mantissa(self) -> usize {
self.mantissa as _
}

/// Gets the padding bits of the digit data type.
#[inline]
pub const fn padding(self) -> usize {
self.nbits() - self.packed() * (self.signed() as usize + self.exponent() + self.mantissa())
}

/// Gets the total bits of the digit data type.
#[inline]
pub const fn nbits(self) -> usize {
self.nbytes() * _8
}

/// Gets the number of bytes of the digit data type.
#[inline]
pub const fn nbytes(self) -> usize {
(self.signed_nbyte & ((1 << _7) - 1)) as _
}

/// Gets the layout of the digit data type.
#[inline]
pub const fn layout(self) -> Layout {
let size = self.nbytes();
let align = if size < MAX_ALIGN { size } else { MAX_ALIGN };
unsafe { Layout::from_size_align_unchecked(size, align) }
}
}
48 changes: 48 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// 定义一个 [`DigitLayout`](crate::DigitLayout) 实例。
#[macro_export]
macro_rules! layout {
($name:ident i($bits:expr)x($packed:expr)) => {
#[allow(non_upper_case_globals)]
pub const $name: $crate::DigitLayout =
$crate::DigitLayout::new($packed, true, 0, $bits - 1);
};
($name:ident u($bits:expr)x($packed:expr)) => {
#[allow(non_upper_case_globals)]
pub const $name: $crate::DigitLayout =
$crate::DigitLayout::new($packed, false, 0, $bits);
};
($name:ident e($exp:expr)m($mant:expr)x($packed:expr)) => {
#[allow(non_upper_case_globals)]
pub const $name: $crate::DigitLayout =
$crate::DigitLayout::new($packed, true, $exp, $mant);
};

($name:ident i($bits:expr)) => {
layout!($name i($bits)x(1));
};
($name:ident u($bits:expr)) => {
layout!($name u($bits)x(1));
};
($name:ident e($exp:expr)m($mant:expr)) => {
layout!($name e($exp)m($mant)x(1));
};
}

/// 为类型实现与 [`DigitLayout`](crate::DigitLayout) 相关的 trait。
#[macro_export]
macro_rules! impl_digit {
($ty:ty : $digit:expr) => {
impl $crate::AsDigit for $ty {
const LAYOUT: $crate::DigitLayout = $digit;
}
};

($ty:ty : $digit:expr, $const:ident) => {
impl_digit!($ty : $digit);

const $const: u32 = $digit.to_u32();
impl $crate::TypeOf_<$const> for $crate::TypeOf<$const> {
type Type = $ty;
}
};
}
45 changes: 45 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! 为一些基本类型和常用类型提供预定义布局。
#![allow(missing_docs)]

layout!(BOOL u( 1) );
layout!(I8 i( 8) );
layout!(I16 i(16) );
layout!(I32 i(32) );
layout!(I64 i(64) );
layout!(U8 u( 8) );
layout!(U16 u(16) );
layout!(U32 u(32) );
layout!(U64 u(64) );
layout!(F16 e(10)m( 5) );
layout!(BF16 e( 7)m( 8) );
layout!(F32 e(23)m( 8) );
layout!(F64 e(52)m(11) );

layout!(F16x2 e(10)m( 5)x(2) );
layout!(BF16x2 e( 7)m( 8)x(2) );

impl_digit!(bool: BOOL, CBOOL);
impl_digit!(i8 : I8 , CI8 );
impl_digit!(i16 : I16 , CI16 );
impl_digit!(i32 : I32 , CI32 );
impl_digit!(i64 : I64 , CI64 );
impl_digit!(u8 : U8 , CU8 );
impl_digit!(u16 : U16 , CU16 );
impl_digit!(u32 : U32 , CU32 );
impl_digit!(u64 : U64 , CU64 );
impl_digit!(f32 : F32 , CF32 );
impl_digit!(f64 : F64 , CF64 );

#[cfg(feature = "half")]
mod half_impl {
use half_::{bf16, f16};

impl_digit!( f16 : super::F16 , CF16 );
impl_digit!([f16 ; 2] : super::F16x2 , CF16_2 );
impl_digit!((f16 , f16): super::F16x2 );

impl_digit!( bf16 : super::BF16 , CBF16 );
impl_digit!([bf16; 2] : super::BF16x2, CBF16_2);
impl_digit!((bf16, bf16): super::BF16x2 );
}

0 comments on commit 18bbe60

Please sign in to comment.