platform.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #if !defined(_CRT_SECURE_NO_WARNINGS)
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #endif
  7. #include <cstddef>
  8. #include <cassert>
  9. #include <cstdlib>
  10. #include <cstdio>
  11. #include <memory>
  12. #include <stdexcept>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <fstream>
  16. #include <string>
  17. #include <cstring>
  18. #include <stdint.h>
  19. #include <functional>
  20. #include <mutex>
  21. #if defined(EMBREE_SYCL_SUPPORT)
  22. #define __SYCL_USE_NON_VARIADIC_SPIRV_OCL_PRINTF__
  23. #pragma clang diagnostic push
  24. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  25. #pragma clang diagnostic ignored "-W#pragma-messages"
  26. #include <sycl/sycl.hpp>
  27. #pragma clang diagnostic pop
  28. #include "sycl.h"
  29. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  30. #define CONSTANT __attribute__((opencl_constant))
  31. #else
  32. #define CONSTANT
  33. #endif
  34. #endif
  35. ////////////////////////////////////////////////////////////////////////////////
  36. /// detect platform
  37. ////////////////////////////////////////////////////////////////////////////////
  38. /* detect 32 or 64 Intel platform */
  39. #if defined(__x86_64__) || defined(__ia64__) || defined(_M_X64)
  40. #define __X86_64__
  41. #define __X86_ASM__
  42. #elif defined(__i386__) || defined(_M_IX86)
  43. #define __X86_ASM__
  44. #endif
  45. /* detect 64 bit platform */
  46. #if defined(__X86_64__) || defined(__aarch64__)
  47. #define __64BIT__
  48. #endif
  49. /* detect Linux platform */
  50. #if defined(linux) || defined(__linux__) || defined(__LINUX__)
  51. # if !defined(__LINUX__)
  52. # define __LINUX__
  53. # endif
  54. # if !defined(__UNIX__)
  55. # define __UNIX__
  56. # endif
  57. #endif
  58. /* detect FreeBSD platform */
  59. #if defined(__FreeBSD__) || defined(__FREEBSD__)
  60. # if !defined(__FREEBSD__)
  61. # define __FREEBSD__
  62. # endif
  63. # if !defined(__UNIX__)
  64. # define __UNIX__
  65. # endif
  66. #endif
  67. /* detect Windows 95/98/NT/2000/XP/Vista/7/8/10 platform */
  68. #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)) && !defined(__CYGWIN__)
  69. # if !defined(__WIN32__)
  70. # define __WIN32__
  71. # endif
  72. #endif
  73. /* detect Cygwin platform */
  74. #if defined(__CYGWIN__)
  75. # if !defined(__UNIX__)
  76. # define __UNIX__
  77. # endif
  78. #endif
  79. /* detect MAC OS X platform */
  80. #if defined(__APPLE__) || defined(MACOSX) || defined(__MACOSX__)
  81. # if !defined(__MACOSX__)
  82. # define __MACOSX__
  83. # endif
  84. # if !defined(__UNIX__)
  85. # define __UNIX__
  86. # endif
  87. #endif
  88. /* try to detect other Unix systems */
  89. #if defined(__unix__) || defined (unix) || defined(__unix) || defined(_unix)
  90. # if !defined(__UNIX__)
  91. # define __UNIX__
  92. # endif
  93. #endif
  94. ////////////////////////////////////////////////////////////////////////////////
  95. /// Macros
  96. ////////////////////////////////////////////////////////////////////////////////
  97. #ifdef __WIN32__
  98. # if defined(EMBREE_STATIC_LIB)
  99. # define dll_export
  100. # define dll_import
  101. # else
  102. # define dll_export __declspec(dllexport)
  103. # define dll_import __declspec(dllimport)
  104. # endif
  105. #else
  106. # define dll_export __attribute__ ((visibility ("default")))
  107. # define dll_import
  108. #endif
  109. #if defined(__WIN32__) && !defined(__MINGW32__)
  110. #if !defined(__noinline)
  111. #define __noinline __declspec(noinline)
  112. #endif
  113. //#define __forceinline __forceinline
  114. //#define __restrict __restrict
  115. #if defined(__INTEL_COMPILER)
  116. #define __restrict__ __restrict
  117. #else
  118. #define __restrict__ //__restrict // causes issues with MSVC
  119. #endif
  120. #if !defined(__thread) && !defined(__INTEL_LLVM_COMPILER)
  121. #define __thread __declspec(thread)
  122. #endif
  123. #if !defined(__aligned)
  124. #define __aligned(...) __declspec(align(__VA_ARGS__))
  125. #endif
  126. //#define __FUNCTION__ __FUNCTION__
  127. #define debugbreak() __debugbreak()
  128. #else
  129. #if !defined(__noinline)
  130. #define __noinline __attribute__((noinline))
  131. #endif
  132. #if !defined(__forceinline)
  133. #define __forceinline inline __attribute__((always_inline))
  134. #endif
  135. //#define __restrict __restrict
  136. //#define __thread __thread
  137. #if !defined(__aligned)
  138. #define __aligned(...) __attribute__((aligned(__VA_ARGS__)))
  139. #endif
  140. #if !defined(__FUNCTION__)
  141. #define __FUNCTION__ __PRETTY_FUNCTION__
  142. #endif
  143. #define debugbreak() asm ("int $3")
  144. #endif
  145. #if defined(__clang__) || defined(__GNUC__)
  146. #define MAYBE_UNUSED __attribute__((unused))
  147. #else
  148. #define MAYBE_UNUSED
  149. #endif
  150. #if !defined(_unused)
  151. #define _unused(x) ((void)(x))
  152. #endif
  153. #if defined(_MSC_VER) && (_MSC_VER < 1900) // before VS2015 deleted functions are not supported properly
  154. #define DELETED
  155. #else
  156. #define DELETED = delete
  157. #endif
  158. #if !defined(likely)
  159. #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) || defined(__SYCL_DEVICE_ONLY__)
  160. #define likely(expr) (expr)
  161. #define unlikely(expr) (expr)
  162. #else
  163. #define likely(expr) __builtin_expect((bool)(expr),true )
  164. #define unlikely(expr) __builtin_expect((bool)(expr),false)
  165. #endif
  166. #endif
  167. ////////////////////////////////////////////////////////////////////////////////
  168. /// Error handling and debugging
  169. ////////////////////////////////////////////////////////////////////////////////
  170. /* debug printing macros */
  171. #define STRING(x) #x
  172. #define TOSTRING(x) STRING(x)
  173. #define PING embree_cout_uniform << __FILE__ << " (" << __LINE__ << "): " << __FUNCTION__ << embree_endl
  174. #define PRINT(x) embree_cout << STRING(x) << " = " << (x) << embree_endl
  175. #define PRINT2(x,y) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << embree_endl
  176. #define PRINT3(x,y,z) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << embree_endl
  177. #define PRINT4(x,y,z,w) embree_cout << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << ", " << STRING(w) << " = " << (w) << embree_endl
  178. #define UPRINT(x) embree_cout_uniform << STRING(x) << " = " << (x) << embree_endl
  179. #define UPRINT2(x,y) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << embree_endl
  180. #define UPRINT3(x,y,z) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << embree_endl
  181. #define UPRINT4(x,y,z,w) embree_cout_uniform << STRING(x) << " = " << (x) << ", " << STRING(y) << " = " << (y) << ", " << STRING(z) << " = " << (z) << ", " << STRING(w) << " = " << (w) << embree_endl
  182. #if defined(DEBUG) // only report file and line in debug mode
  183. //#define THROW_RUNTIME_ERROR(str) \
  184. // throw std::runtime_error(std::string(__FILE__) + " (" + toString(__LINE__) + "): " + std::string(str));
  185. #define THROW_RUNTIME_ERROR(str) \
  186. printf("%s (%d): %s", __FILE__, __LINE__, std::string(str).c_str()), abort();
  187. #else
  188. //#define THROW_RUNTIME_ERROR(str) \
  189. // throw std::runtime_error(str);
  190. #define THROW_RUNTIME_ERROR(str) \
  191. abort();
  192. #endif
  193. #define FATAL(x) THROW_RUNTIME_ERROR(x)
  194. #define WARNING(x) { std::cerr << "Warning: " << x << embree_endl << std::flush; }
  195. #define NOT_IMPLEMENTED FATAL(std::string(__FUNCTION__) + " not implemented")
  196. ////////////////////////////////////////////////////////////////////////////////
  197. /// Basic types
  198. ////////////////////////////////////////////////////////////////////////////////
  199. /* default floating-point type */
  200. namespace embree {
  201. typedef float real;
  202. }
  203. /* windows does not have ssize_t */
  204. #if defined(__WIN32__)
  205. #if defined(__64BIT__)
  206. typedef int64_t ssize_t;
  207. #else
  208. typedef int32_t ssize_t;
  209. #endif
  210. #endif
  211. ////////////////////////////////////////////////////////////////////////////////
  212. /// Basic utility functions
  213. ////////////////////////////////////////////////////////////////////////////////
  214. __forceinline std::string toString(long long value) {
  215. return std::to_string(value);
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. /// Disable some compiler warnings
  219. ////////////////////////////////////////////////////////////////////////////////
  220. #if defined(__INTEL_COMPILER)
  221. //#pragma warning(disable:265 ) // floating-point operation result is out of range
  222. //#pragma warning(disable:383 ) // value copied to temporary, reference to temporary used
  223. //#pragma warning(disable:869 ) // parameter was never referenced
  224. //#pragma warning(disable:981 ) // operands are evaluated in unspecified order
  225. //#pragma warning(disable:1418) // external function definition with no prior declaration
  226. //#pragma warning(disable:1419) // external declaration in primary source file
  227. //#pragma warning(disable:1572) // floating-point equality and inequality comparisons are unreliable
  228. //#pragma warning(disable:94 ) // the size of an array must be greater than zero
  229. //#pragma warning(disable:1599) // declaration hides parameter
  230. //#pragma warning(disable:424 ) // extra ";" ignored
  231. #pragma warning(disable:2196) // routine is both "inline" and "noinline"
  232. //#pragma warning(disable:177 ) // label was declared but never referenced
  233. //#pragma warning(disable:114 ) // function was referenced but not defined
  234. //#pragma warning(disable:819 ) // template nesting depth does not match the previous declaration of function
  235. #pragma warning(disable:15335) // was not vectorized: vectorization possible but seems inefficient
  236. #endif
  237. #if defined(_MSC_VER)
  238. //#pragma warning(disable:4200) // nonstandard extension used : zero-sized array in struct/union
  239. #pragma warning(disable:4800) // forcing value to bool 'true' or 'false' (performance warning)
  240. //#pragma warning(disable:4267) // '=' : conversion from 'size_t' to 'unsigned long', possible loss of data
  241. #pragma warning(disable:4244) // 'argument' : conversion from 'ssize_t' to 'unsigned int', possible loss of data
  242. #pragma warning(disable:4267) // conversion from 'size_t' to 'const int', possible loss of data
  243. //#pragma warning(disable:4355) // 'this' : used in base member initializer list
  244. //#pragma warning(disable:391 ) // '<=' : signed / unsigned mismatch
  245. //#pragma warning(disable:4018) // '<' : signed / unsigned mismatch
  246. //#pragma warning(disable:4305) // 'initializing' : truncation from 'double' to 'float'
  247. //#pragma warning(disable:4068) // unknown pragma
  248. //#pragma warning(disable:4146) // unary minus operator applied to unsigned type, result still unsigned
  249. //#pragma warning(disable:4838) // conversion from 'unsigned int' to 'const int' requires a narrowing conversion)
  250. //#pragma warning(disable:4227) // anachronism used : qualifiers on reference are ignored
  251. #pragma warning(disable:4503) // decorated name length exceeded, name was truncated
  252. #pragma warning(disable:4180) // qualifier applied to function type has no meaning; ignored
  253. #pragma warning(disable:4258) // definition from the for loop is ignored; the definition from the enclosing scope is used
  254. # if _MSC_VER < 1910 // prior to Visual studio 2017 (V141)
  255. # pragma warning(disable:4101) // warning C4101: 'x': unreferenced local variable // a compiler bug issues wrong warnings
  256. # pragma warning(disable:4789) // buffer '' of size 8 bytes will be overrun; 32 bytes will be written starting at offset 0
  257. # endif
  258. #endif
  259. #if defined(__clang__) && !defined(__INTEL_COMPILER)
  260. //#pragma clang diagnostic ignored "-Wunknown-pragmas"
  261. //#pragma clang diagnostic ignored "-Wunused-variable"
  262. //#pragma clang diagnostic ignored "-Wreorder"
  263. //#pragma clang diagnostic ignored "-Wmicrosoft"
  264. //#pragma clang diagnostic ignored "-Wunused-private-field"
  265. //#pragma clang diagnostic ignored "-Wunused-local-typedef"
  266. //#pragma clang diagnostic ignored "-Wunused-function"
  267. //#pragma clang diagnostic ignored "-Wnarrowing"
  268. //#pragma clang diagnostic ignored "-Wc++11-narrowing"
  269. //#pragma clang diagnostic ignored "-Wdeprecated-register"
  270. //#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  271. #endif
  272. #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__)
  273. #pragma GCC diagnostic ignored "-Wpragmas"
  274. //#pragma GCC diagnostic ignored "-Wnarrowing"
  275. #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  276. //#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  277. //#pragma GCC diagnostic ignored "-Warray-bounds"
  278. #pragma GCC diagnostic ignored "-Wattributes"
  279. #pragma GCC diagnostic ignored "-Wmisleading-indentation"
  280. #pragma GCC diagnostic ignored "-Wsign-compare"
  281. #pragma GCC diagnostic ignored "-Wparentheses"
  282. #endif
  283. #if defined(__clang__) && defined(__WIN32__)
  284. #pragma clang diagnostic ignored "-Wunused-parameter"
  285. #pragma clang diagnostic ignored "-Wmicrosoft-cast"
  286. #pragma clang diagnostic ignored "-Wmicrosoft-enum-value"
  287. #pragma clang diagnostic ignored "-Wmicrosoft-include"
  288. #pragma clang diagnostic ignored "-Wunused-function"
  289. #pragma clang diagnostic ignored "-Wunknown-pragmas"
  290. #endif
  291. /* disabling deprecated warning, please use only where use of deprecated Embree API functions is desired */
  292. #if defined(__WIN32__) && defined(__INTEL_COMPILER)
  293. #define DISABLE_DEPRECATED_WARNING __pragma(warning (disable: 1478)) // warning: function was declared deprecated
  294. #define ENABLE_DEPRECATED_WARNING __pragma(warning (enable: 1478)) // warning: function was declared deprecated
  295. #elif defined(__INTEL_COMPILER)
  296. #define DISABLE_DEPRECATED_WARNING _Pragma("warning (disable: 1478)") // warning: function was declared deprecated
  297. #define ENABLE_DEPRECATED_WARNING _Pragma("warning (enable : 1478)") // warning: function was declared deprecated
  298. #elif defined(__clang__)
  299. #define DISABLE_DEPRECATED_WARNING _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") // warning: xxx is deprecated
  300. #define ENABLE_DEPRECATED_WARNING _Pragma("clang diagnostic warning \"-Wdeprecated-declarations\"") // warning: xxx is deprecated
  301. #elif defined(__GNUC__)
  302. #define DISABLE_DEPRECATED_WARNING _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") // warning: xxx is deprecated
  303. #define ENABLE_DEPRECATED_WARNING _Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"") // warning: xxx is deprecated
  304. #elif defined(_MSC_VER)
  305. #define DISABLE_DEPRECATED_WARNING __pragma(warning (disable: 4996)) // warning: function was declared deprecated
  306. #define ENABLE_DEPRECATED_WARNING __pragma(warning (enable : 4996)) // warning: function was declared deprecated
  307. #endif
  308. ////////////////////////////////////////////////////////////////////////////////
  309. /// SYCL specific
  310. ////////////////////////////////////////////////////////////////////////////////
  311. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  312. #define sycl_printf0(format, ...) { \
  313. static const CONSTANT char fmt[] = format; \
  314. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true))) \
  315. sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \
  316. }
  317. #define sycl_printf0_(format) { \
  318. static const CONSTANT char fmt[] = format; \
  319. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true))) \
  320. sycl::ext::oneapi::experimental::printf(fmt); \
  321. }
  322. #else
  323. #define sycl_printf0(format, ...) { \
  324. static const CONSTANT char fmt[] = format; \
  325. sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \
  326. }
  327. #define sycl_printf0_(format) { \
  328. static const CONSTANT char fmt[] = format; \
  329. sycl::ext::oneapi::experimental::printf(fmt); \
  330. }
  331. #endif
  332. #define sycl_printf(format, ...) { \
  333. static const CONSTANT char fmt[] = format; \
  334. sycl::ext::oneapi::experimental::printf(fmt, __VA_ARGS__ ); \
  335. }
  336. #define sycl_printf_(format) { \
  337. static const CONSTANT char fmt[] = format; \
  338. sycl::ext::oneapi::experimental::printf(fmt); \
  339. }
  340. #if defined(EMBREE_SYCL_SUPPORT) && defined(__SYCL_DEVICE_ONLY__)
  341. namespace embree
  342. {
  343. struct sycl_ostream_ {
  344. sycl_ostream_ (bool uniform) : uniform(uniform) {}
  345. bool uniform = false;
  346. };
  347. struct sycl_endl_ {};
  348. #define embree_ostream embree::sycl_ostream_
  349. #define embree_cout embree::sycl_ostream_(false)
  350. #define embree_cout_uniform embree::sycl_ostream_(true)
  351. #define embree_endl embree::sycl_endl_()
  352. inline sycl_ostream_ operator <<(sycl_ostream_ cout, int i)
  353. {
  354. if (cout.uniform) {
  355. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  356. sycl_printf("%i",i);
  357. }
  358. else
  359. sycl_printf("%i ",i);
  360. return cout;
  361. }
  362. inline sycl_ostream_ operator <<(sycl_ostream_ cout, unsigned int i)
  363. {
  364. if (cout.uniform) {
  365. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  366. sycl_printf("%u",i);
  367. } else
  368. sycl_printf("%u ",i);
  369. return cout;
  370. }
  371. inline sycl_ostream_ operator <<(sycl_ostream_ cout, float f)
  372. {
  373. if (cout.uniform) {
  374. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  375. sycl_printf("%f",f);
  376. } else
  377. sycl_printf("%f ",f);
  378. return cout;
  379. }
  380. inline sycl_ostream_ operator <<(sycl_ostream_ cout, double d)
  381. {
  382. if (cout.uniform) {
  383. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  384. sycl_printf("%f",d);
  385. } else
  386. sycl_printf("%f ",d);
  387. return cout;
  388. }
  389. inline sycl_ostream_ operator <<(sycl_ostream_ cout, uint64_t l)
  390. {
  391. if (cout.uniform) {
  392. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  393. sycl_printf("%lu",l);
  394. } else
  395. sycl_printf("%lu ",l);
  396. return cout;
  397. }
  398. inline sycl_ostream_ operator <<(sycl_ostream_ cout, long l)
  399. {
  400. if (cout.uniform) {
  401. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  402. sycl_printf("%l",l);
  403. } else
  404. sycl_printf("%l ",l);
  405. return cout;
  406. }
  407. inline sycl_ostream_ operator <<(sycl_ostream_ cout, void* p)
  408. {
  409. if (cout.uniform) {
  410. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  411. sycl_printf("%p",p);
  412. } else
  413. sycl_printf("%p ",p);
  414. return cout;
  415. }
  416. inline sycl_ostream_ operator <<(sycl_ostream_ cout, const char* c)
  417. {
  418. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  419. sycl_printf("%s",c);
  420. return cout;
  421. }
  422. inline sycl_ostream_ operator <<(sycl_ostream_ cout, sycl_endl_)
  423. {
  424. if (get_sub_group_local_id() == sycl::ctz(intel_sub_group_ballot(true)))
  425. sycl_printf_("\n");
  426. return cout;
  427. }
  428. }
  429. #else
  430. #define embree_ostream std::ostream&
  431. #define embree_cout std::cout
  432. #define embree_cout_uniform std::cout
  433. #define embree_endl std::endl
  434. #endif
  435. #if defined(EMBREE_SYCL_SUPPORT)
  436. /* printing out sycle vector types */
  437. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::float4& v) {
  438. return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";
  439. }
  440. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::float3& v) {
  441. return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";
  442. }
  443. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::float2& v) {
  444. return out << "(" << v.x() << "," << v.y() << ")";
  445. }
  446. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::int4& v) {
  447. return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";
  448. }
  449. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::int3& v) {
  450. return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";
  451. }
  452. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::int2& v) {
  453. return out << "(" << v.x() << "," << v.y() << ")";
  454. }
  455. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint4& v) {
  456. return out << "(" << v.x() << "," << v.y() << "," << v.z() << "," << v.w() << ")";
  457. }
  458. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint3& v) {
  459. return out << "(" << v.x() << "," << v.y() << "," << v.z() << ")";
  460. }
  461. __forceinline embree_ostream operator<<(embree_ostream out, const sycl::uint2& v) {
  462. return out << "(" << v.x() << "," << v.y() << ")";
  463. }
  464. #endif
  465. inline void tab(std::ostream& cout, int n) {
  466. for (int i=0; i<n; i++) cout << " ";
  467. }
  468. inline std::string tab(int depth) {
  469. return std::string(2*depth,' ');
  470. }
  471. ////////////////////////////////////////////////////////////////////////////////
  472. /// Some macros for static profiling
  473. ////////////////////////////////////////////////////////////////////////////////
  474. #if defined (__GNUC__)
  475. #define IACA_SSC_MARK( MARK_ID ) \
  476. __asm__ __volatile__ ( \
  477. "\n\t movl $"#MARK_ID", %%ebx" \
  478. "\n\t .byte 0x64, 0x67, 0x90" \
  479. : : : "memory" );
  480. #define IACA_UD_BYTES __asm__ __volatile__ ("\n\t .byte 0x0F, 0x0B");
  481. #else
  482. #define IACA_UD_BYTES {__asm _emit 0x0F \
  483. __asm _emit 0x0B}
  484. #define IACA_SSC_MARK(x) {__asm mov ebx, x\
  485. __asm _emit 0x64 \
  486. __asm _emit 0x67 \
  487. __asm _emit 0x90 }
  488. #define IACA_VC64_START __writegsbyte(111, 111);
  489. #define IACA_VC64_END __writegsbyte(222, 222);
  490. #endif
  491. #define IACA_START {IACA_UD_BYTES \
  492. IACA_SSC_MARK(111)}
  493. #define IACA_END {IACA_SSC_MARK(222) \
  494. IACA_UD_BYTES}
  495. namespace embree
  496. {
  497. template<typename Closure>
  498. struct OnScopeExitHelper
  499. {
  500. OnScopeExitHelper (const Closure f) : active(true), f(f) {}
  501. ~OnScopeExitHelper() { if (active) f(); }
  502. void deactivate() { active = false; }
  503. bool active;
  504. const Closure f;
  505. };
  506. template <typename Closure>
  507. OnScopeExitHelper<Closure> OnScopeExit(const Closure f) {
  508. return OnScopeExitHelper<Closure>(f);
  509. }
  510. #define STRING_JOIN2(arg1, arg2) DO_STRING_JOIN2(arg1, arg2)
  511. #define DO_STRING_JOIN2(arg1, arg2) arg1 ## arg2
  512. #define ON_SCOPE_EXIT(code) \
  513. auto STRING_JOIN2(on_scope_exit_, __LINE__) = OnScopeExit([&](){code;})
  514. template<typename Ty>
  515. std::unique_ptr<Ty> make_unique(Ty* ptr) {
  516. return std::unique_ptr<Ty>(ptr);
  517. }
  518. }