Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
TestBasicDataType.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include <gtest/gtest.h>
8
10#include "arccore/base/String.h"
11#include "arccore/base/BFloat16.h"
12#include "arccore/base/Float16.h"
13
14#include <string>
15
16/*---------------------------------------------------------------------------*/
17/*---------------------------------------------------------------------------*/
18
19using namespace Arccore;
20
21namespace
22{
23void _check(eBasicDataType basic_type, int value, int nb_byte, const char* const_char_name)
24{
25 const String name = const_char_name;
26 ASSERT_EQ((int)basic_type, value);
27 ASSERT_EQ(basicDataTypeSize(basic_type), nb_byte);
28 String returned_name = basicDataTypeName(basic_type);
29 ASSERT_EQ(returned_name, name);
30 {
31 std::ostringstream ostr;
32 ostr << basic_type;
33 String s2(ostr.str());
34 ASSERT_EQ(name, s2);
35 }
36 {
37 std::string str(name.toStdStringView());
38 std::istringstream istr(str);
39 eBasicDataType expected_type = eBasicDataType::Unknown;
40 istr >> expected_type;
41 ASSERT_FALSE(istr.bad());
42 }
43 {
44 bool has_error = false;
45 eBasicDataType dt = basicDataTypeFromName(const_char_name, has_error);
46 ASSERT_FALSE(has_error);
47 ASSERT_EQ(basic_type, dt);
48 [[maybe_unused]] eBasicDataType dt2 = basicDataTypeFromName("Test1", has_error);
49 ASSERT_TRUE(has_error);
50 }
51 {
52 eBasicDataType dt = basicDataTypeFromName(const_char_name);
53 ASSERT_EQ(basic_type, dt);
54 }
55}
56
57} // namespace
58
59/*---------------------------------------------------------------------------*/
60/*---------------------------------------------------------------------------*/
61
62TEST(BasicDataType,Misc)
63{
64 ASSERT_EQ(NB_BASIC_DATA_TYPE,12);
65
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");
78}
79
80/*---------------------------------------------------------------------------*/
81/*---------------------------------------------------------------------------*/
82
83namespace
84{
85class RefValue
86{
87 public:
88
89 float original_value = 0.0;
90 uint16_t raw_value = 0;
91 float converted_value = 0.0;
92
93 public:
94
95 static void checkBFloat16(const RefValue& v)
96 {
97 checkConvertBFloat16(v);
98 checkDirectBFloat16(v);
99 }
100
101 static void checkConvertBFloat16(const RefValue& v)
102 {
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);
108 }
109
110 static void checkDirectBFloat16(const RefValue& v)
111 {
112 BFloat16 bf(v.original_value);
113 float cv = bf;
114 std::cout << "DirectBF16: V=" << v.original_value << " expected_cv=" << v.converted_value << "\n";
115 EXPECT_FLOAT_EQ(cv, v.converted_value);
116 }
117
118 static void checkFloat16(const RefValue& v)
119 {
120 checkConvertFloat16(v);
121 checkDirectFloat16(v);
122 }
123
124 static void checkConvertFloat16(const RefValue& v)
125 {
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);
131 }
132
133 static void checkDirectFloat16(const RefValue& v)
134 {
135 Float16 bf(v.original_value);
136 float cv = bf;
137 std::cout << "DirectF16: V=" << v.original_value << " expected_cv=" << v.converted_value << "\n";
138 EXPECT_FLOAT_EQ(cv, v.converted_value);
139 }
140};
141
142} // namespace
143
144TEST(BasicDataType, BFloat16)
145{
146 BFloat16 x(2.3f);
147 std::cout << "BF16_X=" << x << "\n";
148 x = 1.4f;
149 std::cout << "BF16_X=" << x << "\n";
150
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";
155
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 });
159
160 BFloat16 bf1(4.5f);
161 BFloat16 bf2(-1.2f);
162 BFloat16 bf3(4.5f);
163 ASSERT_EQ(bf3, bf1);
164 ASSERT_TRUE(bf2 < bf1);
165 ASSERT_FALSE(bf1 < bf2);
166}
167
168/*---------------------------------------------------------------------------*/
169/*---------------------------------------------------------------------------*/
170
171TEST(BasicDataType, Float16)
172{
173 Float16 x(2.3f);
174 std::cout << "F16_X=" << x << "\n";
175 x = 1.4f;
176 std::cout << "F16_X=" << x << "\n";
177
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";
182
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 });
186
187 Float16 bf1(4.5f);
188 Float16 bf2(-1.2f);
189 Float16 bf3(4.5f);
190 ASSERT_EQ(bf3, bf1);
191 ASSERT_TRUE(bf2 < bf1);
192 ASSERT_FALSE(bf1 < bf2);
193}
194
195/*---------------------------------------------------------------------------*/
196/*---------------------------------------------------------------------------*/
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
Definition Assertion.h:138
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Definition Assertion.h:126
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.
Definition ArcaneTypes.h:24
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.