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

FoR array holds encoded values as unsinged #401

Merged
merged 10 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 8 additions & 10 deletions vortex-scalar/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ impl Scalar {
"can't reinterpret cast between integers of two different widths"
);

match_each_native_ptype!(ptype, |$P| {
Self {
dtype: DType::Primitive(ptype, self.dtype.nullability()),
value: primitive
.pvalue
.map(|p| p.reinterpret_cast::<$P>())
.map(ScalarValue::Primitive)
.unwrap_or_else(|| ScalarValue::Null),
}
})
Self {
dtype: DType::Primitive(ptype, self.dtype.nullability()),
value: primitive
.pvalue
.map(|p| p.reinterpret_cast(ptype))
.map(ScalarValue::Primitive)
.unwrap_or_else(|| ScalarValue::Null),
}
}

pub fn zero<T: NativePType + Into<PValue>>(nullability: Nullability) -> Self {
Expand Down
61 changes: 49 additions & 12 deletions vortex-scalar/src/pvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;

use num_traits::NumCast;
use vortex_dtype::half::f16;
use vortex_dtype::{NativePType, PType};
use vortex_dtype::PType;
use vortex_error::vortex_err;
use vortex_error::VortexError;

Expand Down Expand Up @@ -38,28 +38,65 @@ impl PValue {
}
}

pub fn reinterpret_cast<T: NativePType + Into<PValue>>(&self) -> Self {
if T::PTYPE == self.ptype() {
pub fn reinterpret_cast(&self, ptype: PType) -> Self {
if ptype == self.ptype() {
return *self;
}

assert!(T::PTYPE.is_int(), "Can only reinterpret cast integers");
assert_eq!(
T::PTYPE.byte_width(),
ptype.byte_width(),
self.ptype().byte_width(),
"Cannot reinterpret cast between types of different widths"
);

match self {
robert3005 marked this conversation as resolved.
Show resolved Hide resolved
PValue::U8(v) => unsafe { mem::transmute::<u8, i8>(*v) }.into(),
PValue::U16(v) => unsafe { mem::transmute::<u16, i16>(*v) }.into(),
PValue::U32(v) => unsafe { mem::transmute::<u32, i32>(*v) }.into(),
PValue::U64(v) => unsafe { mem::transmute::<u64, i64>(*v) }.into(),
PValue::U16(v) => match ptype {
PType::I16 => unsafe { mem::transmute::<u16, i16>(*v) }.into(),
PType::F16 => f16::from_bits(*v).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::U32(v) => match ptype {
PType::I32 => unsafe { mem::transmute::<u32, i32>(*v) }.into(),
PType::F32 => f32::from_bits(*v).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::U64(v) => match ptype {
PType::I64 => unsafe { mem::transmute::<u64, i64>(*v) }.into(),
PType::F64 => f64::from_bits(*v).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::I8(v) => unsafe { mem::transmute::<i8, u8>(*v) }.into(),
PValue::I16(v) => unsafe { mem::transmute::<i16, u16>(*v) }.into(),
PValue::I32(v) => unsafe { mem::transmute::<i32, u32>(*v) }.into(),
PValue::I64(v) => unsafe { mem::transmute::<i64, u64>(*v) }.into(),
_ => unreachable!("Can't reinterpret cast floats"),
PValue::I16(v) => match ptype {
PType::U16 => unsafe { mem::transmute::<i16, u16>(*v) }.into(),
PType::F16 => f16::from_bits(unsafe { mem::transmute::<i16, u16>(*v) }).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::I32(v) => match ptype {
PType::U32 => unsafe { mem::transmute::<i32, u32>(*v) }.into(),
PType::F32 => f32::from_bits(unsafe { mem::transmute::<i32, u32>(*v) }).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::I64(v) => match ptype {
PType::U64 => unsafe { mem::transmute::<i64, u64>(*v) }.into(),
PType::F64 => f64::from_bits(unsafe { mem::transmute::<i64, u64>(*v) }).into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::F16(v) => match ptype {
PType::U16 => v.to_bits().into(),
PType::I16 => unsafe { mem::transmute::<u16, i16>(v.to_bits()) }.into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::F32(v) => match ptype {
PType::U32 => v.to_bits().into(),
PType::I32 => unsafe { mem::transmute::<u32, i32>(v.to_bits()) }.into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
PValue::F64(v) => match ptype {
PType::U64 => v.to_bits().into(),
PType::I64 => unsafe { mem::transmute::<u64, i64>(v.to_bits()) }.into(),
_ => unreachable!("Only same width type are allowed to be reinterpreted"),
},
}
}
}
Expand Down
Loading