GCC Code Coverage Report


Directory: ./
File: basic/numeric.h
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 22 28 78.6%
Functions: 11 16 68.8%
Branches: 2 12 16.7%

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 298143825 inline Sign geo_cmp(const T& a, const T& b) {
94 298143825 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 298064935 inline Sign geo_sgn(const T& x) {
112 298064935 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 112836442 inline constexpr float64 max_float64() {
178 112836442 return std::numeric_limits<float64>::max();
179 }
180
181 /**
182 * \brief Gets 64 bits float minimum negative value
183 */
184 884958 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 884958 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 4492675577 inline T geo_sqr(T x) {
314 4492675577 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 1484 inline constexpr index_t max_index_t() {
347 1484 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 516 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 1 not taken.
✓ Branch 2 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 516 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