summaryrefslogtreecommitdiff
path: root/zap/include/zap/bs.hh
blob: 00f66b1f8e82ee02c76c78f12a3e4e765be7855a (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
/*
	Copyright 2022-2023 Gabriel Jensen.
	This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
	If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
*/

#ifndef zap_priv_cxxhdr_bs
#define zap_priv_cxxhdr_bs

// C++11: constexpr
// C++14: template variables

#ifndef __cplusplus
#error C++ header included from C!
#endif

#include <zap/sys.hh>
#include <zap/bs.h>

namespace zap {
	namespace impl {
		template<typename typ> struct minval {constexpr static typ val = {};};

		template<> struct minval<bool>               {constexpr static auto val = false;};
		template<> struct minval<signed char>        {constexpr static auto val = zap_minvalsc;};
		template<> struct minval<short>              {constexpr static auto val = zap_minvals;};
		template<> struct minval<int>                {constexpr static auto val = zap_minvali;};
		template<> struct minval<long>               {constexpr static auto val = zap_minvall;};
		template<> struct minval<long long>          {constexpr static auto val = zap_minvalll;};
		template<> struct minval<unsigned char>      {constexpr static auto val = zap_minvaluc;};
		template<> struct minval<unsigned short>     {constexpr static auto val = zap_minvalus;};
		template<> struct minval<unsigned int>       {constexpr static auto val = zap_minvalui;};
		template<> struct minval<unsigned long>      {constexpr static auto val = zap_minvalul;};
		template<> struct minval<unsigned long long> {constexpr static auto val = zap_minvalull;};
		template<> struct minval<char>               {constexpr static auto val = zap_minvalc;};
		template<> struct minval<wchar_t>            {constexpr static auto val = zap_minvalwc;};
#if __cpp_char8_t >= 201811
		template<> struct minval<char8_t>            {constexpr static auto val = zap_minvaluc;};
#endif
		template<> struct minval<char16_t>           {constexpr static auto val = zap_minval01;};
		template<> struct minval<char32_t>           {constexpr static auto val = zap_minval02;};

		template<typename typ> struct maxval {constexpr static typ val = {};};

		template<> struct maxval<bool>               {constexpr static auto val = true;};	
		template<> struct maxval<signed char>        {constexpr static auto val = zap_maxvalsc;};
		template<> struct maxval<int>                {constexpr static auto val = zap_maxvali;};
		template<> struct maxval<short>              {constexpr static auto val = zap_maxvals;};
		template<> struct maxval<long>               {constexpr static auto val = zap_maxvall;};
		template<> struct maxval<long long>          {constexpr static auto val = zap_maxvalll;};
		template<> struct maxval<unsigned char>      {constexpr static auto val = zap_maxvaluc;};
		template<> struct maxval<unsigned short>     {constexpr static auto val = zap_maxvalus;};
		template<> struct maxval<unsigned int>       {constexpr static auto val = zap_maxvalui;};
		template<> struct maxval<unsigned long>      {constexpr static auto val = zap_maxvalul;};
		template<> struct maxval<unsigned long long> {constexpr static auto val = zap_maxvalull;};
		template<> struct maxval<char>               {constexpr static auto val = zap_maxvalc;};
		template<> struct maxval<wchar_t>            {constexpr static auto val = zap_maxvalwc;};
#if __cpp_char8_t >= 201811
		template<> struct maxval<char8_t>            {constexpr static auto val = zap_maxvaluc;};
#endif
		template<> struct maxval<char16_t>           {constexpr static auto val = zap_maxval01;};
		template<> struct maxval<char32_t>           {constexpr static auto val = zap_maxval02;};
	}

	template<typename typ> constexpr auto minval = ::zap::impl::minval<typ>::val;

	template<typename typ> constexpr auto maxval = ::zap::impl::maxval<typ>::val;
}

namespace zap {
	namespace impl {
		template<typename ityp> struct sign {using typ = ityp;};

		template<> struct sign<unsigned char>      {using typ = signed char;};
		template<> struct sign<unsigned int>       {using typ = int;};
		template<> struct sign<unsigned long>      {using typ = long;};
		template<> struct sign<unsigned long long> {using typ = long long;};
		template<> struct sign<unsigned short>     {using typ = short;};

		template<typename ityp> struct usign {using typ = ityp;};

		template<> struct usign<signed char> {using typ = unsigned char;};
		template<> struct usign<int>         {using typ = unsigned int;};
		template<> struct usign<long>        {using typ = unsigned long;};
		template<> struct usign<long long>   {using typ = unsigned long long;};
		template<> struct usign<short>       {using typ = unsigned short;};
	}

	template<typename typ> using sign = typename ::zap::impl::sign<typ>::typ;

	template<typename typ> using usign = typename ::zap::impl::usign<typ>::typ;
}

namespace zap {
	namespace impl {
		template<typename ityp> struct remqual {using typ = ityp;};

		template<typename ityp> struct remqual<ityp const>          {using typ = ityp;};
		template<typename ityp> struct remqual<ityp volatile>       {using typ = ityp;};
		template<typename ityp> struct remqual<ityp const volatile> {using typ = ityp;};
	}

	template<typename typ> using remqual = typename ::zap::impl::remqual<typ>::typ;
}

namespace zap {
	namespace impl {
		template<typename ltyp,typename rtyp> struct typeq {constexpr static bool eq = false;};

		template<typename typ> struct typeq<typ,typ>       {constexpr static auto eq = true;};
	}

	template<typename ltyp,typename rtyp> constexpr auto typeq = ::zap::impl::typeq<ltyp,rtyp>::eq;
}

namespace zap {
	template<typename typ> constexpr auto ischrtyp =
		   ::zap::typeq<typ,char>
		|| ::zap::typeq<typ,char16_t>
		|| ::zap::typeq<typ,char32_t>
#if __cpp_char8_t
		|| ::zap::typeq<typ,char8_t>
#endif
		|| ::zap::typeq<typ,unsigned char>
		|| ::zap::typeq<typ,wchar_t>;

	template<typename typ> constexpr auto isflttyp = 
		::zap::typeq<typ,double>
		|| ::zap::typeq<typ,float>
		|| ::zap::typeq<typ,long double>
#if __STDCPP_BFLOAT16_T__
		|| ::zap::typeq<typ,decltype (0x0.0p0bf16)>
#endif
#if __STDCPP_FLOAT128_T__
		|| ::zap::typeq<typ,decltype (0x0.0p0f16)>
#endif
#if __STDCPP_FLOAT16_T__
		|| ::zap::typeq<typ,decltype (0x0.0p0f32)>
#endif
#if __STDCPP_FLOAT32_T__
		|| ::zap::typeq<typ,decltype (0x0.0p0f64)>
#endif
#if __STDCPP_FLOAT64_T__
		|| ::zap::typeq<typ,decltype (0x0.0p0f128)>
#endif
		;

	template<typename typ> constexpr auto isinttyp = 
		   ::zap::typeq<typ,int>
		|| ::zap::typeq<typ,long>
		|| ::zap::typeq<typ,long long>
		|| ::zap::typeq<typ,short>
		|| ::zap::typeq<typ,signed char>
		|| ::zap::typeq<typ,unsigned char>
		|| ::zap::typeq<typ,unsigned int>
		|| ::zap::typeq<typ,unsigned long>
		|| ::zap::typeq<typ,unsigned long long>
		|| ::zap::typeq<typ,unsigned short>;
		
	template<typename typ> constexpr auto isarithtyp = 
		   ::zap::isflttyp<typ>
		|| ::zap::isinttyp<typ>;
}

namespace zap {
	using i8  = ::zap_i8;
	using i8s = ::zap_i8s;
	
	using i01  = ::zap_i01;
	using i01s = ::zap_i01s;
	
	using i02  = ::zap_i02;
	using i02s = ::zap_i02s;
	
	using i04  = ::zap_i04;
	using i04s = ::zap_i04s;

	using ptr = ::zap_ptr;
	using sz  = ::zap_sz;

#if zap_fixflt02
	using f02 = ::zap_f02;
#endif
#if zap_fixflt04
	using f04 = ::zap_f04;
#endif
}

namespace zap {
	constexpr auto ver     = zap_ver;
	constexpr auto bytelen = zap_bytelen;
	constexpr auto nopos   = zap_nopos;
}

namespace zap {
	[[noreturn]] zap_attr_iln inline void trap() noexcept {::zap_trap();}

	[[noreturn]] zap_attr_iln inline void unreach() noexcept {zap_unreach();}
}

#endif