| 1 | #ifndef _melder_real_h_ |
| 2 | #define _melder_real_h_ |
| 3 | /* melder_real.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2021,2023-2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | The following is checked in praat.h. |
| 23 | |
| 24 | Ideally, "long double" is 80 bits ("extended") precision (but stored in 96 or 128 bits), or even really 96 or 128 bits. |
| 25 | And ideally, long doubles should be fast. |
| 26 | |
| 27 | They *are* fast on Intel64 processors, which have hardware support for them. |
| 28 | But they are *very* slow (i.e. software-emulated) on ARM64 processors, which is why Xcode on the Mac and Clang on Windows |
| 29 | typically regard a "long double" as being 64 bits, i.e. identical to a "double". |
| 30 | On ARM64 Linux, Gcc regards "long double" as 128 bits, but this is very slow (more than 100 times slower than "double"), |
| 31 | so we give up and define "longdouble" as "double" on ARM64 Linux, just as was already the case on ARM64 Windows and ARM64 Mac. |
| 32 | |
| 33 | The condition could therefore be |
| 34 | #if defined (linux) && (defined (__aarch64__) || defined (_M_ARM64_)) |
| 35 | but just to make sure that no other Windows or Mac compilers happen to do 128-bit long double emulation, |
| 36 | we include Windows and Mac into our condition: |
| 37 | */ |
| 38 | #if defined (__aarch64__) || defined (_M_ARM64_) |
| 39 | using longdouble = double; |
| 40 | #else |
| 41 | using longdouble = long double; |
| 42 | #endif |
| 43 | |
| 44 | static const double undefined = (0.0/0.0); // NaN |
| 45 | |
| 46 | /* |
| 47 | isdefined() shall capture not only `undefined`, but all infinities and NaNs. |
| 48 | This can be done with a single test for the set bits in 0x7FF0'0000'0000'0000, |
| 49 | at least for 64-bit IEEE implementations. The correctness of this assumption is checked in sys/praat.cpp. |
| 50 | The portable version of isdefined() involves both isinf() and isnan(), or perhaps just isfinite(), |
| 51 | but that would be slower (as tested in fon/Praat_tests.cpp) |
| 52 | and it would also run into problems on some platforms when both <cmath> and <math.h> are included, |
| 53 | as in dwsys/NUMcomplex.cpp. |
| 54 | */ |
| 55 | //inline bool isdefined (double x) { return ! isinf (x) && ! isnan (x); } /* portable */ |
| 56 | //inline bool isdefined (double x) { return isfinite (x); } /* portable */ |
| 57 | inline bool isdefined (double x) { return ((* (uint64 *) & x) & 0x7FF0'0000'0000'0000) != 0x7FF0'0000'0000'0000; } |
| 58 | inline bool isundef (double x) { return ((* (uint64 *) & x) & 0x7FF0'0000'0000'0000) == 0x7FF0'0000'0000'0000; } |
| 59 | |
| 60 | template <typename T> |
| 61 | constexpr T sqr (T x) { |
| 62 | return x * x; |
| 63 | } |
| 64 | |
| 65 | struct MelderPoint { |
| 66 | double x = undefined, y = undefined; |
| 67 | }; |
| 68 | |
| 69 | struct MelderRealRange { |
| 70 | double min = undefined, max = undefined; |
| 71 | bool isundef () const { return ::isundef (min) || ::isundef (max); } |
| 72 | bool isEmpty () const { return ! (max > min); } // note edge case: will return true if min or max is NaN |
| 73 | //double size () { |
| 74 | // const double result = max - min; |
| 75 | // return std::max (result, 0.0); |
| 76 | //} |
| 77 | }; |
| 78 | |
| 79 | inline bool isundef (const MelderRealRange me) { |
| 80 | return isundef (me.min) || isundef (me.max); |
| 81 | } |
| 82 | |
| 83 | struct MelderExtremaWithInit { |
| 84 | double min = std::numeric_limits<double>::max(); |
| 85 | double max = std::numeric_limits<double>::lowest(); |
| 86 | void update (const double val) { |
| 87 | if (val < min) |
| 88 | min = val; |
| 89 | if (val > max) |
| 90 | max = val; |
| 91 | } |
| 92 | bool isValid () const { |
| 93 | return min <= max; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | struct MelderGaussianStats { |
| 98 | double mean = undefined, stdev = undefined; |
| 99 | }; |
| 100 | |
| 101 | struct MelderFraction { |
| 102 | double numerator = 0.0, denominator = 0.0; |
| 103 | double get () const { |
| 104 | return our denominator == 0.0 ? undefined : our numerator / our denominator; |
| 105 | } |
| 106 | bool isValid () const { |
| 107 | return our denominator != 0.0; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | struct MelderCountAndFraction { |
| 112 | integer count = 0; |
| 113 | double numerator = 0.0, denominator = 0.0; |
| 114 | double getFraction () const { |
| 115 | return our denominator == 0.0 ? undefined : our numerator / our denominator; |
| 116 | } |
| 117 | bool isValid () const { |
| 118 | return our denominator != 0.0; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | /* End of file melder_real.h */ |
| 123 | #endif |