Arcane  v3.15.0.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-2024 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#include "arccore/base/Float128.h"
14#include "arccore/base/Int128.h"
15
16#include <string>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21using namespace Arccore;
22
23namespace
24{
25void _check(eBasicDataType basic_type, int value, int nb_byte, const char* const_char_name)
26{
27 const String name = const_char_name;
28 ASSERT_EQ((int)basic_type, value);
29 ASSERT_EQ(basicDataTypeSize(basic_type), nb_byte);
30 String returned_name = basicDataTypeName(basic_type);
31 ASSERT_EQ(returned_name, name);
32 {
33 std::ostringstream ostr;
34 ostr << basic_type;
35 String s2(ostr.str());
36 ASSERT_EQ(name, s2);
37 }
38 {
39 std::string str(name.toStdStringView());
40 std::istringstream istr(str);
41 eBasicDataType expected_type = eBasicDataType::Unknown;
42 istr >> expected_type;
43 ASSERT_FALSE(istr.bad());
44 }
45 {
46 bool has_error = false;
47 eBasicDataType dt = basicDataTypeFromName(const_char_name, has_error);
48 ASSERT_FALSE(has_error);
49 ASSERT_EQ(basic_type, dt);
50 [[maybe_unused]] eBasicDataType dt2 = basicDataTypeFromName("Test1", has_error);
51 ASSERT_TRUE(has_error);
52 }
53 {
54 eBasicDataType dt = basicDataTypeFromName(const_char_name);
55 ASSERT_EQ(basic_type, dt);
56 }
57}
58
59} // namespace
60
61/*---------------------------------------------------------------------------*/
62/*---------------------------------------------------------------------------*/
63
64TEST(BasicDataType,Misc)
65{
66 ASSERT_EQ(NB_BASIC_DATA_TYPE,12);
67
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");
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85namespace
86{
87class RefValue
88{
89 public:
90
91 float original_value = 0.0f;
92 uint16_t raw_value = 0;
93 float converted_value = 0.0f;
94
95 public:
96
97 static void checkBFloat16(const RefValue& v)
98 {
99 checkConvertBFloat16(v);
100 checkDirectBFloat16(v);
101 }
102
103 static void checkConvertBFloat16(const RefValue& v)
104 {
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);
110 }
111
112 static void checkDirectBFloat16(const RefValue& v)
113 {
114 BFloat16 bf(v.original_value);
115 float cv = bf;
116 std::cout << "DirectBF16: V=" << v.original_value << " expected_cv=" << v.converted_value << "\n";
117 EXPECT_FLOAT_EQ(cv, v.converted_value);
118 }
119
120 static void checkFloat16(const RefValue& v)
121 {
122 checkConvertFloat16(v);
123 checkDirectFloat16(v);
124 }
125
126 static void checkConvertFloat16(const RefValue& v)
127 {
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);
133 }
134
135 static void checkDirectFloat16(const RefValue& v)
136 {
137 Float16 bf(v.original_value);
138 float cv = bf;
139 std::cout << "DirectF16: V=" << v.original_value << " expected_cv=" << v.converted_value << "\n";
140 EXPECT_FLOAT_EQ(cv, v.converted_value);
141 }
142};
143
144} // namespace
145
146TEST(BasicDataType, BFloat16)
147{
148 BFloat16 x(2.3f);
149 std::cout << "BF16_X=" << x << "\n";
150 x = 1.4f;
151 std::cout << "BF16_X=" << x << "\n";
152
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";
157
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 });
161
162 BFloat16 bf1(4.5f);
163 BFloat16 bf2(-1.2f);
164 BFloat16 bf3(4.5f);
165 ASSERT_EQ(bf3, bf1);
166 ASSERT_TRUE(bf2 < bf1);
167 ASSERT_FALSE(bf1 < bf2);
168}
169
170/*---------------------------------------------------------------------------*/
171/*---------------------------------------------------------------------------*/
172
173TEST(BasicDataType, Float16)
174{
175 Float16 x(2.3f);
176 std::cout << "F16_X=" << x << "\n";
177 x = 1.4f;
178 std::cout << "F16_X=" << x << "\n";
179
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";
184
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 });
188
189 Float16 bf1(4.5f);
190 Float16 bf2(-1.2f);
191 Float16 bf3(4.5f);
192 ASSERT_EQ(bf3, bf1);
193 ASSERT_TRUE(bf2 < bf1);
194 ASSERT_FALSE(bf1 < bf2);
195}
196
197#pragma clang diagnostic push
198#pragma clang diagnostic ignored "-Wlanguage-extension-token"
199#pragma GCC diagnostic push
200#pragma GCC diagnostic ignored "-Wpedantic"
201
202TEST(BasicDataType, Float128)
203{
204 Float128 a = 1.0;
205 Float128 b = 2.0;
206 Float128 c = a + b;
207 double c_as_double = static_cast<double>(c);
208 std::cout << "F128=" << c_as_double << "\n";
209 ASSERT_EQ(sizeof(Float128), 16);
210}
211
212TEST(BasicDataType, Int128)
213{
214 Int128 a = 1;
215 Int128 b = 2;
216 Int128 c = a + b;
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);
221}
222
223/*---------------------------------------------------------------------------*/
224/*---------------------------------------------------------------------------*/
#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.