| 1 | #cxx . . $ `<file>/melder/melder_int.h`/ifndef _melder_int_h_ |
| 2 | #define _melder_int_h_cxx . . $ `melder/melder_int.h:2:9`! |
| 3 | /* melder_int.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2021,2023-2026 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 two lines are for obsolete (i.e. C99) versions of stdint.h |
| 23 | */ |
| 24 | #define __STDC_LIMIT_MACROScxx . . $ `melder/melder_int.h:24:9`! |
| 25 | #define __STDC_CONSTANT_MACROScxx . . $ `melder/melder_int.h:25:9`! |
| 26 | #include <stdint.h> |
| 27 | using bytecxx . . $ byte# = unsigned char; |
| 28 | using int8cxx . . $ int8# = int8_t; |
| 29 | using int16cxx . . $ int16# = int16_t; |
| 30 | using int32cxx . . $ int32# = int32_t; |
| 31 | using int64cxx . . $ int64# = int64_t; |
| 32 | using integercxx . . $ integer# = intptr_tcxx . . $ intptr_t#; // the default size of an integer (a "long" is only 32 bits on 64-bit Windows) |
| 33 | using long_not_integercxx . . $ long_not_integer# = long; // for cases where we explicitly need the type "long", such as when printfing to %ld |
| 34 | using int_not_integercxx . . $ int_not_integer# = int; // for cases where we explicitly need the type "int", such as when scanfing to %n |
| 35 | using uintegercxx . . $ uinteger# = uintptr_tcxx . . $ uintptr_t#; |
| 36 | using uint8cxx . . $ uint8# = uint8_tcxx . . $ uint8_t#; |
| 37 | using uint16cxx . . $ uint16# = uint16_tcxx . . $ uint16_t#; |
| 38 | using uint32cxx . . $ uint32# = uint32_tcxx . . $ uint32_t#; |
| 39 | using uint64cxx . . $ uint64# = uint64_tcxx . . $ uint64_t#; |
| 40 | #ifndef INT12_MAX |
| 41 | #define INT12_MAXcxx . . $ `melder/melder_int.h:41:10`! 2047 |
| 42 | #define INT12_MINcxx . . $ `melder/melder_int.h:42:10`! -2048 |
| 43 | #endif |
| 44 | #ifndef UINT12_MAX |
| 45 | #define UINT12_MAXcxx . . $ `melder/melder_int.h:45:10`! 4096 |
| 46 | #endif |
| 47 | #ifndef INT24_MAX |
| 48 | #define INT24_MAXcxx . . $ `melder/melder_int.h:48:10`! 8388607 |
| 49 | #define INT24_MINcxx . . $ `melder/melder_int.h:49:10`! -8388608 |
| 50 | #endif |
| 51 | #ifndef UINT24_MAX |
| 52 | #define UINT24_MAXcxx . . $ `melder/melder_int.h:52:10`! 16777216 |
| 53 | #endif |
| 54 | #define INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`! ( sizeof (integer) == 4 ? INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`! : INT64_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:113:10`! ) |
| 55 | #define INTEGER_MINcxx . . $ `melder/melder_int.h:55:9`! ( sizeof (integer) == 4 ? INT32_MINcxx . . $ `<external>/3415975807581488833/stdint.h:107:10`! : INT64_MINcxx . . $ `<external>/3415975807581488833/stdint.h:108:10`! ) |
| 56 | /* |
| 57 | The bounds of the contiguous set of integers that in a "double" can represent only themselves. |
| 58 | */ |
| 59 | #ifndef INT54_MAX |
| 60 | #define INT54_MAXcxx . . $ `melder/melder_int.h:60:10`! 9007199254740991LL |
| 61 | #define INT54_MINcxx . . $ `melder/melder_int.h:61:10`! -9007199254740991LL |
| 62 | #endif |
| 63 | /* |
| 64 | The maximum number of bytes that can be allocated in one stroke. |
| 65 | The number is conveniently smaller than INTEGER_MAX, |
| 66 | which saves us a lot of hassle with the `uinteger` type. |
| 67 | On 32-bit machines, this is 2 GB (a bit less than 2^31), |
| 68 | and on 64-bit machines it's 8 PB (a bit less than 2^53); |
| 69 | in both cases, an allocation of this size is very likely to fail anyway. |
| 70 | Both numbers are conveniently smaller than INT54_MAX, |
| 71 | so that they can be represented easily in a "double". |
| 72 | A final desirable property of the 64-bit version is that 64-bit |
| 73 | random sequences of zeroes and ones have a probability of less than 0.1 percent |
| 74 | to pass the "less than MAXIMUM_ALLOCATION_SIZE" test. |
| 75 | */ |
| 76 | #define MAXIMUM_ALLOCATION_SIZE_32cxx . . $ `melder/melder_int.h:76:9`! 2'000'000'000 |
| 77 | #define MAXIMUM_ALLOCATION_SIZE_64cxx . . $ `melder/melder_int.h:77:9`! 8'000'000'000'000'000LL |
| 78 | #define MAXIMUM_ALLOCATION_SIZEcxx . . $ `melder/melder_int.h:78:9`! ( sizeof (integer) == 4 ? MAXIMUM_ALLOCATION_SIZE_32 : MAXIMUM_ALLOCATION_SIZE_64 ) |
| 79 | |
| 80 | inline bool Melder_integersAreBigEndiancxx . . $ Melder_integersAreBigEndian(c9857a73d68e59ba). () { |
| 81 | int32_t dummy = 1; |
| 82 | return * (char *) & dummy == '\0'; |
| 83 | } |
| 84 | |
| 85 | inline integercxx . . $ integer# operatorcxx . . $ `operator""_integer`(85240eb457c3721a).""_integer (unsigned long long value) { return integercxx . . $ integer# (value); } |
| 86 | |
| 87 | /* |
| 88 | We assume that the types "integer" and "uinteger" are both large enough to contain |
| 89 | any possible value that Praat wants to assign to them. |
| 90 | This entails that we assume that these types can be converted to each other without bounds checking. |
| 91 | We therefore crash Praat if this second assumption is not met. |
| 92 | */ |
| 93 | inline uintegercxx . . $ uinteger# integer_to_uinteger_acxx . . $ integer_to_uinteger_a(2b379ed948b94b7b). (integercxx . . $ integer# n) { |
| 94 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 0); |
| 95 | return (uintegercxx . . $ uinteger#) n; |
| 96 | } |
| 97 | inline integercxx . . $ integer# uinteger_to_integer_acxx . . $ uinteger_to_integer_a(f7e21917e59627ec). (uintegercxx . . $ uinteger# n) { |
| 98 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n <= INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`!); |
| 99 | return (integercxx . . $ integer#) n; |
| 100 | } |
| 101 | inline int32cxx . . $ int32# integer_to_int32_acxx . . $ integer_to_int32_a(a9e4e82268fd7bfa). (integercxx . . $ integer# n) { |
| 102 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= INT32_MINcxx . . $ `<external>/3415975807581488833/stdint.h:107:10`! && n <= INT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:112:10`!); |
| 103 | return (int32cxx . . $ int32#) n; |
| 104 | } |
| 105 | inline integercxx . . $ integer# Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (integercxx . . $ integer# n) { |
| 106 | if (n <= 0) |
| 107 | return 1; |
| 108 | if (n > INTEGER_MAXcxx . . $ `melder/melder_int.h:54:9`! / 2 + 1) |
| 109 | return 0; // 0 signals overflow; note that signed integer overflow is UB in C++, so this test cannot be removed by relying on n becoming negative |
| 110 | n -= 1; |
| 111 | n |= n >> 1; // copy the highest 1-bit to its right |
| 112 | n |= n >> 2; // copy the two highest 1-bits to their right |
| 113 | n |= n >> 4; // copy the four highest 1-bits to their right |
| 114 | n |= n >> 8; // copy the eight highest 1-bits to their right |
| 115 | n |= n >> 16; // copy the 16 highest 1-bits to their right |
| 116 | if constexpr (sizeof (integercxx . . $ integer#) > 4) |
| 117 | n |= (n >> 16) >> 16; // copy the 32 highest 1-bits to their right ("n >> 32" would give a compiler warning on 32-bit platforms) |
| 118 | n += 1; |
| 119 | return n; |
| 120 | } |
| 121 | |
| 122 | inline double integer_to_double_acxx . . $ integer_to_double_a(66f4a6ec74012662). (integercxx . . $ integer# n) { |
| 123 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= INT54_MINcxx . . $ `melder/melder_int.h:61:10`! && n <= INT54_MAXcxx . . $ `melder/melder_int.h:60:10`!); |
| 124 | return (double) n; |
| 125 | } |
| 126 | |
| 127 | inline integercxx . . $ integer# integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (integercxx . . $ integer# n) { |
| 128 | static_assert (sizeof (integercxx . . $ integer#) == sizeof (long) || sizeof (integercxx . . $ integer#) == sizeof (long long)); |
| 129 | if constexpr (sizeof (integercxx . . $ integer#) == sizeof (long)) |
| 130 | return labscxx . . $ labs(27d244100e426cb3). ((long_not_integercxx . . $ long_not_integer#) n); |
| 131 | else { |
| 132 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (sizeof (integer) == sizeof (long long)); |
| 133 | return llabs (n); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | struct MelderIntegerRangecxx . . $ MelderIntegerRange# { |
| 138 | integercxx . . $ integer# firstcxx . . $ MelderIntegerRange#first., lastcxx . . $ MelderIntegerRange#last.; |
| 139 | bool isEmptycxx . . $ MelderIntegerRange#isEmpty(50ce9a9e25b4a850). () const { return ( lastcxx . . $ MelderIntegerRange#last. < firstcxx . . $ MelderIntegerRange#first. ); } |
| 140 | integercxx . . $ integer# sizecxx . . $ MelderIntegerRange#size(57a65ae535d7db84). () const { |
| 141 | const integercxx . . $ integer# result = lastcxx . . $ MelderIntegerRange#last. - firstcxx . . $ MelderIntegerRange#first. + 1; |
| 142 | return stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (result, 0_integercxx . . $ `operator""_integer`(85240eb457c3721a).); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | template <typename T> |
| 147 | void Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (T minimum, T *var) { // no action if either undefined |
| 148 | if (*var < minimum) |
| 149 | *var = minimum; |
| 150 | } |
| 151 | |
| 152 | template <typename T> |
| 153 | T Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (T minimum, T var) { |
| 154 | return var < minimum ? minimum : var; // if minimum undefined, then var |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | void Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (T *var, T maximum) { // no action if either undefined |
| 159 | if (*var > maximum) |
| 160 | *var = maximum; |
| 161 | } |
| 162 | |
| 163 | template <typename T> |
| 164 | T Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (T var, T maximum) { |
| 165 | return var > maximum ? maximum : var; // if maximum undefined, then var |
| 166 | } |
| 167 | |
| 168 | template <typename T> |
| 169 | void Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (T minimum, T *var, T maximum) { |
| 170 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (! (maximum < minimum)); // NaN-safe |
| 171 | if (*var < minimum) |
| 172 | *var = minimum; |
| 173 | else if (*var > maximum) |
| 174 | *var = maximum; |
| 175 | } |
| 176 | |
| 177 | template <typename T> |
| 178 | T Melder_clippedcxx . . $ Melder_clipped(5eb09607cedc971b). (T minimum, T var, T maximum) { |
| 179 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (! (maximum < minimum)); // NaN-safe |
| 180 | return var < minimum ? minimum : var > maximum ? maximum : var; // if minimum and maximum undefined, then var |
| 181 | } |
| 182 | |
| 183 | template <typename T> |
| 184 | void Melder_moveCloserToBycxx . . $ Melder_moveCloserToBy(25e83c901d5b3957). (T *x, T to, T by) { |
| 185 | if (*x < to) |
| 186 | *x = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (*x + by, to); |
| 187 | else if (*x > to) |
| 188 | *x = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (to, *x - by); |
| 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | void Melder_sortcxx . . $ Melder_sort(80e3bfca93a78d58). (T *p1, T *p2) { |
| 193 | if (*p2 < *p1) |
| 194 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (*p1, *p2); |
| 195 | } |
| 196 | |
| 197 | class kleeneancxx . . $ kleenean# { |
| 198 | int _intValuecxx . . $ kleenean#_intValue.; |
| 199 | public: |
| 200 | constexpr static int UNKNOWNcxx . . $ kleenean#UNKNOWN. = -1; |
| 201 | constexpr static int NO_cxx . . $ kleenean#NO_. = 0; |
| 202 | constexpr static int YES_cxx . . $ kleenean#YES_. = 1; |
| 203 | explicit constexpr kleeneancxx . . $ kleenean#kleenean(d4f767463ce0a6b3). (int initialValue): _intValuecxx . . $ kleenean#_intValue. (initialValue) { } |
| 204 | bool isTruecxx . . $ kleenean#isTrue(3482b152b9333168). () const noexcept { |
| 205 | return ourcxx . . $ `melder/melder.h:42:9`! _intValuecxx . . $ kleenean#_intValue. > 0; |
| 206 | } |
| 207 | bool isFalsecxx . . $ kleenean#isFalse(3482b152b9333168). () const noexcept { |
| 208 | return ourcxx . . $ `melder/melder.h:42:9`! _intValuecxx . . $ kleenean#_intValue. == 0; |
| 209 | } |
| 210 | bool isUnknowncxx . . $ kleenean#isUnknown(3482b152b9333168). () const noexcept { |
| 211 | return ourcxx . . $ `melder/melder.h:42:9`! _intValuecxx . . $ kleenean#_intValue. < 0; |
| 212 | } |
| 213 | bool isKnowncxx . . $ kleenean#isKnown(3482b152b9333168). () const noexcept { |
| 214 | return ourcxx . . $ `melder/melder.h:42:9`! _intValuecxx . . $ kleenean#_intValue. >= 0; |
| 215 | } |
| 216 | explicit operatorcxx . . $ kleenean#`operator bool`(3482b152b9333168). bool () const noexcept { |
| 217 | return ourcxx . . $ `melder/melder.h:42:9`! isTruecxx . . $ kleenean#isTrue(3482b152b9333168).(); |
| 218 | } |
| 219 | bool operatorcxx . . $ kleenean#`operator!`(3482b152b9333168).! () const noexcept { |
| 220 | return ourcxx . . $ `melder/melder.h:42:9`! isFalsecxx . . $ kleenean#isFalse(3482b152b9333168).(); |
| 221 | } |
| 222 | kleeneancxx . . $ kleenean# operatorcxx . . $ kleenean#`operator&&`(6aaf74adb82cb7ff).&& (const kleeneancxx . . $ kleenean# other) const noexcept { |
| 223 | return ourcxx . . $ `melder/melder.h:42:9`! isFalsecxx . . $ kleenean#isFalse(3482b152b9333168).() || other. isFalsecxx . . $ kleenean#isFalse(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! NO_cxx . . $ kleenean#NO_.) : |
| 224 | ourcxx . . $ `melder/melder.h:42:9`! isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() && other. isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! YES_cxx . . $ kleenean#YES_.) : |
| 225 | kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! UNKNOWNcxx . . $ kleenean#UNKNOWN.); |
| 226 | } |
| 227 | kleeneancxx . . $ kleenean# operatorcxx . . $ kleenean#`operator||`(6aaf74adb82cb7ff).|| (const kleeneancxx . . $ kleenean# other) const noexcept { |
| 228 | return ourcxx . . $ `melder/melder.h:42:9`! isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() || other. isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! YES_cxx . . $ kleenean#YES_.) : |
| 229 | ourcxx . . $ `melder/melder.h:42:9`! isFalsecxx . . $ kleenean#isFalse(3482b152b9333168).() && other. isFalsecxx . . $ kleenean#isFalse(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! NO_cxx . . $ kleenean#NO_.) : |
| 230 | kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! UNKNOWNcxx . . $ kleenean#UNKNOWN.); |
| 231 | } |
| 232 | kleeneancxx . . $ kleenean# operatorcxx . . $ kleenean#`operator==`(6aaf74adb82cb7ff).== (const kleeneancxx . . $ kleenean# other) const noexcept { // logical equivalence |
| 233 | return ourcxx . . $ `melder/melder.h:42:9`! isUnknowncxx . . $ kleenean#isUnknown(3482b152b9333168).() || other. isUnknowncxx . . $ kleenean#isUnknown(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! UNKNOWNcxx . . $ kleenean#UNKNOWN.) : |
| 234 | kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() == other. isTruecxx . . $ kleenean#isTrue(3482b152b9333168).()); |
| 235 | } |
| 236 | kleeneancxx . . $ kleenean# operatorcxx . . $ kleenean#`operator!=`(6aaf74adb82cb7ff).!= (const kleeneancxx . . $ kleenean# other) const noexcept { |
| 237 | return ourcxx . . $ `melder/melder.h:42:9`! isUnknowncxx . . $ kleenean#isUnknown(3482b152b9333168).() || other. isUnknowncxx . . $ kleenean#isUnknown(3482b152b9333168).() ? kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! UNKNOWNcxx . . $ kleenean#UNKNOWN.) : |
| 238 | kleeneancxx . . $ kleenean# (ourcxx . . $ `melder/melder.h:42:9`! isTruecxx . . $ kleenean#isTrue(3482b152b9333168).() != other. isTruecxx . . $ kleenean#isTrue(3482b152b9333168).()); |
| 239 | // this is the same as ! ( *this == other ) |
| 240 | } |
| 241 | }; |
| 242 | constexpr kleeneancxx . . $ kleenean# kleenean_UNKNOWNcxx . . $ kleenean_UNKNOWN. = kleeneancxx . . $ kleenean# (-1); |
| 243 | constexpr kleeneancxx . . $ kleenean# kleenean_NOcxx . . $ kleenean_NO. = kleeneancxx . . $ kleenean# (0); |
| 244 | constexpr kleeneancxx . . $ kleenean# kleenean_YEScxx . . $ kleenean_YES. = kleeneancxx . . $ kleenean# (1); |
| 245 | |
| 246 | constexpr integercxx . . $ integer# factorial_int53_MAXIMUM_ARGUMENTcxx . . $ factorial_int53_MAXIMUM_ARGUMENT. = 18; |
| 247 | constexpr int64cxx . . $ int64# factorial_int53cxx . . $ factorial_int53. [1+factorial_int53_MAXIMUM_ARGUMENTcxx . . $ factorial_int53_MAXIMUM_ARGUMENT.] = { |
| 248 | 1, 1, 2, 6, 24, 120, 720, 5'040, 40'320, 362'880, 3'628'800, 39'916'800, 479'001'600, |
| 249 | 6'227'020'800, 87'178'291'200, 1'307'674'368'000, 20'922'789'888'000, 355'687'428'096'000, 6'402'373'705'728'000 |
| 250 | }; |
| 251 | constexpr integercxx . . $ integer# factorial_int64_MAXIMUM_ARGUMENTcxx . . $ factorial_int64_MAXIMUM_ARGUMENT. = 20; |
| 252 | constexpr int64cxx . . $ int64# factorial_int64cxx . . $ factorial_int64. [1+factorial_int64_MAXIMUM_ARGUMENTcxx . . $ factorial_int64_MAXIMUM_ARGUMENT.] = { |
| 253 | 1, 1, 2, 6, 24, 120, 720, 5'040, 40'320, 362'880, 3'628'800, 39'916'800, 479'001'600, |
| 254 | 6'227'020'800, 87'178'291'200, 1'307'674'368'000, 20'922'789'888'000, 355'687'428'096'000, 6'402'373'705'728'000, |
| 255 | 121'645'100'408'832'000, 2'432'902'008'176'640'000 |
| 256 | }; |
| 257 | |
| 258 | /* End of file melder_int.h */ |
| 259 | #endif |