Skip to content

Commit 1d36549

Browse files
authored
Add arithmetic instructions to arch (#446)
1 parent 6e8453f commit 1d36549

File tree

4 files changed

+926
-11
lines changed

4 files changed

+926
-11
lines changed

crates/spirv-builder/src/test/arch.rs

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
use super::val;
2+
3+
#[test]
4+
fn any() {
5+
val(r#"
6+
7+
#[allow(unused_attributes)]
8+
#[spirv(fragment)]
9+
pub fn main() {
10+
let vector = glam::BVec2::new(true, false);
11+
assert!(arch::any(vector));
12+
}
13+
"#);
14+
}
15+
16+
#[test]
17+
fn all() {
18+
val(r#"
19+
#[allow(unused_attributes)]
20+
#[spirv(fragment)]
21+
pub fn main() {
22+
let vector = glam::BVec2::new(true, true);
23+
assert!(spirv_std::arch::all(vector));
24+
}
25+
"#);
26+
}
27+
28+
#[test]
29+
fn s_negate() {
30+
val(r#"
31+
#[allow(unused_attributes)]
32+
#[spirv(fragment)]
33+
pub fn main() {
34+
let operand: i32 = -5;
35+
let vector = glam::IVec2::new(-5, -0);
36+
assert!(arch::s_negate_vector(vector) == glam::IVec2::new(5, 0));
37+
}
38+
"#);
39+
}
40+
41+
#[test]
42+
fn f_negate() {
43+
val(r#"
44+
#[allow(unused_attributes)]
45+
#[spirv(fragment)]
46+
pub fn main() {
47+
let operand: f32 = -5.0;
48+
let vector = glam::Vec2::new(-5.0, -0.0);
49+
assert!(arch::f_negate_vector(vector) == glam::Vec2::new(5.0, 0.0));
50+
}
51+
"#);
52+
}
53+
54+
#[test]
55+
fn i_add() {
56+
val(r#"
57+
#[allow(unused_attributes)]
58+
#[spirv(fragment)]
59+
pub fn main() {
60+
let x = 5;
61+
let y = 2;
62+
let vx = glam::IVec2::new(2, 5);
63+
let vy = glam::IVec2::new(5, 2);
64+
assert!(arch::i_add_vector(vx, vy) == glam::IVec2::new(7, 7));
65+
}
66+
"#);
67+
}
68+
69+
#[test]
70+
fn f_add() {
71+
val(r#"
72+
#[allow(unused_attributes)]
73+
#[spirv(fragment)]
74+
pub fn main() {
75+
let x = 5.0;
76+
let y = 2.0;
77+
let vx = glam::Vec2::new(2.0, 5.0);
78+
let vy = glam::Vec2::new(5.0, 2.0);
79+
assert!(arch::f_add_vector(vx, vy) == glam::Vec2::new(7.0, 7.0));
80+
}
81+
"#);
82+
}
83+
84+
#[test]
85+
fn i_sub() {
86+
val(r#"
87+
#[allow(unused_attributes)]
88+
#[spirv(fragment)]
89+
pub fn main() {
90+
let x = 5;
91+
let y = 5;
92+
let vx = glam::IVec2::new(5, 7);
93+
let vy = glam::IVec2::new(5, 7);
94+
assert!(arch::i_sub_vector(vx, vy) == glam::IVec2::new(0, 0));
95+
}
96+
"#);
97+
}
98+
99+
#[test]
100+
fn f_sub() {
101+
val(r#"
102+
#[allow(unused_attributes)]
103+
#[spirv(fragment)]
104+
pub fn main() {
105+
let x = 5.0;
106+
let y = 5.0;
107+
let vx = glam::Vec2::new(5.0, 7.0);
108+
let vy = glam::Vec2::new(5.0, 7.0);
109+
assert!(arch::f_sub_vector(vx, vy) == glam::Vec2::new(0.0, 0.0));
110+
}
111+
"#);
112+
}
113+
114+
#[test]
115+
fn i_mul() {
116+
val(r#"
117+
#[allow(unused_attributes)]
118+
#[spirv(fragment)]
119+
pub fn main() {
120+
let x = 5;
121+
let y = 2;
122+
let vx = glam::IVec2::new(5, 2);
123+
let vy = glam::IVec2::new(2, 5);
124+
assert!(arch::i_mul_vector(vx, vy) == glam::IVec2::new(10, 10));
125+
}
126+
"#);
127+
}
128+
129+
#[test]
130+
fn f_mul() {
131+
val(r#"
132+
#[allow(unused_attributes)]
133+
#[spirv(fragment)]
134+
pub fn main() {
135+
let x = 5.0;
136+
let y = 2.0;
137+
let vx = glam::Vec2::new(5.0, 2.0);
138+
let vy = glam::Vec2::new(2.0, 5.0);
139+
assert!(arch::f_mul_vector(vx, vy) == glam::Vec2::new(10.0, 10.0));
140+
}
141+
"#);
142+
}
143+
144+
#[test]
145+
fn s_div() {
146+
val(r#"
147+
#[allow(unused_attributes)]
148+
#[spirv(fragment)]
149+
pub fn main() {
150+
let x = 10;
151+
let y = 2;
152+
let vx = glam::IVec2::new(10, 10);
153+
let vy = glam::IVec2::new(2, 2);
154+
assert!(arch::s_div_vector(vx, vy) == glam::IVec2::new(5, 5));
155+
}
156+
"#);
157+
}
158+
159+
#[test]
160+
fn u_div() {
161+
val(r#"
162+
#[allow(unused_attributes)]
163+
#[spirv(fragment)]
164+
pub fn main() {
165+
let x: u32 = 10;
166+
let y = 2;
167+
let vx = glam::UVec2::new(10, 10);
168+
let vy = glam::UVec2::new(2, 2);
169+
assert!(arch::u_div_vector(vx, vy) == glam::UVec2::new(5, 5));
170+
}
171+
"#);
172+
}
173+
174+
#[test]
175+
fn f_div() {
176+
val(r#"
177+
#[allow(unused_attributes)]
178+
#[spirv(fragment)]
179+
pub fn main() {
180+
let x = 10.0;
181+
let y = 2.0;
182+
let vx = glam::Vec2::new(10.0, 10.0);
183+
let vy = glam::Vec2::new(2.0, 2.0);
184+
assert!(arch::f_div_vector(vx, vy) == glam::Vec2::new(5.0, 5.0));
185+
}
186+
"#);
187+
}
188+
189+
#[test]
190+
fn u_mod() {
191+
val(r#"
192+
#[allow(unused_attributes)]
193+
#[spirv(fragment)]
194+
pub fn main() {
195+
let x: u32 = 10;
196+
let y = 2;
197+
let vx = glam::UVec2::new(10, 10);
198+
let vy = glam::UVec2::new(2, 2);
199+
assert!(arch::u_mod_vector(vx, vy) == glam::UVec2::new(0, 0));
200+
}
201+
"#);
202+
}
203+
204+
#[test]
205+
fn s_mod() {
206+
val(r#"
207+
#[allow(unused_attributes)]
208+
#[spirv(fragment)]
209+
pub fn main() {
210+
let x = 10;
211+
let y = 2;
212+
let vx = glam::IVec2::new(10, 10);
213+
let vy = glam::IVec2::new(2, 2);
214+
assert!(arch::s_mod_vector(vx, vy) == glam::IVec2::new(0, 0));
215+
}
216+
"#);
217+
}
218+
219+
#[test]
220+
fn s_rem() {
221+
val(r#"
222+
#[allow(unused_attributes)]
223+
#[spirv(fragment)]
224+
pub fn main() {
225+
let x = -10;
226+
let y = -2;
227+
let vx = glam::IVec2::new(-10, -10);
228+
let vy = glam::IVec2::new(-2, -2);
229+
assert!(arch::s_rem_vector(vx, vy) == glam::IVec2::new(-0, -0));
230+
}
231+
"#);
232+
}
233+
234+
#[test]
235+
fn f_mod() {
236+
val(r#"
237+
#[allow(unused_attributes)]
238+
#[spirv(fragment)]
239+
pub fn main() {
240+
let x = 10.0;
241+
let y = 2.0;
242+
let vx = glam::Vec2::new(10.0, 10.0);
243+
let vy = glam::Vec2::new(2.0, 2.0);
244+
assert!(arch::f_mod_vector(vx, vy) == glam::Vec2::new(0.0, 0.0));
245+
}
246+
"#);
247+
}
248+
249+
#[test]
250+
fn f_rem() {
251+
val(r#"
252+
#[allow(unused_attributes)]
253+
#[spirv(fragment)]
254+
pub fn main() {
255+
let x = -10.0;
256+
let y = -2.0;
257+
let vx = glam::Vec2::new(-10.0, -10.0);
258+
let vy = glam::Vec2::new(-2.0, -2.0);
259+
assert!(arch::f_mod_vector(vx, vy) == glam::Vec2::new(-0.0, -0.0));
260+
}
261+
"#);
262+
}
263+
264+
#[test]
265+
fn vector_times_scalar() {
266+
val(r#"
267+
#[allow(unused_attributes)]
268+
#[spirv(fragment)]
269+
pub fn main() {
270+
let vector = glam::Vec2::new(10.0, 10.0);
271+
let scalar = 2.0;
272+
assert!(arch::vector_times_scalar(vector, scalar) == glam::Vec2::new(20.0, 20.0));
273+
}
274+
"#);
275+
}
276+
277+
#[test]
278+
fn vector_extract_dynamic() {
279+
val(r#"
280+
#[allow(unused_attributes)]
281+
#[spirv(fragment)]
282+
pub fn main() {
283+
let vector = glam::Vec2::new(1.0, 2.0);
284+
let element = unsafe { spirv_std::arch::vector_extract_dynamic(vector, 1) };
285+
assert!(2.0 == element);
286+
}
287+
"#);
288+
}
289+
290+
#[test]
291+
fn vector_insert_dynamic() {
292+
val(r#"
293+
#[allow(unused_attributes)]
294+
#[spirv(fragment)]
295+
pub fn main() {
296+
let vector = glam::Vec2::new(1.0, 2.0);
297+
let expected = glam::Vec2::new(1.0, 3.0);
298+
let new_vector = unsafe { spirv_std::arch::vector_insert_dynamic(vector, 1, 3.0) };
299+
assert!(new_vector == expected);
300+
}
301+
"#);
302+
}
303+
304+

crates/spirv-std/src/arch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
#[cfg(feature = "const-generics")]
77
use crate::{scalar::Scalar, vector::Vector};
88

9+
#[cfg(feature = "const-generics")]
10+
mod arithmetic;
911
mod derivative;
1012

13+
#[cfg(feature = "const-generics")]
14+
pub use arithmetic::*;
1115
pub use derivative::*;
1216

1317
/// Result is true if any component of `vector` is true, otherwise result is

0 commit comments

Comments
 (0)