use crate::GpuValue;
use std::marker::PhantomData;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LaneId(u8);
impl LaneId {
pub const fn new(id: u8) -> Self {
assert!(id < 32, "Lane ID must be < 32");
LaneId(id)
}
pub const fn get(self) -> u8 {
self.0
}
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct WarpId(u16);
impl WarpId {
pub const fn new(id: u16) -> Self {
WarpId(id)
}
pub const fn get(self) -> u16 {
self.0
}
}
#[derive(Clone, Copy, Debug)]
pub struct Uniform<T: GpuValue> {
value: T,
}
impl<T: GpuValue> Uniform<T> {
pub const fn from_const(value: T) -> Self {
Uniform { value }
}
pub fn get(self) -> T {
self.value
}
pub fn broadcast(self) -> PerLane<T> {
PerLane { value: self.value }
}
}
#[derive(Clone, Copy, Debug)]
pub struct PerLane<T: GpuValue> {
value: T,
}
impl<T: GpuValue> PerLane<T> {
pub fn new(value: T) -> Self {
PerLane { value }
}
pub fn get(self) -> T {
self.value
}
pub unsafe fn assume_uniform(self) -> Uniform<T> {
Uniform { value: self.value }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Role {
pub mask: u32,
pub name: &'static str,
}
impl Role {
pub const fn lanes(start: u8, end: u8, name: &'static str) -> Self {
assert!(start < 32 && end <= 32 && start < end);
let width = (end - start) as u32;
let mask = if width >= 32 {
u32::MAX
} else {
((1u32 << width) - 1) << start
};
Role { mask, name }
}
pub const fn lane(id: u8, name: &'static str) -> Self {
assert!(id < 32);
Role {
mask: 1u32 << id,
name,
}
}
pub const fn contains(self, lane: LaneId) -> bool {
(self.mask & (1u32 << lane.0)) != 0
}
pub const fn count(self) -> u32 {
self.mask.count_ones()
}
}
pub struct Warp<const N: usize> {
roles: [Role; N],
_phantom: PhantomData<()>,
}
impl<const N: usize> Warp<N> {
pub const fn new(roles: [Role; N]) -> Self {
let mut coverage = 0u32;
let mut i = 0;
while i < N {
assert!(coverage & roles[i].mask == 0, "Roles must not overlap");
coverage |= roles[i].mask;
i += 1;
}
assert!(coverage == 0xFFFFFFFF, "Roles must cover all 32 lanes");
Warp {
roles,
_phantom: PhantomData,
}
}
pub const fn roles(&self) -> &[Role; N] {
&self.roles
}
}
pub const COORDINATOR_WORKER: Warp<2> = Warp::new([
Role::lanes(0, 4, "coordinator"), Role::lanes(4, 32, "worker"), ]);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lane_id() {
let lane = LaneId::new(15);
assert_eq!(lane.get(), 15);
assert_eq!(lane.index(), 15);
}
#[test]
#[should_panic]
fn test_lane_id_out_of_range() {
LaneId::new(32);
}
#[test]
fn test_uniform_broadcast() {
let u: Uniform<i32> = Uniform::from_const(42);
let p: PerLane<i32> = u.broadcast();
assert_eq!(p.get(), 42);
}
#[test]
fn test_role_coverage() {
let coordinator = Role::lanes(0, 4, "coordinator");
let worker = Role::lanes(4, 32, "worker");
assert!(coordinator.contains(LaneId::new(0)));
assert!(coordinator.contains(LaneId::new(3)));
assert!(!coordinator.contains(LaneId::new(4)));
assert!(!worker.contains(LaneId::new(3)));
assert!(worker.contains(LaneId::new(4)));
assert!(worker.contains(LaneId::new(31)));
assert_eq!(coordinator.count(), 4);
assert_eq!(worker.count(), 28);
}
#[test]
fn test_warp_roles() {
let _warp = Warp::new([Role::lanes(0, 16, "left"), Role::lanes(16, 32, "right")]);
}
}