mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
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:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user