8.2
general documentation
cs_debug.h
Go to the documentation of this file.
1 #ifndef CS_DEBUG_H
2 #define CS_DEBUG_H
3 
4 /*============================================================================
5  * Debug macros and utilities
6  *============================================================================*/
7 
8 /*
9  This file is part of code_saturne, a general-purpose CFD tool.
10 
11  Copyright (C) 1998-2024 EDF S.A.
12 
13  This program is free software; you can redistribute it and/or modify it under
14  the terms of the GNU General Public License as published by the Free Software
15  Foundation; either version 2 of the License, or (at your option) any later
16  version.
17 
18  This program is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  details.
22 
23  You should have received a copy of the GNU General Public License along with
24  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
25  Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27 
28 /*----------------------------------------------------------------------------*/
29 
30 #if defined(HAVE_MPI)
31 #include <mpi.h>
32 #endif
33 
34 /*----------------------------------------------------------------------------
35  * Local headers
36  *----------------------------------------------------------------------------*/
37 
38 #include "cs_defs.h"
39 
40 /*----------------------------------------------------------------------------*/
41 
43 
44 /*=============================================================================
45  * Macro definitions
46  *============================================================================*/
47 
48 /*============================================================================
49  * Type definitions
50  *============================================================================*/
51 
52 /*=============================================================================
53  * Global variables
54  *============================================================================*/
55 
56 /*=============================================================================
57  * Public function prototypes
58  *============================================================================*/
59 
60 /*=============================================================================
61  * Templated function definitions
62  *============================================================================*/
63 
64 #ifdef __cplusplus
65 
66 /* Compute the unit in the last place (ULP) */
67 template <class T>
68 typename std::enable_if<!std::numeric_limits<T>::is_integer, T>::type
69 cs_diff_ulp(T x,
70  T y)
71 {
72  // Since `epsilon()` is the gap size (ULP, unit in the last place)
73  // of floating-point numbers in interval [1, 2), we can scale it to
74  // the gap size in interval [2^e, 2^{e+1}), where `e` is the exponent
75  // of `x` and `y`.
76 
77  // If `x` and `y` have different gap sizes (which means they have
78  // different exponents), we take the smaller one. Taking the bigger
79  // one is also reasonable, I guess.
80  const T m = std::min(std::fabs(x), std::fabs(y));
81 
82  // Subnormal numbers have fixed exponent, which is `min_exponent - 1`.
83  const int exp = m < std::numeric_limits<T>::min()
84  ? std::numeric_limits<T>::min_exponent - 1
85  : std::ilogb(m);
86 
87  // We divide the absolute difference by
88  // the epsilon times the exponent (1 ulp)
89  return std::fabs(x - y) / std::ldexp(std::numeric_limits<T>::epsilon(), exp);
90 }
91 
92 #endif // __cplusplus
93 
94 /*----------------------------------------------------------------------------*/
95 
97 
98 #endif /* CS_DEBUG_H */
#define BEGIN_C_DECLS
Definition: cs_defs.h:528
#define END_C_DECLS
Definition: cs_defs.h:529