Skip to main content

eunomia/types/
mod.rs

1mod complex;
2mod floats;
3mod ints;
4
5pub use complex::{Complex, Complex32, Complex64};
6pub use floats::{Bf16, Bf4, Bf8, F16, F32, F4, F64, F8};
7pub use ints::{I16, I32, I8};
8
9// SAFETY: `Complex<T>` is `#[repr(C)]` with two `T` fields, so it is zeroable
10// and plain-old-data exactly when `T` is.
11unsafe impl<T: bytemuck::Zeroable> bytemuck::Zeroable for Complex<T> {}
12unsafe impl<T: bytemuck::Pod> bytemuck::Pod for Complex<T> {}
13
14const _: () = {
15    assert!(core::mem::size_of::<Complex32>() == 2 * core::mem::size_of::<f32>());
16    assert!(core::mem::align_of::<Complex32>() == core::mem::align_of::<f32>());
17    assert!(core::mem::offset_of!(Complex32, re) == 0);
18    assert!(core::mem::offset_of!(Complex32, im) == core::mem::size_of::<f32>());
19
20    assert!(core::mem::size_of::<Complex64>() == 2 * core::mem::size_of::<f64>());
21    assert!(core::mem::align_of::<Complex64>() == core::mem::align_of::<f64>());
22    assert!(core::mem::offset_of!(Complex64, re) == 0);
23    assert!(core::mem::offset_of!(Complex64, im) == core::mem::size_of::<f64>());
24};
25
26// SAFETY: every wrapper is `#[repr(transparent)]` over a type that is itself
27// `Pod`/`Zeroable` — `f32`/`f64`/`i8`/`i16`/`i32`, a `u16` for `F16`/`Bf16`, or
28// a `u8` for the sub-byte formats (all bit patterns are valid encodings). None
29// carry padding or invalid bit patterns, and the `const _` block below pins each
30// type's size and alignment.
31unsafe impl bytemuck::Zeroable for F16 {}
32unsafe impl bytemuck::Pod for F16 {}
33unsafe impl bytemuck::Zeroable for F32 {}
34unsafe impl bytemuck::Pod for F32 {}
35unsafe impl bytemuck::Zeroable for F64 {}
36unsafe impl bytemuck::Pod for F64 {}
37unsafe impl bytemuck::Zeroable for Bf16 {}
38unsafe impl bytemuck::Pod for Bf16 {}
39unsafe impl bytemuck::Zeroable for Bf8 {}
40unsafe impl bytemuck::Pod for Bf8 {}
41unsafe impl bytemuck::Zeroable for Bf4 {}
42unsafe impl bytemuck::Pod for Bf4 {}
43unsafe impl bytemuck::Zeroable for F8 {}
44unsafe impl bytemuck::Pod for F8 {}
45unsafe impl bytemuck::Zeroable for F4 {}
46unsafe impl bytemuck::Pod for F4 {}
47unsafe impl bytemuck::Zeroable for I8 {}
48unsafe impl bytemuck::Pod for I8 {}
49unsafe impl bytemuck::Zeroable for I16 {}
50unsafe impl bytemuck::Pod for I16 {}
51unsafe impl bytemuck::Zeroable for I32 {}
52unsafe impl bytemuck::Pod for I32 {}
53
54const _: () = {
55    assert!(core::mem::size_of::<F16>() == 2);
56    assert!(core::mem::align_of::<F16>() == 2);
57    assert!(core::mem::size_of::<F32>() == 4);
58    assert!(core::mem::align_of::<F32>() == 4);
59    assert!(core::mem::size_of::<F64>() == 8);
60    assert!(core::mem::align_of::<F64>() == 8);
61    assert!(core::mem::size_of::<Bf16>() == 2);
62    assert!(core::mem::align_of::<Bf16>() == 2);
63    assert!(core::mem::size_of::<Bf8>() == 1);
64    assert!(core::mem::align_of::<Bf8>() == 1);
65    assert!(core::mem::size_of::<Bf4>() == 1);
66    assert!(core::mem::align_of::<Bf4>() == 1);
67    assert!(core::mem::size_of::<F8>() == 1);
68    assert!(core::mem::align_of::<F8>() == 1);
69    assert!(core::mem::size_of::<F4>() == 1);
70    assert!(core::mem::align_of::<F4>() == 1);
71    assert!(core::mem::size_of::<I8>() == 1);
72    assert!(core::mem::align_of::<I8>() == 1);
73    assert!(core::mem::size_of::<I16>() == 2);
74    assert!(core::mem::align_of::<I16>() == 2);
75    assert!(core::mem::size_of::<I32>() == 4);
76    assert!(core::mem::align_of::<I32>() == 4);
77};