Graphite Version 3
An experimental 3D geometry processing program
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 <cmath>
46#include <float.h>
47#include <limits.h>
48#include <algorithm> // for std::min / std::max
49#include <stdint.h>
50#include <limits>
51#include <type_traits>
52#include <iostream>
53#include <cstdlib>
54
55#ifndef M_PI
59#define M_PI 3.14159265358979323846
60#endif
61
67namespace GEO {
68
72 enum Sign {
76 ZERO = 0,
78 POSITIVE = 1
79 };
80
81
91 template <class T>
92 inline Sign geo_cmp(const T& a, const T& b) {
93 return Sign((a > b) - (a < b));
94 }
95
96
109 template <class T>
110 inline Sign geo_sgn(const T& x) {
111 return geo_cmp(x, T(0));
112 }
113
121 namespace Numeric {
122
124 typedef void* pointer;
125
127 typedef int8_t int8;
128
130 typedef int16_t int16;
131
133 typedef int32_t int32;
134
136 typedef int64_t int64;
137
139 typedef uint8_t uint8;
140
142 typedef uint16_t uint16;
143
145 typedef uint32_t uint32;
146
148 typedef uint64_t uint64;
149
151 typedef float float32;
152
154 typedef double float64;
155
159 inline constexpr float32 max_float32() {
160 return std::numeric_limits<float32>::max();
161 }
162
166 inline constexpr float32 min_float32() {
167 // Note: numeric_limits<>::min() is not
168 // what we want (it returns the smallest
169 // positive non-denormal).
170 return -max_float32();
171 }
172
176 inline constexpr float64 max_float64() {
177 return std::numeric_limits<float64>::max();
178 }
179
183 inline constexpr float64 min_float64() {
184 // Note: numeric_limits<>::min() is not
185 // what we want (it returns the smallest
186 // positive non-denormal).
187 return -max_float64();
188 }
189
193 bool GEOGRAM_API is_nan(float32 x);
194
198 bool GEOGRAM_API is_nan(float64 x);
199
204 void GEOGRAM_API random_reset();
205
210 void GEOGRAM_API random_reset(int seed);
211
215 int32 GEOGRAM_API random_int32();
216
220 float32 GEOGRAM_API random_float32();
221
225 float64 GEOGRAM_API random_float64();
226
241 template <class T, bool is_numeric>
242 struct LimitsHelper : std::numeric_limits<T> {
243 };
244
252 template <class T>
253 struct LimitsHelper<T, true> : std::numeric_limits<T> {
255 static const size_t size = sizeof(T);
257 static const size_t numbits = 8 * sizeof(T);
258 };
259
269 template <class T>
270 struct Limits :
271 LimitsHelper<T, std::numeric_limits<T>::is_specialized> {
272 };
273
278 template <class T> inline void optimize_number_representation(T& x) {
279 geo_argused(x);
280 }
281
289 template <class T> inline Sign ratio_compare(
290 const T& a_num, const T& a_denom, const T& b_num, const T& b_denom
291 ) {
292 if(a_denom == b_denom) {
293 return Sign(geo_cmp(a_num,b_num)*geo_sgn(a_denom));
294 }
295 return Sign(
296 geo_cmp(a_num*b_denom, b_num*a_denom) *
297 geo_sgn(a_denom) * geo_sgn(b_denom)
298 );
299 }
300 }
301
302 /************************************************************************/
303
304
311 template <class T>
312 inline T geo_sqr(T x) {
313 return x * x;
314 }
315
324 template <class T>
325 inline void geo_clamp(T& x, T min, T max) {
326 if(x < min) {
327 x = min;
328 } else if(x > max) {
329 x = max;
330 }
331 }
332
341
345 inline constexpr index_t max_index_t() {
346 return std::numeric_limits<index_t>::max();
347 }
348
355
360 return std::numeric_limits<signed_index_t>::max();
361 }
362
367 return std::numeric_limits<signed_index_t>::min();
368 }
369
375
379 inline double round(double x) {
380 return ((x - floor(x)) > 0.5 ? ceil(x) : floor(x));
381 }
382
383 /************************************************************************/
384
390 static constexpr index_t NO_INDEX = index_t(-1);
391
392 /************************************************************************/
393
400 template <class T> struct is_scalar {
401 typedef typename std::is_arithmetic<T>::type type;
402 static constexpr bool value = std::is_arithmetic<T>::value;
403 };
404
405 /************************************************************************/
406}
407
408
409/************* Disable floating point contraction **************************/
410
418#if defined(GOMGEN)
419# define GEO_FP_CONTRACT_OFF(x)
420#elif defined(__clang__)
421# define GEO_FP_CONTRACT_OFF _Pragma("clang fp contract(off)")
422#elif defined(_MSC_VER)
423# define GEO_FP_CONTRACT_OFF _Pragma("fp_contract(off)")
424#elif defined(__GNUC__)
425
426// GCC does not have any pragma to deactivate FMA generation,
427// so instead we check that they are deactivated (by the command-line
428// option -ffp-contract=off) and fire an assertion fail if it was not
429// the case.
430struct GeoAssertNoFpContract {
431 GeoAssertNoFpContract() {
432#ifdef GEOGRAM_PSM
433 if(fp_contraction_enabled()) {
434 std::cerr << "Needs to be compiled with -ffp-contract-off"
435 << std::endl;
436 abort();
437 }
438#else
439 geo_assert(!fp_contraction_enabled());
440#endif
441 }
442 static bool fp_contraction_enabled() {
443 return (a2plusb(0x1.0000002p0, -0x1.0000004p0) != 0.0);
444 }
445 __attribute__((noipa)) static double a2plusb(double a, double b) {
446 return a * a + b;
447 }
448};
449# define GEO_FP_CONTRACT_OFF \
450 static GeoAssertNoFpContract CPP_CONCAT(assert_no_fp_contract_,__LINE__);
451#else
452# define GEO_FP_CONTRACT_OFF _Pragma("STDC FP_CONTRACT OFF")
453#endif
454
455#endif
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
Assertion checking mechanism.
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:149
Common include file, providing basic definitions. Should be included before anything else by all head...
float float32
Definition numeric.h:151
constexpr float32 max_float32()
Gets 32 bits float maximum positive value.
Definition numeric.h:159
uint8_t uint8
Definition numeric.h:139
uint64_t uint64
Definition numeric.h:148
int8_t int8
Definition numeric.h:127
double float64
Definition numeric.h:154
int32_t int32
Definition numeric.h:133
void * pointer
Definition numeric.h:124
constexpr float64 min_float64()
Gets 64 bits float minimum negative value.
Definition numeric.h:183
uint16_t uint16
Definition numeric.h:142
float32 random_float32()
Returns a 32 bits float between 0 and 1.
uint32_t uint32
Definition numeric.h:145
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:289
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:278
constexpr float64 max_float64()
Gets 64 bits float maximum positive value.
Definition numeric.h:176
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:166
bool is_nan(float32 x)
Checks whether a 32 bits float is "not a number".
int16_t int16
Definition numeric.h:130
int64_t int64
Definition numeric.h:136
Global Vorpaline namespace.
signed_index_t min_signed_index_t()
Gets the minimum negative value of type signed_index_t.
Definition numeric.h:366
T geo_sqr(T x)
Gets the square value of a value.
Definition numeric.h:312
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:354
constexpr signed_index_t max_signed_index_t()
Gets the maximum positive value of type signed_index_t.
Definition numeric.h:359
void geo_clamp(T &x, T min, T max)
Clamps a value to a range.
Definition numeric.h:325
Sign geo_sgn(const T &x)
Gets the sign of a value.
Definition numeric.h:110
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:340
Sign
Integer constants that represent the sign of a value.
Definition numeric.h:72
@ ZERO
Definition numeric.h:76
@ NEGATIVE
Definition numeric.h:74
@ POSITIVE
Definition numeric.h:78
Sign geo_cmp(const T &a, const T &b)
Compares two values.
Definition numeric.h:92
double round(double x)
Definition numeric.h:379
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition numeric.h:374
constexpr index_t max_index_t()
Gets the maximum positive value of type index_t.
Definition numeric.h:345
Limits helper class that extends std::numeric_limits.
Definition numeric.h:242
Extends std::numeric_limits with additional information.
Definition numeric.h:271
type traits for scalars
Definition numeric.h:400