Add bitsizeof to Builtins (#2376)

* Add `bitsizeof` to Builtins

---------

Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
Zack Puhl
2025-08-06 07:55:39 -04:00
committed by GitHub
parent 6471728ee5
commit aae873c044
3 changed files with 24 additions and 0 deletions

View File

@@ -89,6 +89,10 @@ macro void @swap(#a, #b) @builtin
#b = temp;
}
macro usz bitsizeof($Type) @builtin @const => $Type.sizeof * 8u;
macro usz @bitsizeof(#expr) @builtin @const => $sizeof(#expr) * 8u;
<*
Convert an `any` type to a type, returning an failure if there is a type mismatch.

View File

@@ -6,6 +6,7 @@
- Support `alias foo = module std::io` module aliasing.
- Add compile-time `@intlog2` macro to math.
- Add compile-time `@clz` builtin. #2367
- Add `bitsizeof` macro builtins. #2376
### Fixes
- List.remove_at would incorrectly trigger ASAN.

View File

@@ -184,3 +184,22 @@ fn void test_ct_clz()
assert(@clz((uint128)0x87) == 120);
assert(@clz((char)(0x44 - 0x40)) == 5);
}
fn void test_bitsizeof()
{
assert(bitsizeof(uint128) == 128);
assert(bitsizeof(ulong) == 64);
assert(bitsizeof(int) == 32);
assert(bitsizeof(short) == 16);
assert(bitsizeof(char) == 8);
assert(bitsizeof(char[200]) == 1600);
// Checks that they may be converted
int x = bitsizeof(char);
int y = @bitsizeof(1);
assert(@bitsizeof((char)0x07) == 8);
assert(@bitsizeof(0x1000ul) == 64);
assert(@bitsizeof(0) == 32);
assert(@bitsizeof((char[*])"abcdefghi") == 72);
}