summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/ptypes.h
blob: ef3dd724388aef819cb0e0759a6dc9bd5e35d4fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PTYPES_H_
#define PTYPES_H_

///
/// \file ptypes.h
///

#include "pconfig.h"

#include <limits>
#include <string>
#include <type_traits>

#if (PUSE_FLOAT128)
#include <quadmath.h>
#endif

// noexcept on move operator -> issue with macosx clang
#define PCOPYASSIGNMOVE(name, def)  \
		name(const name &) = def; \
		name(name &&) noexcept = def; \
		name &operator=(const name &) = def; \
		name &operator=(name &&) noexcept = def;

#define PCOPYASSIGN(name, def)  \
		name(const name &) = def; \
		name &operator=(const name &) = def; \

#define PMOVEASSIGN(name, def)  \
		name(name &&) noexcept = def; \
		name &operator=(name &&) noexcept = def;

namespace plib
{
	//============================================================
	//  compile time information
	//============================================================

	/// \brief Dummy 128 bit types for platforms which don't support 128 bit
	///
	/// Users should always consult compile_info::has_int128 prior to
	/// using the UINT128/INT128 data type.
	///
	struct UINT128_DUMMY {};
	struct INT128_DUMMY {};

	enum class ci_compiler
	{
		UNKNOWN,
		CLANG,
		GCC,
		MSC
	};

	enum class ci_os
	{
		UNKNOWN,
		LINUX,
		FREEBSD,
		OPENBSD,
		WINDOWS,
		MACOSX,
		EMSCRIPTEN
	};

	enum class ci_arch
	{
		UNKNOWN,
		X86,
		ARM,
		MIPS
	};

	struct compile_info
	{
	#ifdef _WIN32
		using win32 = std::integral_constant<bool, true>;
	#ifdef UNICODE
		using unicode = std::integral_constant<bool, true>;
	#else
		using unicode = std::integral_constant<bool, false>;
	#endif
	#else
		using win32 = std::integral_constant<bool, false>;
		using unicode = std::integral_constant<bool, true>;
	#endif
	#ifdef __SIZEOF_INT128__
		using has_int128 = std::integral_constant<bool, true>;
		using int128_type  = __int128_t;
		using uint128_type = __uint128_t;
		static constexpr int128_type  int128_max() { return (~static_cast<uint128_type>(0)) >> 1; }
		static constexpr uint128_type  uint128_max() { return ~(static_cast<uint128_type>(0)); }
	#else
		using has_int128 = std::integral_constant<bool, false>;
		using int128_type  = INT128_DUMMY;
		using uint128_type = UINT128_DUMMY;
		static constexpr int128_type int128_max() { return int128_type(); }
		static constexpr uint128_type uint128_max() { return uint128_type(); }
	#endif
	#ifdef __clang__
		using type = std::integral_constant<ci_compiler, ci_compiler::CLANG>;
		using version = std::integral_constant<int, (__clang_major__) * 100 + (__clang_minor__)>;
	#elif defined(__GNUC__)
		using type = std::integral_constant<ci_compiler, ci_compiler::GCC>;
		using version = std::integral_constant<int, (__GNUC__) * 100 + (__GNUC_MINOR__)>;
	#elif defined(_MSC_VER)
		using type = std::integral_constant<ci_compiler, ci_compiler::MSC>;
		using version = std::integral_constant<int, _MSC_VER>;
	#else
		using type = std::integral_constant<ci_compiler, ci_compiler::UNKNOWN>;
		using version = std::integral_constant<int, 0>;
	#endif
	#ifdef __unix__
		using is_unix = std::integral_constant<bool, true>;
	#else
		using is_unix = std::integral_constant<bool, false>;
	#endif
	#if defined(__linux__)
		using os = std::integral_constant<ci_os, ci_os::LINUX>;
	#elif defined(__FreeBSD__)
		using os = std::integral_constant<ci_os, ci_os::FREEBSD>;
	#elif defined(__OpenBSD__)
		using os = std::integral_constant<ci_os, ci_os::OPENBSD>;
	#elif defined(__EMSCRIPTEN__)
		using os = std::integral_constant<ci_os, ci_os::EMSCRIPTEN>;
	#elif defined(_WIN32)
		using os = std::integral_constant<ci_os, ci_os::WINDOWS>;
	#elif defined(__APPLE__)
		using os = std::integral_constant<ci_os, ci_os::MACOSX>;
	#else
		using os = std::integral_constant<ci_os, ci_os::UNKNOWN>;
	#endif
	#if defined(__x86_64__) || defined (_M_X64) || defined(__aarch64__) || defined(__mips64) || defined(_M_AMD64) || defined(_M_ARM64)
		using m64 = std::integral_constant<bool, true>;
	#else
		using m64 = std::integral_constant<bool, false>;
	#endif
	#if defined(__x86_64__) || defined(__i386__)
		using arch = std::integral_constant<ci_arch, ci_arch::X86>;
	#elif defined(__arm__) || defined(__ARMEL__) || defined(__aarch64__) || defined(_M_ARM)
		using arch = std::integral_constant<ci_arch, ci_arch::ARM>;
	#elif defined(__MIPSEL__) || defined(__mips_isa_rev) || defined(__mips64)
		using arch = std::integral_constant<ci_arch, ci_arch::MIPS>;
 	#else
		using arch = std::integral_constant<ci_arch, ci_arch::UNKNOWN>;
	#endif
	#if defined(__MINGW32__)
		using mingw = std::integral_constant<bool, true>;
	#else
		using mingw = std::integral_constant<bool, false>;
	#endif
	};

} // namespace plib

