HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
os_detect.hpp
1// Copyright Take Vos 2019-2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include <exception>
8#include <cstddef>
9#include <type_traits>
10
11namespace tt {
12
13#define TT_BT_DEBUG 'D'
14#define TT_BT_RELEASE 'R'
15
16#if defined(NDEBUG)
17#define TT_BUILD_TYPE TT_BT_RELEASE
18#else
19#define TT_BUILD_TYPE TT_BT_DEBUG
20#endif
21
22enum class BuildType {
23 Debug = TT_BT_DEBUG,
24 Release = TT_BT_RELEASE,
25
26 current = TT_BUILD_TYPE
27};
28
29#define TT_OS_WINDOWS 'W'
30#define TT_OS_MACOS 'M'
31#define TT_OS_IOS 'i'
32#define TT_OS_LINUX 'L'
33#define TT_OS_POSIX 'P'
34#define TT_OS_UNIX 'U'
35#define TT_OS_ANDROID 'A'
36
37/* Create specific macros to detect the operating system.
38 */
39#if defined(_WIN64)
40#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
41#elif defined(_WIN32)
42#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
43#elif defined(__APPLE__)
44 #include "TargetConditionals.h"
45 #if TARGET_OS_IPHONE == 1
46 #define TT_OPERATING_SYSTEM TT_OS_IOS
47 #else
48 #define TT_OPERATING_SYSTEM TT_OS_MACOS
49 #endif
50#elif defined(__ANDROID__)
51#define TT_OPERATING_SYSTEM TT_OS_ANDROID
52#elif defined(__linux)
53#define TT_OPERATING_SYSTEM TT_OS_LINUX
54#elif defined(__unix)
55#define TT_OPERATING_SYSTEM TT_OS_UNIX
56#elif defined(__posix)
57#define TT_OPERATING_SYSTEM TT_OS_POSIX
58#else
59#error "Could not detect the operating system."
60#endif
61
62enum class OperatingSystem {
63 Windows = TT_OS_WINDOWS,
64 MacOS = TT_OS_MACOS,
65 iOS = TT_OS_IOS,
66 Linux = TT_OS_LINUX,
67 Android = TT_OS_ANDROID,
68 UNIX = TT_OS_UNIX,
69 Posix = TT_OS_POSIX,
70
71 current = TT_OPERATING_SYSTEM
72};
73
74#define TT_CC_MSVC 'm'
75#define TT_CC_GCC 'g'
76#define TT_CC_CLANG 'c'
77
78#if defined(__clang__)
79#define TT_COMPILER TT_CC_CLANG
80#elif defined(_MSC_BUILD)
81#define TT_COMPILER TT_CC_MSVC
82#elif defined(__GNUC__)
83#define TT_COMPILER TT_CC_GCC
84#else
85#error "Could not detect the compiler."
86#endif
87
88enum class Compiler {
89 MSVC = TT_CC_MSVC,
90 gcc = TT_CC_GCC,
91 clang = TT_CC_CLANG,
92
93 current = TT_COMPILER
94};
95
96#define TT_CPU_X64 'i'
97#define TT_CPU_ARM 'a'
98
99#define TT_CPPVER_17 '7'
100#define TT_CPPVER_20 '2'
101
102#if __cplusplus > 201703L
103#define TT_CPP_VERSION TT_CPPVER_20
104#elif __cplusplus == 201703L
105#define TT_CPP_VERSION TT_CPPVER_17
106#else
107#error "Unknown C++ version"
108#endif
109enum class CPPVersion {
110 CPP17 = TT_CPPVER_17,
111 CPP20 = TT_CPPVER_20,
112
113 current = TT_CPP_VERSION
114};
115
116#if defined(__amd64__) || defined(__x86_64__) || defined(_M_AMD64)
117#define TT_PROCESSOR TT_CPU_X64
118#elif defined(__arm__) || defined(_M_ARM)
119#define TT_PROCESSOR TT_CPU_ARM
120#else
121#error "Could not detect processor."
122#endif
123
124enum class Processor {
125 x64 = TT_CPU_X64,
126 ARM = TT_CPU_ARM,
127
128 current = TT_PROCESSOR
129};
130
131#define tt_stringify(a) #a
132
133#if TT_COMPILER == TT_CC_MSVC
134#define tt_unreachable() __assume(0)
135#define tt_assume(condition) __assume(condition)
136#define tt_assume2(condition, msg) __assume(condition)
137#define tt_force_inline __forceinline
138#define tt_no_inline __declspec(noinline)
139#define clang_suppress(a)
140#define msvc_suppress(a) _Pragma(tt_stringify(warning(disable:a)))
141
142#elif TT_COMPILER == TT_CC_CLANG
143#define tt_unreachable() __builtin_unreachable()
144#define tt_assume(condition) __builtin_assume(static_cast<bool>(condition))
145#define tt_assume2(condition, msg) __builtin_assume(static_cast<bool>(condition))
146#define tt_force_inline inline __attribute__((always_inline))
147#define tt_no_inline __attribute__((noinline))
148#define clang_suppress(a) _Pragma(tt_stringify(clang diagnostic ignored a))
149#define msvc_suppress(a)
150
151#elif TT_COMPILER == TT_CC_GCC
152#define tt_unreachable() __builtin_unreachable()
153#define tt_assume(condition) do { if (!(condition)) tt_unreachable(); } while (false)
154#define tt_assume2(condition, msg) do { if (!(condition)) tt_unreachable(); } while (false)
155#define tt_force_inline inline __attribute__((always_inline))
156#define tt_no_inline __attribute__((noinline))
157#define clang_suppress(a)
158#define msvc_suppress(a)
159
160#else
161#define tt_unreachable() std::terminate()
162#define tt_assume(condition) static_assert(sizeof(condition) == 1)
163#define tt_assume2(condition, msg) static_assert(sizeof(condition) == 1, msg)
164#define tt_force_inline inline
165#define tt_no_inline
166#define clang_suppress(a)
167#define msvc_suppress(a)
168
169#endif
170
171#if TT_PROCESSOR == TT_CPU_X64
175constexpr size_t hardware_destructive_interference_size = 128;
176
180constexpr size_t hardware_constructive_interference_size = 64;
181#elif TT_PROCESSOR == TT_CPU_ARM
185constexpr size_t hardware_destructive_interference_size = 64;
186
190constexpr size_t hardware_constructive_interference_size = 64;
191#else
192#error "Missing implementation of hardware_destructive_interference_size and hardware_constructive_interference_size"
193#endif
194
195constexpr bool has_sse = Processor::current == Processor::x64;
196
197#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
198using os_handle = void *;
199using file_handle = os_handle;
200
201#elif TT_OPERATING_SYSTEM == TT_OS_MACOS
202using os_handle = int;
203using file_handle = int;
204
205#elif TT_OPERATING_SYSTEM == TT_OS_LINUX
206using os_handle = int;
207using file_handle = int;
208
209#else
210#error "file_handle Not implemented."
211#endif
212
213}