mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 03:51:18 +00:00
Add @intlog2 for Floored CT log-base2 (#2355)
* Add `@intlog2` for Floored CT log-base2 --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
@@ -344,6 +344,23 @@ macro log(x, base)
|
||||
*>
|
||||
macro log2(x) => $$log2(values::promote_int(x));
|
||||
|
||||
<*
|
||||
@require values::@is_int($x) : `The input value must be an integer.`
|
||||
@require $x >= 0 : `The input value must be a positive integer.`
|
||||
@return `A floored base-2 log of an input integer value.`
|
||||
*>
|
||||
macro @intlog2($x)
|
||||
{
|
||||
$if $x <= 1:
|
||||
return 0;
|
||||
$endif
|
||||
|
||||
$typeof($x) $z = 0;
|
||||
$for var $y = $x; $y > 0; $y >>= 1, ++$z: $endfor
|
||||
|
||||
return $z - 1;
|
||||
}
|
||||
|
||||
<*
|
||||
@require values::@is_promotable_to_floatlike(x) : `The input must be a number or a float vector`
|
||||
*>
|
||||
|
||||
Reference in New Issue
Block a user