Path: blob/master/thirdparty/embree/common/sys/platform.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#if !defined(_CRT_SECURE_NO_WARNINGS)6#define _CRT_SECURE_NO_WARNINGS7#endif89#include <cstddef>10#include <cassert>11#include <cstdlib>12#include <cstdio>13#include <memory>14#include <stdexcept>15#include <iostream>16#include <iomanip>17#include <fstream>18#include <string>19#include <cstring>20#include <stdint.h>21#include <functional>22#include <mutex>2324#if defined(EMBREE_SYCL_SUPPORT)2526#define __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__2728#pragma clang diagnostic push29#pragma clang diagnostic ignored "-Wdeprecated-declarations"30#pragma clang diagnostic ignored "-W#pragma-messages"3132#include <sycl/sycl.hpp>3334#pragma clang diagnostic pop3536#include "sycl.h"3738#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)39#define CONSTANT __attribute__((opencl_constant))40#else41#define CONSTANT42#endif4344#endif454647////////////////////////////////////////////////////////////////////////////////48/// detect platform49////////////////////////////////////////////////////////////////////////////////5051/* detect 32 or 64 Intel platform */52#if defined(__x86_64__) || defined(__ia64__) || defined(_M_X64)53#define __X86_64__54#define __X86_ASM__55#elif defined(__i386__) || defined(_M_IX86)56#define __X86_ASM__57#endif5859/* detect 64 bit platform */60#if defined(__X86_64__) || defined(__aarch64__)61#define __64BIT__62#endif6364/* detect Linux platform */65#if defined(linux) || defined(__linux__) || defined(__LINUX__)66# if !defined(__LINUX__)67# define __LINUX__68# endif69# if !defined(__UNIX__)70# define __UNIX__71# endif72#endif7374/* detect FreeBSD platform */75#if defined(__FreeBSD__) || defined(__FREEBSD__)76# if !defined(__FREEBSD__)77# define __FREEBSD__78# endif79# if !defined(__UNIX__)80# define __UNIX__81# endif82#endif8384/* detect Windows 95/98/NT/2000/XP/Vista/7/8/10 platform */85#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)) && !defined(__CYGWIN__)86# if !defined(__WIN32__)87# define __WIN32__88# endif89#endif9091/* detect Cygwin platform */92#if defined(__CYGWIN__)93# if !defined(__UNIX__)94# define __UNIX__95# endif96#endif9798/* detect MAC OS X platform */99#if defined(__APPLE__) || defined(MACOSX) || defined(__MACOSX__)100# if !defined(__MACOSX__)101# define __MACOSX__102# endif103# if !defined(__UNIX__)104# define __UNIX__105# endif106#endif107108/* try to detect other Unix systems */109#if defined(__unix__) || defined (unix) || defined(__unix) || defined(_unix)110# if !defined(__UNIX__)111# define __UNIX__112# endif113#endif114115////////////////////////////////////////////////////////////////////////////////116/// Macros117////////////////////////////////////////////////////////////////////////////////118119#ifdef __WIN32__120# if defined(EMBREE_STATIC_LIB)121# define dll_export122# define dll_import123# else124# define dll_export __declspec(dllexport)125# define dll_import __declspec(dllimport)126# endif127#else128# define dll_export __attribute__ ((visibility ("default")))129# define dll_import130#endif131132#if defined(__WIN32__) && !defined(__MINGW32__)133#if !defined(__noinline)134#define __noinline __declspec(noinline)135#endif136//#define __forceinline __forceinline137//#define __restrict __restrict138#if defined(__INTEL_COMPILER)139#define __restrict__ __restrict140#else141#define __restrict__ //__restrict // causes issues with MSVC142#endif143#if !defined(__thread) && !defined(__INTEL_LLVM_COMPILER)144#define __thread __declspec(thread)145#endif146#if !defined(__aligned)147#define __aligned(...) __declspec(align(__VA_ARGS__))148#endif149//#define __FUNCTION__ __FUNCTION__150#define debugbreak() __debugbreak()151152#else153#if !defined(__noinline)154#define __noinline __attribute__((noinline))155#endif156#if !defined(__forceinline)157#define __forceinline inline __attribute__((always_inline))158#endif159//#define __restrict __restrict160//#define __thread __thread161#if !defined(__aligned)162#define __aligned(...) __attribute__((aligned(__VA_ARGS__)))163#endif164#if !defined(__FUNCTION__)165#define __FUNCTION__ __PRETTY_FUNCTION__166#endif167#define debugbreak() asm ("int $3")168#endif169170#if defined(__clang__) || defined(__GNUC__)171#define MAYBE_UNUSED __attribute__((unused))172#else173#define MAYBE_UNUSED174#endif175176#if !defined(_unused)177#define _unused(x) ((void)(x))178#endif179180#if defined(_MSC_VER) && (_MSC_VER < 1900) // before VS2015 deleted functions are not supported properly181#define DELETED182#else183#define DELETED = delete184#endif185186#if !defined(likely)187#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || defined(__SYCL_DEVICE_ONLY__)188#define likely(expr) (expr)189#define unlikely(expr) (expr)190#else191#define likely(expr) __builtin_expect((bool)(expr),true )192#define unlikely(expr) __builtin_expect((bool)(expr),false)193#endif194#endif195196////////////////////////////////////////////////////////////////////////////////197/// Error handling and debugging198////////////////////////////////////////////////////////////////////////////////199200/* debug printing macros */201#define STRING(x) #x202#define TOSTRING(x) STRING(x)203#define PING embree_cout_uniform << __FILE__ << " (" << __LINE__ << "): " << __FUNCTION__ << embree_endl204#define PRINT(x) embree_cout << STRING(x) << " = " << (x) << embree_endl205#define PRINT2(x,y) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << embree_endl206#define PRINT3(x,y,z) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << embree_endl207#define PRINT4(x,y,z,w) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << ", " << STRING(w) << " = " << (w) << embree_endl208209#define UPRINT(x) embree_cout_uniform << STRING(x) << " = " << (x) << embree_endl210#define UPRINT2(x,y) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << embree_endl211#define UPRINT3(x,y,z) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << embree_endl212#define UPRINT4(x,y,z,w) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << ", " << STRING(w) << " = " << (w) << embree_endl213214#if defined(DEBUG) // only report file and line in debug mode215#define THROW_RUNTIME_ERROR(str) \216printf("%s (%d): %s", __FILE__, __LINE__, std::string(str).c_str()), abort();217//throw std::runtime_error(std::string(__FILE__) + " (" + toString(__LINE__) + "): " + std::string(str));218#else219#define THROW_RUNTIME_ERROR(str) \220abort(); //throw std::runtime_error(str);221#endif222223#define FATAL(x) THROW_RUNTIME_ERROR(x)224#define WARNING(x) { std::cerr << "Warning: " << x << embree_endl << std::flush; }225226#define NOT_IMPLEMENTED FATAL(std::string(__FUNCTION__) + " not implemented")227228////////////////////////////////////////////////////////////////////////////////229/// Basic types230////////////////////////////////////////////////////////////////////////////////231232/* default floating-point type */233namespace embree {234typedef float real;235}236237/* windows does not have ssize_t */238#if defined(__WIN32__)239#if defined(__64BIT__)240typedef int64_t ssize_t;241#else242typedef int32_t ssize_t;243#endif244#endif245246////////////////////////////////////////////////////////////////////////////////247/// Basic utility functions248////////////////////////////////////////////////////////////////////////////////249250__forceinline std::string toString(long long value) {251return std::to_string(value);252}253254////////////////////////////////////////////////////////////////////////////////255/// Disable some compiler warnings256////////////////////////////////////////////////////////////////////////////////257258#if defined(__INTEL_COMPILER)259//#pragma warning(disable:265 ) // floating-point operation result is out of range260//#pragma warning(disable:383 ) // value copied to temporary, reference to temporary used261//#pragma warning(disable:869 ) // parameter was never referenced262//#pragma warning(disable:981 ) // operands are evaluated in unspecified order263//#pragma warning(disable:1418) // external function definition with no prior declaration264//#pragma warning(disable:1419) // external declaration in primary source file265//#pragma warning(disable:1572) // floating-point equality and inequality comparisons are unreliable266//#pragma warning(disable:94 ) // the size of an array must be greater than zero267//#pragma warning(disable:1599) // declaration hides parameter268//#pragma warning(disable:424 ) // extra ";" ignored269#pragma warning(disable:2196) // routine is both "inline" and "noinline"270//#pragma warning(disable:177 ) // label was declared but never referenced271//#pragma warning(disable:114 ) // function was referenced but not defined272//#pragma warning(disable:819 ) // template nesting depth does not match the previous declaration of function273#pragma warning(disable:15335) // was not vectorized: vectorization possible but seems inefficient274#endif275276#if defined(_MSC_VER)277//#pragma warning(disable:4200) // nonstandard extension used : zero-sized array in struct/union278#pragma warning(disable:4800) // forcing value to bool 'true' or 'false' (performance warning)279//#pragma warning(disable:4267) // '=' : conversion from 'size_t' to 'unsigned long', possible loss of data280#pragma warning(disable:4244) // 'argument' : conversion from 'ssize_t' to 'unsigned int', possible loss of data281#pragma warning(disable:4267) // conversion from 'size_t' to 'const int', possible loss of data282//#pragma warning(disable:4355) // 'this' : used in base member initializer list283//#pragma warning(disable:391 ) // '<=' : signed / unsigned mismatch284//#pragma warning(disable:4018) // '<' : signed / unsigned mismatch285//#pragma warning(disable:4305) // 'initializing' : truncation from 'double' to 'float'286//#pragma warning(disable:4068) // unknown pragma287//#pragma warning(disable:4146) // unary minus operator applied to unsigned type, result still unsigned288//#pragma warning(disable:4838) // conversion from 'unsigned int' to 'const int' requires a narrowing conversion)289//#pragma warning(disable:4227) // anachronism used : qualifiers on reference are ignored290#pragma warning(disable:4503) // decorated name length exceeded, name was truncated291#pragma warning(disable:4180) // qualifier applied to function type has no meaning; ignored292#pragma warning(disable:4258) // definition from the for loop is ignored; the definition from the enclosing scope is used293294# if _MSC_VER < 1910 // prior to Visual studio 2017 (V141)295# pragma warning(disable:4101) // warning C4101: 'x': unreferenced local variable // a compiler bug issues wrong warnings296# pragma warning(disable:4789) // buffer '' of size 8 bytes will be overrun; 32 bytes will be written starting at offset 0297# endif298299#endif300301#if defined(__clang__) && !defined(__INTEL_COMPILER)302//#pragma clang diagnostic ignored "-Wunknown-pragmas"303//#pragma clang diagnostic ignored "-Wunused-variable"304//#pragma clang diagnostic ignored "-Wreorder"305//#pragma clang diagnostic ignored "-Wmicrosoft"306//#pragma clang diagnostic ignored "-Wunused-private-field"307//#pragma clang diagnostic ignored "-Wunused-local-typedef"308//#pragma clang diagnostic ignored "-Wunused-function"309//#pragma clang diagnostic ignored "-Wnarrowing"310//#pragma clang diagnostic ignored "-Wc++11-narrowing"311//#pragma clang diagnostic ignored "-Wdeprecated-register"312//#pragma clang diagnostic ignored "-Wdeprecated-declarations"313#endif314315#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)316#pragma GCC diagnostic ignored "-Wpragmas"317//#pragma GCC diagnostic ignored "-Wnarrowing"318#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"319//#pragma GCC diagnostic ignored "-Wdeprecated-declarations"320//#pragma GCC diagnostic ignored "-Warray-bounds"321#pragma GCC diagnostic ignored "-Wattributes"322#pragma GCC diagnostic ignored "-Wmisleading-indentation"323#pragma GCC diagnostic ignored "-Wsign-compare"324#pragma GCC diagnostic ignored "-Wparentheses"325#endif326327#if defined(__clang__) && defined(__WIN32__)328#pragma clang diagnostic ignored "-Wunused-parameter"329#pragma clang diagnostic ignored "-Wmicrosoft-cast"330#pragma clang diagnostic ignored "-Wmicrosoft-enum-value"331#pragma clang diagnostic ignored "-Wmicrosoft-include"332#pragma clang diagnostic ignored "-Wunused-function"333#pragma clang diagnostic ignored "-Wunknown-pragmas"334#endif335336/* disabling deprecated warning, please use only where use of deprecated Embree API functions is desired */337#if defined(__WIN32__) && defined(__INTEL_COMPILER)338#define DISABLE_DEPRECATED_WARNING __pragma(warning (disable: 1478)) // warning: function was declared deprecated339#define ENABLE_DEPRECATED_WARNING __pragma(warning (enable: 1478)) // warning: function was declared deprecated340#elif defined(__INTEL_COMPILER)341#define DISABLE_DEPRECATED_WARNING _Pragma("warning (disable: 1478)") // warning: function was declared deprecated342#define ENABLE_DEPRECATED_WARNING _Pragma("warning (enable : 1478)") // warning: function was declared deprecated343#elif defined(__clang__)344#define DISABLE_DEPRECATED_WARNING _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") // warning: xxx is deprecated345#define ENABLE_DEPRECATED_WARNING _Pragma("clang diagnostic warning \"-Wdeprecated-declarations\"") // warning: xxx is deprecated346#elif defined(__GNUC__)347#define DISABLE_DEPRECATED_WARNING _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") // warning: xxx is deprecated348#define ENABLE_DEPRECATED_WARNING _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"") // warning: xxx is deprecated349#elif defined(_MSC_VER)350#define DISABLE_DEPRECATED_WARNING __pragma(warning (disable: 4996)) // warning: function was declared deprecated351#define ENABLE_DEPRECATED_WARNING __pragma(warning (enable : 4996)) // warning: function was declared deprecated352#endif353354////////////////////////////////////////////////////////////////////////////////355/// SYCL specific356////////////////////////////////////////////////////////////////////////////////357358359#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)360361#define sycl_printf0(format, ...) { \362static const CONSTANT char fmt[] = format; \363if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true))) \364sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \365}366367#define sycl_printf0_(format) { \368static const CONSTANT char fmt[] = format; \369if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true))) \370sycl::ext::oneapi::experimental::printf(fmt); \371}372373#else374375#define sycl_printf0(format, ...) { \376static const CONSTANT char fmt[] = format; \377sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \378}379380#define sycl_printf0_(format) { \381static const CONSTANT char fmt[] = format; \382sycl::ext::oneapi::experimental::printf(fmt); \383}384385#endif386387#define sycl_printf(format, ...) { \388static const CONSTANT char fmt[] = format; \389sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \390}391392#define sycl_printf_(format) { \393static const CONSTANT char fmt[] = format; \394sycl::ext::oneapi::experimental::printf(fmt); \395}396397#if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)398399namespace embree400{401struct sycl_ostream_ {402sycl_ostream_ (bool uniform) : uniform(uniform) {}403bool uniform = false;404};405struct sycl_endl_ {};406407#define embree_ostream embree::sycl_ostream_408#define embree_cout embree::sycl_ostream_(false)409#define embree_cout_uniform embree::sycl_ostream_(true)410#define embree_endl embree::sycl_endl_()411412inline sycl_ostream_ operator <<(sycl_ostream_ cout, int i)413{414if (cout.uniform) {415if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))416sycl_printf("%i",i);417}418else419sycl_printf("%i ",i);420421return cout;422}423424inline sycl_ostream_ operator <<(sycl_ostream_ cout, unsigned int i)425{426if (cout.uniform) {427if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))428sycl_printf("%u",i);429} else430sycl_printf("%u ",i);431432return cout;433}434435inline sycl_ostream_ operator <<(sycl_ostream_ cout, float f)436{437if (cout.uniform) {438if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))439sycl_printf("%f",f);440} else441sycl_printf("%f ",f);442443return cout;444}445446inline sycl_ostream_ operator <<(sycl_ostream_ cout, double d)447{448if (cout.uniform) {449if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))450sycl_printf("%f",d);451} else452sycl_printf("%f ",d);453454return cout;455}456457inline sycl_ostream_ operator <<(sycl_ostream_ cout, uint64_t l)458{459if (cout.uniform) {460if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))461sycl_printf("%lu",l);462} else463sycl_printf("%lu ",l);464465return cout;466}467468inline sycl_ostream_ operator <<(sycl_ostream_ cout, long l)469{470if (cout.uniform) {471if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))472sycl_printf("%l",l);473} else474sycl_printf("%l ",l);475476return cout;477}478479480inline sycl_ostream_ operator <<(sycl_ostream_ cout, void* p)481{482if (cout.uniform) {483if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))484sycl_printf("%p",p);485} else486sycl_printf("%p ",p);487488return cout;489}490491inline sycl_ostream_ operator <<(sycl_ostream_ cout, const char* c)492{493if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))494sycl_printf("%s",c);495return cout;496}497498inline sycl_ostream_ operator <<(sycl_ostream_ cout, sycl_endl_)499{500if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))501sycl_printf_("\n");502return cout;503}504}505506#else507508#define embree_ostream std::ostream&509#define embree_cout std::cout510#define embree_cout_uniform std::cout511#define embree_endl std::endl512513#endif514515#if defined(EMBREE_SYCL_SUPPORT)516517/* printing out sycle vector types */518__forceinline embree_ostream operator<<(embree_ostream out, const sycl::float4& v) {519return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";520}521__forceinline embree_ostream operator<<(embree_ostream out, const sycl::float3& v) {522return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";523}524__forceinline embree_ostream operator<<(embree_ostream out, const sycl::float2& v) {525return out << "(" << v.x() << "," << v.y() << ")";526}527__forceinline embree_ostream operator<<(embree_ostream out, const sycl::int4& v) {528return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";529}530__forceinline embree_ostream operator<<(embree_ostream out, const sycl::int3& v) {531return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";532}533__forceinline embree_ostream operator<<(embree_ostream out, const sycl::int2& v) {534return out << "(" << v.x() << "," << v.y() << ")";535}536__forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint4& v) {537return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";538}539__forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint3& v) {540return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";541}542__forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint2& v) {543return out << "(" << v.x() << "," << v.y() << ")";544}545546#endif547548inline void tab(std::ostream& cout, int n) {549for (int i=0; i<n; i++) cout << " ";550}551552inline std::string tab(int depth) {553return std::string(2*depth,' ');554}555556////////////////////////////////////////////////////////////////////////////////557/// Some macros for static profiling558////////////////////////////////////////////////////////////////////////////////559560#if defined (__GNUC__)561#define IACA_SSC_MARK( MARK_ID ) \562__asm__ __volatile__ ( \563"\n\t movl $"#MARK_ID", %%ebx" \564"\n\t .byte 0x64, 0x67, 0x90" \565: : : "memory" );566567#define IACA_UD_BYTES __asm__ __volatile__ ("\n\t .byte 0x0F, 0x0B");568569#else570#define IACA_UD_BYTES {__asm _emit 0x0F \571__asm _emit 0x0B}572573#define IACA_SSC_MARK(x) {__asm mov ebx, x\574__asm _emit 0x64 \575__asm _emit 0x67 \576__asm _emit 0x90 }577578#define IACA_VC64_START __writegsbyte(111, 111);579#define IACA_VC64_END __writegsbyte(222, 222);580581#endif582583#define IACA_START {IACA_UD_BYTES \584IACA_SSC_MARK(111)}585#define IACA_END {IACA_SSC_MARK(222) \586IACA_UD_BYTES}587588namespace embree589{590template<typename Closure>591struct OnScopeExitHelper592{593OnScopeExitHelper (const Closure f) : active(true), f(f) {}594~OnScopeExitHelper() { if (active) f(); }595void deactivate() { active = false; }596bool active;597const Closure f;598};599600template <typename Closure>601OnScopeExitHelper<Closure> OnScopeExit(const Closure f) {602return OnScopeExitHelper<Closure>(f);603}604605#define STRING_JOIN2(arg1, arg2) DO_STRING_JOIN2(arg1, arg2)606#define DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2607#define ON_SCOPE_EXIT(code) \608auto STRING_JOIN2(on_scope_exit_, __LINE__) = OnScopeExit([&](){code;})609610template<typename Ty>611std::unique_ptr<Ty> make_unique(Ty* ptr) {612return std::unique_ptr<Ty>(ptr);613}614615}616617618