mirror of
https://github.com/EmbarkStudios/rust-gpu.git
synced 2024-11-22 14:56:27 +00:00
Clean up some of the cases of duplication found by clippy::match_same_arms
. (#487)
This commit is contained in:
parent
c53c351987
commit
c3cf9fe5f3
@ -41,29 +41,21 @@ impl<'tcx> CodegenCx<'tcx> {
|
||||
|
||||
pub fn constant_int(&self, ty: Word, val: u64) -> SpirvValue {
|
||||
match self.lookup_type(ty) {
|
||||
SpirvType::Integer(8, false) => self
|
||||
.builder
|
||||
.def_constant(SpirvConst::U32(ty, val as u8 as u32)),
|
||||
SpirvType::Integer(16, false) => self
|
||||
.builder
|
||||
.def_constant(SpirvConst::U32(ty, val as u16 as u32)),
|
||||
SpirvType::Integer(32, false) => {
|
||||
self.builder.def_constant(SpirvConst::U32(ty, val as u32))
|
||||
SpirvType::Integer(bits @ 8..=32, signed) => {
|
||||
let size = Size::from_bits(bits);
|
||||
let val = val as u128;
|
||||
self.builder.def_constant(SpirvConst::U32(
|
||||
ty,
|
||||
if signed {
|
||||
size.sign_extend(val)
|
||||
} else {
|
||||
size.truncate(val)
|
||||
} as u32,
|
||||
))
|
||||
}
|
||||
SpirvType::Integer(64, false) => self.builder.def_constant(SpirvConst::U64(ty, val)),
|
||||
SpirvType::Integer(8, true) => self
|
||||
.builder
|
||||
.def_constant(SpirvConst::U32(ty, val as i64 as i8 as u32)),
|
||||
SpirvType::Integer(16, true) => self
|
||||
.builder
|
||||
.def_constant(SpirvConst::U32(ty, val as i64 as i16 as u32)),
|
||||
SpirvType::Integer(32, true) => self
|
||||
.builder
|
||||
.def_constant(SpirvConst::U32(ty, val as i64 as i32 as u32)),
|
||||
SpirvType::Integer(64, true) => self.builder.def_constant(SpirvConst::U64(ty, val)),
|
||||
SpirvType::Integer(64, _) => self.builder.def_constant(SpirvConst::U64(ty, val)),
|
||||
SpirvType::Bool => match val {
|
||||
0 => self.builder.def_constant(SpirvConst::Bool(ty, false)),
|
||||
1 => self.builder.def_constant(SpirvConst::Bool(ty, true)),
|
||||
0 | 1 => self.builder.def_constant(SpirvConst::Bool(ty, val != 0)),
|
||||
_ => self
|
||||
.tcx
|
||||
.sess
|
||||
|
@ -271,8 +271,7 @@ impl<'tcx> StaticMethods for CodegenCx<'tcx> {
|
||||
if self.lookup_type(v.ty) == SpirvType::Bool {
|
||||
let val = self.builder.lookup_const(v).unwrap();
|
||||
let val_int = match val {
|
||||
SpirvConst::Bool(_, false) => 0,
|
||||
SpirvConst::Bool(_, true) => 0,
|
||||
SpirvConst::Bool(_, val) => val as u8,
|
||||
_ => bug!(),
|
||||
};
|
||||
v = self.constant_u8(span, val_int);
|
||||
|
@ -156,7 +156,7 @@ impl<'tcx> BaseTypeMethods<'tcx> for CodegenCx<'tcx> {
|
||||
fn type_kind(&self, ty: Self::Type) -> TypeKind {
|
||||
match self.lookup_type(ty) {
|
||||
SpirvType::Void => TypeKind::Void,
|
||||
SpirvType::Bool => TypeKind::Integer, // thanks llvm
|
||||
SpirvType::Bool | // thanks llvm
|
||||
SpirvType::Integer(_, _) => TypeKind::Integer,
|
||||
SpirvType::Float(width) => match width {
|
||||
16 => TypeKind::Half,
|
||||
|
@ -305,21 +305,23 @@ impl SpirvType {
|
||||
|
||||
pub fn sizeof<'tcx>(&self, cx: &CodegenCx<'tcx>) -> Option<Size> {
|
||||
let result = match *self {
|
||||
Self::Void => Size::ZERO,
|
||||
// Types that have a dynamic size, or no concept of size at all.
|
||||
Self::Void
|
||||
| Self::Opaque { .. }
|
||||
| Self::RuntimeArray { .. }
|
||||
| Self::Function { .. } => return None,
|
||||
|
||||
Self::Bool => Size::from_bytes(1),
|
||||
Self::Integer(width, _) => Size::from_bits(width),
|
||||
Self::Float(width) => Size::from_bits(width),
|
||||
Self::Adt { size, .. } => size?,
|
||||
Self::Opaque { .. } => Size::ZERO,
|
||||
Self::Vector { element, count } => {
|
||||
cx.lookup_type(element).sizeof(cx)? * count.next_power_of_two() as u64
|
||||
}
|
||||
Self::Array { element, count } => {
|
||||
cx.lookup_type(element).sizeof(cx)? * cx.builder.lookup_const_u64(count).unwrap()
|
||||
}
|
||||
Self::RuntimeArray { .. } => return None,
|
||||
Self::Pointer { .. } => cx.tcx.data_layout.pointer_size,
|
||||
Self::Function { .. } => cx.tcx.data_layout.pointer_size,
|
||||
Self::Image { .. } => Size::from_bytes(4),
|
||||
Self::Sampler => Size::from_bytes(4),
|
||||
Self::SampledImage { .. } => Size::from_bytes(4),
|
||||
@ -329,12 +331,15 @@ impl SpirvType {
|
||||
|
||||
pub fn alignof<'tcx>(&self, cx: &CodegenCx<'tcx>) -> Align {
|
||||
match *self {
|
||||
Self::Void => Align::from_bytes(0).unwrap(),
|
||||
// Types that have no concept of size or alignment.
|
||||
Self::Void | Self::Opaque { .. } | Self::Function { .. } => {
|
||||
Align::from_bytes(0).unwrap()
|
||||
}
|
||||
|
||||
Self::Bool => Align::from_bytes(1).unwrap(),
|
||||
Self::Integer(width, _) => Align::from_bits(width as u64).unwrap(),
|
||||
Self::Float(width) => Align::from_bits(width as u64).unwrap(),
|
||||
Self::Adt { align, .. } => align,
|
||||
Self::Opaque { .. } => Align::from_bytes(0).unwrap(),
|
||||
// Vectors have size==align
|
||||
Self::Vector { .. } => Align::from_bytes(
|
||||
self.sizeof(cx)
|
||||
@ -345,7 +350,6 @@ impl SpirvType {
|
||||
Self::Array { element, .. } => cx.lookup_type(element).alignof(cx),
|
||||
Self::RuntimeArray { element } => cx.lookup_type(element).alignof(cx),
|
||||
Self::Pointer { .. } => cx.tcx.data_layout.pointer_align.abi,
|
||||
Self::Function { .. } => cx.tcx.data_layout.pointer_align.abi,
|
||||
Self::Image { .. } => Align::from_bytes(4).unwrap(),
|
||||
Self::Sampler => Align::from_bytes(4).unwrap(),
|
||||
Self::SampledImage { .. } => Align::from_bytes(4).unwrap(),
|
||||
|
Loading…
Reference in New Issue
Block a user