using INT128 = plib::compile_info::int128_type;
using UINT128 = plib::compile_info::uint128_type;

namespace plib
{

	template<typename T> struct is_integral : public std::is_integral<T> { };
	template<typename T> struct is_signed : public std::is_signed<T> { };
	template<typename T> struct is_unsigned : public std::is_unsigned<T> { };
	template<typename T> struct numeric_limits : public std::numeric_limits<T> { };

	// 128 bit support at least on GCC is not fully supported

	template<> struct is_integral<UINT128> { static constexpr bool value = true; };
	template<> struct is_integral<INT128> { static constexpr bool value = true; };

	template<> struct is_signed<UINT128> { static constexpr bool value = false; };
	template<> struct is_signed<INT128> { static constexpr bool value = true; };

	template<> struct is_unsigned<UINT128> { static constexpr bool value = true; };
	template<> struct is_unsigned<INT128> { static constexpr bool value = false; };

	template<> struct numeric_limits<UINT128>
	{
		static constexpr UINT128 max() noexcept
		{
			return compile_info::uint128_max();
		}
	};
	template<> struct numeric_limits<INT128>
	{
		static constexpr INT128 max() noexcept
		{
			return compile_info::int128_max();
		}
	};

	template<typename T> struct is_floating_point : public std::is_floating_point<T> { };

	template< class T >
	struct is_arithmetic : std::integral_constant<bool,
		plib::is_integral<T>::value || plib::is_floating_point<T>::value> {};

#if PUSE_FLOAT128
	template<> struct is_floating_point<FLOAT128> { static constexpr bool value = true; };
	template<> struct numeric_limits<FLOAT128>
	{
		static constexpr FLOAT128 max() noexcept
		{
			return FLT128_MAX;
		}
		static constexpr FLOAT128 lowest() noexcept
		{
			return -FLT128_MAX;
		}
	};
#endif

	template<unsigned bits>
	struct size_for_bits
	{
		static_assert(bits <= 64 || (bits <= 128 && compile_info::has_int128::value), "not supported");
		enum { value =
			bits <= 8       ?   1 :
			bits <= 16      ?   2 :
			bits <= 32      ?   4 :
			bits <= 64      ?   8 :
							   16
		};
	};

	template<unsigned N> struct least_type_for_size;
	template<> struct least_type_for_size<1> { using type = uint_least8_t; };
	template<> struct least_type_for_size<2> { using type = uint_least16_t; };
	template<> struct least_type_for_size<4> { using type = uint_least32_t; };
	template<> struct least_type_for_size<8> { using type = uint_least64_t; };
	template<> struct least_type_for_size<16> { using type = UINT128; };

	// This is different to the standard library. Mappings provided in stdint
	// are not always fastest.
	template<unsigned N> struct fast_type_for_size;
	template<> struct fast_type_for_size<1> { using type = uint32_t; };
	template<> struct fast_type_for_size<2> { using type = uint32_t; };
	template<> struct fast_type_for_size<4> { using type = uint32_t; };
	template<> struct fast_type_for_size<8> { using type = uint_fast64_t; };
	template<> struct fast_type_for_size<16> { using type = UINT128; };

	template<unsigned bits>
	struct least_type_for_bits
	{
		static_assert(bits <= 64 || (bits <= 128 && compile_info::has_int128::value), "not supported");
		using type = typename least_type_for_size<size_for_bits<bits>::value>::type;
	};

	template<unsigned bits>
	struct fast_type_for_bits
	{
		static_assert(bits <= 64 || (bits <= 128 && compile_info::has_int128::value), "not supported");
		using type = typename fast_type_for_size<size_for_bits<bits>::value>::type;
	};

	//============================================================
	// Avoid unused variable warnings
	//============================================================
	template<typename... Ts>
	inline void unused_var(Ts&&...) noexcept {} // NOLINT(readability-named-parameter)

} // namespace plib

//============================================================
// Define a "has member" trait.
//============================================================

#define PDEFINE_HAS_MEMBER(name, member)                                        \
	template <typename T> class name                                            \
	{                                                                           \
		template <typename U> static long test(decltype(&U:: member));          \
		template <typename U> static char  test(...);                           \
	public:                                                                     \
		static constexpr const bool value = sizeof(test<T>(nullptr)) == sizeof(long);   \
	}

#endif // PTYPES_H_