Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not operator depending on expression type #1760

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1751,10 +1751,21 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
}
}
Expression::Unary { op, expr } => {
use crate::{ScalarKind as Sk, UnaryOperator as Uo};
// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#unary-operators
let op_str = match op {
crate::UnaryOperator::Negate => "-",
crate::UnaryOperator::Not => "!",
Uo::Negate => "-",
Uo::Not => match *func_ctx.info[expr].ty.inner_with(&module.types) {
TypeInner::Scalar { kind: Sk::Sint, .. } => "~",
TypeInner::Scalar { kind: Sk::Uint, .. } => "~",
TypeInner::Scalar { kind: Sk::Bool, .. } => "!",
ref other => {
return Err(Error::Custom(format!(
"Cannot apply not to type {:?}",
other
)))
}
},
};
write!(self.out, "{}", op_str)?;
self.write_expr(module, expr, func_ctx)?;
Expand Down
10 changes: 8 additions & 2 deletions src/back/msl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,9 +1406,15 @@ impl<W: Write> Writer<W> {
}
},
crate::Expression::Unary { op, expr } => {
use crate::{ScalarKind as Sk, UnaryOperator as Uo};
let op_str = match op {
crate::UnaryOperator::Negate => "-",
crate::UnaryOperator::Not => "!",
Uo::Negate => "-",
Uo::Not => match *context.resolve_type(expr) {
crate::TypeInner::Scalar { kind: Sk::Sint, .. } => "~",
crate::TypeInner::Scalar { kind: Sk::Uint, .. } => "~",
crate::TypeInner::Scalar { kind: Sk::Bool, .. } => "!",
_ => return Err(Error::Validation),
},
};
write!(self.out, "{}", op_str)?;
self.put_expression(expr, context, false)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/out/hlsl/operators.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int unary()
if (!true) {
return 1;
} else {
return !1;
return ~1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/out/msl/operators.msl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int unary(
if (!true) {
return 1;
} else {
return !1;
return ~1;
}
}

Expand Down