Skip to content

Commit

Permalink
Add FindLsb / FindMsb
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia committed Oct 25, 2021
1 parent 5cf11ab commit 7506ddd
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2443,6 +2443,8 @@ impl<'a, W: Write> Writer<'a, W> {
Mf::ReverseBits => "bitfieldReverse",
Mf::ExtractBits => "bitfieldExtract",
Mf::InsertBits => "bitfieldInsert",
Mf::FindLsb => "findLSB",
Mf::FindMsb => "findMSB",
// data packing
Mf::Pack4x8snorm => "packSnorm4x8",
Mf::Pack4x8unorm => "packUnorm4x8",
Expand Down
2 changes: 2 additions & 0 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,8 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
// bits
Mf::CountOneBits => Function::Regular("countbits"),
Mf::ReverseBits => Function::Regular("reversebits"),
Mf::FindLsb => Function::Regular("firstbit_lo"),
Mf::FindMsb => Function::Regular("firstbit_hi"),
_ => return Err(Error::Unimplemented(format!("write_expr_math {:?}", fun))),
};

Expand Down
2 changes: 2 additions & 0 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,8 @@ impl<W: Write> Writer<W> {
Mf::ReverseBits => "reverse_bits",
Mf::ExtractBits => "extract_bits",
Mf::InsertBits => "insert_bits",
Mf::FindLsb => return Err(Error::UnsupportedCall(format!("{:?}", fun))),
Mf::FindMsb => return Err(Error::UnsupportedCall(format!("{:?}", fun))),
// data packing
Mf::Pack4x8snorm => "pack_float_to_unorm4x8",
Mf::Pack4x8unorm => "pack_float_to_snorm4x8",
Expand Down
6 changes: 6 additions & 0 deletions src/back/spv/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,12 @@ impl<'w> BlockContext<'w> {
arg2_id,
arg3_id,
)),
Mf::FindLsb => MathOp::Ext(spirv::GLOp::FindILsb),
Mf::FindMsb => MathOp::Ext(match arg_scalar_kind {
Some(crate::ScalarKind::Uint) => spirv::GLOp::FindUMsb,
Some(crate::ScalarKind::Sint) => spirv::GLOp::FindSMsb,
other => unimplemented!("Unexpected findMSB({:?})", other),
}),
Mf::Pack4x8unorm => MathOp::Ext(spirv::GLOp::PackUnorm4x8),
Mf::Pack4x8snorm => MathOp::Ext(spirv::GLOp::PackSnorm4x8),
Mf::Pack2x16float => MathOp::Ext(spirv::GLOp::PackHalf2x16),
Expand Down
5 changes: 4 additions & 1 deletion src/front/glsl/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,15 @@ pub fn inject_builtin(declaration: &mut FunctionDeclaration, module: &mut Module
))
}
}
"bitCount" | "bitfieldReverse" | "bitfieldExtract" | "bitfieldInsert" => {
"bitCount" | "bitfieldReverse" | "bitfieldExtract" | "bitfieldInsert" | "findLSB"
| "findMSB" => {
let fun = match name {
"bitCount" => MathFunction::CountOneBits,
"bitfieldReverse" => MathFunction::ReverseBits,
"bitfieldExtract" => MathFunction::ExtractBits,
"bitfieldInsert" => MathFunction::InsertBits,
"findLSB" => MathFunction::FindLsb,
"findMSB" => MathFunction::FindMsb,
_ => unreachable!(),
};

Expand Down
2 changes: 2 additions & 0 deletions src/front/spv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,8 @@ impl<I: Iterator<Item = u32>> Parser<I> {
Glo::UnpackHalf2x16 => Mf::Unpack2x16float,
Glo::UnpackUnorm2x16 => Mf::Unpack2x16unorm,
Glo::UnpackSnorm2x16 => Mf::Unpack2x16snorm,
Glo::FindILsb => Mf::FindLsb,
Glo::FindUMsb | Glo::FindSMsb => Mf::FindMsb,
_ => return Err(Error::UnsupportedExtInst(inst_id)),
};

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ pub enum MathFunction {
ReverseBits,
ExtractBits,
InsertBits,
FindLsb,
FindMsb,
// data packing
Pack4x8snorm,
Pack4x8unorm,
Expand Down
2 changes: 2 additions & 0 deletions src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ impl super::MathFunction {
Self::ReverseBits => 1,
Self::ExtractBits => 3,
Self::InsertBits => 4,
Self::FindLsb => 1,
Self::FindMsb => 1,
// data packing
Self::Pack4x8snorm => 1,
Self::Pack4x8unorm => 1,
Expand Down
4 changes: 3 additions & 1 deletion src/proc/typifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,9 @@ impl<'a> ResolveContext<'a> {
Mf::CountOneBits |
Mf::ReverseBits |
Mf::ExtractBits |
Mf::InsertBits => res_arg.clone(),
Mf::InsertBits |
Mf::FindLsb |
Mf::FindMsb => res_arg.clone(),
// data packing
Mf::Pack4x8snorm |
Mf::Pack4x8unorm |
Expand Down
2 changes: 1 addition & 1 deletion src/valid/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ impl super::Validator {
_ => return Err(ExpressionError::InvalidArgumentType(fun, 0, arg)),
}
}
Mf::CountOneBits | Mf::ReverseBits => {
Mf::CountOneBits | Mf::ReverseBits | Mf::FindLsb | Mf::FindMsb => {
if arg1_ty.is_some() | arg2_ty.is_some() | arg3_ty.is_some() {
return Err(ExpressionError::WrongArgumentCount(fun));
}
Expand Down

0 comments on commit 7506ddd

Please sign in to comment.