Geogram Version 1.10.2
A programming library of geometric algorithms
Loading...
Searching...
No Matches
numeric.h
Go to the documentation of this file.
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
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
60#define M_PI 3.14159265358979323846
61#endif
62
67
68namespace GEO {
69
73 enum Sign {
77 ZERO = 0,
80 };
81
82
92 template <class T>
93 inline Sign geo_cmp(const T& a, const T& b) {
94 return Sign((a > b) - (a < b));
95 }
96
97
110 template <class T>
111 inline Sign geo_sgn(const T& x) {
112 return geo_cmp(x, T(0));
113 }
114
122 namespace Numeric {
123
125 typedef void* pointer;
126
128 typedef int8_t int8;
129
131 typedef int16_t int16;
132
134 typedef int32_t int32;
135
137 typedef int64_t int64;
138
140 typedef uint8_t uint8;
141
143 typedef uint16_t uint16;
144
146 typedef uint32_t uint32;
147
149 typedef uint64_t uint64;
150
152 typedef float float32;
153
155 typedef double float64;
156
160 inline constexpr float32 max_float32() {
161 return std::numeric_limits<float32>::max();
162 }
163
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
177 inline constexpr float64 max_float64() {
178 return std::numeric_limits<float64>::max();
179 }
180
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
194 bool GEOGRAM_API is_nan(float32 x);
195
199 bool GEOGRAM_API is_nan(float64 x);
200
205 void GEOGRAM_API random_reset();
206
211 void GEOGRAM_API random_reset(int seed);
212
216 int32 GEOGRAM_API random_int32();
217
221 float32 GEOGRAM_API random_float32();
222
226 float64 GEOGRAM_API random_float64();
227
242 template <class T, bool is_numeric>
243 struct LimitsHelper : std::numeric_limits<T> {
244 };
245
253 template <class T>
254 struct LimitsHelper<T, true> : std::numeric_limits<T> {
256 static const size_t size = sizeof(T);
258 static const size_t numbits = 8 * sizeof(T);
259 };
260
270 template <class T>
271 struct Limits :
272 LimitsHelper<T, std::numeric_limits<T>::is_specialized> {
273 };
274
279 template <class T> inline void optimize_number_representation(T& x) {
280 geo_argused(x);
281 }
282
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
312 template <class T>
313 inline T geo_sqr(T x) {
314 return x * x;
315 }
316
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
342
346 inline constexpr index_t max_index_t() {
347 return std::numeric_limits<index_t>::max();
348 }
349
356
361 return std::numeric_limits<signed_index_t>::max();
362 }
363
368 return std::numeric_limits<signed_index_t>::min();
369 }
370
376
380 inline double round(double x) {
381 return ((x - floor(x)) > 0.5 ? ceil(x) : floor(x));
382 }
383
384 /************************************************************************/
385
391 static constexpr index_t NO_INDEX = index_t(-1);
392
393 /************************************************************************/
394
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
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
427namespace {
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 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 geo_assert(!fp_contraction_enabled());
448#endif
449 }
450 static bool fp_contraction_enabled() {
451 return (a2plusb(0x1.0000002p0, -0x1.0000004p0) != 0.0);
452 }
453 __attribute__((noipa)) static double a2plusb(double a, double b) {
454 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
Assertion checking mechanism.
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:155
unsigned char geo_coord_index_t
Represents dimension (e.g. 3 for 3d, 4 for 4d ...).
Definition defs.h:105
unsigned int geo_index_t
Represents indices.
Definition defs.h:133
int geo_signed_index_t
Represents possibly negative indices.
Definition defs.h:139
Common include file, providing basic definitions. Should be included before anything else by all head...
Defines numeric types used in Vorpaline.
Definition numeric.h:122
float float32
Definition numeric.h:152
constexpr float32 max_float32()
Gets 32 bits float maximum positive value.
Definition numeric.h:160
uint8_t uint8
Definition numeric.h:140
uint64_t uint64
Definition numeric.h:149
int8_t int8
Definition numeric.h:128
double float64
Definition numeric.h:155
int32_t int32
Definition numeric.h:134
void * pointer
Definition numeric.h:125
constexpr float64 min_float64()
Gets 64 bits float minimum negative value.
Definition numeric.h:184
uint16_t uint16
Definition numeric.h:143
float32 random_float32()
Returns a 32 bits float between 0 and 1.
uint32_t uint32
Definition numeric.h:146
Sign ratio_compare(const T &a_num, const T &a_denom, const T &b_num, const T &b_denom)
Compares two rational numbers given as separate numerators and denominators.
Definition numeric.h:290
float64 random_float64()
Returns a 64 bits float between 0 and 1.
void optimize_number_representation(T &x)
place holder for optimizing internal number representation
Definition numeric.h:279
constexpr float64 max_float64()
Gets 64 bits float maximum positive value.
Definition numeric.h:177
void random_reset()
Resets the random number generator.
int32 random_int32()
Returns a 32 bits integer between 0 and RAND_MAX.
constexpr float32 min_float32()
Gets 32 bits float minimum negative value.
Definition numeric.h:167
bool is_nan(float32 x)
Checks whether a 32 bits float is "not a number".
int16_t int16
Definition numeric.h:131
int64_t int64
Definition numeric.h:137
Global Vorpaline namespace.
Definition basic.h:55
signed_index_t min_signed_index_t()
Gets the minimum negative value of type signed_index_t.
Definition numeric.h:367
T geo_sqr(T x)
Gets the square value of a value.
Definition numeric.h:313
void geo_argused(const T &)
Suppresses compiler warnings about unused parameters.
Definition argused.h:60
geo_signed_index_t signed_index_t
The type for storing and manipulating indices differences.
Definition numeric.h:355
constexpr signed_index_t max_signed_index_t()
Gets the maximum positive value of type signed_index_t.
Definition numeric.h:360
void geo_clamp(T &x, T min, T max)
Clamps a value to a range.
Definition numeric.h:326
Sign geo_sgn(const T &x)
Gets the sign of a value.
Definition numeric.h:111
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:341
Sign
Integer constants that represent the sign of a value.
Definition numeric.h:73
@ ZERO
Definition numeric.h:77
@ NEGATIVE
Definition numeric.h:75
@ POSITIVE
Definition numeric.h:79
Sign geo_cmp(const T &a, const T &b)
Compares two values.
Definition numeric.h:93
double round(double x)
Definition numeric.h:380
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition numeric.h:375
constexpr index_t max_index_t()
Gets the maximum positive value of type index_t.
Definition numeric.h:346
Limits helper class that extends std::numeric_limits.
Definition numeric.h:243
Extends std::numeric_limits with additional information.
Definition numeric.h:272
type traits for scalars
Definition numeric.h:401