MDL SDK API nvidia_logo_transpbg.gif Up
types.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2026 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)
19#if (!defined(_WIN32) || (defined(_HAS_CXX20) && _HAS_CXX20) || _MSVC_LANG >= 202002L)
20#if __has_include(<bit>)
21#include <bit>
22#endif
23#endif
24#endif
25
26#ifndef MI_BASE_NO_STL
27#include <algorithm> // define min, max
28#endif // MI_BASE_NO_STL
29
30namespace mi {
31
42// primitive types of known bit size
43
44using Sint8 = signed char;
45using Sint16 = short;
46using Sint32 = int;
47using Uint8 = unsigned char;
48using Uint16 = unsigned short;
49using Uint32 = unsigned int;
50
51using Float32 = float;
52using Float64 = double;
53
54#if defined(MI_COMPILER_MSC)
55
56using Sint64 = signed __int64;
57using Uint64 = unsigned __int64;
58
59#elif defined(MI_COMPILER_GCC) // defined(MI_COMPILER_MSC)
60
61using Sint64 = long long;
62using Uint64 = unsigned long long;
63
64#else // defined(MI_COMPILER_GCC)
65
66using Sint64 = signed long long;
67using Uint64 = unsigned long long;
68
69#endif // defined(MI_COMPILER_GCC)
70
71mi_static_assert( sizeof( Sint8) == 1);
72mi_static_assert( sizeof( Sint16) == 2);
73mi_static_assert( sizeof( Sint32) == 4);
74mi_static_assert( sizeof( Sint64) == 8);
75mi_static_assert( sizeof( Uint8) == 1);
76mi_static_assert( sizeof( Uint16) == 2);
77mi_static_assert( sizeof( Uint32) == 4);
78mi_static_assert( sizeof( Uint64) == 8);
79mi_static_assert( sizeof(Float32) == 4);
80mi_static_assert( sizeof(Float64) == 8);
81
88#define MI_BASE_FMT_MI_SINT64 "lld"
89
96#define MI_BASE_FMT_MI_UINT64 "llu"
97
103using Size = Uint64;
104
110
111mi_static_assert( sizeof(Size) == 8);
112mi_static_assert( sizeof(Difference) == 8);
113
118static const Size SIZE_MAX_VALUE = 18446744073709551615ULL;
119
124static const Difference DIFFERENCE_MIN_VALUE = -9223372036854775807LL - 1LL;
125
130static const Difference DIFFERENCE_MAX_VALUE = 9223372036854775807LL;
131
138#define MI_BASE_FMT_MI_SIZE "llu"
139
146#define MI_BASE_FMT_MI_DIFFERENCE "lld"
147
148// primitive types related constants
149
151#define MI_PI 3.14159265358979323846
153#define MI_PI_2 1.57079632679489661923
155#define MI_PI_4 0.78539816339744830962
156
162{
163 NEGATIVE = -1,
164 ZERO = 0,
166
167 LESS = -1,
168 EQUAL = 0,
169 GREATER = 1
171
174{
175 return Comparison_result( -static_cast<int>( sign));
176}
177
183template <typename T>
185{
186 if (t < 0) return NEGATIVE;
187 else if (t > 0) return POSITIVE;
188 else return ZERO;
189}
190
196template <typename T>
198{
199 if (lhs < rhs) return LESS;
200 else if (rhs < lhs) return GREATER;
201 else return EQUAL;
202}
203
204namespace base {
205
206// Define min/max within mi::base if and only if no STL usage is selected by defining the
207// MI_BASE_NO_STL macro. Use std definitions otherwise.
208#ifdef MI_BASE_NO_STL
209
210template <class T>
211inline const T& min MI_PREVENT_MACRO_EXPAND (const T& a, const T& b)
212{ return a < b ? a : b; }
213
214template <class T>
215inline const T& max MI_PREVENT_MACRO_EXPAND (const T& a, const T& b)
216{ return a > b ? a : b; }
217
218#else // MI_BASE_NO_STL
219
220using std::min;
221using std::max;
222
223#endif // MI_BASE_NO_STL
224
225// For simple types, we add variants that pass the arguments by value. This can result in better
226// performance on some compilers.
227inline Sint8 min MI_PREVENT_MACRO_EXPAND (const Sint8 a, const Sint8 b)
228{ return a < b ? a : b; }
229inline Sint16 min MI_PREVENT_MACRO_EXPAND (const Sint16 a, const Sint16 b)
230{ return a < b ? a : b; }
231inline Sint32 min MI_PREVENT_MACRO_EXPAND (const Sint32 a, const Sint32 b)
232{ return a < b ? a : b; }
233inline Sint64 min MI_PREVENT_MACRO_EXPAND (const Sint64 a, const Sint64 b)
234{ return a < b ? a : b; }
235inline Uint8 min MI_PREVENT_MACRO_EXPAND (const Uint8 a, const Uint8 b)
236{ return a < b ? a : b; }
237inline Uint16 min MI_PREVENT_MACRO_EXPAND (const Uint16 a, const Uint16 b)
238{ return a < b ? a : b; }
239inline Uint32 min MI_PREVENT_MACRO_EXPAND (const Uint32 a, const Uint32 b)
240{ return a < b ? a : b; }
241inline Uint64 min MI_PREVENT_MACRO_EXPAND (const Uint64 a, const Uint64 b)
242{ return a < b ? a : b; }
243inline Float32 min MI_PREVENT_MACRO_EXPAND (const Float32 a, const Float32 b)
244{ return a < b ? a : b; }
245inline Float64 min MI_PREVENT_MACRO_EXPAND (const Float64 a, const Float64 b)
246{ return a < b ? a : b; }
247
248inline Sint8 max MI_PREVENT_MACRO_EXPAND (const Sint8 a, const Sint8 b)
249{ return a > b ? a : b; }
250inline Sint16 max MI_PREVENT_MACRO_EXPAND (const Sint16 a, const Sint16 b)
251{ return a > b ? a : b; }
252inline Sint32 max MI_PREVENT_MACRO_EXPAND (const Sint32 a, const Sint32 b)
253{ return a > b ? a : b; }
254inline Sint64 max MI_PREVENT_MACRO_EXPAND (const Sint64 a, const Sint64 b)
255{ return a > b ? a : b; }
256inline Uint8 max MI_PREVENT_MACRO_EXPAND (const Uint8 a, const Uint8 b)
257{ return a > b ? a : b; }
258inline Uint16 max MI_PREVENT_MACRO_EXPAND (const Uint16 a, const Uint16 b)
259{ return a > b ? a : b; }
260inline Uint32 max MI_PREVENT_MACRO_EXPAND (const Uint32 a, const Uint32 b)
261{ return a > b ? a : b; }
262inline Uint64 max MI_PREVENT_MACRO_EXPAND (const Uint64 a, const Uint64 b)
263{ return a > b ? a : b; }
264inline Float32 max MI_PREVENT_MACRO_EXPAND (const Float32 a, const Float32 b)
265{ return a > b ? a : b; }
266inline Float64 max MI_PREVENT_MACRO_EXPAND (const Float64 a, const Float64 b)
267{ return a > b ? a : b; }
268
269// Take the abs function overloads from the Standard Library header cmath.
270
278using std::abs;
279
280namespace {
281 // helper class for the \c binary_cast function defined below
282 template <class Target, class Source>
283 union Binary_cast
284 {
285 Source source;
286 Target target;
287 };
288}
289
295#ifdef __cpp_lib_bit_cast
296template<class T, class S>
297constexpr T binary_cast(const S& src) noexcept { return std::bit_cast<T,S>(src); }
298#else
299template <class Target, class Source>
300inline Target binary_cast(Source const & val)
301{
302 mi_static_assert( sizeof(Source) == sizeof(Target));
303 Binary_cast<Target, Source> val_;
304 val_.source = val;
305 return val_.target;
306}
307#endif
308
317template <typename T>
319{
321 static const bool is_specialized = false;
322
326 static const bool has_infinity = false;
327
331 static const bool has_quiet_NaN = false;
332
337 static const bool has_signaling_NaN = false;
338
339
347 static T (min)() noexcept { return T(); }
348
355 static T (max)() noexcept { return T(); }
356
363 static T negative_max() noexcept { return T(); }
364
366 static T infinity() noexcept { return T(); }
367
369 static T quiet_NaN() noexcept { return T(); }
370
372 static T signaling_NaN() noexcept { return T(); }
373};
374
385template <typename T>
387{};
388
401template <> struct numeric_traits<Sint8> : public numeric_traits_base<Sint8>
402{
403 static const bool is_specialized = true;
404 static Sint8 (min)() noexcept { return -128; }
405 static Sint8 (max)() noexcept { return 127; }
406 static Sint8 negative_max() noexcept { return -128; }
407};
408
410template <> struct numeric_traits<Sint16> : public numeric_traits_base<Sint16>
411{
412 static const bool is_specialized = true;
413 static Sint16 (min)() noexcept { return -32768; }
414 static Sint16 (max)() noexcept { return 32767; }
415 static Sint16 negative_max() noexcept { return -32768; }
416};
417
419template <> struct numeric_traits<Sint32> : public numeric_traits_base<Sint32>
420{
421 static const bool is_specialized = true;
422 static Sint32 (min)() noexcept { return -2147483647 - 1; }
423 static Sint32 (max)() noexcept { return 2147483647; }
424 static Sint32 negative_max() noexcept { return -2147483647 - 1; }
425};
426
428template <> struct numeric_traits<Sint64> : public numeric_traits_base<Sint64>
429{
430 static const bool is_specialized = true;
431 static Sint64 (min)() noexcept { return -9223372036854775807LL - 1LL; }
432 static Sint64 (max)() noexcept { return 9223372036854775807LL; }
433 static Sint64 negative_max() noexcept { return -9223372036854775807LL - 1LL; }
434};
435
437template <> struct numeric_traits<Uint8> : public numeric_traits_base<Uint8>
438{
439 static const bool is_specialized = true;
440 static Uint8 (max)() noexcept { return 255; }
441};
442
444template <> struct numeric_traits<Uint16> : public numeric_traits_base<Uint16>
445{
446 static const bool is_specialized = true;
447 static Uint16 (max)() noexcept { return 65535; }
448};
449
451template <> struct numeric_traits<Uint32> : public numeric_traits_base<Uint32>
452{
453 static const bool is_specialized = true;
454 static Uint32 (max)() noexcept { return 4294967295U; }
455};
456
458template <> struct numeric_traits<Uint64> : public numeric_traits_base<Uint64>
459{
460 static const bool is_specialized = true;
461 static Uint64 (max)() noexcept { return 18446744073709551615ULL; }
462};
463
464// Architecture dependent definition of quiet NaN
465#ifdef MI_ARCH_BIG_ENDIAN
466# define MI__FLOAT32_INF_REP { 0x7f80, 0 }
467# define MI__FLOAT32_QNAN_REP { 0x7fc1, 0 }
468# define MI__FLOAT32_SNAN_REP { 0x7f81, 0 }
469# define MI__FLOAT64_INF_REP { 0x7ff0, 0, 0, 0 }
470# define MI__FLOAT64_QNAN_REP { 0x7ff9, 0, 0, 0 }
471# define MI__FLOAT64_SNAN_REP { 0x7ff1, 0, 0, 0 }
472#endif // MI_ARCH_BIG_ENDIAN
473#ifdef MI_ARCH_LITTLE_ENDIAN
474# define MI__FLOAT32_INF_REP { 0, 0x7f80 }
475# define MI__FLOAT32_QNAN_REP { 0, 0x7fc0 }
476# define MI__FLOAT32_SNAN_REP { 0, 0x7fa0 }
477# define MI__FLOAT64_INF_REP { 0, 0, 0, 0x7ff0 }
478# define MI__FLOAT64_QNAN_REP { 0, 0, 0, 0x7ff8 }
479# define MI__FLOAT64_SNAN_REP { 0, 0, 0, 0x7ff4 }
480#endif // MI_ARCH_LITTLE_ENDIAN
481
482namespace {
483 // Float number type representation for bitwise inits with NaNs
484 union Float32_rep { Uint16 rep[2]; Float32 val; };
485 union Float64_rep { Uint16 rep[4]; Float64 val; };
486}
487
489template <> struct numeric_traits<Float32> : public numeric_traits_base<Float32>
490{
491 static const bool is_specialized = true;
492 static const bool has_infinity = true;
493 static const bool has_quiet_NaN = true;
494 static const bool has_signaling_NaN = true;
495 static Float32 (min)() noexcept { return 1.17549435e-38F; }
496 static Float32 (max)() noexcept { return 3.402823466e+38F; }
497 static Float32 negative_max() noexcept { return -3.402823466e+38F; }
498 static Float32 infinity() noexcept {
499 Float32_rep rep = { MI__FLOAT32_INF_REP };
500 return rep.val;
501 }
502 static Float32 quiet_NaN() noexcept {
503 Float32_rep rep = { MI__FLOAT32_QNAN_REP };
504 return rep.val;
505 }
506 static Float32 signaling_NaN() noexcept {
507 Float32_rep rep = { MI__FLOAT32_SNAN_REP };
508 return rep.val;
509 }
510};
511
513template <> struct numeric_traits<Float64> : public numeric_traits_base<Float64>
514{
515 static const bool is_specialized = true;
516 static const bool has_infinity = true;
517 static const bool has_quiet_NaN = true;
518 static const bool has_signaling_NaN = true;
519 static Float64 (min)() noexcept { return 2.2250738585072014e-308; }
520 static Float64 (max)() noexcept { return 1.7976931348623158e+308; }
521 static Float64 negative_max() noexcept { return -1.7976931348623158e+308; }
522 static Float64 infinity() noexcept {
523 Float64_rep rep = { MI__FLOAT64_INF_REP };
524 return rep.val;
525 }
526 static Float64 quiet_NaN() noexcept {
527 Float64_rep rep = { MI__FLOAT64_QNAN_REP };
528 return rep.val;
529 }
530 static Float64 signaling_NaN() noexcept {
531 Float64_rep rep = { MI__FLOAT64_SNAN_REP };
532 return rep.val;
533 }
534};
535 // end group mi_base_number_traits_specialization
537
538} // namespace base
539 // end group mi_base_types
541
542} // namespace mi
543
544#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:58
#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 T negative_max() noexcept
Returns the smallest finite negative value for T.
Definition: types.h:363
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:337
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:331
static T infinity() noexcept
Returns an infinity value for T, if one exists, and T() otherwise.
Definition: types.h:366
static Sint64 negative_max() noexcept
LLONG_MIN.
Definition: types.h:433
static T() max() noexcept
Returns the maximum finite value for T.
Definition: types.h:355
static Sint8 negative_max() noexcept
SCHAR_MIN.
Definition: types.h:406
static const bool has_infinity
Constant that is true if and only if T has an infinity representation.
Definition: types.h:326
static const bool is_specialized
Constant that is true if and only if this traits is specialized for T.
Definition: types.h:321
static Sint16 negative_max() noexcept
SHRT_MIN.
Definition: types.h:415
static T() min() noexcept
Returns the minimum finite value for T (and for floating point types the minimum positive value).
Definition: types.h:347
static T quiet_NaN() noexcept
Returns a quiet NaN value for T, if one exists, and T() otherwise.
Definition: types.h:369
static Float64 negative_max() noexcept
-DBL_MAX
Definition: types.h:521
static Float32 negative_max() noexcept
-FLT_MAX
Definition: types.h:497
static T signaling_NaN() noexcept
Returns a signaling NaN value for T, if one exists, and T() otherwise.
Definition: types.h:372
static Sint32 negative_max() noexcept
INT_MIN.
Definition: types.h:424
unsigned long long Uint64
64-bit unsigned integer.
Definition: types.h:62
long long Sint64
64-bit signed integer.
Definition: types.h:61
static const Difference DIFFERENCE_MIN_VALUE
The minimum value for Difference.
Definition: types.h:124
Target binary_cast(Source const &val)
Cast an immutable 'Source' value to an immutable 'Target' value.
Definition: types.h:300
short Sint16
16-bit signed integer.
Definition: types.h:45
unsigned char Uint8
8-bit unsigned integer.
Definition: types.h:47
int Sint32
32-bit signed integer.
Definition: types.h:46
static const Size SIZE_MAX_VALUE
The maximum value for Size.
Definition: types.h:118
unsigned short Uint16
16-bit unsigned integer.
Definition: types.h:48
Comparison_result three_valued_sign(T t)
Returns the three valued sign for a numerical type T.
Definition: types.h:184
float Float32
32-bit float.
Definition: types.h:51
Comparison_result operator-(Comparison_result sign)
Reverses the sign of a three valued enum.
Definition: types.h:173
Comparison_result
An enum for a three-valued comparison result.
Definition: types.h:162
signed char Sint8
8-bit signed integer.
Definition: types.h:44
double Float64
64-bit float.
Definition: types.h:52
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:103
Sint64 Difference
Signed integral type that is large enough to hold the difference of two pointers.
Definition: types.h:109
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:197
unsigned int Uint32
32-bit unsigned integer.
Definition: types.h:49
static const Difference DIFFERENCE_MAX_VALUE
The maximum value for Difference.
Definition: types.h:130
@ POSITIVE
= +1. Sign of a value is positive.
Definition: types.h:165
@ GREATER
= +1. First value is greater than second value.
Definition: types.h:169
@ EQUAL
= 0. First value is equal to second value.
Definition: types.h:168
@ NEGATIVE
= -1. Sign of a value is negative.
Definition: types.h:163
@ LESS
= -1. First value is less than second value.
Definition: types.h:167
@ ZERO
= 0. Value is zero.
Definition: types.h:164
Color abs(const Color &c)
Returns a color with the elementwise absolute values of the color c.
Definition: color.h:459
Color sign(const Color &c)
Returns the elementwise sign of color c.
Definition: color.h:743
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:319
Helper class to deduce properties of numeric types defined in this API.
Definition: types.h:387