Move safe_mul_div macro and make it generic on integer types (#1334)

Move safe_mul_div macro and make it generic on integer types
This commit is contained in:
Lexi
2024-08-09 16:54:26 -04:00
committed by GitHub
parent d997445284
commit 696d39b922
3 changed files with 141 additions and 5 deletions

View File

@@ -1096,3 +1096,112 @@ macro overflow_mul_helper(x, y) @local
if ($$overflow_mul(x, y, &res)) return MathError.OVERFLOW?;
return res;
}
macro char char.mult_div(self, char mul, char div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro ichar ichar.mult_div(self, ichar mul, ichar div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro short short.mult_div(self, short mul, short div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro ushort ushort.mult_div(self, ushort mul, ushort div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro int int.mult_div(self, int mul, int div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro uint uint.mult_div(self, uint mul, uint div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro long long.mult_div(self, long mul, long div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro ulong ulong.mult_div(self, ulong mul, ulong div)
{
return mul * (self / div) + mul * (self % div) / div;
}
macro bool @is_same_vector_or_scalar(#vector_value, #vector_or_scalar) @private {
return (values::@is_vector(#vector_or_scalar) &&& values::@is_same_vector_type(#vector_value, #vector_or_scalar)) ||| values::@is_int(#vector_or_scalar);
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro char[<*>] char[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro ichar[<*>] ichar[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro short[<*>] short[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro ushort[<*>] ushort[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro int[<*>] int[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro uint[<*>] uint[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro long[<*>] long[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}
/**
* @require @is_same_vector_or_scalar(self, mul) `mul must be a vector of the same type as self, or be an integer scalar`
* @require @is_same_vector_or_scalar(self, div) `div must be a vector of the same type as self, or be an integer scalar`
*/
macro ulong[<*>] ulong[<*>].mult_div(self, mul, div) {
return ($typeof(self))mul * (self / ($typeof(self))div) + ($typeof(self))mul * (self % ($typeof(self))div) / ($typeof(self))div;
}