spirv_std/
integer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Traits related to integers.

/// Abstract trait representing any SPIR-V integer type.
///
/// # Safety
/// Implementing this trait on non-primitive-integer types breaks assumptions of other unsafe code,
/// and should not be done.
pub unsafe trait Integer: num_traits::PrimInt + crate::scalar::Scalar {
    /// Width of the integer, in bits.
    const WIDTH: usize;
    /// If the integer is signed: true means signed, false means unsigned.
    const SIGNED: bool;
}

/// A trait for being generic over signed integer types.
pub trait SignedInteger: Integer {}
/// A trait for being generic over unsigned integer types.
pub trait UnsignedInteger: Integer {}

macro_rules! impl_numbers {
    (impl UnsignedInteger for $typ:ty;) => {
        unsafe impl Integer for $typ {
            const WIDTH: usize = core::mem::size_of::<$typ>() * 8;
            const SIGNED: bool = false;
        }

        impl UnsignedInteger for $typ {}
    };
    (impl SignedInteger for $typ:ty;) => {
        unsafe impl Integer for $typ {
            const WIDTH: usize = core::mem::size_of::<$typ>() * 8;
            const SIGNED: bool = true;
        }

        impl SignedInteger for $typ {}
    };
    ($(impl $trait:ident for $typ:ty;)+) => {
        $(impl_numbers!(impl $trait for $typ;);)+
    };

}

impl_numbers! {
    impl UnsignedInteger for u8;
    impl UnsignedInteger for u16;
    impl UnsignedInteger for u32;
    impl UnsignedInteger for u64;
    impl SignedInteger for i8;
    impl SignedInteger for i16;
    impl SignedInteger for i32;
    impl SignedInteger for i64;
}