use std::mem::MaybeUninit;
use std::slice;
use num_traits::CheckedMul;
use vortex_buffer::Alignment;
use vortex_buffer::BufferMut;
use vortex_buffer::ByteBuffer;
use vortex_buffer::ByteBufferMut;
use vortex_compute::lane_kernels::IndexedSourceExt;
use vortex_error::VortexExpect;
use vortex_mask::Mask;
use crate::dtype::DecimalDType;
use crate::dtype::DecimalType;
use crate::dtype::NativeDecimalType;
use crate::dtype::PType;
use crate::dtype::i256;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum IntegerType {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
I128,
I256,
}
impl IntegerType {
pub(crate) fn of_ptype(ptype: PType) -> Self {
match ptype {
PType::U8 => Self::U8,
PType::U16 => Self::U16,
PType::U32 => Self::U32,
PType::U64 => Self::U64,
PType::I8 => Self::I8,
PType::I16 => Self::I16,
PType::I32 => Self::I32,
PType::I64 => Self::I64,
PType::F16 | PType::F32 | PType::F64 => unreachable!("{ptype} is not an integer"),
}
}
pub(crate) fn of_decimal(decimal_type: DecimalType) -> Self {
match decimal_type {
DecimalType::I8 => Self::I8,
DecimalType::I16 => Self::I16,
DecimalType::I32 => Self::I32,
DecimalType::I64 => Self::I64,
DecimalType::I128 => Self::I128,
DecimalType::I256 => Self::I256,
}
}
fn byte_width(self) -> usize {
match self {
Self::U8 | Self::I8 => 1,
Self::U16 | Self::I16 => 2,
Self::U32 | Self::I32 => 4,
Self::U64 | Self::I64 => 8,
Self::I128 => 16,
Self::I256 => 32,
}
}
fn alignment(self) -> Alignment {
match_each_integer_type!(self, |T| { Alignment::of::<T>() })
}
pub(crate) fn range(self) -> (i256, i256) {
match_each_integer_type!(self, |T| { (T::MIN.to_i256(), T::MAX.to_i256()) })
}
pub(crate) fn value_at(self, values: &ByteBuffer, index: usize) -> i256 {
match_each_integer_type!(self, |T| { values_as::<T>(values)[index].to_i256() })
}
}
macro_rules! match_each_integer_type {
($ty:expr, | $T:ident | $body:block) => {{
match $ty {
IntegerType::U8 => {
type $T = u8;
$body
}
IntegerType::U16 => {
type $T = u16;
$body
}
IntegerType::U32 => {
type $T = u32;
$body
}
IntegerType::U64 => {
type $T = u64;
$body
}
IntegerType::I8 => {
type $T = i8;
$body
}
IntegerType::I16 => {
type $T = i16;
$body
}
IntegerType::I32 => {
type $T = i32;
$body
}
IntegerType::I64 => {
type $T = i64;
$body
}
IntegerType::I128 => {
type $T = i128;
$body
}
IntegerType::I256 => {
type $T = i256;
$body
}
}
}};
}
use match_each_integer_type;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Remainder {
Reject,
Truncate,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct IntegerCast {
from: IntegerType,
to: IntegerType,
scale_delta: i16,
remainder: Remainder,
min: i256,
max: i256,
}
#[derive(Clone, Copy, Debug)]
enum SourceCheck {
AllFit,
NoneFit,
Range {
min: i256,
max: i256,
divisor: Option<i256>,
},
}
#[derive(Clone, Copy, Debug)]
enum Rescale {
None,
Multiply(i256),
Divide(i256),
Zero,
}
impl IntegerCast {
pub(crate) fn new(
from: IntegerType,
to: IntegerType,
scale_delta: i16,
remainder: Remainder,
) -> Self {
let (min, max) = to.range();
Self {
from,
to,
scale_delta,
remainder,
min,
max,
}
}
pub(crate) fn to_decimal(
from: IntegerType,
from_scale: i8,
decimal_dtype: DecimalDType,
) -> Self {
let precision = usize::from(decimal_dtype.precision());
Self {
min: i256::MIN_BY_PRECISION[precision],
max: i256::MAX_BY_PRECISION[precision],
..Self::new(
from,
IntegerType::of_decimal(decimal_dtype.canonical_values_type()),
i16::from(decimal_dtype.scale()) - i16::from(from_scale),
Remainder::Reject,
)
}
}
pub(crate) fn source_type(&self) -> IntegerType {
self.from
}
fn rescale(&self) -> Rescale {
let factor = i256::from_i128(10).checked_pow(u32::from(self.scale_delta.unsigned_abs()));
match (self.scale_delta.signum(), factor) {
(0, _) => Rescale::None,
(_, None) => Rescale::Zero,
(1, Some(factor)) => Rescale::Multiply(factor),
(_, Some(factor)) => Rescale::Divide(factor),
}
}
fn source_bounds(&self) -> (i256, i256, Option<i256>) {
match (self.rescale(), self.remainder) {
(Rescale::None, _) => (self.min, self.max, None),
(Rescale::Multiply(factor), _) => (self.min / factor, self.max / factor, None),
(Rescale::Divide(factor), remainder) => (
self.min.checked_mul(&factor).unwrap_or(i256::MIN),
self.max.checked_mul(&factor).unwrap_or(i256::MAX),
(remainder == Remainder::Reject).then_some(factor),
),
(Rescale::Zero, _) => (i256::ZERO, i256::ZERO, None),
}
}
fn source_check(&self) -> SourceCheck {
let (mut min, mut max, mut divisor) = self.source_bounds();
let (from_min, from_max) = self.from.range();
if divisor.is_some_and(|d| d > from_max) {
(min, max, divisor) = (min.max(i256::ZERO), max.min(i256::ZERO), None);
}
if divisor.is_none() && min <= from_min && max >= from_max {
return SourceCheck::AllFit;
}
if min > max || max < from_min || min > from_max {
return SourceCheck::NoneFit;
}
SourceCheck::Range {
min: min.max(from_min),
max: max.min(from_max),
divisor,
}
}
pub(crate) fn first_misfit(&self, values: &ByteBuffer, mask: &Mask) -> Option<usize> {
let (min, max, divisor) = match self.source_check() {
SourceCheck::AllFit => return None,
SourceCheck::NoneFit => return mask.first(),
SourceCheck::Range { min, max, divisor } => (min, max, divisor),
};
match_each_integer_type!(self.from, |T| {
first_outside::<T>(values_as(values), mask, min, max, divisor)
})
}
pub(crate) fn cast(&self, values: &ByteBuffer, mask: &Mask) -> Result<ByteBuffer, usize> {
if let SourceCheck::Range {
min,
max,
divisor: Some(divisor),
} = self.source_check()
{
let quotients = match_each_integer_type!(self.from, |T| {
divide_exactly::<T>(values_as(values), mask, min, max, divisor)
})?;
return Ok(resize("ients, self.from, self.to));
}
if let Rescale::Multiply(factor) = self.rescale()
&& self.from.byte_width() == self.to.byte_width()
{
let (min, max) = match self.source_check() {
SourceCheck::AllFit => self.from.range(),
SourceCheck::NoneFit => {
return mask
.first()
.map_or_else(|| Ok(resize(values, self.from, self.to)), Err);
}
SourceCheck::Range { min, max, .. } => (min, max),
};
return match_each_integer_type!(self.from, |T| {
multiply_within::<T>(values_as(values), mask, min, max, factor)
});
}
if let Some(index) = self.first_misfit(values, mask) {
return Err(index);
}
Ok(self.convert(values))
}
fn convert(&self, values: &ByteBuffer) -> ByteBuffer {
let len = values.len() / self.from.byte_width();
match self.rescale() {
Rescale::None => resize(values, self.from, self.to),
Rescale::Multiply(factor) => {
let mut values = resize(values, self.from, self.to).into_mut();
match_each_integer_type!(self.to, |T| {
multiply_in_place::<T>(values_as_mut(&mut values), factor);
});
values.freeze()
}
Rescale::Divide(factor) if factor <= self.from.range().1 => {
let quotients = match_each_integer_type!(self.from, |T| {
divide::<T>(values_as(values), T::from_i256(factor))
});
resize("ients, self.from, self.to)
}
Rescale::Divide(_) | Rescale::Zero => match_each_integer_type!(self.to, |T| {
BufferMut::<T>::zeroed(len).freeze().into_byte_buffer()
}),
}
}
}
fn first_outside<T: IntegerValue>(
values: &[T],
mask: &Mask,
min: i256,
max: i256,
divisor: Option<i256>,
) -> Option<usize> {
let (min, max) = (T::from_i256(min), T::from_i256(max));
match divisor {
None => first_misfit(values, mask, |v| (min <= v) & (v <= max)),
Some(divisor) => {
let divisor = T::from_i256(divisor);
first_misfit(values, mask, |v| {
(min <= v) & (v <= max) & (v.rem(divisor) == T::default())
})
}
}
}
fn first_misfit<T: Copy>(values: &[T], mask: &Mask, fits: impl Fn(T) -> bool) -> Option<usize> {
if all_valid_lanes(values, mask, &fits) {
return None;
}
first_misfit_index(values, mask, &fits)
}
#[cold]
#[inline(never)]
fn first_misfit_index<T: Copy>(
values: &[T],
mask: &Mask,
fits: &impl Fn(T) -> bool,
) -> Option<usize> {
values
.iter()
.enumerate()
.position(|(index, &v)| mask.value(index) && !fits(v))
}
pub(crate) fn all_valid_lanes<T: Copy>(
values: &[T],
mask: &Mask,
fits: impl Fn(T) -> bool,
) -> bool {
let all_lanes = || {
values
.chunks(64)
.all(|chunk| chunk.iter().fold(true, |all, &v| all & fits(v)))
};
match mask {
Mask::AllFalse(_) => true,
Mask::AllTrue(_) => all_lanes(),
Mask::Values(mask) => {
all_lanes()
|| values
.chunks(64)
.zip(mask.bit_buffer().chunks().iter_padded())
.all(|(chunk, valid)| {
let misfits = chunk.iter().enumerate().fold(0u64, |acc, (i, &v)| {
acc | (<u64 as From<bool>>::from(!fits(v)) << i)
});
misfits & valid == 0
})
}
}
}
fn all_valid_pairs<A: Copy, B: Copy>(
a: &[A],
b: &[B],
mask: &Mask,
fits: impl Fn(A, B) -> bool,
) -> bool {
let chunks = || a.chunks(64).zip(b.chunks(64));
let all_lanes =
|| chunks().all(|(a, b)| a.iter().zip(b).fold(true, |all, (&a, &b)| all & fits(a, b)));
match mask {
Mask::AllFalse(_) => true,
Mask::AllTrue(_) => all_lanes(),
Mask::Values(mask) => {
all_lanes()
|| chunks()
.zip(mask.bit_buffer().chunks().iter_padded())
.all(|((a, b), valid)| {
let misfits =
a.iter()
.zip(b)
.enumerate()
.fold(0u64, |acc, (i, (&a, &b))| {
acc | (<u64 as From<bool>>::from(!fits(a, b)) << i)
});
misfits & valid == 0
})
}
}
}
pub(crate) fn resize(values: &ByteBuffer, from: IntegerType, to: IntegerType) -> ByteBuffer {
let (from_width, to_width) = (from.byte_width(), to.byte_width());
if from_width == to_width {
return values.clone().aligned(to.alignment());
}
let signed = |width| match width {
1 => IntegerType::I8,
2 => IntegerType::I16,
4 => IntegerType::I32,
8 => IntegerType::I64,
16 => IntegerType::I128,
_ => IntegerType::I256,
};
let (from, to) = if from_width > to_width {
(signed(from_width), signed(to_width))
} else {
let unsigned = matches!(
from,
IntegerType::U8 | IntegerType::U16 | IntegerType::U32 | IntegerType::U64
);
let to = match to_width {
2 if unsigned => IntegerType::U16,
4 if unsigned => IntegerType::U32,
8 if unsigned => IntegerType::U64,
width => signed(width),
};
(from, to)
};
match_each_integer_type!(from, |A| {
match_each_integer_type!(to, |B| { convert_buffer::<A, B>(values_as(values)) })
})
}
fn convert_buffer<A: IntegerValue, B: IntegerValue>(src: &[A]) -> ByteBuffer {
let len = src.len();
let mut out = BufferMut::<B>::with_capacity(len);
convert::<A, B>(src, &mut out.spare_capacity_mut()[..len]);
unsafe { out.set_len(len) };
out.freeze().into_byte_buffer()
}
#[inline(never)]
fn convert<A: IntegerValue, B: IntegerValue>(src: &[A], dst: &mut [MaybeUninit<B>]) {
if A::IS_I256 || B::IS_I256 {
src.map_into(dst, |v| B::from_i256(v.to_i256()));
} else {
src.map_into(dst, |v| B::from_i128(v.to_i128()));
}
}
#[inline(never)]
fn multiply_in_place<T: IntegerValue>(values: &mut [T], factor: i256) {
let factor = T::from_i256(factor);
for v in values {
*v = v.wrapping_mul(factor);
}
}
#[inline(never)]
fn multiply_within<T: IntegerValue>(
values: &[T],
mask: &Mask,
min: i256,
max: i256,
factor: i256,
) -> Result<ByteBuffer, usize> {
let (min, max, factor) = (T::from_i256(min), T::from_i256(max), T::from_i256(factor));
let fits = |v: T| (min <= v) & (v <= max);
let len = values.len();
let mut out = BufferMut::<T>::with_capacity(len);
let mut all_fit = true;
for (src, dst) in values
.chunks(64)
.zip(out.spare_capacity_mut()[..len].chunks_mut(64))
{
let mut chunk_fits = true;
for (&v, d) in src.iter().zip(dst) {
chunk_fits &= fits(v);
d.write(v.wrapping_mul(factor));
}
all_fit &= chunk_fits;
}
unsafe { out.set_len(len) };
if all_fit || all_valid_lanes(values, mask, fits) {
return Ok(out.freeze().into_byte_buffer());
}
Err(first_misfit_index(values, mask, &fits).vortex_expect("some valid value does not fit"))
}
#[inline(never)]
fn divide_exactly<T: IntegerValue>(
values: &[T],
mask: &Mask,
min: i256,
max: i256,
divisor: i256,
) -> Result<ByteBuffer, usize> {
let (min, max, divisor) = (T::from_i256(min), T::from_i256(max), T::from_i256(divisor));
let quotients = divide(values, divisor);
let fits = |v: T, q: T| (min <= v) & (v <= max) & (q.wrapping_mul(divisor) == v);
if all_valid_pairs(values, values_as::<T>("ients), mask, fits) {
return Ok(quotients);
}
let fits = |v: T| (min <= v) & (v <= max) & (v.rem(divisor) == T::default());
Err(first_misfit_index(values, mask, &fits).vortex_expect("some valid value does not fit"))
}
#[inline(never)]
fn divide<T: IntegerValue>(values: &[T], divisor: T) -> ByteBuffer {
let len = values.len();
let mut out = BufferMut::<T>::with_capacity(len);
values.map_into(&mut out.spare_capacity_mut()[..len], |v| v.div(divisor));
unsafe { out.set_len(len) };
out.freeze().into_byte_buffer()
}
fn values_as<T: IntegerValue>(values: &ByteBuffer) -> &[T] {
unsafe { slice::from_raw_parts(values.as_ptr().cast::<T>(), values.len() / size_of::<T>()) }
}
fn values_as_mut<T: IntegerValue>(values: &mut ByteBufferMut) -> &mut [T] {
unsafe {
slice::from_raw_parts_mut(
values.as_mut_ptr().cast::<T>(),
values.len() / size_of::<T>(),
)
}
}
pub(crate) trait IntegerValue: Copy + Default + PartialOrd + 'static {
const IS_I256: bool = false;
fn to_i128(self) -> i128;
fn from_i128(value: i128) -> Self;
fn to_i256(self) -> i256;
fn from_i256(value: i256) -> Self;
fn wrapping_mul(self, factor: Self) -> Self;
fn div(self, divisor: Self) -> Self;
fn rem(self, divisor: Self) -> Self;
}
macro_rules! impl_native_integer_value {
($($T:ty),*) => {$(
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss, clippy::cast_lossless)]
impl IntegerValue for $T {
#[inline]
fn to_i128(self) -> i128 {
self as i128
}
#[inline]
fn from_i128(value: i128) -> Self {
value as $T
}
#[inline]
fn to_i256(self) -> i256 {
i256::from_parts(self as i128 as u128, if (self as i128) < 0 { -1 } else { 0 })
}
#[inline]
fn from_i256(value: i256) -> Self {
value.to_parts().0 as $T
}
#[inline]
fn wrapping_mul(self, factor: Self) -> Self {
<$T>::wrapping_mul(self, factor)
}
#[inline]
fn div(self, divisor: Self) -> Self {
self.wrapping_div(divisor)
}
#[inline]
fn rem(self, divisor: Self) -> Self {
self.wrapping_rem(divisor)
}
}
)*};
}
impl_native_integer_value!(u8, u16, u32, u64, i8, i16, i32, i64, i128);
impl IntegerValue for i256 {
const IS_I256: bool = true;
#[inline]
fn to_i128(self) -> i128 {
self.to_parts().0.cast_signed()
}
#[inline]
fn from_i128(value: i128) -> Self {
i256::from_i128(value)
}
#[inline]
fn to_i256(self) -> i256 {
self
}
#[inline]
fn from_i256(value: i256) -> Self {
value
}
#[inline]
fn wrapping_mul(self, factor: Self) -> Self {
i256::wrapping_mul(&self, factor)
}
#[inline]
fn div(self, divisor: Self) -> Self {
self / divisor
}
#[inline]
fn rem(self, divisor: Self) -> Self {
self % divisor
}
}