19#ifndef _USE_MATH_DEFINES
20# define _USE_MATH_DEFINES
40#ifndef WIN32_LEAN_AND_MEAN
41#define WIN32_LEAN_AND_MEAN
57# define OWL_DLL_EXPORT __declspec(dllexport)
58# define OWL_DLL_IMPORT __declspec(dllimport)
59#elif defined(__clang__) || defined(__GNUC__)
60# define OWL_DLL_EXPORT __attribute__((visibility("default")))
61# define OWL_DLL_IMPORT __attribute__((visibility("default")))
63# define OWL_DLL_EXPORT
64# define OWL_DLL_IMPORT
86# define __PRETTY_FUNCTION__ __FUNCTION__
91# define PRINT(var) std::cout << #var << "=" << var << std::endl;
93# define PING std::cout << __FILE__ << "::" << __LINE__ << ": " << __FUNCTION__ << std::endl;
95# define PING std::cout << __FILE__ << "::" << __LINE__ << ": " << __PRETTY_FUNCTION__ << std::endl;
99#if defined(__CUDA_ARCH__)
100# define __owl_device __device__
101# define __owl_host __host__
107# define __both__ __owl_host __owl_device
111#define MAYBE_UNUSED __attribute__((unused))
117inline static std::string backtrace()
120 static const int max_frames = 16;
122 void* buffer[max_frames] = { 0 };
123 int cnt = ::backtrace(buffer,max_frames);
125 char** symbols = backtrace_symbols(buffer,cnt);
128 std::stringstream str;
129 for (
int n = 1; n < cnt; ++n)
131 str << symbols[n] <<
'\n';
138 return "not implemented yet";
142inline void owlRaise_impl(std::string str)
144 fprintf(stderr,
"%s\n",str.c_str());
146 if (IsDebuggerPresent())
149 throw std::runtime_error(str);
152 std::string bt = ::detail::backtrace();
153 fprintf(stderr,
"%s\n",bt.c_str());
160#define OWL_RAISE(MSG) ::detail::owlRaise_impl(MSG);
163#define OWL_NOTIMPLEMENTED throw std::runtime_error(std::string(__PRETTY_FUNCTION__)+" not implemented")
166# define OWL_TERMINAL_RED ""
167# define OWL_TERMINAL_GREEN ""
168# define OWL_TERMINAL_LIGHT_GREEN ""
169# define OWL_TERMINAL_YELLOW ""
170# define OWL_TERMINAL_BLUE ""
171# define OWL_TERMINAL_LIGHT_BLUE ""
172# define OWL_TERMINAL_RESET ""
173# define OWL_TERMINAL_DEFAULT OWL_TERMINAL_RESET
174# define OWL_TERMINAL_BOLD ""
176# define OWL_TERMINAL_MAGENTA ""
177# define OWL_TERMINAL_LIGHT_MAGENTA ""
178# define OWL_TERMINAL_CYAN ""
179# define OWL_TERMINAL_LIGHT_RED ""
181# define OWL_TERMINAL_RED "\033[0;31m"
182# define OWL_TERMINAL_GREEN "\033[0;32m"
183# define OWL_TERMINAL_LIGHT_GREEN "\033[1;32m"
184# define OWL_TERMINAL_YELLOW "\033[1;33m"
185# define OWL_TERMINAL_BLUE "\033[0;34m"
186# define OWL_TERMINAL_LIGHT_BLUE "\033[1;34m"
187# define OWL_TERMINAL_RESET "\033[0m"
188# define OWL_TERMINAL_DEFAULT OWL_TERMINAL_RESET
189# define OWL_TERMINAL_BOLD "\033[1;1m"
191# define OWL_TERMINAL_MAGENTA "\e[35m"
192# define OWL_TERMINAL_LIGHT_MAGENTA "\e[95m"
193# define OWL_TERMINAL_CYAN "\e[36m"
194# define OWL_TERMINAL_LIGHT_RED "\033[1;31m"
198# define OWL_ALIGN(alignment) __declspec(align(alignment))
200# define OWL_ALIGN(alignment) __attribute__((aligned(alignment)))
229 inline __both__
float rcp(
float f) {
return 1.f/f; }
230 inline __both__
double rcp(
double d) {
return 1./d; }
232 inline __both__ int32_t divRoundUp(int32_t a, int32_t b) {
return (a+b-1)/b; }
233 inline __both__ uint32_t divRoundUp(uint32_t a, uint32_t b) {
return (a+b-1)/b; }
234 inline __both__ int64_t divRoundUp(int64_t a, int64_t b) {
return (a+b-1)/b; }
235 inline __both__ uint64_t divRoundUp(uint64_t a, uint64_t b) {
return (a+b-1)/b; }
253 namespace polymorphic {
255 inline __both__
float sqrt(
const float f) { return ::sqrtf(f); }
256 inline __both__
double sqrt(
const double d) { return ::sqrt(d); }
258 inline __both__
float sqrt(
const float f) { return ::sqrtf(f); }
259 inline __both__
double sqrt(
const double d) { return ::sqrt(d); }
262 inline __both__
float rsqrt(
const float f) {
return 1.f/owl::common::polymorphic::sqrt(f); }
263 inline __both__
double rsqrt(
const double d) {
return 1./owl::common::polymorphic::sqrt(d); }
268# define osp_snprintf sprintf_s
270# define osp_snprintf snprintf
274 inline std::string prettyDouble(
const double val) {
275 const double absVal = abs(val);
278 if (absVal >= 1e+18f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e18f),
'E');
279 else if (absVal >= 1e+15f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e15f),
'P');
280 else if (absVal >= 1e+12f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e12f),
'T');
281 else if (absVal >= 1e+09f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e09f),
'G');
282 else if (absVal >= 1e+06f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e06f),
'M');
283 else if (absVal >= 1e+03f) osp_snprintf(result,1000,
"%.1f%c",
float(val/1e03f),
'k');
284 else if (absVal <= 1e-12f) osp_snprintf(result,1000,
"%.1f%c",
float(val*1e15f),
'f');
285 else if (absVal <= 1e-09f) osp_snprintf(result,1000,
"%.1f%c",
float(val*1e12f),
'p');
286 else if (absVal <= 1e-06f) osp_snprintf(result,1000,
"%.1f%c",
float(val*1e09f),
'n');
287 else if (absVal <= 1e-03f) osp_snprintf(result,1000,
"%.1f%c",
float(val*1e06f),
'u');
288 else if (absVal <= 1e-00f) osp_snprintf(result,1000,
"%.1f%c",
float(val*1e03f),
'm');
289 else osp_snprintf(result,1000,
"%f",(
float)val);
299 inline std::string prettyNumber(
const size_t s)
302 if (s >= (1000LL*1000LL*1000LL*1000LL)) {
303 osp_snprintf(buf, 1000,
"%.2fT",s/(1000.f*1000.f*1000.f*1000.f));
304 }
else if (s >= (1000LL*1000LL*1000LL)) {
305 osp_snprintf(buf, 1000,
"%.2fG",s/(1000.f*1000.f*1000.f));
306 }
else if (s >= (1000LL*1000LL)) {
307 osp_snprintf(buf, 1000,
"%.2fM",s/(1000.f*1000.f));
308 }
else if (s >= (1000LL)) {
309 osp_snprintf(buf, 1000,
"%.2fK",s/(1000.f));
311 osp_snprintf(buf,1000,
"%zi",s);
319 inline std::string prettyBytes(
const size_t s)
322 if (s >= (1024LL*1024LL*1024LL*1024LL)) {
323 osp_snprintf(buf, 1000,
"%.2fT",s/(1024.f*1024.f*1024.f*1024.f));
324 }
else if (s >= (1024LL*1024LL*1024LL)) {
325 osp_snprintf(buf, 1000,
"%.2fG",s/(1024.f*1024.f*1024.f));
326 }
else if (s >= (1024LL*1024LL)) {
327 osp_snprintf(buf, 1000,
"%.2fM",s/(1024.f*1024.f));
328 }
else if (s >= (1024LL)) {
329 osp_snprintf(buf, 1000,
"%.2fK",s/(1024.f));
331 osp_snprintf(buf,1000,
"%zi",s);
336 inline double getCurrentTime()
339 SYSTEMTIME tp; GetSystemTime(&tp);
343 size_t numSecsSince2020
345 + (60ull) * tp.wMinute
346 + (60ull * 60ull) * tp.wHour
347 + (60ull * 60ul * 24ull) * tp.wDay
348 + (60ull * 60ul * 24ull * 365ull) * (tp.wYear - 2020);
349 return double(numSecsSince2020 + tp.wMilliseconds * 1e-3);
351 struct timeval tp; gettimeofday(&tp,
nullptr);
352 return double(tp.tv_sec) + double(tp.tv_usec)/1E6;
356 inline bool hasSuffix(
const std::string &s,
const std::string &suffix)
358 return s.substr(s.size()-suffix.size()) == suffix;