Skip to content

Commit

Permalink
Add arithmetic instructions to arch
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Feb 23, 2021
1 parent 185c9e6 commit 38e58e3
Show file tree
Hide file tree
Showing 8 changed files with 1,379 additions and 11 deletions.
320 changes: 320 additions & 0 deletions crates/spirv-builder/src/test/arch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
use super::val;

#[test]
fn any() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::BVec2::new(true, false);
assert!(arch::any(vector));
}
"#);
}

#[test]
fn all() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::BVec2::new(true, true);
assert!(spirv_std::arch::all(vector));
}
"#);
}

#[test]
fn s_negate() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let operand: i32 = -5;
let vector = glam::IVec2::new(-5, -0);
assert!(arch::s_negate(operand) == 5);
assert!(arch::s_negate_vector(vector) == glam::IVec2::new(5, 0));
}
"#);
}

#[test]
fn f_negate() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let operand: f32 = -5.0;
let vector = glam::Vec2::new(-5.0, -0.0);
assert!(arch::f_negate(operand) == 5.0);
assert!(arch::f_negate_vector(vector) == glam::Vec2::new(5.0, 0.0));
}
"#);
}

#[test]
fn i_add() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5;
let y = 2;
let vx = glam::IVec2::new(2, 5);
let vy = glam::IVec2::new(5, 2);
assert!(arch::i_add(x, y) == 7);
assert!(arch::i_add_vector(vx, vy) == glam::IVec2::new(7, 7));
}
"#);
}

#[test]
fn f_add() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5.0;
let y = 2.0;
let vx = glam::Vec2::new(2.0, 5.0);
let vy = glam::Vec2::new(5.0, 2.0);
assert!(arch::f_add(x, y) == 7.0);
assert!(arch::f_add_vector(vx, vy) == glam::Vec2::new(7.0, 7.0));
}
"#);
}

#[test]
fn i_sub() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5;
let y = 5;
let vx = glam::IVec2::new(5, 7);
let vy = glam::IVec2::new(5, 7);
assert!(arch::i_sub(x, y) == 0);
assert!(arch::i_sub_vector(vx, vy) == glam::IVec2::new(0, 0));
}
"#);
}

#[test]
fn f_sub() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5.0;
let y = 5.0;
let vx = glam::Vec2::new(5.0, 7.0);
let vy = glam::Vec2::new(5.0, 7.0);
assert!(arch::f_sub(x, y) == 0.0);
assert!(arch::f_sub_vector(vx, vy) == glam::Vec2::new(0.0, 0.0));
}
"#);
}

#[test]
fn i_mul() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5;
let y = 2;
let vx = glam::IVec2::new(5, 2);
let vy = glam::IVec2::new(2, 5);
assert!(arch::i_mul(x, y) == 10);
assert!(arch::i_mul_vector(vx, vy) == glam::IVec2::new(10, 10));
}
"#);
}

#[test]
fn f_mul() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 5.0;
let y = 2.0;
let vx = glam::Vec2::new(5.0, 2.0);
let vy = glam::Vec2::new(2.0, 5.0);
assert!(arch::f_mul(x, y) == 10.0);
assert!(arch::f_mul_vector(vx, vy) == glam::Vec2::new(10.0, 10.0));
}
"#);
}

#[test]
fn s_div() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 10;
let y = 2;
let vx = glam::IVec2::new(10, 10);
let vy = glam::IVec2::new(2, 2);
assert!(arch::s_div(x, y) == 5);
assert!(arch::s_div_vector(vx, vy) == glam::IVec2::new(5, 5));
}
"#);
}

#[test]
fn u_div() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x: u32 = 10;
let y = 2;
let vx = glam::UVec2::new(10, 10);
let vy = glam::UVec2::new(2, 2);
assert!(arch::u_div(x, y) == 5);
assert!(arch::u_div_vector(vx, vy) == glam::UVec2::new(5, 5));
}
"#);
}

#[test]
fn f_div() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 10.0;
let y = 2.0;
let vx = glam::Vec2::new(10.0, 10.0);
let vy = glam::Vec2::new(2.0, 2.0);
assert!(arch::f_div(x, y) == 5.0);
assert!(arch::f_div_vector(vx, vy) == glam::Vec2::new(5.0, 5.0));
}
"#);
}

#[test]
fn u_mod() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x: u32 = 10;
let y = 2;
let vx = glam::UVec2::new(10, 10);
let vy = glam::UVec2::new(2, 2);
assert!(arch::u_mod(x, y) == 0);
assert!(arch::u_mod_vector(vx, vy) == glam::UVec2::new(0, 0));
}
"#);
}

#[test]
fn s_mod() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 10;
let y = 2;
let vx = glam::IVec2::new(10, 10);
let vy = glam::IVec2::new(2, 2);
assert!(arch::s_mod(x, y) == 0);
assert!(arch::s_mod_vector(vx, vy) == glam::IVec2::new(0, 0));
}
"#);
}

#[test]
fn s_rem() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = -10;
let y = -2;
let vx = glam::IVec2::new(-10, -10);
let vy = glam::IVec2::new(-2, -2);
assert!(arch::s_rem(x, y) == -0);
assert!(arch::s_rem_vector(vx, vy) == glam::IVec2::new(-0, -0));
}
"#);
}

#[test]
fn f_mod() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = 10.0;
let y = 2.0;
let vx = glam::Vec2::new(10.0, 10.0);
let vy = glam::Vec2::new(2.0, 2.0);
assert!(arch::f_mod(x, y) == 0.0);
assert!(arch::f_mod_vector(vx, vy) == glam::Vec2::new(0.0, 0.0));
}
"#);
}

#[test]
fn f_rem() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let x = -10.0;
let y = -2.0;
let vx = glam::Vec2::new(-10.0, -10.0);
let vy = glam::Vec2::new(-2.0, -2.0);
assert!(arch::f_mod(x, y) == -0.0);
assert!(arch::f_mod_vector(vx, vy) == glam::Vec2::new(-0.0, -0.0));
}
"#);
}

#[test]
fn vector_times_scalar() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(10.0, 10.0);
let scalar = 2.0;
assert!(arch::vector_times_scalar(vector, scalar) == glam::Vec2::new(20.0, 20.0));
}
"#);
}

#[test]
fn vector_extract_dynamic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
let element = unsafe { spirv_std::arch::vector_extract_dynamic(vector, 1) };
assert!(2.0 == element);
}
"#);
}

#[test]
fn vector_insert_dynamic() {
val(r#"
#[allow(unused_attributes)]
#[spirv(fragment)]
pub fn main() {
let vector = glam::Vec2::new(1.0, 2.0);
let expected = glam::Vec2::new(1.0, 3.0);
let new_vector = unsafe { spirv_std::arch::vector_insert_dynamic(vector, 1, 3.0) };
assert!(new_vector == expected);
}
"#);
}


4 changes: 4 additions & 0 deletions crates/spirv-std/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
//! This module is intended as a low level abstraction over SPIR-V instructions.
//! These functions will typically map to a single instruction, and will perform
//! no additional safety checks beyond type-checking.
mod arithmetic;

use crate::{scalar::Scalar, vector::Vector};

pub use arithmetic::*;

/// Result is true if any component of `vector` is true, otherwise result is
/// false.
#[spirv_std_macros::gpu_only]
Expand Down
Loading

0 comments on commit 38e58e3

Please sign in to comment.