BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
hdf5_types.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California, through
4 * Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_HDF5_TYPES_HPP
13#define BELFEM_HDF5_TYPES_HPP
14
15#ifdef BELFEM_HDF5
16#include <hdf5.h>
17#else
18namespace belfem
19{
20 typedef int hid_t;
21 typedef int herr_t;
22 typedef int hsize_t;
23}
24#endif
25
26#include <cstddef>
27#include <type_traits>
28
29#include "assert.hpp"
30
31namespace belfem
32{
33 namespace hdf5
34 {
35 template<typename T>
37 {
38#ifdef BELFEM_HDF5
39 BELFEM_ERROR( false, "Unsupported datatype");
40 return 0;
41#else
42 BELFEM_ERROR( false, "We are not linked against HDF5");
43 return 0 ;
44#endif
45 }
46
47// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48// datatype<T>() above is the MEMORY type: the native C type, with whatever
49// width and byte order this build happens to use. filetype<T>() below is the
50// ON-DISK type, pinned to an explicit width and to little endian, so that the
51// layout of a dataset is a property of the file format and not of the build
52// configuration that produced it.
53// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54
55 template<typename T>
57 {
58#ifdef BELFEM_HDF5
59 BELFEM_ERROR( false, "Unsupported datatype");
60 return 0;
61#else
62 BELFEM_ERROR( false, "We are not linked against HDF5");
63 return 0 ;
64#endif
65 }
66#ifdef BELFEM_HDF5
67// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68// chars
69// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
70 template<> inline
72 {
73 return H5T_NATIVE_CHAR;
74 }
75
76 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77
78 template<> inline
80 {
81 return H5T_NATIVE_SCHAR;
82 }
83
84// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85
86 template<> inline
88 {
89 return H5T_NATIVE_UCHAR;
90 }
91
92// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93// signed integers
94// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95
96 template<> inline
98 {
99 return H5T_NATIVE_SHORT;
100 }
101
102 template<> inline
104 {
105 return H5T_NATIVE_INT;
106 }
107
108 template<> inline
110 {
111 return H5T_NATIVE_LONG;
112 }
113
114 template<> inline
116 {
117 return H5T_NATIVE_LLONG;
118 }
119
120// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
121// unsigned integers
122// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123
124 template<> inline
126 {
127 return H5T_NATIVE_USHORT;
128 }
129
130 template<> inline
132 {
133 return H5T_NATIVE_UINT;
134 }
135
136 template<> inline
138 {
139 return H5T_NATIVE_ULONG;
140 }
141
142 template<> inline
144 {
145 return H5T_NATIVE_ULLONG;
146 }
147
148// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
149// floating point
150// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151
152 template<> inline
154 {
155 return H5T_NATIVE_FLOAT;
156 }
157
158 template<> inline
160 {
161 return H5T_NATIVE_DOUBLE;
162 }
163
164 template<> inline
166 {
167 return H5T_NATIVE_LDOUBLE;
168 }
169
170// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
171// bool
172// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173
174 template<> inline
176 {
177 return H5T_NATIVE_HBOOL;
178 }
179
180// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
181// fixed-width file types
182//
183// The integer cases dispatch on size and signedness rather than on the C type
184// name, because the name-to-width mapping is platform dependent: `long` is
185// 8 bytes under LP64 and 4 under LLP64, and plain `char` is signed on x86 but
186// unsigned on ARM. Dispatching on the property reproduces the native type
187// exactly on every platform; dispatching on the name would not.
188// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
189
190 inline hid_t
191 int_filetype( const std::size_t aSize, const bool aSigned )
192 {
193 switch ( aSize )
194 {
195 case 1 : return aSigned ? H5T_STD_I8LE : H5T_STD_U8LE ;
196 case 2 : return aSigned ? H5T_STD_I16LE : H5T_STD_U16LE ;
197 case 4 : return aSigned ? H5T_STD_I32LE : H5T_STD_U32LE ;
198 case 8 : return aSigned ? H5T_STD_I64LE : H5T_STD_U64LE ;
199 default :
200 {
201 BELFEM_ERROR( false,
202 "No fixed-width HDF5 file type for a %d-byte integer",
203 ( int ) aSize );
204 return 0 ;
205 }
206 }
207 }
208
209// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210// chars
211// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212
213 template<> inline hid_t filetype<char>()
214 { return int_filetype( sizeof( char ), std::is_signed< char >::value ); }
215
216 template<> inline hid_t filetype<signed char>()
217 { return int_filetype( sizeof( signed char ), true ); }
218
219 template<> inline hid_t filetype<unsigned char>()
220 { return int_filetype( sizeof( unsigned char ), false ); }
221
222// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
223// signed integers
224// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
225
226 template<> inline hid_t filetype<short>()
227 { return int_filetype( sizeof( short ), true ); }
228
229 template<> inline hid_t filetype<int>()
230 { return int_filetype( sizeof( int ), true ); }
231
232 template<> inline hid_t filetype<long>()
233 { return int_filetype( sizeof( long ), true ); }
234
235 template<> inline hid_t filetype<long long>()
236 { return int_filetype( sizeof( long long ), true ); }
237
238// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
239// unsigned integers
240// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
241
242 template<> inline hid_t filetype<unsigned short>()
243 { return int_filetype( sizeof( unsigned short ), false ); }
244
245 template<> inline hid_t filetype<unsigned int>()
246 { return int_filetype( sizeof( unsigned int ), false ); }
247
248 template<> inline hid_t filetype<unsigned long>()
249 { return int_filetype( sizeof( unsigned long ), false ); }
250
251 template<> inline hid_t filetype<unsigned long long>()
252 { return int_filetype( sizeof( unsigned long long ), false ); }
253
254// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
255// floating point
256// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257
258 template<> inline hid_t filetype<float>()
259 { return H5T_IEEE_F32LE; }
260
261 template<> inline hid_t filetype<double>()
262 { return H5T_IEEE_F64LE; }
263
264 // long double is the one type with no fixed-width IEEE equivalent: on
265 // x86 it is 16 bytes carrying 80 bits of precision, and H5T_IEEE_F64LE
266 // would silently truncate it. It therefore keeps the native layout.
267 // No writer instantiates it today ( `real` is `double` ).
268 template<> inline hid_t filetype<long double>()
269 { return H5T_NATIVE_LDOUBLE; }
270
271// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272// bool
273// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
274
275 // written through an hbool_t, so the file width follows that type
276 template<> inline hid_t filetype<bool>()
277 { return int_filetype( sizeof( hbool_t ), false ); }
278
279#endif
280 }
281// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
282}
283#endif // BELFEM_HDF5_TYPES_HPP
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
hid_t datatype()
Definition hdf5_types.hpp:36
hid_t filetype()
Definition hdf5_types.hpp:56
USER GUIDES:
Definition cl_Capacitor.cpp:16
int hsize_t
Definition hdf5_types.hpp:22
int hid_t
Definition hdf5_types.hpp:20
int herr_t
Definition hdf5_types.hpp:21