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