xref: /petsc/src/sys/utils/mathinf.c (revision 55cda6f51c86a483abe5781578f3bbdf986e1625)
1 #if !defined(PETSC_SKIP_COMPLEX)
2   #define PETSC_SKIP_COMPLEX
3 #endif
4 
5 #include <petscsys.h>
6 /*@C
7   PetscIsNormalReal - Returns `PETSC_TRUE` if the input value satisfies `isnormal()`
8 
9   Input Parameter:
10 . a - the `PetscReal` Value
11 
12   Level: beginner
13 
14   Developer Notes:
15   Uses the C99 standard `isnormal()` on systems where they exist.
16 
17   Uses `isnormalq()` with `__float128`
18 
19   Otherwise always returns true
20 
21 .seealso: `PetscIsInfReal()`, `PetscIsNanReal()`
22 @*/
23 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
PetscIsNormalReal(PetscReal a)24 PetscBool PetscIsNormalReal(PetscReal a)
25 {
26   return PETSC_TRUE;
27 }
28 #elif defined(PETSC_HAVE_ISNORMAL)
PetscIsNormalReal(PetscReal a)29 PetscBool PetscIsNormalReal(PetscReal a)
30 {
31   return isnormal(a) ? PETSC_TRUE : PETSC_FALSE;
32 }
33 #else
PetscIsNormalReal(PetscReal a)34 PetscBool PetscIsNormalReal(PetscReal a)
35 {
36   return PETSC_TRUE;
37 }
38 #endif
39 
40 #if defined(PETSC_HAVE_NO_FINITE_MATH_ONLY)
41   #define PETSC_FORCE_NO_FINITE_MATH_ONLY __attribute__((optimize("no-finite-math-only")))
42 #else
43   #define PETSC_FORCE_NO_FINITE_MATH_ONLY
44 #endif
45 
46 /*@C
47   PetscIsInfReal - Returns whether the `PetscReal` input is an infinity value.
48 
49   Input Parameter:
50 . a - the floating point number
51 
52   Level: beginner
53 
54   Developer Notes:
55   Uses the C99 standard `isinf()` on systems where it exists.
56 
57   Otherwise uses (a && a/2 == a), note that some optimizing compilers compile out this form, thus removing the check.
58 
59 .seealso: `PetscIsNormalReal()`, `PetscIsNanReal()`
60 @*/
61 #if defined(PETSC_USE_REAL___FLOAT128)
PetscIsInfReal(PetscReal a)62 PetscBool PetscIsInfReal(PetscReal a)
63 {
64   return isinfq(a) ? PETSC_TRUE : PETSC_FALSE;
65 }
66 #elif defined(PETSC_HAVE_ISINF)
PetscIsInfReal(PetscReal a)67 PETSC_FORCE_NO_FINITE_MATH_ONLY PetscBool PetscIsInfReal(PetscReal a)
68 {
69   return isinf(a) ? PETSC_TRUE : PETSC_FALSE;
70 }
71 #elif defined(PETSC_HAVE__FINITE)
72   #if defined(PETSC_HAVE_FLOAT_H)
73     #include <float.h> /* Microsoft Windows defines _finite() in float.h */
74   #endif
75   #if defined(PETSC_HAVE_IEEEFP_H)
76     #include <ieeefp.h> /* Solaris prototypes these here */
77   #endif
PetscIsInfReal(PetscReal a)78 PetscBool PetscIsInfReal(PetscReal a)
79 {
80   return !_finite(a) ? PETSC_TRUE : PETSC_FALSE;
81 }
82 #else
PetscIsInfReal(PetscReal a)83 PetscBool PetscIsInfReal(PetscReal a)
84 {
85   return (a && a / 2 == a) ? PETSC_TRUE : PETSC_FALSE;
86 }
87 #endif
88 
89 /*@C
90   PetscIsNanReal - Returns whether the `PetscReal` input is a Not-a-Number (NaN) value.
91 
92   Input Parameter:
93 . a - the floating point number
94 
95   Level: beginner
96 
97   Developer Notes:
98   Uses the C99 standard `isnan()` on systems where it exists.
99 
100   Otherwise uses (a != a), note that some optimizing compilers compile
101   out this form, thus removing the check.
102 
103 .seealso: `PetscIsNormalReal()`, `PetscIsInfReal()`
104 @*/
105 #if defined(PETSC_USE_REAL___FLOAT128)
PetscIsNanReal(PetscReal a)106 PetscBool PetscIsNanReal(PetscReal a)
107 {
108   return isnanq(a) ? PETSC_TRUE : PETSC_FALSE;
109 }
110 #elif defined(PETSC_HAVE_ISNAN)
PetscIsNanReal(PetscReal a)111 PETSC_FORCE_NO_FINITE_MATH_ONLY PetscBool PetscIsNanReal(PetscReal a)
112 {
113   return isnan(a) ? PETSC_TRUE : PETSC_FALSE;
114 }
115 #elif defined(PETSC_HAVE__ISNAN)
116   #if defined(PETSC_HAVE_FLOAT_H)
117     #include <float.h> /* Microsoft Windows defines _isnan() in float.h */
118   #endif
119   #if defined(PETSC_HAVE_IEEEFP_H)
120     #include <ieeefp.h> /* Solaris prototypes these here */
121   #endif
PetscIsNanReal(PetscReal a)122 PetscBool PetscIsNanReal(PetscReal a)
123 {
124   return _isnan(a) ? PETSC_TRUE : PETSC_FALSE;
125 }
126 #else
PetscIsNanReal(PetscReal a)127 PetscBool PetscIsNanReal(PetscReal a)
128 {
129   return (a != a) ? PETSC_TRUE : PETSC_FALSE;
130 }
131 #endif
132