| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef GEOGRAM_BASIC_NUMERIC | ||
| 41 | #define GEOGRAM_BASIC_NUMERIC | ||
| 42 | |||
| 43 | #include <geogram/basic/common.h> | ||
| 44 | #include <geogram/basic/assert.h> | ||
| 45 | #include <geogram/basic/i387.h> | ||
| 46 | #include <cmath> | ||
| 47 | #include <float.h> | ||
| 48 | #include <limits.h> | ||
| 49 | #include <algorithm> // for std::min / std::max | ||
| 50 | #include <stdint.h> | ||
| 51 | #include <limits> | ||
| 52 | #include <type_traits> | ||
| 53 | #include <iostream> | ||
| 54 | #include <cstdlib> | ||
| 55 | |||
| 56 | #ifndef M_PI | ||
| 57 | /** | ||
| 58 | * \brief Value of the constant PI if not defined by the system | ||
| 59 | */ | ||
| 60 | #define M_PI 3.14159265358979323846 | ||
| 61 | #endif | ||
| 62 | |||
| 63 | /** | ||
| 64 | * \file geogram/basic/numeric.h | ||
| 65 | * \brief Types and functions for numbers manipulation | ||
| 66 | */ | ||
| 67 | |||
| 68 | namespace GEO { | ||
| 69 | |||
| 70 | /** | ||
| 71 | * \brief Integer constants that represent the sign of a value | ||
| 72 | */ | ||
| 73 | enum Sign { | ||
| 74 | /** Value is negative */ | ||
| 75 | NEGATIVE = -1, | ||
| 76 | /** Value is zero */ | ||
| 77 | ZERO = 0, | ||
| 78 | /** Value is positive */ | ||
| 79 | POSITIVE = 1 | ||
| 80 | }; | ||
| 81 | |||
| 82 | |||
| 83 | /** | ||
| 84 | * \brief Compares two values | ||
| 85 | * \param[in] a , b the two values to compare | ||
| 86 | * \tparam T the type of the value | ||
| 87 | * \retval POSITIVE if \p a is greater than \p b | ||
| 88 | * \retval ZERO if \p a is equal to \p b | ||
| 89 | * \retval NEGATIVE if \p a is smaller than \p b | ||
| 90 | * \see Sign | ||
| 91 | */ | ||
| 92 | template <class T> | ||
| 93 | inline Sign geo_cmp(const T& a, const T& b) { | ||
| 94 |
58/78✓ Branch 0 taken 834930 times.
✓ Branch 1 taken 9976780 times.
✓ Branch 2 taken 14259845 times.
✓ Branch 3 taken 38765501 times.
✓ Branch 4 taken 21460 times.
✓ Branch 5 taken 18400 times.
✓ Branch 6 taken 26612114 times.
✓ Branch 7 taken 78377386 times.
✓ Branch 8 taken 37803 times.
✓ Branch 9 taken 9883 times.
✓ Branch 10 taken 37803 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 42355 times.
✓ Branch 13 taken 680985 times.
✓ Branch 14 taken 32376 times.
✓ Branch 15 taken 9979 times.
✓ Branch 16 taken 32375 times.
✓ Branch 17 taken 3215 times.
✓ Branch 18 taken 49452 times.
✓ Branch 19 taken 118160 times.
✓ Branch 20 taken 40468 times.
✓ Branch 21 taken 9020 times.
✓ Branch 22 taken 5667 times.
✓ Branch 23 taken 34909 times.
✓ Branch 24 taken 1516 times.
✓ Branch 25 taken 4430 times.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✓ Branch 39 taken 179898 times.
✓ Branch 40 taken 855 times.
✓ Branch 41 taken 89313 times.
✓ Branch 42 taken 30381 times.
✓ Branch 43 taken 326 times.
✓ Branch 44 taken 30344 times.
✓ Branch 45 taken 29966 times.
✓ Branch 46 taken 30334 times.
✓ Branch 47 taken 59652 times.
✗ Branch 48 not taken.
✓ Branch 49 taken 144 times.
✓ Branch 50 taken 60 times.
✓ Branch 51 taken 84 times.
✓ Branch 52 taken 8 times.
✗ Branch 53 not taken.
✓ Branch 54 taken 16 times.
✗ Branch 55 not taken.
✓ Branch 56 taken 15 times.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✓ Branch 59 taken 80637 times.
✓ Branch 60 taken 72924 times.
✓ Branch 61 taken 7713 times.
✓ Branch 62 taken 15603 times.
✓ Branch 63 taken 20314 times.
✓ Branch 64 taken 10384 times.
✓ Branch 65 taken 141 times.
✓ Branch 66 taken 35164 times.
✓ Branch 67 taken 10677 times.
✗ Branch 68 not taken.
✓ Branch 69 taken 279520 times.
✓ Branch 70 taken 271431 times.
✓ Branch 71 taken 8089 times.
✓ Branch 72 taken 96912 times.
✓ Branch 73 taken 32953 times.
✓ Branch 74 taken 114073 times.
✓ Branch 75 taken 20264 times.
✓ Branch 76 taken 204580 times.
✓ Branch 77 taken 2450 times.
|
270047575 | return Sign((a > b) - (a < b)); |
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | /** | ||
| 99 | * \brief Gets the sign of a value | ||
| 100 | * \details Returns -1, 0, or 1 whether value \p x is resp. negative, zero | ||
| 101 | * or positive. The function uses operator<() and operator>() to compare | ||
| 102 | * the value to 0 (zero). The integer constant zero must make | ||
| 103 | * senses for the type of the value, or T must be constructible from | ||
| 104 | * integer constant zero. | ||
| 105 | * \param[in] x the value to test | ||
| 106 | * \tparam T the type of the value | ||
| 107 | * \return the sign of the value | ||
| 108 | * \see Sign | ||
| 109 | */ | ||
| 110 | template <class T> | ||
| 111 | inline Sign geo_sgn(const T& x) { | ||
| 112 | return geo_cmp(x, T(0)); | ||
| 113 | } | ||
| 114 | |||
| 115 | /** | ||
| 116 | * \brief Defines numeric types used in Vorpaline. | ||
| 117 | * \details | ||
| 118 | * These types names have the form (u)int<size> or float<size>, | ||
| 119 | * where the (optional) u denotes an unsigned type, | ||
| 120 | * and the size is in bits. | ||
| 121 | */ | ||
| 122 | namespace Numeric { | ||
| 123 | |||
| 124 | /** Generic pointer type */ | ||
| 125 | typedef void* pointer; | ||
| 126 | |||
| 127 | /** Integer type with a width of 8 bits */ | ||
| 128 | typedef int8_t int8; | ||
| 129 | |||
| 130 | /** Integer type with a width of 16 bits */ | ||
| 131 | typedef int16_t int16; | ||
| 132 | |||
| 133 | /** Integer type with a width of 32 bits */ | ||
| 134 | typedef int32_t int32; | ||
| 135 | |||
| 136 | /** Integer type with a width of 64 bits */ | ||
| 137 | typedef int64_t int64; | ||
| 138 | |||
| 139 | /** Unsigned integer type with a width of 8 bits */ | ||
| 140 | typedef uint8_t uint8; | ||
| 141 | |||
| 142 | /** Unsigned integer type with a width of 16 bits */ | ||
| 143 | typedef uint16_t uint16; | ||
| 144 | |||
| 145 | /** Unsigned integer type with a width of 32 bits */ | ||
| 146 | typedef uint32_t uint32; | ||
| 147 | |||
| 148 | /** Unsigned integer type with a width of 64 bits */ | ||
| 149 | typedef uint64_t uint64; | ||
| 150 | |||
| 151 | /** Floating point type with a width of 32 bits */ | ||
| 152 | typedef float float32; | ||
| 153 | |||
| 154 | /** Floating point type with a width of 64 bits */ | ||
| 155 | typedef double float64; | ||
| 156 | |||
| 157 | /** | ||
| 158 | * \brief Gets 32 bits float maximum positive value | ||
| 159 | */ | ||
| 160 | inline constexpr float32 max_float32() { | ||
| 161 | return std::numeric_limits<float32>::max(); | ||
| 162 | } | ||
| 163 | |||
| 164 | /** | ||
| 165 | * \brief Gets 32 bits float minimum negative value | ||
| 166 | */ | ||
| 167 | inline constexpr float32 min_float32() { | ||
| 168 | // Note: numeric_limits<>::min() is not | ||
| 169 | // what we want (it returns the smallest | ||
| 170 | // positive non-denormal). | ||
| 171 | return -max_float32(); | ||
| 172 | } | ||
| 173 | |||
| 174 | /** | ||
| 175 | * \brief Gets 64 bits float maximum positive value | ||
| 176 | */ | ||
| 177 | inline constexpr float64 max_float64() { | ||
| 178 | return std::numeric_limits<float64>::max(); | ||
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 182 | * \brief Gets 64 bits float minimum negative value | ||
| 183 | */ | ||
| 184 | inline constexpr float64 min_float64() { | ||
| 185 | // Note: numeric_limits<>::min() is not | ||
| 186 | // what we want (it returns the smallest | ||
| 187 | // positive non-denormal). | ||
| 188 | return -max_float64(); | ||
| 189 | } | ||
| 190 | |||
| 191 | /** | ||
| 192 | * \brief Checks whether a 32 bits float is "not a number" | ||
| 193 | */ | ||
| 194 | bool GEOGRAM_API is_nan(float32 x); | ||
| 195 | |||
| 196 | /** | ||
| 197 | * \brief Checks whether a 64 bits float is "not a number" | ||
| 198 | */ | ||
| 199 | bool GEOGRAM_API is_nan(float64 x); | ||
| 200 | |||
| 201 | /** | ||
| 202 | * \brief Resets the random number generator. | ||
| 203 | * \details Uses "algo:random_seed" | ||
| 204 | */ | ||
| 205 | void GEOGRAM_API random_reset(); | ||
| 206 | |||
| 207 | /** | ||
| 208 | * \brief Resets the random number generator. | ||
| 209 | * \param[in] seed the random seed or -1 to use default | ||
| 210 | */ | ||
| 211 | void GEOGRAM_API random_reset(int seed); | ||
| 212 | |||
| 213 | /** | ||
| 214 | * \brief Returns a 32 bits integer between 0 and RAND_MAX | ||
| 215 | */ | ||
| 216 | int32 GEOGRAM_API random_int32(); | ||
| 217 | |||
| 218 | /** | ||
| 219 | * \brief Returns a 32 bits float between 0 and 1 | ||
| 220 | */ | ||
| 221 | float32 GEOGRAM_API random_float32(); | ||
| 222 | |||
| 223 | /** | ||
| 224 | * \brief Returns a 64 bits float between 0 and 1 | ||
| 225 | */ | ||
| 226 | float64 GEOGRAM_API random_float64(); | ||
| 227 | |||
| 228 | /** | ||
| 229 | * \brief Limits helper class that extends std::numeric_limits | ||
| 230 | * \details LimitsHelper extends std::numeric_limits to provide | ||
| 231 | * additional information about numeric types \p T. | ||
| 232 | * Template parameter \p is_numeric receives the value \c | ||
| 233 | * std::numeric_limits<T>::is_specialized which is \c true for all | ||
| 234 | * numeric types and \c false for the other types. The template is | ||
| 235 | * specialized for \p is_numeric == \c true to define additional | ||
| 236 | * information. For non-numeric types, the default template does not | ||
| 237 | * define anything. | ||
| 238 | * \tparam T an object type | ||
| 239 | * \tparam is_numeric is true if type \p T is a numeric type, false | ||
| 240 | * otherwise. | ||
| 241 | */ | ||
| 242 | template <class T, bool is_numeric> | ||
| 243 | struct LimitsHelper : std::numeric_limits<T> { | ||
| 244 | }; | ||
| 245 | |||
| 246 | /** | ||
| 247 | * \brief Specialization of LimitsHelper for numeric types | ||
| 248 | * \details This specialization defines the following values: | ||
| 249 | * - size - the size of the numeric type in bytes | ||
| 250 | * - numbits - the size of the numeric type in bits | ||
| 251 | * \tparam T a numeric type | ||
| 252 | */ | ||
| 253 | template <class T> | ||
| 254 | struct LimitsHelper<T, true> : std::numeric_limits<T> { | ||
| 255 | /** The size of the numeric type in bytes */ | ||
| 256 | static const size_t size = sizeof(T); | ||
| 257 | /** The size of the numeric type in bits */ | ||
| 258 | static const size_t numbits = 8 * sizeof(T); | ||
| 259 | }; | ||
| 260 | |||
| 261 | /** | ||
| 262 | * \brief Extends std::numeric_limits with additional information | ||
| 263 | * \details Limits provides additional information about numeric types | ||
| 264 | * that are not available in std::numeric_limits: | ||
| 265 | * - size: the size of the numeric type in bytes | ||
| 266 | * - numbits: the size of the numeric type in bits | ||
| 267 | * These types are defined in the helper class LimitsHelper for | ||
| 268 | * numeric types only. They are not defined for non-numeric types. | ||
| 269 | */ | ||
| 270 | template <class T> | ||
| 271 | struct Limits : | ||
| 272 | LimitsHelper<T, std::numeric_limits<T>::is_specialized> { | ||
| 273 | }; | ||
| 274 | |||
| 275 | /** | ||
| 276 | * \brief place holder for optimizing internal number representation | ||
| 277 | * \details there are specializations for expansion_nt, rational_nt | ||
| 278 | */ | ||
| 279 | template <class T> inline void optimize_number_representation(T& x) { | ||
| 280 | geo_argused(x); | ||
| 281 | } | ||
| 282 | |||
| 283 | /** | ||
| 284 | * \brief Compares two rational numbers given as separate | ||
| 285 | * numerators and denominators. | ||
| 286 | * \param[in] a_num , a_denom defines a = \p a_num / \p a_denom | ||
| 287 | * \param[in] b_num , b_denom defines b = \p b_num / \p b_denom | ||
| 288 | * \return the sign of a - b | ||
| 289 | */ | ||
| 290 | template <class T> inline Sign ratio_compare( | ||
| 291 | const T& a_num, const T& a_denom, const T& b_num, const T& b_denom | ||
| 292 | ) { | ||
| 293 | if(a_denom == b_denom) { | ||
| 294 | return Sign(geo_cmp(a_num,b_num)*geo_sgn(a_denom)); | ||
| 295 | } | ||
| 296 | return Sign( | ||
| 297 | geo_cmp(a_num*b_denom, b_num*a_denom) * | ||
| 298 | geo_sgn(a_denom) * geo_sgn(b_denom) | ||
| 299 | ); | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | /************************************************************************/ | ||
| 304 | |||
| 305 | |||
| 306 | /** | ||
| 307 | * \brief Gets the square value of a value | ||
| 308 | * \param[in] x a value of type \p T | ||
| 309 | * \tparam T the type of the value | ||
| 310 | * \return the square value of \p x | ||
| 311 | */ | ||
| 312 | template <class T> | ||
| 313 | inline T geo_sqr(T x) { | ||
| 314 |
20/30✓ Branch 0 taken 21348620 times.
✓ Branch 1 taken 2435924 times.
✓ Branch 2 taken 20929163 times.
✓ Branch 3 taken 1952652 times.
✓ Branch 4 taken 407578 times.
✓ Branch 5 taken 4864 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 412442 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4864 times.
✓ Branch 10 taken 81 times.
✓ Branch 11 taken 112534 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 112615 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 112534 times.
✓ Branch 16 taken 81 times.
✓ Branch 17 taken 112534 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 112534 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 683 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 683 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 683 times.
✗ Branch 26 not taken.
✓ Branch 27 taken 683 times.
✗ Branch 28 not taken.
✓ Branch 29 taken 2781 times.
|
3892532546 | return x * x; |
| 315 | } | ||
| 316 | |||
| 317 | /** | ||
| 318 | * \brief Clamps a value to a range | ||
| 319 | * \details Clamps the value \p x to a range defined by \p min and \p max. | ||
| 320 | * This modifies the value of \p x directly. | ||
| 321 | * \param[in,out] x a value of type \p T | ||
| 322 | * \param[in] min the lower bound of the clamping range | ||
| 323 | * \param[in] max the upper bound of the clamping range | ||
| 324 | */ | ||
| 325 | template <class T> | ||
| 326 | inline void geo_clamp(T& x, T min, T max) { | ||
| 327 | ✗ | if(x < min) { | |
| 328 | ✗ | x = min; | |
| 329 | ✗ | } else if(x > max) { | |
| 330 | ✗ | x = max; | |
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | /** | ||
| 335 | * \brief The type for storing and manipulating indices. | ||
| 336 | * \internal | ||
| 337 | * Vorpaline uses 32 bit indices (can be changed to 64 bits | ||
| 338 | * if need be, but this will double memory consumption of | ||
| 339 | * all combinatorial data structures). | ||
| 340 | */ | ||
| 341 | typedef geo_index_t index_t; | ||
| 342 | |||
| 343 | /** | ||
| 344 | * \brief Gets the maximum positive value of type index_t. | ||
| 345 | */ | ||
| 346 | inline constexpr index_t max_index_t() { | ||
| 347 | return std::numeric_limits<index_t>::max(); | ||
| 348 | } | ||
| 349 | |||
| 350 | /** | ||
| 351 | * \brief The type for storing and manipulating indices differences. | ||
| 352 | * \details Can be negative (for instance to indicate special values like | ||
| 353 | * borders). | ||
| 354 | */ | ||
| 355 | typedef geo_signed_index_t signed_index_t; | ||
| 356 | |||
| 357 | /** | ||
| 358 | * \brief Gets the maximum positive value of type signed_index_t. | ||
| 359 | */ | ||
| 360 | inline constexpr signed_index_t max_signed_index_t() { | ||
| 361 | return std::numeric_limits<signed_index_t>::max(); | ||
| 362 | } | ||
| 363 | |||
| 364 | /** | ||
| 365 | * \brief Gets the minimum negative value of type signed_index_t. | ||
| 366 | */ | ||
| 367 | inline signed_index_t min_signed_index_t() { | ||
| 368 | return std::numeric_limits<signed_index_t>::min(); | ||
| 369 | } | ||
| 370 | |||
| 371 | /** | ||
| 372 | * \brief The type for storing coordinate indices, and iterating on | ||
| 373 | * the coordinates of a point. | ||
| 374 | */ | ||
| 375 | typedef geo_coord_index_t coord_index_t; | ||
| 376 | |||
| 377 | /** | ||
| 378 | * \TODOC | ||
| 379 | */ | ||
| 380 | 24 | inline double round(double x) { | |
| 381 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | return ((x - floor(x)) > 0.5 ? ceil(x) : floor(x)); |
| 382 | } | ||
| 383 | |||
| 384 | /************************************************************************/ | ||
| 385 | |||
| 386 | /** | ||
| 387 | * \brief The dummy index value. | ||
| 388 | * \details Used for instance on the border of a surface, where adjacent | ||
| 389 | * facets are set to NO_INDEX. | ||
| 390 | */ | ||
| 391 | static constexpr index_t NO_INDEX = index_t(-1); | ||
| 392 | |||
| 393 | /************************************************************************/ | ||
| 394 | |||
| 395 | /** | ||
| 396 | * \brief type traits for scalars | ||
| 397 | * \details all basic C++ arithmetic types plus expansion_nt, interval_nt, | ||
| 398 | * rational_nt and geogramplus exact_nt. | ||
| 399 | * Used to avoid ambiguous declarations in scalar * vector products. | ||
| 400 | */ | ||
| 401 | template <class T> struct is_scalar { | ||
| 402 | typedef typename std::is_arithmetic<T>::type type; | ||
| 403 | static constexpr bool value = std::is_arithmetic<T>::value; | ||
| 404 | }; | ||
| 405 | |||
| 406 | /************************************************************************/ | ||
| 407 | } | ||
| 408 | |||
| 409 | |||
| 410 | /************* Disable floating point contraction **************************/ | ||
| 411 | |||
| 412 | /** | ||
| 413 | * \brief add GEO_FP_CONTRACT_OFF at the beginning of files or functions | ||
| 414 | * where FMA operations should not be generated in place of a*b+c | ||
| 415 | * \details it is important for instance for geometric predicates, that | ||
| 416 | * rely on strict IEEE754 implementation of product and addition. | ||
| 417 | */ | ||
| 418 | |||
| 419 | #if defined(GOMGEN) | ||
| 420 | # define GEO_FP_CONTRACT_OFF(x) | ||
| 421 | #elif defined(__clang__) | ||
| 422 | # define GEO_FP_CONTRACT_OFF _Pragma("clang fp contract(off)") | ||
| 423 | #elif defined(_MSC_VER) | ||
| 424 | # define GEO_FP_CONTRACT_OFF _Pragma("fp_contract(off)") | ||
| 425 | #elif defined(__GNUC__) | ||
| 426 | |||
| 427 | namespace { | ||
| 428 | |||
| 429 | // GCC does not have any pragma to deactivate FMA generation, | ||
| 430 | // so instead we check that they are deactivated (by the command-line | ||
| 431 | // option -ffp-contract=off) and fire an assertion fail if it was not | ||
| 432 | // the case. If targeting (very old) x87, deactivates the old extended | ||
| 433 | // precision mode . | ||
| 434 | struct GeoAssertStrictIEEE754 { | ||
| 435 | 516 | GeoAssertStrictIEEE754() { | |
| 436 | #ifdef GEO_387 // disable (very old) 387 extended precision | ||
| 437 | geo_disable_387_extended_precision(); | ||
| 438 | #endif | ||
| 439 | #ifdef GEOGRAM_PSM | ||
| 440 | if(fp_contraction_enabled()) { | ||
| 441 | std::cerr << "Needs to be compiled with -ffp-contract-off " | ||
| 442 | << "(or no x87 extended precision)" | ||
| 443 | << std::endl; | ||
| 444 | abort(); | ||
| 445 | } | ||
| 446 | #else | ||
| 447 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 516 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
516 | geo_assert(!fp_contraction_enabled()); |
| 448 | #endif | ||
| 449 | 516 | } | |
| 450 | static bool fp_contraction_enabled() { | ||
| 451 | 516 | return (a2plusb(0x1.0000002p0, -0x1.0000004p0) != 0.0); | |
| 452 | } | ||
| 453 | 516 | __attribute__((noipa)) static double a2plusb(double a, double b) { | |
| 454 | 516 | return a * a + b; | |
| 455 | } | ||
| 456 | }; | ||
| 457 | } | ||
| 458 | |||
| 459 | # define GEO_FP_CONTRACT_OFF \ | ||
| 460 | static GeoAssertStrictIEEE754 CPP_CONCAT(assert_strict_ieee_754_,__LINE__); | ||
| 461 | #else | ||
| 462 | # define GEO_FP_CONTRACT_OFF _Pragma("STDC FP_CONTRACT OFF") | ||
| 463 | #endif | ||
| 464 | |||
| 465 | #endif | ||
| 466 |