Crafter.Math/tests/VectorF16.cpp
2026-03-24 03:48:56 +01:00

84 lines
No EOL
2.8 KiB
C++

/*
Crafter® Build
Copyright (C) 2026 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 3.0 as published by the Free Software Foundation;
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cmath>
import Crafter.Math;
import std;
using namespace Crafter;
template <std::uint32_t MaxSize, std::uint32_t Len, std::uint32_t Packing>
std::string* TestLoadStore() {
_Float16 floats[Len * Packing];
for (std::uint32_t i = 0; i < Len * Packing; i++) {
floats[i] = static_cast<_Float16>(i);
}
VectorF16<Len, Packing> vec(floats);
auto stored = vec.Store();
for (std::uint32_t i = 0; i < Len * Packing; i++) {
if (stored.v[i] != floats[i]) {
return new std::string(std::format("Load/Store mismatch at Len={} Packing={}, Expected: {}, Got: {}", Len, Packing, (float)(floats[i]), (float)stored.v[i]));
}
}
return nullptr;
}
template <std::uint32_t MaxSize, std::uint32_t Len, std::uint32_t Packing>
std::string* TestAdd() {
_Float16 floats[Len * Packing];
for (std::uint32_t i = 0; i < Len * Packing; i++) {
floats[i] = static_cast<_Float16>(i);
}
VectorF16<Len, Packing> vec(floats);
vec = vec + vec;
auto stored = vec.Store();
for (std::uint32_t i = 0; i < Len * Packing; i++) {
if (stored.v[i] != floats[i] + floats[i]) {
return new std::string(std::format("Add mismatch at Len={} Packing={}, Expected: {}, Got: {}", Len, Packing, (float)(floats[i] + floats[i]), (float)stored.v[i]));
}
}
return nullptr;
}
template <std::uint32_t MaxSize, std::uint32_t Len = 1, std::uint32_t Packing = 1>
std::string* TestAllCombinations() {
if constexpr (Len > MaxSize) {
return nullptr;
} else if constexpr (Len * Packing > MaxSize) {
return TestAllCombinations<MaxSize, Len + 1, 1>();
} else {
std::string* result = TestLoadStore<MaxSize, Len, Packing>();
if (result) return result;
result = TestAdd<MaxSize, Len, Packing>();
if (result) return result;
return TestAllCombinations<MaxSize, Len, Packing + 1>();
}
}
extern "C" {
std::string* RunTest() {
std::string* err = TestAllCombinations<VectorF16<1, 1>::MaxSize>();
if (err) {
return err;
}
return nullptr;
}
}