| 1 | #ifndef _melder_int_h_ |
| 2 | #define _melder_int_h_ |
| 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_MACROS |
| 25 | #define __STDC_CONSTANT_MACROS |
| 26 | #include <stdint.h> |
| 27 | using byte = unsigned char; |
| 28 | using int8 = int8_t; |
| 29 | using int16 = int16_t; |
| 30 | using int32 = int32_t; |
| 31 | using int64 = int64_t; |
| 32 | using integer = intptr_t; // the default size of an integer (a "long" is only 32 bits on 64-bit Windows) |
| 33 | using long_not_integer = long; // for cases where we explicitly need the type "long", such as when printfing to %ld |
| 34 | using int_not_integer = int; // for cases where we explicitly need the type "int", such as when scanfing to %n |
| 35 | using uinteger = uintptr_t; |
| 36 | using uint8 = uint8_t; |
| 37 | using uint16 = uint16_t; |
| 38 | using uint32 = uint32_t; |
| 39 | using uint64 = uint64_t; |
| 40 | #ifndef INT12_MAX |
| 41 | #define INT12_MAX 2047 |
| 42 | #define INT12_MIN -2048 |
| 43 | #endif |
| 44 | #ifndef UINT12_MAX |
| 45 | #define UINT12_MAX 4096 |
| 46 | #endif |
| 47 | #ifndef INT24_MAX |
| 48 | #define INT24_MAX 8388607 |
| 49 | #define INT24_MIN -8388608 |
| 50 | #endif |
| 51 | #ifndef UINT24_MAX |
| 52 | #define UINT24_MAX 16777216 |
| 53 | #endif |
| 54 | #define INTEGER_MAX ( sizeof (integer) == 4 ? INT32_MAX : INT64_MAX ) |
| 55 | #define INTEGER_MIN ( sizeof (integer) == 4 ? INT32_MIN : INT64_MIN ) |
| 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_MAX 9007199254740991LL |
| 61 | #define INT54_MIN -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_32 2'000'000'000 |
| 77 | #define MAXIMUM_ALLOCATION_SIZE_64 8'000'000'000'000'000LL |
| 78 | #define MAXIMUM_ALLOCATION_SIZE ( sizeof (integer) == 4 ? MAXIMUM_ALLOCATION_SIZE_32 : MAXIMUM_ALLOCATION_SIZE_64 ) |
| 79 | |
| 80 | inline bool Melder_integersAreBigEndian () { |
| 81 | int32_t dummy = 1; |
| 82 | return * (char *) & dummy == '\0'; |
| 83 | } |
| 84 | |
| 85 | inline integer operator""_integer (unsigned long long value) { return 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 uinteger integer_to_uinteger_a (integer n) { |
| 94 | Melder_assert (n >= 0); |
| 95 | return (uinteger) n; |
| 96 | } |
| 97 | inline integer uinteger_to_integer_a (uinteger n) { |
| 98 | Melder_assert (n <= INTEGER_MAX); |
| 99 | return (integer) n; |
| 100 | } |
| 101 | inline int32 integer_to_int32_a (integer n) { |
| 102 | Melder_assert (n >= INT32_MIN && n <= INT32_MAX); |
| 103 | return (int32) n; |
| 104 | } |
| 105 | inline integer Melder_iroundUpToPowerOfTwo (integer n) { |
| 106 | if (n <= 0) |
| 107 | return 1; |
| 108 | if (n > INTEGER_MAX / 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 (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_a (integer n) { |
| 123 | Melder_assert (n >= INT54_MIN && n <= INT54_MAX); |
| 124 | return (double) n; |
| 125 | } |
| 126 | |
| 127 | inline integer integer_abs (integer n) { |
| 128 | static_assert (sizeof (integer) == sizeof (long) || sizeof (integer) == sizeof (long long)); |
| 129 | if constexpr (sizeof (integer) == sizeof (long)) |
| 130 | return labs ((long_not_integer) n); |
| 131 | else { |
| 132 | Melder_assert (sizeof (integer) == sizeof (long long)); |
| 133 | return llabs (n); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | struct MelderIntegerRange { |
| 138 | integer first, last; |
| 139 | bool isEmpty () const { return ( last < first ); } |
| 140 | integer size () const { |
| 141 | const integer result = last - first + 1; |
| 142 | return std::max (result, 0_integer); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | template <typename T> |
| 147 | void Melder_clipLeft (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_clippedLeft (T minimum, T var) { |
| 154 | return var < minimum ? minimum : var; // if minimum undefined, then var |
| 155 | } |
| 156 | |
| 157 | template <typename T> |
| 158 | void Melder_clipRight (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_clippedRight (T var, T maximum) { |
| 165 | return var > maximum ? maximum : var; // if maximum undefined, then var |
| 166 | } |
| 167 | |
| 168 | template <typename T> |
| 169 | void Melder_clip (T minimum, T *var, T maximum) { |
| 170 | Melder_assert (! (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_clipped (T minimum, T var, T maximum) { |
| 179 | Melder_assert (! (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_moveCloserToBy (T *x, T to, T by) { |
| 185 | if (*x < to) |
| 186 | *x = Melder_clippedRight (*x + by, to); |
| 187 | else if (*x > to) |
| 188 | *x = Melder_clippedLeft (to, *x - by); |
| 189 | } |
| 190 | |
| 191 | template <typename T> |
| 192 | void Melder_sort (T *p1, T *p2) { |
| 193 | if (*p2 < *p1) |
| 194 | std::swap (*p1, *p2); |
| 195 | } |
| 196 | |
| 197 | class kleenean { |
| 198 | int _intValue; |
| 199 | public: |
| 200 | constexpr static int UNKNOWN = -1; |
| 201 | constexpr static int NO_ = 0; |
| 202 | constexpr static int YES_ = 1; |
| 203 | explicit constexpr kleenean (int initialValue): _intValue (initialValue) { } |
| 204 | bool isTrue () const noexcept { |
| 205 | return our _intValue > 0; |
| 206 | } |
| 207 | bool isFalse () const noexcept { |
| 208 | return our _intValue == 0; |
| 209 | } |
| 210 | bool isUnknown () const noexcept { |
| 211 | return our _intValue < 0; |
| 212 | } |
| 213 | bool isKnown () const noexcept { |
| 214 | return our _intValue >= 0; |
| 215 | } |
| 216 | explicit operator bool () const noexcept { |
| 217 | return our isTrue(); |
| 218 | } |
| 219 | bool operator! () const noexcept { |
| 220 | return our isFalse(); |
| 221 | } |
| 222 | kleenean operator&& (const kleenean other) const noexcept { |
| 223 | return our isFalse() || other. isFalse() ? kleenean (our NO_) : |
| 224 | our isTrue() && other. isTrue() ? kleenean (our YES_) : |
| 225 | kleenean (our UNKNOWN); |
| 226 | } |
| 227 | kleenean operator|| (const kleenean other) const noexcept { |
| 228 | return our isTrue() || other. isTrue() ? kleenean (our YES_) : |
| 229 | our isFalse() && other. isFalse() ? kleenean (our NO_) : |
| 230 | kleenean (our UNKNOWN); |
| 231 | } |
| 232 | kleenean operator== (const kleenean other) const noexcept { // logical equivalence |
| 233 | return our isUnknown() || other. isUnknown() ? kleenean (our UNKNOWN) : |
| 234 | kleenean (our isTrue() == other. isTrue()); |
| 235 | } |
| 236 | kleenean operator!= (const kleenean other) const noexcept { |
| 237 | return our isUnknown() || other. isUnknown() ? kleenean (our UNKNOWN) : |
| 238 | kleenean (our isTrue() != other. isTrue()); |
| 239 | // this is the same as ! ( *this == other ) |
| 240 | } |
| 241 | }; |
| 242 | constexpr kleenean kleenean_UNKNOWN = kleenean (-1); |
| 243 | constexpr kleenean kleenean_NO = kleenean (0); |
| 244 | constexpr kleenean kleenean_YES = kleenean (1); |
| 245 | |
| 246 | constexpr integer factorial_int53_MAXIMUM_ARGUMENT = 18; |
| 247 | constexpr int64 factorial_int53 [1+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 integer factorial_int64_MAXIMUM_ARGUMENT = 20; |
| 252 | constexpr int64 factorial_int64 [1+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 |