7#include <gtest/gtest.h>
10#include "arccore/base/String.h"
11#include "arccore/base/BFloat16.h"
12#include "arccore/base/Float16.h"
13#include "arccore/base/Float128.h"
14#include "arccore/base/Int128.h"
25void _check(
eBasicDataType basic_type,
int value,
int nb_byte,
const char* const_char_name)
27 const String name = const_char_name;
28 ASSERT_EQ((
int)basic_type, value);
31 ASSERT_EQ(returned_name, name);
33 std::ostringstream ostr;
39 std::string str(name.toStdStringView());
40 std::istringstream istr(str);
42 istr >> expected_type;
46 bool has_error =
false;
49 ASSERT_EQ(basic_type, dt);
55 ASSERT_EQ(basic_type, dt);
64TEST(BasicDataType,Misc)
68 _check(eBasicDataType::Unknown,0,0,
"Unknown");
69 _check(eBasicDataType::Byte,1,1,
"Byte");
70 _check(eBasicDataType::Float16,2,2,
"Float16");
71 _check(eBasicDataType::Float32,3,4,
"Float32");
72 _check(eBasicDataType::Float64,4,8,
"Float64");
73 _check(eBasicDataType::Float128,5,16,
"Float128");
74 _check(eBasicDataType::Int16,6,2,
"Int16");
75 _check(eBasicDataType::Int32,7,4,
"Int32");
76 _check(eBasicDataType::Int64,8,8,
"Int64");
77 _check(eBasicDataType::Int128,9,16,
"Int128");
78 _check(eBasicDataType::BFloat16,10,2,
"BFloat16");
79 _check(eBasicDataType::Int8,11,1,
"Int8");
91 float original_value = 0.0f;
92 uint16_t raw_value = 0;
93 float converted_value = 0.0f;
97 static void checkBFloat16(
const RefValue& v)
99 checkConvertBFloat16(v);
100 checkDirectBFloat16(v);
103 static void checkConvertBFloat16(
const RefValue& v)
105 uint16_t x = Arccore::impl::convertBFloat16ToUint16Impl(v.original_value);
106 float cv = Arccore::impl::convertToBFloat16Impl(x);
107 std::cout <<
"ConvertBF16: V=" << v.original_value <<
" expected_raw=" << v.raw_value <<
" expected_cv=" << v.converted_value <<
"\n";
108 EXPECT_FLOAT_EQ(x, v.raw_value);
109 EXPECT_FLOAT_EQ(cv, v.converted_value);
112 static void checkDirectBFloat16(
const RefValue& v)
116 std::cout <<
"DirectBF16: V=" << v.original_value <<
" expected_cv=" << v.converted_value <<
"\n";
117 EXPECT_FLOAT_EQ(cv, v.converted_value);
120 static void checkFloat16(
const RefValue& v)
122 checkConvertFloat16(v);
123 checkDirectFloat16(v);
126 static void checkConvertFloat16(
const RefValue& v)
128 uint16_t x = Arccore::impl::convertFloat16ToUint16Impl(v.original_value);
129 float cv = Arccore::impl::convertToFloat16Impl(x);
130 std::cout <<
"ConvertF16: V=" << v.original_value <<
" expected_raw=" << v.raw_value <<
" expected_cv=" << v.converted_value <<
"\n";
131 EXPECT_FLOAT_EQ(x, v.raw_value);
132 EXPECT_FLOAT_EQ(cv, v.converted_value);
135 static void checkDirectFloat16(
const RefValue& v)
139 std::cout <<
"DirectF16: V=" << v.original_value <<
" expected_cv=" << v.converted_value <<
"\n";
140 EXPECT_FLOAT_EQ(cv, v.converted_value);
149 std::cout <<
"BF16_X=" << x <<
"\n";
151 std::cout <<
"BF16_X=" << x <<
"\n";
153 uint16_t x2 = Arccore::impl::convertBFloat16ToUint16Impl(2.3f);
154 std::cout <<
"BF16_X2=" << x2 <<
"\n";
155 float x2_f = Arccore::impl::convertToBFloat16Impl(x2);
156 std::cout <<
"BF16_X2F=" << x2_f <<
"\n";
158 RefValue::checkBFloat16({ 2.3f, 16403, 2.296875f });
159 RefValue::checkBFloat16({ 1.4f, 16307, 1.3984375f });
160 RefValue::checkBFloat16({ -1.2e-2f, 48197, -0.012023926f });
176 std::cout <<
"F16_X=" << x <<
"\n";
178 std::cout <<
"F16_X=" << x <<
"\n";
180 uint16_t x2 = Arccore::impl::convertFloat16ToUint16Impl(2.3f);
181 std::cout <<
"F16_X2=" << x2 <<
"\n";
182 float x2_f = Arccore::impl::convertToFloat16Impl(x2);
183 std::cout <<
"F16_X2F=" << x2_f <<
"\n";
185 RefValue::checkFloat16({ 2.3f, 16538, 2.3007812f });
186 RefValue::checkFloat16({ 1.4f, 15770, 1.4003906f });
187 RefValue::checkFloat16({ -1.2e-2f, 41509, -0.012001038f });
197#pragma clang diagnostic push
198#pragma clang diagnostic ignored "-Wlanguage-extension-token"
199#pragma GCC diagnostic push
200#pragma GCC diagnostic ignored "-Wpedantic"
207 double c_as_double =
static_cast<double>(c);
208 std::cout <<
"F128=" << c_as_double <<
"\n";
212TEST(BasicDataType,
Int128)
217 std::cout <<
"I128=" <<
static_cast<int64_t
>(c) <<
"\n";
218 std::cout <<
"sizeof(Int128) = " <<
sizeof(
Int128) <<
"\n";
219 std::cout <<
"sizeof(intmax_t) = " <<
sizeof(intmax_t) <<
"\n";
220 ASSERT_EQ(
sizeof(
Int128), 16);
#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 sur 128 bits.
Type flottant demi-précision.
Type entier sur 128 bits.
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.