platform.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // ======================================================================== //
  2. // Copyright 2009-2019 Intel Corporation //
  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. #pragma once
  17. #if defined(_WIN32)
  18. #define WIN32_LEAN_AND_MEAN
  19. #define NOMINMAX
  20. #include <windows.h>
  21. #elif defined(__APPLE__)
  22. #include <sys/sysctl.h>
  23. #endif
  24. #include <xmmintrin.h>
  25. #include <cstdint>
  26. #include <climits>
  27. #include <limits>
  28. #include <atomic>
  29. #include <algorithm>
  30. #include <memory>
  31. #include <cmath>
  32. #include <string>
  33. #include <sstream>
  34. #include <iostream>
  35. #include <cassert>
  36. #include "include/OpenImageDenoise/oidn.hpp"
  37. namespace oidn {
  38. // ----------------------------------------------------------------------------
  39. // Macros
  40. // ----------------------------------------------------------------------------
  41. #if defined(_WIN32)
  42. // Windows
  43. #if !defined(__noinline)
  44. #define __noinline __declspec(noinline)
  45. #endif
  46. #else
  47. // Unix
  48. #if !defined(__forceinline)
  49. #define __forceinline inline __attribute__((always_inline))
  50. #endif
  51. #if !defined(__noinline)
  52. #define __noinline __attribute__((noinline))
  53. #endif
  54. #endif
  55. #ifndef UNUSED
  56. #define UNUSED(x) ((void)x)
  57. #endif
  58. #ifndef MAYBE_UNUSED
  59. #define MAYBE_UNUSED(x) UNUSED(x)
  60. #endif
  61. // ----------------------------------------------------------------------------
  62. // Error handling and debugging
  63. // ----------------------------------------------------------------------------
  64. struct Verbose
  65. {
  66. int verbose;
  67. Verbose(int v = 0) : verbose(v) {}
  68. __forceinline bool isVerbose(int v = 1) const { return v <= verbose; }
  69. };
  70. #define OIDN_WARNING(message) { if (isVerbose()) std::cerr << "Warning: " << message << std::endl; }
  71. #define OIDN_FATAL(message) throw std::runtime_error(message);
  72. // ----------------------------------------------------------------------------
  73. // Common functions
  74. // ----------------------------------------------------------------------------
  75. using std::min;
  76. using std::max;
  77. template<typename T>
  78. __forceinline T clamp(const T& value, const T& minValue, const T& maxValue)
  79. {
  80. return min(max(value, minValue), maxValue);
  81. }
  82. void* alignedMalloc(size_t size, size_t alignment);
  83. void alignedFree(void* ptr);
  84. template<typename T>
  85. inline std::string toString(const T& a)
  86. {
  87. std::stringstream sm;
  88. sm << a;
  89. return sm.str();
  90. }
  91. #if defined(__APPLE__)
  92. template<typename T>
  93. bool getSysctl(const char* name, T& value)
  94. {
  95. int64_t result = 0;
  96. size_t size = sizeof(result);
  97. if (sysctlbyname(name, &result, &size, nullptr, 0) != 0)
  98. return false;
  99. value = T(result);
  100. return true;
  101. }
  102. #endif
  103. // ----------------------------------------------------------------------------
  104. // System information
  105. // ----------------------------------------------------------------------------
  106. std::string getPlatformName();
  107. std::string getCompilerName();
  108. std::string getBuildName();
  109. } // namespace oidn