Skip to content

Commit 06a7cbc

Browse files
committed
scalar or vector: move all scalar traits and impl macros to mod scalar
1 parent bd040ca commit 06a7cbc

File tree

7 files changed

+100
-120
lines changed

7 files changed

+100
-120
lines changed

crates/spirv-std/src/float.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,6 @@
44
use core::arch::asm;
55
use glam::{Vec2, Vec4};
66

7-
/// Abstract trait representing a SPIR-V floating point type.
8-
///
9-
/// # Safety
10-
/// Implementing this trait on non-primitive-float types breaks assumptions of other unsafe code,
11-
/// and should not be done.
12-
pub unsafe trait Float: num_traits::Float + crate::scalar::Scalar + Default {
13-
/// Width of the float, in bits.
14-
const WIDTH: usize;
15-
}
16-
17-
unsafe impl Float for f32 {
18-
const WIDTH: usize = 32;
19-
}
20-
21-
unsafe impl Float for f64 {
22-
const WIDTH: usize = 64;
23-
}
24-
257
/// Converts two f32 values (floats) into two f16 values (halfs). The result is a u32, with the low
268
/// 16 bits being the first f16, and the high 16 bits being the second f16.
279
#[spirv_std_macros::gpu_only]

crates/spirv-std/src/integer.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.

crates/spirv-std/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ pub mod byte_addressable_buffer;
9494
pub mod float;
9595
pub mod image;
9696
pub mod indirect_command;
97-
mod integer;
9897
pub mod matrix;
9998
pub mod memory;
100-
mod number;
10199
pub mod ray_tracing;
102100
mod runtime_array;
103101
mod sampler;
@@ -109,10 +107,7 @@ mod vector;
109107
pub use self::sampler::Sampler;
110108
pub use crate::macros::Image;
111109
pub use byte_addressable_buffer::ByteAddressableBuffer;
112-
pub use float::Float;
113-
pub use integer::*;
114110
pub use num_traits;
115-
pub use number::*;
116111
pub use runtime_array::*;
117112
pub use scalar::*;
118113
pub use typed_buffer::*;

crates/spirv-std/src/number.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

crates/spirv-std/src/scalar.rs

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,113 @@
11
//! Traits related to scalars.
22
3-
use crate::vector::VectorOrScalar;
3+
use crate::VectorOrScalar;
4+
use crate::sealed::Sealed;
45
use core::num::NonZeroUsize;
56

6-
/// Abstract trait representing a SPIR-V scalar type.
7-
///
8-
/// Implemented on types that map to spirv "scalar" types, which includes:
7+
/// Abstract trait representing a SPIR-V scalar type, which includes:
98
/// * Floating-point type: f32, f64
109
/// * Integer type: u8, u16, u32, u64, i8, i16, i32, i64
1110
/// * Boolean type: bool
1211
///
1312
/// See the SPIRV spec on [Types](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_types).
1413
///
1514
/// # Safety
16-
/// Must only be implemented on spirv "scalar" types, as mentioned above.
15+
/// Implementing this trait on non-scalar types breaks assumptions of other unsafe code, and should not be done.
1716
pub unsafe trait Scalar: VectorOrScalar<Scalar = Self> + crate::sealed::Sealed {}
1817

