MDL SDK API nvidia_logo_transpbg.gif Up
types.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2022 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
8
9#ifndef MI_BASE_TYPES_H
10#define MI_BASE_TYPES_H
11
12#include <mi/base/config.h>
13#include <mi/base/assert.h>
14#include <cstddef> // define size_t, ptrdiff_t
15#include <cstdlib> // declare abs
16#include <cmath> // declare abs overloads
17
18#if defined(__has_include) && (!defined(_WIN32) || (defined(_HAS_CXX20) && _HAS_CXX20) || _MSVC_LANG >= 202002L)
19#if __has_include(<bit>)
20#include <bit>
21#endif
22#endif
23
24#ifndef MI_BASE_NO_STL
25#include <algorithm> // define min, max
26#endif // MI_BASE_NO_STL
27
28namespace mi {
29
40// primitive types of known bit size
41
42typedef signed char Sint8;
43typedef signed short Sint16;
44typedef signed int Sint32;
45typedef unsigned char Uint8;
46typedef unsigned short Uint16;
47typedef unsigned int Uint32;
48
49typedef float Float32;
50typedef double Float64;
51
52#if defined(MI_COMPILER_MSC)
53
54typedef signed __int64 Sint64;
55typedef unsigned __int64 Uint64;
56
57#elif defined(MI_COMPILER_GCC) // defined(MI_COMPILER_MSC)
58
59typedef long long Sint64;
60typedef unsigned long long Uint64;
61
62#else // defined(MI_COMPILER_GCC)
63
64typedef signed long long Sint64;
65typedef unsigned long long Uint64;
66
67#endif // defined(MI_COMPILER_GCC)
68
69mi_static_assert( sizeof( Sint8) == 1);
70mi_static_assert( sizeof( Sint16) == 2);
71mi_static_assert( sizeof( Sint32) == 4);
72mi_static_assert( sizeof( Sint64) == 8);
73mi_static_assert( sizeof( Uint8) == 1);
74mi_static_assert( sizeof( Uint16) == 2);
75mi_static_assert( sizeof( Uint32) == 4);
76mi_static_assert( sizeof( Uint64) == 8);
77mi_static_assert( sizeof(Float32) == 4);
78mi_static_assert( sizeof(Float64) == 8);
79
86#ifdef MI_ARCH_64BIT
87#define MI_BASE_FMT_MI_SINT64 "lld"
88#else // MI_ARCH_64BIT
89#define MI_BASE_FMT_MI_SINT64 "d"
90#endif // MI_ARCH_64BIT
91
98#ifdef MI_ARCH_64BIT
99#define MI_BASE_FMT_MI_UINT64 "llu"
100#else // MI_ARCH_64BIT
101#define MI_BASE_FMT_MI_UINT64 "u"
102#endif // MI_ARCH_64BIT
103
109#ifdef MI_ARCH_64BIT
110typedef Uint64 Size;
111#else // MI_ARCH_64BIT
112typedef Uint32 Size;
113#endif // MI_ARCH_64BIT
114
119#ifdef MI_ARCH_64BIT
121#else // MI_ARCH_64BIT
122typedef Sint32 Difference;
123#endif // MI_ARCH_64BIT
124
125#ifdef MI_ARCH_64BIT
126mi_static_assert( sizeof(Size) == 8);
127mi_static_assert( sizeof(Difference) == 8);
128#else // MI_ARCH_64BIT
129mi_static_assert( sizeof(Size) == 4);
130mi_static_assert( sizeof(Difference) == 4);
131#endif // MI_ARCH_64BIT
132
137#ifdef MI_ARCH_64BIT
138static const Size SIZE_MAX_VALUE = 18446744073709551615ULL;
139#else // MI_ARCH_64BIT
140static const Size SIZE_MAX_VALUE = 4294967295U;
141#endif // MI_ARCH_64BIT
142
147#ifdef MI_ARCH_64BIT
148static const Difference DIFFERENCE_MIN_VALUE = -9223372036854775807LL - 1LL;
149#else // MI_ARCH_64BIT
150static const Difference DIFFERENCE_MIN_VALUE = -2147483647 - 1;
151#endif // MI_ARCH_64BIT
152
157#ifdef MI_ARCH_64BIT
158static const Difference DIFFERENCE_MAX_VALUE = 9223372036854775807LL;
159#else // MI_ARCH_64BIT
160static const Difference DIFFERENCE_MAX_VALUE = 2147483647;
161#endif // MI_ARCH_64BIT
162
169#ifdef MI_ARCH_64BIT
170#define MI_BASE_FMT_MI_SIZE "llu"
171#else // MI_ARCH_64BIT
172#define MI_BASE_FMT_MI_SIZE "u"
173#endif // MI_ARCH_64BIT
174
181#ifdef MI_ARCH_64BIT
182#define MI_BASE_FMT_MI_DIFFERENCE "lld"
183#else // MI_ARCH_64BIT
184#define MI_BASE_FMT_MI_DIFFERENCE "d"
185#endif // MI_ARCH_64BIT
186
187// primitive types related constants
188
190#define MI_PI 3.14159265358979323846
192#define MI_PI_2 1.57079632679489661923
194#define MI_PI_4 0.78539816339744830962
195
201{
202 NEGATIVE = -1,
203 ZERO = 0,
205
206 LESS = -1,
207 EQUAL = 0,
208 GREATER = 1
210
213{
214 return Comparison_result( -static_cast<int>( sign));
215}
216
222template <typename T>
224{
225 if (t < 0) return NEGATIVE;
226 else if (t > 0) return POSITIVE;
227 else return ZERO;
228}
229
235template <typename T>
237{
238 if (lhs < rhs) return LESS;
239 else if (rhs < lhs) return GREATER;
240 else return EQUAL;
241}
242
243namespace base {
244
245// Define min/max within mi::base if and only if no STL usage is selected by defining the
246// MI_BASE_NO_STL macro. Use std definitions otherwise.
247#ifdef MI_BASE_NO_STL
248
249template <class T>
250inline const T& min MI_PREVENT_MACRO_EXPAND (const T& a, const T& b)
251{ return a < b ? a : b; }
252
253template <class T>
254inline const T& max MI_PREVENT_MACRO_EXPAND (const T& a, const T& b)
255{ return a > b ? a : b; }
256
257#else // MI_BASE_NO_STL
258
259using std::min;
260using std::max;
261
262#endif // MI_BASE_NO_STL
263
264// For simple types, we add variants that pass the arguments by value. This can result in better
265// performance on some compilers.
266inline Sint8 min MI_PREVENT_MACRO_EXPAND (const Sint8 a, const Sint8 b)
267{ return a < b ? a : b; }
268inline Sint16 min MI_PREVENT_MACRO_EXPAND (const Sint16 a, const Sint16 b)
269{ return a < b ? a : b; }
270inline Sint32 min MI_PREVENT_MACRO_EXPAND (const Sint32 a, const Sint32 b)
271{ return a < b ? a : b; }
272inline Sint64 min MI_PREVENT_MACRO_EXPAND (const Sint64 a, const Sint64 b)
273{ return a < b ? a : b; }
274inline Uint8 min MI_PREVENT_MACRO_EXPAND (const Uint8 a, const Uint8 b)
275{ return a < b ? a : b; }
276inline Uint16 min MI_PREVENT_MACRO_EXPAND (const Uint16 a, const Uint16 b)
277{ return a < b ? a : b; }
278inline Uint32 min MI_PREVENT_MACRO_EXPAND (const Uint32 a, const Uint32 b)
279{ return a < b ? a : b; }
280inline Uint64 min MI_PREVENT_MACRO_EXPAND (const Uint64 a, const Uint64 b)
281{ return a < b ? a : b; }
282inline Float32 min MI_PREVENT_MACRO_EXPAND (const Float32 a, const Float32 b)
283{ return a < b ? a : b; }
284inline Float64 min MI_PREVENT_MACRO_EXPAND (const Float64 a, const Float64 b)
285{ return a < b ? a : b; }
286
287inline Sint8 max MI_PREVENT_MACRO_EXPAND (const Sint8 a, const Sint8 b)
288{ return a > b ? a : b; }
289inline Sint16 max MI_PREVENT_MACRO_EXPAND (const Sint16 a, const Sint16 b)
290{ return a > b ? a : b; }
291inline Sint32 max MI_PREVENT_MACRO_EXPAND (const Sint32 a, const Sint32 b)
292{ return a > b ? a : b; }
293inline Sint64 max MI_PREVENT_MACRO_EXPAND (const Sint64 a, const Sint64 b)
294{ return a > b ? a : b; }
295inline Uint8 max MI_PREVENT_MACRO_EXPAND (const Uint8 a, const Uint8 b)
296{ return a > b ? a : b; }
297inline Uint16 max MI_PREVENT_MACRO_EXPAND (const Uint16 a, const Uint16 b)
298{ return a > b ? a : b; }
299inline Uint32 max MI_PREVENT_MACRO_EXPAND (const Uint32 a, const Uint32 b)
300{ return a > b ? a : b; }
301inline Uint64 max MI_PREVENT_MACRO_EXPAND (const Uint64 a, const Uint64 b)
302{ return a > b ? a : b; }
303inline Float32 max MI_PREVENT_MACRO_EXPAND (const Float32 a, const Float32 b)
304{ return a > b ? a : b; }
305inline Float64 max MI_PREVENT_MACRO_EXPAND (const Float64 a, const Float64 b)
306{ return a > b ? a : b; }
307
308// Take the abs function overloads from the Standard Library header cmath.
309
317using std::abs;
318
319namespace {
320 // helper class for the \c binary_cast function defined below
321 template <class Target, class Source>
322 union Binary_cast
323 {
324 Source source;
325 Target target;
326 };
327}
328
334#ifdef __cpp_lib_bit_cast
335template<class T, class S> constexpr T binary_cast(const S& src) noexcept { return std::bit_cast<T,S>(src); }
336#else
337template <class Target, class Source>
338inline Target binary_cast(Source const & val)
339{
340 mi_static_assert( sizeof(Source) == sizeof(Target));
341 Binary_cast<Target, Source> val_;
342 val_.source = val;
343 return val_.target;
344}
345#endif
346
355template <typename T>
357{
359 static const bool is_specialized = false;
360
364 static const bool has_infinity = false;
365
369 static const bool has_quiet_NaN = false;
370
375 static const bool has_signaling_NaN = false;
376
377
385 static T (min)() throw() { return T(); }
386
393 static T (max)() throw() { return T(); }
394
401 static T negative_max() throw() { return T(); }
402
404 static T infinity() throw() { return T(); }
405
407 static T quiet_NaN() throw() { return T(); }
408
410 static T signaling_NaN() throw() { return T(); }
411};
412
423template <typename T>
425{};
426
439template <> struct numeric_traits<Sint8> : public numeric_traits_base<Sint8>
440{
441 static const bool is_specialized = true;
442 static Sint8 (min)() throw() { return -128; }
443 static Sint8 (max)() throw() { return 127; }
444 static Sint8 negative_max() throw() { return -128; }
445};
446
448template <> struct numeric_traits<Sint16> : public numeric_traits_base<Sint16>
449{
450 static const bool is_specialized = true;
451 static Sint16 (min)() throw() { return -32768; }
452 static Sint16 (max)() throw() { return 32767; }
453 static Sint16 negative_max() throw() { return -32768; }
454};
455
457template <> struct numeric_traits<Sint32> : public numeric_traits_base<Sint32>
458{
459 static const bool is_specialized = true;
460 static Sint32 (min)() throw() { return -2147483647 - 1; }
461 static Sint32 (max)() throw() { return 2147483647; }
462 static Sint32 negative_max() throw() { return -2147483647 - 1; }
463};
464
466template <> struct numeric_traits<Sint64> : public numeric_traits_base<Sint64>
467{
468 static const bool is_specialized = true;
469 static Sint64 (min)() throw() { return -9223372036854775807LL - 1LL; }
470 static Sint64 (max)() throw() { return 9223372036854775807LL; }
471 static Sint64 negative_max() throw() { return -9223372036854775807LL - 1LL; }
472};
473
475template <> struct numeric_traits<Uint8> : public numeric_traits_base<Uint8>
476{
477 static const bool is_specialized = true;
478 static Uint8 (max)() throw() { return 255; }
479};
480
482template <> struct numeric_traits<Uint16> : public numeric_traits_base<Uint16>
483{
484 static const bool is_specialized = true;
485 static Uint16 (max)() throw() { return 65535; }
486};
487
489template <> struct numeric_traits<Uint32> : public numeric_traits_base<Uint32>
490{
491 static const bool is_specialized = true;
492 static Uint32 (max)() throw() { return 4294967295U; }
493};
494
496template <> struct numeric_traits<Uint64> : public numeric_traits_base<Uint64>
497{
498 static const bool is_specialized = true;
499 static Uint64 (max)() throw() { return 18446744073709551615ULL; }
500};
501
502// Architecture dependent definition of quiet NaN
503#ifdef MI_ARCH_BIG_ENDIAN
504# define MI__FLOAT32_INF_REP { 0x7f80, 0 }
505# define MI__FLOAT32_QNAN_REP { 0x7fc1, 0 }
506# define MI__FLOAT32_SNAN_REP { 0x7f81, 0 }
507# define MI__FLOAT64_INF_REP { 0x7ff0, 0, 0, 0 }
508# define MI__FLOAT64_QNAN_REP { 0x7ff9, 0, 0, 0 }
509# define MI__FLOAT64_SNAN_REP { 0x7ff1, 0, 0, 0 }
510#endif // MI_ARCH_BIG_ENDIAN
511#ifdef MI_ARCH_LITTLE_ENDIAN
512# define MI__FLOAT32_INF_REP { 0, 0x7f80 }
513# define MI__FLOAT32_QNAN_REP { 0, 0x7fc0 }
514# define MI__FLOAT32_SNAN_REP { 0, 0x7fa0 }
515# define MI__FLOAT64_INF_REP { 0, 0, 0, 0x7ff0 }
516# define MI__FLOAT64_QNAN_REP { 0, 0, 0, 0x7ff8 }
517# define MI__FLOAT64_SNAN_REP { 0, 0, 0, 0x7ff4 }
518#endif // MI_ARCH_LITTLE_ENDIAN
519
520namespace {
521 // Float number type representation for bitwise inits with NaNs
522 union Float32_rep { Uint16 rep[2]; Float32 val; };
523 union Float64_rep { Uint16 rep[4]; Float64 val; };
524}
525
527template <> struct numeric_traits<Float32> : public numeric_traits_base<Float32>
528{
529 static const bool is_specialized = true;
530 static const bool has_infinity = true;
531 static const bool has_quiet_NaN = true;
532 static const bool has_signaling_NaN = true;
533 static Float32 (min)() throw() { return 1.17549435e-38F; }
534 static Float32 (max)() throw() { return 3.402823466e+38F; }
535 static Float32 negative_max() throw() { return -3.402823466e+38F; }
536 static Float32 infinity() throw() {
537 Float32_rep rep = { MI__FLOAT32_INF_REP };
538 return rep.val;
539 }
540 static Float32 quiet_NaN() throw() {
541 Float32_rep rep = { MI__FLOAT32_QNAN_REP };
542 return rep.val;
543 }
544 static Float32 signaling_NaN() throw() {
545 Float32_rep rep = { MI__FLOAT32_SNAN_REP };
546 return rep.val;
547 }
548};
549
551template <> struct numeric_traits<Float64> : public numeric_traits_base<Float64>
552{
553 static const bool is_specialized = true;
554 static const bool has_infinity = true;
555 static const bool has_quiet_NaN = true;
556 static const bool has_signaling_NaN = true;
557 static Float64 (min)() throw() { return 2.2250738585072014e-308; }
558 static Float64 (max)() throw() { return 1.7976931348623158e+308; }
559 static Float64 negative_max() throw() { return -1.7976931348623158e+308; }
560 static Float64 infinity() throw() {
561 Float64_rep rep = { MI__FLOAT64_INF_REP };
562 return rep.val;
563 }
564 static Float64 quiet_NaN() throw() {
565 Float64_rep rep = { MI__FLOAT64_QNAN_REP };
566 return rep.val;
567 }
568 static Float64 signaling_NaN() throw() {
569 Float64_rep rep = { MI__FLOAT64_SNAN_REP };
570 return rep.val;
571 }
572};
573 // end group mi_base_number_traits_specialization
575
576} // namespace base
577 // end group mi_base_types
579
580} // namespace mi
581
582#endif // MI_BASE_TYPES_H
Assertions and compile-time assertions.
Configuration of the Base API.
#define mi_static_assert(expr)
Compile time assertion that raises a compilation error if the constant expression expr evaluates to f...
Definition: assert.h:65
#define MI_PREVENT_MACRO_EXPAND
Empty macro that can be used after function names to prevent macro expansion that happen to have the ...
Definition: config.h:97
static Sint16 negative_max()
SHRT_MIN.
Definition: types.h:453
static T signaling_NaN()
Returns a signaling NaN value for T, if one exists, and T() otherwise.
Definition: types.h:410
static T quiet_NaN()
Returns a quiet NaN value for T, if one exists, and T() otherwise.
Definition: types.h:407
static Float32 negative_max()
-FLT_MAX
Definition: types.h:535
static Sint8 negative_max()
SCHAR_MIN.
Definition: types.h:444
static const bool has_signaling_NaN
Constant that is true if and only if T has a signaling NaN (not-a-number) representation.
Definition: types.h:375
static const bool has_quiet_NaN
Constant that is true if and only if T has a quiet NaN (not-a-number) representation.
Definition: types.h:369
static Float64 negative_max()
-DBL_MAX
Definition: types.h:559
static Sint32 negative_max()
INT_MIN.
Definition: types.h:462
static T infinity()
Returns an infinity value for T, if one exists, and T() otherwise.
Definition: types.h:404
static const bool has_infinity
Constant that is true if and only if T has an infinity representation.
Definition: types.h:364
static Sint64 negative_max()
LLONG_MIN.
Definition: types.h:471
static const bool is_specialized
Constant that is true if and only if this traits is specialized for T.
Definition: types.h:359
static T() max()
Returns the maximum finite value for T.
Definition: types.h:393
static T() min()
Returns the minimum finite value for T (and for floating point types the minimum positive value).
Definition: types.h:385
static T negative_max()
Returns the smallest finite negative value for T.
Definition: types.h:401
long long Sint64
64-bit signed integer.
Definition: types.h:59
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:60
signed short Sint16
16-bit signed integer.
Definition: types.h:43
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:47
static const Difference DIFFERENCE_MIN_VALUE
The minimum value for Difference.
Definition: types.h:148
Target binary_cast(Source const &val)
Cast an immutable 'Source' value to an immutable 'Target' value.
Definition: types.h:338
unsigned char Uint8
8-bit unsigned integer.
Definition: types.h:45
static const Size SIZE_MAX_VALUE
The maximum value for Size.
Definition: types.h:138
Comparison_result three_valued_sign(T t)
Returns the three valued sign for a numerical type T.
Definition: types.h:223
Sint64 Difference
Signed integral type that is large enough to hold the difference of two pointers.
Definition: types.h:120
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:110
Comparison_result operator-(Comparison_result sign)
Reverses the sign of a three valued enum.
Definition: types.h:212
signed char Sint8
8-bit signed integer.
Definition: types.h:42
Comparison_result
An enum for a three-valued comparison result.
Definition: types.h:201
double Float64
64-bit float.
Definition: types.h:50
float Float32
32-bit float.
Definition: types.h:49
unsigned short Uint16
16-bit unsigned integer.
Definition: types.h:46
Comparison_result three_valued_compare(T lhs, T rhs)
Returns the three valued comparison result between two values of a numerical type T.
Definition: types.h:236
static const Difference DIFFERENCE_MAX_VALUE
The maximum value for Difference.
Definition: types.h:158
signed int Sint32
32-bit signed integer.
Definition: types.h:44
@ POSITIVE
= +1. Sign of a value is positive.
Definition: types.h:204
@ GREATER
= +1. First value is greater than second value.
Definition: types.h:208
@ EQUAL
= 0. First value is equal to second value.
Definition: types.h:207
@ NEGATIVE
= -1. Sign of a value is negative.
Definition: types.h:202
@ LESS
= -1. First value is less than second value.
Definition: types.h:206
@ ZERO
= 0. Value is zero.
Definition: types.h:203
Color abs(const Color &c)
Returns a color with the elementwise absolute values of the color c.
Definition: color.h:474
Color sign(const Color &c)
Returns the elementwise sign of color c.
Definition: color.h:758
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Base class for the helper class to deduce properties of numeric types defined in this API.
Definition: types.h:357
Helper class to deduce properties of numeric types defined in this API.
Definition: types.h:425