split test_hash_vector_macro into per-type functions (#2940)

* split `test_hash_vector_macro` into per-type functions

it went from a single 18367 instructions function, to eleven separate
functions with (1000 to 1600) instructions each
thats 15649 instructions total, 2718 less instructions overall.

```
$ build/c3c compile-test --suppress-run -O1 --print-large-functions -D
SLOW_TESTS test/unit/ 2>&1 | sort -n | grep "test_hash_vector"
    1041 instructions found in
std::core::builtins:test_hash_vector_int128
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1041 instructions found in
std::core::builtins:test_hash_vector_uint128
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1305 instructions found in std::core::builtins:test_hash_vector_long
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1305 instructions found in
std::core::builtins:test_hash_vector_ulong
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1453 instructions found in std::core::builtins:test_hash_vector_int
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1453 instructions found in std::core::builtins:test_hash_vector_uint
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1565 instructions found in
std::core::builtins:test_hash_vector_short
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1565 instructions found in
std::core::builtins:test_hash_vector_ushort
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1629 instructions found in std::core::builtins:test_hash_vector_char
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1629 instructions found in
std::core::builtins:test_hash_vector_ichar
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
    1663 instructions found in std::core::builtins:test_hash_vector_bool
(/home/mb/scripts/c3c/test/unit/stdlib/core/builtintests.c3) - function
is very long.
```

* made the `test_hash_vector_internal` generic
This commit is contained in:
Manu Linares
2026-02-16 22:30:30 -03:00
committed by GitHub
parent ec6ba8e7ca
commit 8cb23cff29

View File

@@ -174,61 +174,19 @@ fn void test_hash_repeat()
assert(int.typeid.hash() == int.typeid.hash());
}
macro test_hash_vector_macro(...)
fn void test_hash_vector() @test @if($feature(SLOW_TESTS))
{
int[] $lens = {1, 2, 4, 7, 8, 13, 16, 23, 32, 43, 64, 103};
$for var $i = 0; $i < $vacount ; $i++:
{
$foreach $vec_len : $lens:
{
$if $vec_len * $vatype[$i].sizeof * 8 <= $$MAX_VECTOR_SIZE:
{
$vatype[$i][<$vec_len>] vec1, vec2;
for (int val = 0; val < $vec_len; val++)
{
$vatype[$i] tval = ($vatype[$i])val;
vec1[val] = (tval | (tval << (($vatype[$i].sizeof - 1) * 8)));
}
vec2 = vec1;
assert(vec1.hash() == vec2.hash(), "hashes don't match for %s and %s", vec1, vec2);
for (int val = 0; val < $vec_len; val++)
{
vec2[val] ^= (($vatype[$i])0b0010_0000 << (($vatype[$i].sizeof - 1) * 8));
assert(vec1.hash() != vec2.hash(), "hashes match for %s and %s", vec1, vec2);
vec2[val] ^= (($vatype[$i])0b0010_0000 << (($vatype[$i].sizeof - 1) * 8));
}
}
$endif
}
$endforeach
}
$endfor
}
macro test_hash_vector_macro_bool()
{
int[] $lens = {1, 2, 4, 7, 8, 13, 16, 23, 32, 43, 64, 103};
$foreach $vec_len : $lens:
{
bool[<$vec_len>] vec1, vec2;
for (int val = 0; val < $vec_len; val++)
{
vec1[val] = ((val & 0x3) == 0);
}
vec2 = vec1;
assert(vec1.hash() == vec2.hash(), "hashes don't match for %s and %s", vec1, vec2);
for (int val = 0; val < $vec_len; val++)
{
vec2[val] = !vec2[val];
assert(vec1.hash() != vec2.hash(), "hashes match for %s and %s", vec1, vec2);
vec2[val] = !vec2[val];
}
}
$endforeach
}
fn void test_hash_vector() @if($feature(SLOW_TESTS))
{
test_hash_vector_macro(char, ichar, short, ushort, int, uint, long, ulong, int128, uint128);
test_hash_vector_macro_bool();
test_hash_vector_internal{char}();
test_hash_vector_internal{ichar}();
test_hash_vector_internal{short}();
test_hash_vector_internal{ushort}();
test_hash_vector_internal{int}();
test_hash_vector_internal{uint}();
test_hash_vector_internal{long}();
test_hash_vector_internal{ulong}();
test_hash_vector_internal{int128}();
test_hash_vector_internal{uint128}();
test_hash_vector_internal{bool}();
}
fn void test_builtin_string_hashing() => @pool()
@@ -291,3 +249,49 @@ fn void test_in()
$assert @in("love", "joy", "cheer", "love", "friend");
$assert !@in("hate", "joy", "cheer", "love", "friend");
}
module std::core::builtins;
fn void test_hash_vector_internal() <Type>
{
int[] $lens = {1, 2, 4, 7, 8, 13, 16, 23, 32, 43, 64, 103};
$foreach $vec_len : $lens:
{
$if $vec_len * Type.sizeof * 8 <= $$MAX_VECTOR_SIZE:
{
Type[<$vec_len>] vec1, vec2;
for (int val = 0; val < $vec_len; val++)
{
$if Type == bool:
vec1[val] = ((val & 0x3) == 0);
$else
{
Type tval = (Type)val;
vec1[val] = (tval | (tval << ((Type.sizeof - 1) * 8)));
}
$endif
}
vec2 = vec1;
assert(vec1.hash() == vec2.hash());
for (int val = 0; val < $vec_len; val++)
{
$if Type == bool:
vec2[val] = !vec2[val];
$else
vec2[val] ^= ((Type)0b0010_0000 << ((Type.sizeof - 1) * 8));
$endif
assert(vec1.hash() != vec2.hash());
$if Type == bool:
vec2[val] = !vec2[val];
$else
vec2[val] ^= ((Type)0b0010_0000 << ((Type.sizeof - 1) * 8));
$endif
}
}
$endif
}
$endforeach
}