7#include <gtest/gtest.h>
10#include "arccore/base/String.h"
11#include "arccore/base/BFloat16.h"
12#include "arccore/base/Float16.h"
23void _check(
eBasicDataType basic_type,
int value,
int nb_byte,
const char* const_char_name)
25 const String name = const_char_name;
26 ASSERT_EQ((
int)basic_type, value);
29 ASSERT_EQ(returned_name, name);
31 std::ostringstream ostr;
37 std::string str(name.toStdStringView());
38 std::istringstream istr(str);
40 istr >> expected_type;
44 bool has_error =
false;
47 ASSERT_EQ(basic_type, dt);
53 ASSERT_EQ(basic_type, dt);
62TEST(BasicDataType,Misc)
66 _check(eBasicDataType::Unknown,0,0,
"Unknown");
67 _check(eBasicDataType::Byte,1,1,
"Byte");
68 _check(eBasicDataType::Float16,2,2,
"Float16");
69 _check(eBasicDataType::Float32,3,4,
"Float32");
70 _check(eBasicDataType::Float64,4,8,
"Float64");
71 _check(eBasicDataType::Float128,5,16,
"Float128");
72 _check(eBasicDataType::Int16,6,2,
"Int16");
73 _check(eBasicDataType::Int32,7,4,
"Int32");
74 _check(eBasicDataType::Int64,8,8,
"Int64");
75 _check(eBasicDataType::Int128,9,16,
"Int128");
76 _check(eBasicDataType::BFloat16,10,2,
"BFloat16");
77 _check(eBasicDataType::Int8,11,1,
"Int8");
89 float original_value = 0.0;
90 uint16_t raw_value = 0;
91 float converted_value = 0.0;
95 static void checkBFloat16(
const RefValue& v)
97 checkConvertBFloat16(v);
98 checkDirectBFloat16(v);
101 static void checkConvertBFloat16(
const RefValue& v)
103 uint16_t x = Arccore::impl::convertBFloat16ToUint16Impl(v.original_value);
104 float cv = Arccore::impl::convertToBFloat16Impl(x);
105 std::cout <<
"ConvertBF16: V=" << v.original_value <<
" expected_raw=" << v.raw_value <<
" expected_cv=" << v.converted_value <<
"\n";
106 EXPECT_FLOAT_EQ(x, v.raw_value);
107 EXPECT_FLOAT_EQ(cv, v.converted_value);
110 static void checkDirectBFloat16(
const RefValue& v)
114 std::cout <<
"DirectBF16: V=" << v.original_value <<
" expected_cv=" << v.converted_value <<
"\n";
115 EXPECT_FLOAT_EQ(cv, v.converted_value);
118 static void checkFloat16(
const RefValue& v)
120 checkConvertFloat16(v);
121 checkDirectFloat16(v);
124 static void checkConvertFloat16(
const RefValue& v)
126 uint16_t x = Arccore::impl::convertFloat16ToUint16Impl(v.original_value);
127 float cv = Arccore::impl::convertToFloat16Impl(x);
128 std::cout <<
"ConvertF16: V=" << v.original_value <<
" expected_raw=" << v.raw_value <<
" expected_cv=" << v.converted_value <<
"\n";
129 EXPECT_FLOAT_EQ(x, v.raw_value);
130 EXPECT_FLOAT_EQ(cv, v.converted_value);
133 static void checkDirectFloat16(
const RefValue& v)
137 std::cout <<
"DirectF16: V=" << v.original_value <<
" expected_cv=" << v.converted_value <<
"\n";
138 EXPECT_FLOAT_EQ(cv, v.converted_value);
147 std::cout <<
"BF16_X=" << x <<
"\n";
149 std::cout <<
"BF16_X=" << x <<
"\n";
151 uint16_t x2 = Arccore::impl::convertBFloat16ToUint16Impl(2.3f);
152 std::cout <<
"BF16_X2=" << x2 <<
"\n";
153 float x2_f = Arccore::impl::convertToBFloat16Impl(x2);
154 std::cout <<
"BF16_X2F=" << x2_f <<
"\n";
156 RefValue::checkBFloat16({ 2.3f, 16403, 2.296875f });
157 RefValue::checkBFloat16({ 1.4f, 16307, 1.3984375f });
158 RefValue::checkBFloat16({ -1.2e-2f, 48197, -0.012023926f });
174 std::cout <<
"F16_X=" << x <<
"\n";
176 std::cout <<
"F16_X=" << x <<
"\n";
178 uint16_t x2 = Arccore::impl::convertFloat16ToUint16Impl(2.3f);
179 std::cout <<
"F16_X2=" << x2 <<
"\n";
180 float x2_f = Arccore::impl::convertToFloat16Impl(x2);
181 std::cout <<
"F16_X2F=" << x2_f <<
"\n";
183 RefValue::checkFloat16({ 2.3f, 16538, 2.3007812f });
184 RefValue::checkFloat16({ 1.4f, 15770, 1.4003906f });
185 RefValue::checkFloat16({ -1.2e-2f, 41509, -0.012001038f });
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Fichier contenant les définitions des types de données basiques gérés par Arccore.
Type flottant demi-précision.
Chaîne de caractères unicode.
Espace de nom de Arccore.
eBasicDataType basicDataTypeFromName(const char *name, bool &has_error)
Trouve le type associé à name.
Integer basicDataTypeSize(eBasicDataType type)
Taille du type de donnée type.
const char * basicDataTypeName(eBasicDataType type)
Nom du type de donnée.
constexpr unsigned char NB_BASIC_DATA_TYPE
Nombre de types de base supportés.
eBasicDataType
Type d'une donnée de base.