SuperDex Physics C++ API
Loading...
Searching...
No Matches
mochi_platform.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**************************************************************************************************
18 This header declares macros which depend on the current platform, compiler, or CPU
19 features. See mochi_config.h for additional macros which enable/disable optional features.
20*/
21
22// no #pragma once for pure C inclusion
23#ifndef MOCHI_PLATFORM_H
24#define MOCHI_PLATFORM_H
25
26#if defined(__cplusplus)
27#include <type_traits>
28#endif
29
30/**************************************************************************************************
31 Language:
32 - MOCHI_LANGUAGE_CPP Compiling with a C++ compiler (any version)
33 - MOCHI_LANGUAGE_CPP17 Compiling with a C++ compiler that supports C++17 (or newer)
34 - MOCHI_LANGUAGE_CPP20 Compiling with a C++ compiler that supports C++20 (or newer)
35 - MOCHI_LANGUAGE_C Compiling with a C compiler
36*/
37#if defined(__cplusplus)
38// MSVC keeps __cplusplus at 199711L unless /Zc:__cplusplus is passed, so use the larger of it and
39// _MSVC_LANG, as MSVC's STL does.
40#if defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus
41#define MOCHI_DETAILS_CPLUSPLUS _MSVC_LANG
42#else
43#define MOCHI_DETAILS_CPLUSPLUS __cplusplus
44#endif
45#define MOCHI_LANGUAGE_CPP 1
46#define MOCHI_LANGUAGE_CPP17 (MOCHI_DETAILS_CPLUSPLUS >= 201703L)
47#ifndef MOCHI_LANGUAGE_CPP20 // Can be defined to 0 in build script to test C++17 fallbacks.
48#define MOCHI_LANGUAGE_CPP20 (MOCHI_DETAILS_CPLUSPLUS >= 202002L)
49#endif
50#define MOCHI_LANGUAGE_C 0
51#else
52#define MOCHI_LANGUAGE_CPP 0
53#define MOCHI_LANGUAGE_CPP17 0
54#ifndef MOCHI_LANGUAGE_CPP20
55#define MOCHI_LANGUAGE_CPP20 0
56#endif
57#define MOCHI_LANGUAGE_C 1
58#endif
59
60/**************************************************************************************************
61 Architecture:
62 - MOCHI_ARCH_CPU Compiling for CPU (regular CPU code or CUDA CPU host)
63 - MOCHI_ARCH_GPU Compiling for GPU (CUDA GPU device code)
64 - MOCHI_ARCH_ARM Compiling for an ARM CPU (64-bit ARMv8 or newer)
65 - MOCHI_ARCH_ARM_NEON Compiling for an ARM CPU with NEON (mandatory for ARMv8 or newer)
66 - MOCHI_ARCH_ARM_NEON_FP16_ARITHMETIC
67 Compiling with the ARM FP16 vector arithmetic feature enabled
68 - MOCHI_ARCH_ARM_SVE Compiling for an ARM CPU with SVE (mandatory for ARMv9 or newer)
69 - MOCHI_ARCH_ARM_SME Compiling for an ARM CPU with SME (scalable matrix extension)
70 - MOCHI_ARCH_X64 Compiling for an x64 CPU (64-bit x86, also called x86_64)
71 - MOCHI_ARCH_X64_AVX512 Compiling for an x64 CPU with AVX-512 F, CD, BW, DQ, and VL
72 - MOCHI_ARCH_X64_AVX2 Compiling for an x64 CPU with AVX2 vector extension
73 - MOCHI_ARCH_X64_FMA Compiling for an x64 CPU with FMA (fused multiply add) extension
74 - MOCHI_ARCH_X64_SVML Compiling for an x64 CPU with SVML (small vector math library) extension
75*/
76#if defined(__CUDA_ARCH__)
77#define MOCHI_ARCH_CPU 0
78#define MOCHI_ARCH_GPU 1
79#else
80#define MOCHI_ARCH_CPU 1
81#define MOCHI_ARCH_GPU 0
82#endif
83
84// NOTE: The Unreal Engine build system has its own macros.
85#if defined(PLATFORM_ENABLE_VECTORINTRINSICS_NEON) && PLATFORM_ENABLE_VECTORINTRINSICS_NEON
86#define MOCHI_UNREAL_ARCH_NEON 1
87#else
88#define MOCHI_UNREAL_ARCH_NEON 0
89#endif
90
91#if defined(__aarch64__) || MOCHI_UNREAL_ARCH_NEON
92#define MOCHI_ARCH_ARM 1
93#else
94#define MOCHI_ARCH_ARM 0
95#endif
96
97#if MOCHI_ARCH_CPU && MOCHI_ARCH_ARM && (defined(__ARM_NEON) || MOCHI_UNREAL_ARCH_NEON)
98#define MOCHI_ARCH_ARM_NEON 1
99#else
100#define MOCHI_ARCH_ARM_NEON 0
101#endif
102
103#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
104#define MOCHI_ARCH_ARM_NEON_FP16_ARITHMETIC 1
105#else
106#define MOCHI_ARCH_ARM_NEON_FP16_ARITHMETIC 0
107#endif
108
109// NOTE: The Unreal Engine build system has its own macros.
110#define MOCHI_UNREAL_ARCH_AVX2 0
111
112#if defined(PLATFORM_CPU_X86_FAMILY)
113#if PLATFORM_CPU_X86_FAMILY
114#undef MOCHI_UNREAL_ARCH_AVX2
115#define MOCHI_UNREAL_ARCH_AVX2 1 // Assume AVX2 for Unreal Engine builds
116#endif
117#endif
118
119// MOCHI_UNREAL_LINUX_EDITOR disables AVX2 for the Linux editor build.
120// The editor target shares its PCH with UnrealEditor, which is compiled without -mavx2.
121// Adding -mavx2 per-module causes a PCH/AST feature mismatch.
122// Game target does use AVX2.
123#if defined(MOCHI_UNREAL_LINUX_EDITOR)
124#if MOCHI_UNREAL_LINUX_EDITOR
125#undef MOCHI_UNREAL_ARCH_AVX2
126#define MOCHI_UNREAL_ARCH_AVX2 0
127#endif
128#endif
129
130#if defined(_M_X64) || defined(__x86_64) || MOCHI_UNREAL_ARCH_AVX2
131#define MOCHI_ARCH_X64 1
132#else
133#define MOCHI_ARCH_X64 0
134#endif
135
136#if MOCHI_ARCH_CPU && MOCHI_ARCH_X64 && defined(__AVX512F__) && defined(__AVX512CD__) && \
137 defined(__AVX512BW__) && defined(__AVX512DQ__) && defined(__AVX512VL__)
138#define MOCHI_ARCH_X64_AVX512 1
139#else
140#define MOCHI_ARCH_X64_AVX512 0
141#endif
142
143#if MOCHI_ARCH_CPU && MOCHI_ARCH_X64 && \
144 (MOCHI_ARCH_X64_AVX512 || defined(__AVX2__) || MOCHI_UNREAL_ARCH_AVX2)
145#define MOCHI_ARCH_X64_AVX2 1
146#else
147#define MOCHI_ARCH_X64_AVX2 0
148#endif
149
150#if MOCHI_ARCH_CPU && MOCHI_ARCH_X64_AVX2 && \
151 (defined(__FMA__) || (defined(_MSC_VER) && !defined(__clang__)))
152#define MOCHI_ARCH_X64_FMA 1
153#else
154#define MOCHI_ARCH_X64_FMA 0
155#endif
156
157#if MOCHI_ARCH_CPU && MOCHI_ARCH_X64_AVX2 && \
158 (defined(__SVML__) || (defined(_MSC_VER) && !defined(__clang__)))
159#define MOCHI_ARCH_X64_SVML 1
160#else
161#define MOCHI_ARCH_X64_SVML 0
162#endif
163
164// Error checking
165#if !MOCHI_ARCH_ARM && (defined(__arm__) || defined(_M_ARM))
166#error Mochi does not support older 32-bit versions of ARM
167#endif
168#if MOCHI_ARCH_ARM && MOCHI_ARCH_X64
169#error Failed to detect correct architecture
170#endif
171#if MOCHI_UNREAL_ARCH_AVX2 && MOCHI_UNREAL_ARCH_NEON
172#error Cannot have both AVX and NEON
173#endif
174
175/**************************************************************************************************
176 Compiler:
177 - MOCHI_COMPILER_MSVC Microsoft Visual C++ (MSVC)
178 - MOCHI_COMPILER_GCC GNU C/C++ compiler
179 - MOCHI_COMPILER_CLANG CLANG C/C++ compiler
180 - MOCHI_COMPILER_CUDA_CPU CUDA compiler (NVCC) compiling a .cu file for the CPU.
181 - MOCHI_COMPILER_CUDA_GPU CUDA compiler (NVCC) compiling a .cu file for the GPU.
182 - MOCHI_COMPILER_CUDA CUDA compiler (NVCC) compiling a .cu file for CPU or GPU.
183
184 NOTE:
185 When compiling a .cu file, (MOCHI_COMPILER_CUDA && MOCHI_COMPILER_CLANG) can both be true
186 because NVCC uses the host compiler (e.g. clang) to preprocess the file. If the host compiler
187 was GCC or MSVC, then MOCHI_COMPILER_GCC or MOCHI_COMPILER_MSVC would be true while compiling
188 that .cu file.
189*/
190
191#if defined(_MSC_VER) && !defined(__clang__)
192// NOTE: Both _MSC_VER and __clang__ are defined when the Clang frontend targets the MSVC
193// backend (clang-cl). The code which uses MOCHI_COMPILER_MSVC only cares about the frontend
194// (e.g. C++ warnings, minor differences in xmmintrin.h, etc...)
195#define MOCHI_COMPILER_MSVC 1
196#else
197#define MOCHI_COMPILER_MSVC 0
198#endif
199
200// MOCHI_MSVC_TRADITIONAL indicates that MSVC is doing its old non-standard compliant behavior.
201// You can make it more standard compliant (less "traditional"), but it is not the default.
202#if defined(_MSVC_TRADITIONAL)
203#define MOCHI_MSVC_TRADITIONAL _MSVC_TRADITIONAL
204#else
205#define MOCHI_MSVC_TRADITIONAL 0
206#endif
207
208#if defined(__GNUC__) && !defined(__clang__)
209#define MOCHI_COMPILER_GCC 1
210#else
211#define MOCHI_COMPILER_GCC 0
212#endif
213#if defined(__clang__)
214#define MOCHI_COMPILER_CLANG 1
215#if (__clang_major__ < 19)
216#define MOCHI_CLANG_AWAIT_SUSPEND_BUG 1
217#else
218#define MOCHI_CLANG_AWAIT_SUSPEND_BUG 0
219#endif
220#else
221#define MOCHI_COMPILER_CLANG 0
222#define MOCHI_CLANG_AWAIT_SUSPEND_BUG 0
223#endif
224
225#if defined(__CUDA_ARCH__) // CUDA GPU
226#define MOCHI_COMPILER_CUDA_CPU 0
227#define MOCHI_COMPILER_CUDA_GPU 1
228#define MOCHI_COMPILER_CUDA 1
229#elif defined(__CUDACC__) // CUDA CPU
230#define MOCHI_COMPILER_CUDA_CPU 1
231#define MOCHI_COMPILER_CUDA_GPU 0
232#define MOCHI_COMPILER_CUDA 1
233#else // Neither
234#define MOCHI_COMPILER_CUDA_CPU 0
235#define MOCHI_COMPILER_CUDA_GPU 0
236#define MOCHI_COMPILER_CUDA 0
237#endif
238
239// Sanitizers
240#if defined(__SANITIZE_ADDRESS__) && (MOCHI_COMPILER_CLANG || MOCHI_COMPILER_GCC)
241#define MOCHI_COMPILER_ASAN 1
242#else
243#define MOCHI_COMPILER_ASAN 0
244#endif
245#if defined(__SANITIZE_THREAD__) && (MOCHI_COMPILER_CLANG || MOCHI_COMPILER_GCC)
246#define MOCHI_COMPILER_TSAN 1
247#else
248#define MOCHI_COMPILER_TSAN 0
249#endif
250#if (MOCHI_COMPILER_CLANG || MOCHI_COMPILER_GCC)
251#if defined(__has_feature)
252#if __has_feature(address_sanitizer)
253#undef MOCHI_COMPILER_ASAN
254#define MOCHI_COMPILER_ASAN 1
255#endif
256#endif
257#if defined(__has_feature)
258#if __has_feature(thread_sanitizer)
259#undef MOCHI_COMPILER_TSAN
260#define MOCHI_COMPILER_TSAN 1
261#endif
262#endif
263#endif
264
265#if MOCHI_COMPILER_CLANG
266#if __has_feature(cxx_rtti)
267#define MOCHI_COMPILER_HAS_RTTI 1
268#else
269#define MOCHI_COMPILER_HAS_RTTI 0
270#endif
271#elif MOCHI_COMPILER_GCC
272#ifdef __GXX_RTTI
273#define MOCHI_COMPILER_HAS_RTTI 1
274#else
275#define MOCHI_COMPILER_HAS_RTTI 0
276#endif
277#elif MOCHI_COMPILER_MSVC
278#ifdef _CPPRTTI
279#define MOCHI_COMPILER_HAS_RTTI 1
280#else
281#define MOCHI_COMPILER_HAS_RTTI 0
282#endif
283#else
284// RTTI is assumed to be enabled on other compilers.
285// If not, then add a check here.
286#define MOCHI_COMPILER_HAS_RTTI 1
287#endif
288
289/**************************************************************************************************
290 MOCHI_HAS_VA_OPT indicates whether or not __VA_OPT__ is supported.
291*/
292#define MOCHI_HAS_VA_OPT (MOCHI_LANGUAGE_CPP20 && !MOCHI_MSVC_TRADITIONAL)
293
294/**************************************************************************************************
295 Platform:
296 - MOCHI_PLATFORM_ANDROID Compiling for Android.
297 - MOCHI_PLATFORM_LINUX Compiling for Linux (not including Android)
298 - MOCHI_PLATFORM_MACOS Compiling for MacOS.
299 - MOCHI_PLATFORM_WINDOWS Compiling for Windows.
300
301*/
302#if defined(__ANDROID__)
303#define MOCHI_PLATFORM_ANDROID 1
304#else
305#define MOCHI_PLATFORM_ANDROID 0
306#endif
307
308#if defined(__linux__) && !defined(__ANDROID__)
309#define MOCHI_PLATFORM_LINUX 1
310#else
311#define MOCHI_PLATFORM_LINUX 0
312#endif
313
314#if defined(__APPLE__)
315#define MOCHI_PLATFORM_MACOS 1
316#else
317#define MOCHI_PLATFORM_MACOS 0
318#endif
319
320#if defined(_WIN32)
321#define MOCHI_PLATFORM_WINDOWS 1
322#else
323#define MOCHI_PLATFORM_WINDOWS 0
324#endif
325
326/**************************************************************************************************
327 CUDA Function Attributes:
328 - MOCHI_ANY Function can run on host or device
329 - MOCHI_CPU Function can only run on CPU host
330 - MOCHI_GPU Function can only run on GPU device
331 - MOCHI_GPU_KERNEL Function is a "global" GPU kernel
332*/
333#if MOCHI_COMPILER_CUDA
334#define MOCHI_ANY __host__ __device__
335#define MOCHI_CPU __host__
336#define MOCHI_GPU __device__
337#define MOCHI_GPU_KERNEL __global__
338#else
339#define MOCHI_ANY
340#define MOCHI_CPU
341#define MOCHI_GPU
342#define MOCHI_GPU_KERNEL
343#endif
344
345/**************************************************************************************************
346 MOCHI_FORCE_INLINE
347 A stronger version of 'inline'. Ignored in Debug configuration.
348 WARNING: Only use this on small functions to avoid code bloat.
349
350 Example:
351 MOCHI_FORCE_INLINE void Foo() {}
352*/
353#ifdef NDEBUG
354// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive. Check CUDA GPU
355// first.
356#if MOCHI_COMPILER_CUDA_GPU
357#define MOCHI_FORCE_INLINE __forceinline__
358#elif MOCHI_COMPILER_MSVC
359#define MOCHI_FORCE_INLINE __forceinline
360#else
361#define MOCHI_FORCE_INLINE __attribute__((always_inline)) inline
362#endif
363#else
364#define MOCHI_FORCE_INLINE inline
365#endif
366
367/**************************************************************************************************
368 MOCHI_FORCE_INLINE_LAMBDA
369 A stronger version of 'inline' for a lambda's call operator (operator()): directs the compiler
370 to inline the lambda body into its call sites.
371
372 WARNING: Only use on small lambdas in hot paths (avoids code bloat).
373
374 NOTE: Only effective for *direct* calls through the concrete closure type (e.g. the lambda held
375 by 'auto' or passed as a template parameter). If the lambda is type-erased into an std::function
376 (or called via function pointer / virtual dispatch), the call is indirect and the body is NOT
377 inlined at that boundary.
378
379 Example:
380 auto foo = [](int x) MOCHI_FORCE_INLINE_LAMBDA { return x + 1; };
381*/
382#ifdef NDEBUG
383// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive. Check CUDA GPU
384// first.
385#if MOCHI_COMPILER_CUDA_GPU
386#define MOCHI_FORCE_INLINE_LAMBDA
387#elif (MOCHI_COMPILER_CLANG || MOCHI_COMPILER_GCC)
388#define MOCHI_FORCE_INLINE_LAMBDA __attribute__((always_inline))
389#else
390#if defined(__has_cpp_attribute)
391#if __has_cpp_attribute(msvc::forceinline)
392#define MOCHI_FORCE_INLINE_LAMBDA [[msvc::forceinline]]
393#else
394#define MOCHI_FORCE_INLINE_LAMBDA
395#endif // #if __has_cpp_attribute(msvc::forceinline)
396#else
397#define MOCHI_FORCE_INLINE_LAMBDA
398#endif // #if defined(__has_cpp_attribute)
399#endif // #if MOCHI_COMPILER_CUDA_GPU
400#else
401#define MOCHI_FORCE_INLINE_LAMBDA
402#endif // #ifdef NDEBUG
403
404/**************************************************************************************************
405 MOCHI_UNROLL_LOOP_N
406 Requests that the compiler unroll the immediately following loop by N.
407
408 NOTE: This is a best-effort hint. The compiler may ignore it. It expands to nothing on MSVC and on
409 unrecognized compilers.
410
411 Example:
412 MOCHI_UNROLL_LOOP_N(4)
413 for (int i = 0; i < 4; ++i) {}
414*/
415// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive. Check CUDA GPU
416// first.
417#if MOCHI_COMPILER_CUDA_GPU
418#define MOCHI_UNROLL_LOOP_N(N) _Pragma(MOCHI_PP_STRINGIFY(unroll N))
419#elif MOCHI_COMPILER_MSVC
420#define MOCHI_UNROLL_LOOP_N(N)
421#elif MOCHI_COMPILER_GCC
422#define MOCHI_UNROLL_LOOP_N(N) _Pragma(MOCHI_PP_STRINGIFY(GCC unroll N))
423#elif MOCHI_COMPILER_CLANG
424#define MOCHI_UNROLL_LOOP_N(N) _Pragma(MOCHI_PP_STRINGIFY(unroll N))
425#else
426#define MOCHI_UNROLL_LOOP_N(N)
427#endif
428
429/**************************************************************************************************
430 MOCHI_NO_INLINE
431 Directs the compiler NOT to inline a function.
432
433 Example:
434 MOCHI_NO_INLINE void Foo() {}
435*/
436#if MOCHI_COMPILER_CUDA_GPU
437#define MOCHI_NO_INLINE __noinline__
438#elif MOCHI_COMPILER_MSVC
439#define MOCHI_NO_INLINE __declspec(noinline)
440#else
441#define MOCHI_NO_INLINE __attribute__((noinline))
442#endif
443
444/**************************************************************************************************
445 MOCHI_RESTRICT
446 Qualifies a pointer argument as restricted, promising to the compiler that
447 the memory accessed via the pointer will not be aliased by any other pointer.
448 This enables some compiler optimizations.
449
450 Example:
451 void MyCopy(float* MOCHI_RESTRICT dst, float const* MOCHI_RESTRICT src, size_t n) {
452 memcpy(dst, src, n);
453 }
454*/
455// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive.
456#if MOCHI_COMPILER_CUDA_GPU || MOCHI_COMPILER_GCC || MOCHI_COMPILER_CLANG
457#define MOCHI_RESTRICT __restrict__
458#elif MOCHI_COMPILER_MSVC
459#define MOCHI_RESTRICT __restrict
460#else
461#define MOCHI_RESTRICT restrict
462#endif
463
464/**************************************************************************************************
465 SIMD:
466 MOCHI_SIMD_REGISTER_COUNT Number of floating-point SIMD registers.
467 MOCHI_SIMD_REGISTER_SIZE_BYTES Size (in bytes) of each floating-point SIMD register.
468*/
469#if MOCHI_ARCH_X64_AVX512
470#define MOCHI_SIMD_REGISTER_COUNT 32
471#define MOCHI_SIMD_REGISTER_SIZE_BYTES 64
472#elif MOCHI_ARCH_X64_AVX2
473#define MOCHI_SIMD_REGISTER_COUNT 16
474#define MOCHI_SIMD_REGISTER_SIZE_BYTES 32
475#elif MOCHI_ARCH_ARM_NEON
476#define MOCHI_SIMD_REGISTER_COUNT 32
477#define MOCHI_SIMD_REGISTER_SIZE_BYTES 16
478#else
479#define MOCHI_SIMD_REGISTER_COUNT Unsupported architecture
480#define MOCHI_SIMD_REGISTER_SIZE_BYTES Unsupported architecture
481#endif
482
483/**************************************************************************************************
484 Memory Cache
485*/
486
487/**
488 * @brief Conservative upper limit for the size (and alignment) of a data cache line.
489 *
490 * @details Cache line size is a property of the CPU, so it is not known at compile time.
491 * This conservative value can be used when compile-time alignment is needed. For a more
492 * accurate runtime value use @ref GetCacheLineInfo.
493 *
494 * @see MOCHI_CONSERVATIVE_CACHE_ALIGN, GetCacheLineInfo
495 */
496#ifndef MOCHI_CONSERVATIVE_CACHE_LINE_SIZE
497#define MOCHI_CONSERVATIVE_CACHE_LINE_SIZE 256
498#endif
499
500/**
501 * @brief Aligns the memory of a variable so it starts at the beginning of a cache line.
502 *
503 * @details Cache line size is a property of the CPU, so it is not known at compile time. This
504 * conservative value will waste some memory on most systems, but that is usually acceptable for
505 * static variables and temporary variables.
506 *
507 * @see MOCHI_CONSERVATIVE_CACHE_LINE_SIZE, GetCacheLineInfo
508 */
509#define MOCHI_CONSERVATIVE_CACHE_ALIGN alignas(MOCHI_CONSERVATIVE_CACHE_LINE_SIZE)
510
511/**************************************************************************************************
512 MOCHI_NO_INIT
513 Add this attribute to the end of a non-static local variable declaration to tell the compiler
514 that it should NOT be initialized to zero. Without the attribute, clang/gcc will sometimes
515 perform the initialization even though the programmer did not request it.
516
517 Example:
518 std::byte buffer0[4096] MOCHI_NO_INIT, buffer1[1024] MOCHI_NO_INIT;
519*/
520#if MOCHI_COMPILER_GCC || MOCHI_COMPILER_CLANG
521// clang-format off
522#define MOCHI_NO_INIT __attribute__((uninitialized)) /* NOLINT(cppcoreguidelines-init-variables) */
523// clang-format on
524#else
525#define MOCHI_NO_INIT /* NOLINT(cppcoreguidelines-init-variables) */
526#endif
527
528/**************************************************************************************************
529 MOCHI_CONSTEXPR_NO_INIT
530 MOCHI_NO_INIT for a local variable of a constexpr function. C++17 requires such variables to be
531 initialized, so they are value-initialized before C++20.
532*/
533#if MOCHI_LANGUAGE_CPP20
534#define MOCHI_CONSTEXPR_NO_INIT MOCHI_NO_INIT
535#else
536// clang-format off
537#define MOCHI_CONSTEXPR_NO_INIT {}
538// clang-format on
539#endif
540
541/**************************************************************************************************
542 MOCHI_DEBUG_BREAK()
543 Trigger a debug breakpoint (fatal if no debugger is connected)
544*/
545// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive. Check CUDA GPU
546// first.
547#if MOCHI_COMPILER_CUDA_GPU
548#define MOCHI_DEBUG_BREAK() asm("brkpt;")
549#elif MOCHI_PLATFORM_WINDOWS
550#define MOCHI_DEBUG_BREAK() __debugbreak()
551#else
552#define MOCHI_DEBUG_BREAK() __builtin_trap()
553#endif
554
555/**************************************************************************************************
556 Warning Suppression:
557 Use these macros if you really need to suppress a specific compiler warning.
558 Targeted suppression is better than global suppression.
559
560 GCC and Clang:
561 Clang is able to understand GCC's warning suppression syntax and they support many of the same
562 warnings. Therefore, we often use the GCC warning suppression syntax for both via
563 MOCHI_WARNING_IGNORE_GCC_CLANG. GCC and Clang both require you to specify the full argument
564 string because _Pragma does not support string literal concatenation.
565
566 Examples:
567 MOCHI_WARNING_PUSH();
568 MOCHI_WARNING_IGNORE_MSVC(4100);
569 MOCHI_WARNING_IGNORE_GCC_CLANG(GCC diagnostic ignored "-Wunused-parameter");
570 MOCHI_WARNING_IGNORE_CLANG(clang diagnostic ignored "-Wself-assign-overloaded");
571 // YOUR CODE HERE
572 MOCHI_WARNING_POP();
573*/
574// NOTE: Order matters because MOCHI_COMPILER_* macros may not be mutually exclusive. Check CUDA GPU
575// first.
576#if MOCHI_COMPILER_CUDA_GPU
577#define MOCHI_WARNING_PUSH()
578#define MOCHI_WARNING_PUSH_IGNORE_ALL()
579#define MOCHI_WARNING_POP()
580#elif MOCHI_COMPILER_MSVC
581#define MOCHI_WARNING_PUSH() __pragma(warning(push))
582#define MOCHI_WARNING_PUSH_IGNORE_ALL() __pragma(warning(push, 0))
583#define MOCHI_WARNING_POP() __pragma(warning(pop))
584#elif MOCHI_COMPILER_CLANG
585#define MOCHI_WARNING_PUSH() _Pragma("GCC diagnostic push")
586#define MOCHI_WARNING_PUSH_IGNORE_ALL() \
587 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Weverything\"")
588#define MOCHI_WARNING_POP() _Pragma("GCC diagnostic pop")
589#elif MOCHI_COMPILER_GCC
590#define MOCHI_WARNING_PUSH() _Pragma("GCC diagnostic push")
591#define MOCHI_WARNING_PUSH_IGNORE_ALL() \
592 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wall\"") \
593 _Pragma("GCC diagnostic ignored \"-Wextra\"")
594#define MOCHI_WARNING_POP() _Pragma("GCC diagnostic pop")
595#else
596#define MOCHI_WARNING_PUSH()
597#define MOCHI_WARNING_PUSH_IGNORE_ALL()
598#define MOCHI_WARNING_POP()
599#endif
600
601#if MOCHI_COMPILER_MSVC
602#define MOCHI_WARNING_IGNORE_MSVC(X) __pragma(warning(disable : X))
603#define MOCHI_WARNING_ENFORCE_MSVC(X) __pragma(warning(error : X))
604#else
605#define MOCHI_WARNING_IGNORE_MSVC(X)
606#define MOCHI_WARNING_ENFORCE_MSVC(X)
607#endif
608
609#if MOCHI_COMPILER_GCC
610#define MOCHI_WARNING_IGNORE_GCC(X) _Pragma(#X)
611#define MOCHI_WARNING_ENFORCE_GCC(X) _Pragma(#X)
612#else
613#define MOCHI_WARNING_IGNORE_GCC(X)
614#define MOCHI_WARNING_ENFORCE_GCC(X)
615#endif
616
617#if MOCHI_COMPILER_CLANG
618#define MOCHI_WARNING_IGNORE_CLANG(X) _Pragma(#X)
619#define MOCHI_WARNING_ENFORCE_CLANG(X) _Pragma(#X)
620#else
621#define MOCHI_WARNING_IGNORE_CLANG(X)
622#define MOCHI_WARNING_ENFORCE_CLANG(X)
623#endif
624
625// Macros for warnings that are supported by both GCC and Clang
626#if MOCHI_COMPILER_GCC || MOCHI_COMPILER_CLANG
627#define MOCHI_WARNING_IGNORE_GCC_CLANG(X) _Pragma(#X)
628#define MOCHI_WARNING_ENFORCE_GCC_CLANG(X) _Pragma(#X)
629#else
630#define MOCHI_WARNING_IGNORE_GCC_CLANG(X)
631#define MOCHI_WARNING_ENFORCE_GCC_CLANG(X)
632#endif
633
634// Opt-ins for compiler specific warnings that are off by default.
635// These warnings are enabled in every supported build.
636/* Data member 'member1' will be initialized after data member 'member2' */
638
639// Opt-outs for other warnings.
640/* function marked as __forceinline not inlined */
642
643/**************************************************************************************************
644 Warning Suppression for CUDA
645*/
646
647#if MOCHI_COMPILER_CUDA
648#define MOCHI_WARNING_IGNORE_CUDA(X) __pragma(diag_suppress = X)
649#define MOCHI_WARNING_ENFORCE_CUDA(X) __pragma(diag_error = X)
650#else
651#define MOCHI_WARNING_IGNORE_CUDA(X)
652#define MOCHI_WARNING_ENFORCE_CUDA(X)
653#endif
654
655// Edge case: some templated utility functions may be instantiated using CPU-only
656// functions, which will result in a warning from NVCC. In such cases, it is
657// necessary to disable this check (until a better solution is found...)
658#if MOCHI_COMPILER_CUDA_GPU
659#if __CUDAVER__ >= 75000
660#define MOCHI_DISABLE_CUDA_GPU_EXEC_CHECK() __pragma(nv_exec_check_disable)
661#else
662#define MOCHI_DISABLE_CUDA_GPU_EXEC_CHECK()
663#endif
664#else
665#define MOCHI_DISABLE_CUDA_GPU_EXEC_CHECK()
666#endif
667
668#define MOCHI_TEMPLATE_FUNCTION MOCHI_DISABLE_CUDA_GPU_EXEC_CHECK()
669
670// Opt-outs for compiler-specific warnings for third-party libraries or
671// other exotic compilers (NVCC). Most of these are unavoidable, so we
672// have to wrap inclusion locations with these directives.
673#define MOCHI_WARNING_SUPPRESS_CUDA() \
674 /* Nonstandard extension used : nameless struct */ \
675 MOCHI_WARNING_IGNORE_MSVC(4201) \
676 /* '=' : conversion from 'OffsetT' to 'int', possible loss of data */ \
677 MOCHI_WARNING_IGNORE_MSVC(4244) \
678 /* Structure was padded due to alignment specifier */ \
679 MOCHI_WARNING_IGNORE_MSVC(4324) \
680 /* Declaration of 'variable' hides class member */ \
681 MOCHI_WARNING_IGNORE_MSVC(4458) \
682 /* Unreferenced local function has been removed */ \
683 MOCHI_WARNING_IGNORE_MSVC(4505) \
684 /* Uninitalized local variable */ \
685 MOCHI_WARNING_IGNORE_MSVC(4700) \
686 /* Assignment within conditional expression */ \
687 MOCHI_WARNING_IGNORE_MSVC(4706) \
688 /* Spurious "missing return statement at end of non-void function" \
689 in constexpr functions. Fixed in later versions of CUDA (>11.1). */ \
690 MOCHI_WARNING_IGNORE_CUDA(implicit_return_from_non_void_function)
691
692/**************************************************************************************************
693 Warning Suppression for Eigen
694*/
696#define MOCHI_WARNING_SUPPRESS_EIGEN() \
697 /* Conditional expression is constant */ \
698 MOCHI_WARNING_IGNORE_MSVC(4127) \
699 /* Check operator precedence for possible error; use parentheses to clarify precedence */ \
700 MOCHI_WARNING_IGNORE_MSVC(4554) \
701 /* Conversion from '__int64' to 'uint64_t', signed/unsigned mismatch */ \
702 MOCHI_WARNING_IGNORE_MSVC(4245) \
703 /* annotation ignored on a function that is explicitly defaulted */ \
704 MOCHI_WARNING_IGNORE_CUDA(esa_on_defaulted_function_ignored)
705
706/**************************************************************************************************
707 Runtime Checks Suppression
708 Some third party headers (e.g. thrust) trigger MSVC's run-time checks when ran
709 with Debug configuration. While these errors are mostly harmless, they are
710 terribly cumbersome. These macros allow us to selectively suppress these checks.
711*/
712#if MOCHI_COMPILER_MSVC
713#define MOCHI_DEBUG_RTCHECKS_ENABLE() __pragma(runtime_checks("sc", restore))
714#define MOCHI_DEBUG_RTCHECKS_DISABLE() __pragma(runtime_checks("sc", off))
715#else
716#define MOCHI_DEBUG_RTCHECKS_ENABLE()
717#define MOCHI_DEBUG_RTCHECKS_DISABLE()
718#endif
719
720/**************************************************************************************************
721 No-Op
722*/
723
724#if MOCHI_COMPILER_MSVC
725#include <intrin.h>
726#define MOCHI_NOP() __nop();
727#else
728#define MOCHI_NOP() __asm__ __volatile__("nop");
729#endif
730// clang-format off
731#define MOCHI_NOP_10() \
732 MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP(); \
733 MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP(); MOCHI_NOP();
734#define MOCHI_NOP_50() \
735 MOCHI_NOP_10(); MOCHI_NOP_10(); MOCHI_NOP_10(); MOCHI_NOP_10(); MOCHI_NOP_10();
736#define MOCHI_NOP_250() \
737 MOCHI_NOP_50(); MOCHI_NOP_50(); MOCHI_NOP_50(); MOCHI_NOP_50(); MOCHI_NOP_50();
738// clang-format on
739
740/**************************************************************************************************
741 Pre-processor Helpers
742*/
743
744// Pre-processor concatenation, for use in other macros.
745#define MOCHI_PP_CAT(a, b) MOCHI_PP_CAT_IMPL(a, b)
746#define MOCHI_PP_CAT_IMPL(a, b) a##b
747
748// Pre-processor stringization, for use in other macros (e.g. _Pragma operands).
749#define MOCHI_PP_STRINGIFY(X) MOCHI_PP_STRINGIFY_IMPL(X)
750#define MOCHI_PP_STRINGIFY_IMPL(X) #X
751
752/**************************************************************************************************
753 Move & Copy Semantics
754*/
755
756// Declares default move operations. Goes inside a class or struct declaration.
757#define MOCHI_DECLARE_MOVE(Name) \
758 Name(Name&&) noexcept = default; \
759 Name& operator=(Name&&) = default;
760
761// Deletes the default move operations. Goes inside a class or struct declaration.
762#define MOCHI_DECLARE_NO_MOVE(Name) \
763 Name(Name&&) = delete; \
764 Name& operator=(Name&&) = delete;
765
766// Declares default copy operations. Goes inside a class or struct declaration.
767// Can be used in private scope to enable an explicit Copy() function.
768#define MOCHI_DECLARE_COPY(Name) \
769 Name(Name const&) = default; \
770 Name& operator=(Name const&) = default;
771
772// Deletes the default copy operations. Goes inside a class or struct declaration.
773#define MOCHI_DECLARE_NO_COPY(Name) \
774 Name(Name const&) = delete; \
775 Name& operator=(Name const&) = delete;
776
777// Deletes both the copy and move operations. Goes inside a class or struct declaration.
778// Note that "pinned" is a term borrowed from other languages. It means the address of the object
779// is not allowed to change.
780#define MOCHI_DECLARE_NO_COPY_NO_MOVE(Name) \
781 MOCHI_DECLARE_NO_COPY(Name) \
782 MOCHI_DECLARE_NO_MOVE(Name)
783
784// Declares unique ownership semantics (move but no copy). Goes inside a class or struct
785// declaration.
786#define MOCHI_DECLARE_MOVE_ONLY(Name) \
787 MOCHI_DECLARE_MOVE(Name); \
788 MOCHI_DECLARE_NO_COPY(Name)
789
790// Declares the default copy constructor and move constructor, but deletes assignment.
791// Used for classes/structs containing view matrices, which cannot be copied via operator= (because
792// that would copy values instead).
793#define MOCHI_DECLARE_NO_ASSIGN(Name) \
794 Name& operator=(Name const&) = delete; \
795 Name& operator=(Name&&) = delete;
796
797// Declares of class with inheritance from an empty base to activate EBCO
798// (Empty Base Class Optimization)
799#if MOCHI_COMPILER_MSVC
800#define MOCHI_EMPTY_BASE __declspec(empty_bases)
801#else
802#define MOCHI_EMPTY_BASE
803#endif
804
805/**************************************************************************************************
806 Concepts
807*/
808
809#if MOCHI_LANGUAGE_CPP
810// A unique type used in MOCHI_CONCEPT macros. Does not allow implicit conversions from other types.
811namespace superdex {
812enum class ConceptMatch { True };
813} // namespace superdex
814#endif // MOCHI_LANGUAGE_CPP
815
816// Use this in function declarations that would 'requires' a type trait. Similar to C++20 concepts.
817//
818// Example:
819//
820// // This function only matches overload resolution for arithmetic types
821// template <typename T, MOCHI_CONCEPT(std::is_arithmetic_v<T>)>
822// void Foo(T value);
824#define MOCHI_CONCEPT(a) std::enable_if_t<a, superdex::ConceptMatch> = superdex::ConceptMatch::True
825
826// Use this in function definitions where the declaration is elsewhere.
827#define MOCHI_CONCEPT_DEF(a) std::enable_if_t<a, superdex::ConceptMatch>
828
829/**************************************************************************************************
830 Branch Prediction Hints
831
832 Examples:
833 if (condition) MOCHI_LIKELY {} // Hint: This branch will probably be taken
834 if (condition) MOCHI_UNLIKELY {} // Hint: This branch will probably NOT be taken.
835*/
836#if MOCHI_LANGUAGE_CPP20
837#define MOCHI_LIKELY [[likely]]
838#define MOCHI_UNLIKELY [[unlikely]]
839#else
840#define MOCHI_LIKELY
841#define MOCHI_UNLIKELY
842#endif
843
844/**************************************************************************************************
845 Validation of the above macros
846*/
847
848#if !(MOCHI_COMPILER_CUDA || MOCHI_COMPILER_MSVC || MOCHI_COMPILER_GCC || MOCHI_COMPILER_CLANG)
849#error "Unknown compiler"
850#endif
851#if !( \
852 MOCHI_PLATFORM_ANDROID || MOCHI_PLATFORM_LINUX || MOCHI_PLATFORM_MACOS || \
853 MOCHI_PLATFORM_WINDOWS)
854#error "Unknown platform"
855#endif
856
857#endif // MOCHI_PLATFORM_H
#define MOCHI_WARNING_IGNORE_MSVC(X)
#define MOCHI_WARNING_ENFORCE_MSVC(X)