HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
architecture.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#if defined(__APPLE__)
11#include <TargetConditionals.h>
12#endif
13
14namespace tt {
15
16#define TT_BT_DEBUG 'D'
17#define TT_BT_RELEASE 'R'
18
19#if defined(NDEBUG)
20#define TT_BUILD_TYPE TT_BT_RELEASE
21#else
22#define TT_BUILD_TYPE TT_BT_DEBUG
23#endif
24
25enum class build_type {
26 debug = TT_BT_DEBUG,
27 release = TT_BT_RELEASE,
28
29 current = TT_BUILD_TYPE
30};
31
32#define TT_OS_WINDOWS 'W'
33#define TT_OS_MACOS 'A'
34#define TT_OS_MOBILE 'M'
35#define TT_OS_OTHER 'O'
36
37#if defined(_WIN32)
38#define TT_OPERATING_SYSTEM TT_OS_WINDOWS
39#elif defined(TARGET_OS_MAC) && !defined(TARGET_OS_IPHONE)
40#define TT_OPERATING_SYSTEM TT_OS_MACOS
41#elif defined(TARGET_OS_IPHONE) || defined(__ANDROID__)
42#define TT_OPERATING_SYSTEM TT_OS_MOBILE
43#else
44#define TT_OPERATING_SYSTEM TT_OS_OTHER
45#endif
46
47enum class operating_system {
48 windows = TT_OS_WINDOWS,
49 macos = TT_OS_MACOS,
50 mobile = TT_OS_MOBILE,
51 other = TT_OS_OTHER,
52
53 current = TT_OPERATING_SYSTEM
54};
55
56#define TT_CC_MSVC 'm'
57#define TT_CC_GCC 'g'
58#define TT_CC_CLANG 'c'
59
60#if defined(__clang__)
61#define TT_COMPILER TT_CC_CLANG
62#elif defined(_MSC_BUILD)
63#define TT_COMPILER TT_CC_MSVC
64#elif defined(__GNUC__)
65#define TT_COMPILER TT_CC_GCC
66#else
67#error "Could not detect the compiler."
68#endif
69
70enum class compiler {
71 msvc = TT_CC_MSVC,
72 gcc = TT_CC_GCC,
73 clang = TT_CC_CLANG,
74
75 current = TT_COMPILER
76};
77
78
79#define TT_CPU_X64 'i'
80#define TT_CPU_ARM 'a'
81
82#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64)
83#define TT_PROCESSOR TT_CPU_X64
84#elif defined(__arm__) || defined(_M_ARM)
85#define TT_PROCESSOR TT_CPU_ARM
86#else
87#error "Could not detect processor."
88#endif
89
90enum class processor {
91 x64 = TT_CPU_X64,
92 arm = TT_CPU_ARM,
93
94 current = TT_PROCESSOR
95};
96
97#if TT_PROCESSOR == TT_CPU_X64
98#if defined(__AVX512BW__) && defined(__AVX512CD__) && defined(__AVX512DQ__) && defined(__AVX512F__) && defined(__AVX512VL__)
99#define TT_X86_64_V4 1
100#define TT_X86_64_V3 1
101#define TT_X86_64_V2_5 1
102#define TT_X86_64_V2 1
103#define TT_X86_64_V1 1
104
105#elif defined(__AVX2__)
106#define TT_X86_64_V3 1
107#define TT_X86_64_V2_5 1
108#define TT_X86_64_V2 1
109#define TT_X86_64_V1 1
110
111#elif defined(__AVX__)
112#define TT_X86_64_V2_5 1
113#define TT_X86_64_V2 1
114#define TT_X86_64_V1 1
115
116// x86_64_v2 can not be selected in MSVC, but can be in gcc and clang.
117#elif defined(__SSE4_2__) && defined(__SSSE3__)
118#define TT_X86_64_V2 1
119#define TT_X86_64_V1 1
120
121#else
122#define TT_X86_64_V1 1
123#endif
124#endif
125
126#if defined(TT_X86_64_V1)
127constexpr bool x86_64_v1 = true;
128#else
129constexpr bool x86_64_v1 = false;
130#endif
131
132#if defined(TT_X86_64_V2)
133constexpr bool x86_64_v2 = true;
134#else
135constexpr bool x86_64_v2 = false;
136#endif
137
138#if defined(TT_X86_64_V2_5)
139constexpr bool x86_64_v2_5 = true;
140#else
141constexpr bool x86_64_v2_5 = false;
142#endif
143
144#if defined(TT_X86_64_V3)
145constexpr bool x86_64_v3 = true;
146#else
147constexpr bool x86_64_v3 = false;
148#endif
149
150#if defined(TT_X86_64_V4)
151constexpr bool x86_64_v4 = true;
152#else
153constexpr bool x86_64_v4 = false;
154#endif
155
156#if TT_COMPILER == TT_CC_MSVC
157#define tt_unreachable() __assume(0)
158#define tt_assume(condition) __assume(condition)
159#define tt_assume2(condition, msg) __assume(condition)
160#define tt_force_inline __forceinline
161#define tt_no_inline __declspec(noinline)
162#define tt_restrict __restrict
163#define clang_suppress(a)
164#define msvc_pragma(a) _Pragma(a)
165
166#elif TT_COMPILER == TT_CC_CLANG
167#define tt_unreachable() __builtin_unreachable()
168#define tt_assume(condition) __builtin_assume(static_cast<bool>(condition))
169#define tt_assume2(condition, msg) __builtin_assume(static_cast<bool>(condition))
170#define tt_force_inline inline __attribute__((always_inline))
171#define tt_no_inline __attribute__((noinline))
172#define tt_restrict __restrict__
173#define clang_suppress(a) _Pragma(tt_stringify(clang diagnostic ignored a))
174#define msvc_pragma(a)
175
176#elif TT_COMPILER == TT_CC_GCC
177#define tt_unreachable() __builtin_unreachable()
178#define tt_assume(condition) do { if (!(condition)) tt_unreachable(); } while (false)
179#define tt_assume2(condition, msg) do { if (!(condition)) tt_unreachable(); } while (false)
180#define tt_force_inline inline __attribute__((always_inline))
181#define tt_no_inline __attribute__((noinline))
182#define tt_restrict __restrict__
183#define clang_suppress(a)
184#define msvc_pragma(a)
185
186#else
187#define tt_unreachable() std::terminate()
188#define tt_assume(condition) static_assert(sizeof(condition) == 1)
189#define tt_assume2(condition, msg) static_assert(sizeof(condition) == 1, msg)
190#define tt_force_inline inline
191#define tt_no_inline
192#define tt_restrict
193#define clang_suppress(a)
194#define msvc_pragma(a)
195
196#endif
197
198#if TT_PROCESSOR == TT_CPU_X64
202constexpr size_t hardware_destructive_interference_size = 128;
203
207constexpr size_t hardware_constructive_interference_size = 64;
208#elif TT_PROCESSOR == TT_CPU_ARM
212constexpr size_t hardware_destructive_interference_size = 64;
213
217constexpr size_t hardware_constructive_interference_size = 64;
218#else
219#error "Missing implementation of hardware_destructive_interference_size and hardware_constructive_interference_size"
220#endif
221
222#if TT_OPERATING_SYSTEM == TT_OS_WINDOWS
223using os_handle = void *;
224using file_handle = os_handle;
225
226#elif TT_OPERATING_SYSTEM == TT_OS_MACOS
227using os_handle = int;
228using file_handle = int;
229
230#elif TT_OPERATING_SYSTEM == TT_OS_LINUX
231using os_handle = int;
232using file_handle = int;
233
234#else
235#error "file_handle Not implemented."
236#endif
237
238}