Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub enum LayoutContent {

#[repr(u32)]
enum DigitLayoutType {
Unsigned = 0xe0_00_00_00, // 0b111...
Real = 0xc0_00_00_00, // 0b110...
Named = 0, // 0b...
Unsigned = 0xE000_0000, // 0b111...
Real = 0xC000_0000, // 0b110...
Named = 0, // 0b...
}
const UNSIGNED: u32 = DigitLayoutType::Unsigned as _;
const SIGNED: u32 = DigitLayoutType::Real as _;
const UNSIGNED: u32 = DigitLayoutType::Unsigned as u32;
const SIGNED: u32 = DigitLayoutType::Real as u32;
const HEAD: u32 = UNSIGNED;

impl DigitLayout {
Expand Down Expand Up @@ -88,7 +88,7 @@ impl DigitLayout {
_ => panic!("Invalid character in digit name"),
};
body += (b as u32 + 1) * exp;
const GUARD: u32 = 0xc0_00_00_00; // 0b110...
const GUARD: u32 = 0xC000_0000; // 0b110...
assert!(body & GUARD != GUARD);
assert!(exp & GUARD != GUARD);
exp *= 37; // 37 = 10 + 26 + 1
Expand All @@ -99,26 +99,26 @@ impl DigitLayout {
#[inline(always)]
const fn new(ty: DigitLayoutType, body: u32, group: u16, size: u16) -> Self {
Self {
code: ((ty as u32) | body),
code: (ty as u32) | body,
group,
size,
}
}

/// Raw transmutation to `u32`.
/// Raw transmutation to `u64`.
#[inline]
pub const fn to_u64(self) -> u64 {
unsafe { core::mem::transmute(self) }
}

/// Get the number of bytes occupied by this layout.
pub const fn group_size(self) -> usize {
self.group as _
self.group as usize
}

/// Get the number of bytes occupied by this layout.
pub const fn nbytes(self) -> usize {
self.size as _
self.size as usize
}

/// Decode the content of the digit layout.
Expand Down Expand Up @@ -154,12 +154,12 @@ impl DigitLayout {

#[inline(always)]
const fn decode_exponent(self) -> u32 {
((self.code & !HEAD) >> 16) & 0xff
((self.code & !HEAD) >> 16) & 0xFF
}

#[inline(always)]
const fn decode_mantissa(self) -> u32 {
self.code & 0xffff
self.code & 0xFFFF
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// 定义一个 [`DigitLayout`](crate::DigitLayout) 实例。
/// Defines an instance of [`DigitLayout`](crate::DigitLayout).
#[macro_export]
macro_rules! layout {
($name:ident u($bits:expr); $group:expr) => {
Expand Down
32 changes: 16 additions & 16 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! 为一些基本类型和常用类型提供预定义布局。
//! Predefined layouts for basic and commonly used types.

#![allow(missing_docs)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码中出现的所有 [allow(...)] 须通过注释说明理由


layout!(U8 u( 8));
layout!(U16 u( 16));
layout!(U32 u( 32));
layout!(U64 u( 64));
layout!(U128 u(128));
layout!(I8 i( 8));
layout!(I16 i( 16));
layout!(I32 i( 32));
layout!(I64 i( 64));
layout!(I128 i(128));
layout!(F16 e( 5)m( 10));
layout!(BF16 e( 8)m( 7));
layout!(F32 e( 8)m( 23));
layout!(F64 e(11)m( 52));
layout!(F128 e(15)m(112));
layout!(U8 u( 8));
layout!(U16 u( 16));
layout!(U32 u( 32));
layout!(U64 u( 64));
layout!(U128 u(128));
layout!(I8 i( 8));
layout!(I16 i( 16));
layout!(I32 i( 32));
layout!(I64 i( 64));
layout!(I128 i(128));
layout!(F16 e( 5)m( 10));
layout!(BF16 e( 8)m( 7));
layout!(F32 e( 8)m( 23));
layout!(F64 e(11)m( 52));
layout!(F128 e(15)m(112));
layout!(Bool; [1] in 1);