18+
/// Abstract trait representing a SPIR-V integer or floating-point type. Unlike [`Scalar`], excludes the boolean type.
19+
///
20+
/// # Safety
21+
/// Implementing this trait on non-primitive-integer or non-primitive-float types breaks assumptions of other unsafe
22+
/// code, and should not be done.
23+
pub unsafe trait Number: Scalar {}
24+
25+
/// Abstract trait representing any SPIR-V integer type.
26+
///
27+
/// # Safety
28+
/// Implementing this trait on non-primitive-integer types breaks assumptions of other unsafe code,
29+
/// and should not be done.
30+
pub unsafe trait Integer: num_traits::PrimInt + Number {
31+
/// Width of the integer, in bits.
32+
const WIDTH: usize;
33+
/// If the integer is signed: true means signed, false means unsigned.
34+
const SIGNED: bool;
35+
}
36+
37+
/// Abstract trait representing any SPIR-V signed integer type.
38+
///
39+
/// # Safety
40+
/// Implementing this trait on non-signed-integer types breaks assumptions of other unsafe code,
41+
/// and should not be done.
42+
pub unsafe trait SignedInteger: num_traits::Signed + Integer {}
43+
44+
/// Abstract trait representing any SPIR-V unsigned integer type.
45+
///
46+
/// # Safety
47+
/// Implementing this trait on non-unsigned-integer types breaks assumptions of other unsafe code,
48+
/// and should not be done.
49+
pub unsafe trait UnsignedInteger: num_traits::Unsigned + Integer {}
50+
51+
/// Abstract trait representing a SPIR-V floating point type.
52+
///
53+
/// # Safety
54+
/// Implementing this trait on non-primitive-float types breaks assumptions of other unsafe code,
55+
/// and should not be done.
56+
pub unsafe trait Float: num_traits::Float + Number {
57+
/// Width of the float, in bits.
58+
const WIDTH: usize;
59+
}
60+
1961
macro_rules! impl_scalar {
20-
($($ty:ty),+) => {
21-
$(
22-
unsafe impl VectorOrScalar for $ty {
23-
type Scalar = Self;
24-
const DIM: NonZeroUsize = NonZeroUsize::new(1).unwrap();
25-
}
26-
unsafe impl Scalar for $ty {}
27-
)+
62+
(impl Scalar for $ty:ty;) => {
63+
impl Sealed for $ty {}
64+
unsafe impl VectorOrScalar for $ty {
65+
type Scalar = Self;
66+
const DIM: NonZeroUsize = NonZeroUsize::new(1).unwrap();
67+
}
68+
unsafe impl Scalar for $ty {}
69+
};
70+
(impl Number for $ty:ty;) => {
71+
unsafe impl Number for $ty {}
72+
impl_scalar!(impl Scalar for $ty;);
73+
};
74+
(impl UnsignedInteger for $ty:ty;) => {
75+
unsafe impl Integer for $ty {
76+
const WIDTH: usize = core::mem::size_of::<$ty>() * 8;
77+
const SIGNED: bool = false;
78+
}
79+
unsafe impl UnsignedInteger for $ty {}
80+
impl_scalar!(impl Number for $ty;);
81+
};
82+
(impl SignedInteger for $ty:ty;) => {
83+
unsafe impl Integer for $ty {
84+
const WIDTH: usize = core::mem::size_of::<$ty>() * 8;
85+
const SIGNED: bool = true;
86+
}
87+
unsafe impl SignedInteger for $ty {}
88+
impl_scalar!(impl Number for $ty;);
89+
};
90+
(impl Float for $ty:ty;) => {
91+
unsafe impl Float for $ty {
92+
const WIDTH: usize = core::mem::size_of::<$ty>() * 8;
93+
}
94+
impl_scalar!(impl Number for $ty;);
95+
};
96+
($(impl $trait:ident for $ty:ty;)+) => {
97+
$(impl_scalar!(impl $trait for $ty;);)+
2898
};
2999
}
30100

31-
impl_scalar!(bool, f32, f64, u8, u16, u32, u64, i8, i16, i32, i64);
101+
impl_scalar! {
102+
impl UnsignedInteger for u8;
103+
impl UnsignedInteger for u16;
104+
impl UnsignedInteger for u32;
105+
impl UnsignedInteger for u64;
106+
impl SignedInteger for i8;
107+
impl SignedInteger for i16;
108+
impl SignedInteger for i32;
109+
impl SignedInteger for i64;
110+
impl Float for f32;
111+
impl Float for f64;
112+
impl Scalar for bool;
113+
}

crates/spirv-std/src/sealed.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22
/// of `spirv-std`.
33
pub trait Sealed {}
44

5-
impl Sealed for bool {}
6-
impl Sealed for f32 {}
7-
impl Sealed for f64 {}
8-
impl Sealed for u8 {}
9-
impl Sealed for u16 {}
10-
impl Sealed for u32 {}
11-
impl Sealed for u64 {}
12-
impl Sealed for i8 {}
13-
impl Sealed for i16 {}
14-
impl Sealed for i32 {}
15-
impl Sealed for i64 {}
16-
175
impl Sealed for glam::Vec2 {}
186
impl Sealed for glam::Vec3 {}
197
impl Sealed for glam::Vec4 {}

tests/compiletests/ui/arch/debug_printf_type_checking.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ LL | debug_printf!("%f", 11_u32);
7575
| |
7676
| this argument influences the return type of `debug_printf_assert_is_type`
7777
note: function defined here
78-
--> $SPIRV_STD_SRC/lib.rs:139:8
78+
--> $SPIRV_STD_SRC/lib.rs:134:8
7979
|
8080
LL | pub fn debug_printf_assert_is_type<T>(ty: T) -> T {
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -103,7 +103,7 @@ LL | debug_printf!("%u", 11.0_f32);
103103
| |
104104
| this argument influences the return type of `debug_printf_assert_is_type`
105105
note: function defined here
106-
--> $SPIRV_STD_SRC/lib.rs:139:8
106+
--> $SPIRV_STD_SRC/lib.rs:134:8
107107
|
108108
LL | pub fn debug_printf_assert_is_type<T>(ty: T) -> T {
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -131,7 +131,7 @@ LL | debug_printf!("%v2f", 11.0);
131131
`IVec3` implements `Vector<i32, 3>`
132132
and 8 others
133133
note: required by a bound in `debug_printf_assert_is_vector`
134-
--> $SPIRV_STD_SRC/lib.rs:144:53
134+
--> $SPIRV_STD_SRC/lib.rs:139:53
135135
|
136136
LL | pub fn debug_printf_assert_is_vector<TY: Scalar, V: Vector<TY, SIZE>, const SIZE: usize>(
137137
| ^^^^^^^^^^^^^^^^ required by this bound in `debug_printf_assert_is_vector`
@@ -154,7 +154,7 @@ LL | debug_printf!("%f", Vec2::splat(33.3));
154154
| |
155155
| this argument influences the return type of `debug_printf_assert_is_type`
156156
note: function defined here
157-
--> $SPIRV_STD_SRC/lib.rs:139:8
157+
--> $SPIRV_STD_SRC/lib.rs:134:8
158158
|
159159
LL | pub fn debug_printf_assert_is_type<T>(ty: T) -> T {
160160
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)