-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
typeinfo.sh
186 lines (161 loc) · 8.61 KB
/
typeinfo.sh
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
#!/bin/bash
# Teal Dulcet
# Outputs C/C++ datatype information
# wget -qO - https://raw.github.com/tdulcet/Linux-System-Information/master/typeinfo.sh | bash -s --
# ./typeinfo.sh
set -e
if [[ $# -ne 0 ]]; then
echo "Usage: $0" >&2
exit 1
fi
if ! command -v g++ >/dev/null; then
echo "Error: This script requires the GNU C compiler" >&2
echo "On Ubuntu and Debian run: 'sudo apt-get update' and 'sudo apt-get install -y build-essential'" >&2
exit 1
fi
if [[ -n $CXX ]] && ! command -v "$CXX" >/dev/null; then
echo "Error: $CXX is not installed." >&2
exit 1
fi
CXX=${CXX:-g++}
cat <<EOF >/tmp/types.cpp
#include <iostream>
#include <sstream>
#include <cmath>
#include <climits>
#include <cfloat>
#include <limits>
#include <cinttypes>
#include <iomanip>
using namespace std;
template <typename T>
string outputbase(const T number)
{
const short base = 10;
typename make_unsigned<T>::type anumber = number;
anumber = number < 0 ? -anumber : anumber;
string str;
do
{
char digit = anumber % base;
digit += '0';
str = digit + str;
anumber /= base;
} while (anumber > 0);
if (number < 0)
str = '-' + str;
return str;
}
template <typename T>
string floattostring(T arg)
{
ostringstream strm;
strm.precision(numeric_limits<T>::max_digits10);
strm << arg;
return strm.str();
}
template <typename T>
T maxint()
{
T max_bit = scalbn(T(1), numeric_limits<T>::digits - 1);
return max_bit + (max_bit - 1);
}
int main()
{
constexpr int width = DECIMAL_DIG + 19;
cout << "\nData Type\t\tSize (bytes)\n\n";
cout << "bool:\t\t\t" << sizeof(bool) << '\n';
cout << "char:\t\t\t" << sizeof(char) << '\n';
cout << "signed char:\t\t" << sizeof(signed char) << '\n';
cout << "unsigned char:\t\t" << sizeof(unsigned char) << '\n';
cout << "wchar_t:\t\t" << sizeof(wchar_t) << '\n';
// cout << "char8_t:\t\t" << sizeof(char8_t) << '\n';
cout << "char16_t:\t\t" << sizeof(char16_t) << '\n';
cout << "char32_t:\t\t" << sizeof(char32_t) << '\n';
cout << "short:\t\t\t" << sizeof(short) << '\n';
cout << "unsigned short:\t\t" << sizeof(unsigned short) << '\n';
cout << "int:\t\t\t" << sizeof(int) << '\n';
cout << "unsigned int:\t\t" << sizeof(unsigned int) << '\n';
cout << "long:\t\t\t" << sizeof(long) << '\n';
cout << "unsigned long:\t\t" << sizeof(unsigned long) << '\n';
cout << "long long:\t\t" << sizeof(long long) << '\n';
cout << "unsigned long long:\t" << sizeof(unsigned long long) << '\n';
cout << "int8_t:\t\t\t" << sizeof(int8_t) << '\n';
cout << "uint8_t:\t\t" << sizeof(uint8_t) << '\n';
cout << "int16_t:\t\t" << sizeof(int16_t) << '\n';
cout << "uint16_t:\t\t" << sizeof(uint16_t) << '\n';
cout << "int32_t:\t\t" << sizeof(int32_t) << '\n';
cout << "uint32_t:\t\t" << sizeof(uint32_t) << '\n';
cout << "int64_t:\t\t" << sizeof(int64_t) << '\n';
cout << "uint64_t:\t\t" << sizeof(uint64_t) << '\n';
cout << "__int16_t:\t\t" << sizeof(__int16_t) << '\n';
cout << "__uint16_t:\t\t" << sizeof(__uint16_t) << '\n';
cout << "__int32_t:\t\t" << sizeof(__int32_t) << '\n';
cout << "__uint32_t:\t\t" << sizeof(__uint32_t) << '\n';
cout << "__int64_t:\t\t" << sizeof(__int64_t) << '\n';
cout << "__uint64_t:\t\t" << sizeof(__uint64_t) << '\n';
#ifdef __SIZEOF_INT128__
cout << "__int128_t:\t\t" << sizeof(__int128_t) << '\n';
cout << "__uint128_t:\t\t" << sizeof(__uint128_t) << '\n';
cout << "__int128:\t\t" << sizeof(__int128) << '\n';
cout << "unsigned __int128:\t" << sizeof(unsigned __int128) << '\n';
#endif
cout << "intmax_t:\t\t" << sizeof(intmax_t) << '\n';
cout << "uintmax_t:\t\t" << sizeof(uintmax_t) << '\n';
cout << "float:\t\t\t" << sizeof(float) << '\n';
cout << "double:\t\t\t" << sizeof(double) << '\n';
cout << "long double:\t\t" << sizeof(long double) << '\n';
cout << "\n\n";
cout << "Data Type\t\t" << left << setw(width) << "Minimum value" << setw(width) << "Maximum value" << "\n\n";
cout << "char:\t\t\t" << right << setw(width) << CHAR_MIN << setw(width) << CHAR_MAX << '\n';
cout << "signed char:\t\t" << right << setw(width) << SCHAR_MIN << setw(width) << SCHAR_MAX << '\n';
cout << "unsigned char:\t\t" << right << setw(width) << 0 << setw(width) << UCHAR_MAX << '\n';
cout << "wchar_t:\t\t" << setw(width) << WCHAR_MIN << setw(width) << WCHAR_MAX << '\n';
// cout << "char8_t:\t\t" << setw(width) << 0 << setw(width) << UCHAR_MAX << '\n';
cout << "char16_t:\t\t" << setw(width) << 0 << setw(width) << UINT_LEAST16_MAX << '\n';
cout << "char32_t:\t\t" << setw(width) << 0 << setw(width) << UINT_LEAST32_MAX << '\n';
cout << "short:\t\t\t" << setw(width) << SHRT_MIN << setw(width) << SHRT_MAX << '\n';
cout << "unsigned short:\t\t" << setw(width) << 0 << setw(width) << USHRT_MAX << '\n';
cout << "int:\t\t\t" << setw(width) << INT_MIN << setw(width) << INT_MAX << '\n';
cout << "unsigned int:\t\t" << setw(width) << 0 << setw(width) << UINT_MAX << '\n';
cout << "long:\t\t\t" << setw(width) << LONG_MIN << setw(width) << LONG_MAX << '\n';
cout << "unsigned long:\t\t" << setw(width) << 0 << setw(width) << ULONG_MAX << '\n';
cout << "long long:\t\t" << setw(width) << LLONG_MIN << setw(width) << LLONG_MAX << '\n';
cout << "unsigned long long:\t" << setw(width) << 0 << setw(width) << ULLONG_MAX << '\n';
cout << "int8_t:\t\t\t" << setw(width) << INT8_MIN << setw(width) << INT8_MAX << '\n';
cout << "uint8_t:\t\t" << setw(width) << 0 << setw(width) << UINT8_MAX << '\n';
cout << "int16_t:\t\t" << setw(width) << INT16_MIN << setw(width) << INT16_MAX << '\n';
cout << "uint16_t:\t\t" << setw(width) << 0 << setw(width) << UINT16_MAX << '\n';
cout << "int32_t:\t\t" << setw(width) << INT32_MIN << setw(width) << INT32_MAX << '\n';
cout << "uint32_t:\t\t" << setw(width) << 0 << setw(width) << UINT32_MAX << '\n';
cout << "int64_t:\t\t" << setw(width) << INT64_MIN << setw(width) << INT64_MAX << '\n';
cout << "uint64_t:\t\t" << setw(width) << 0 << setw(width) << UINT64_MAX << '\n';
cout << "__int16_t:\t\t" << setw(width) << numeric_limits<__int16_t>::min() << setw(width) << numeric_limits<__int16_t>::max() << '\n';
cout << "__uint16_t:\t\t" << setw(width) << numeric_limits<__uint16_t>::min() << setw(width) << numeric_limits<__uint16_t>::max() << '\n';
cout << "__int32_t:\t\t" << setw(width) << numeric_limits<__int32_t>::min() << setw(width) << numeric_limits<__int32_t>::max() << '\n';
cout << "__uint32_t:\t\t" << setw(width) << numeric_limits<__uint32_t>::min() << setw(width) << numeric_limits<__uint32_t>::max() << '\n';
cout << "__int64_t:\t\t" << setw(width) << numeric_limits<__int64_t>::min() << setw(width) << numeric_limits<__int64_t>::max() << '\n';
cout << "__uint64_t:\t\t" << setw(width) << numeric_limits<__uint64_t>::min() << setw(width) << numeric_limits<__uint64_t>::max() << '\n';
#ifdef __SIZEOF_INT128__
cout << "__int128_t:\t\t" << setw(width) << outputbase(numeric_limits<__int128_t>::min()) << setw(width) << outputbase(numeric_limits<__int128_t>::max()) << '\n';
cout << "__uint128_t:\t\t" << setw(width) << outputbase(numeric_limits<__uint128_t>::min()) << setw(width) << outputbase(numeric_limits<__uint128_t>::max()) << '\n';
cout << "__int128:\t\t" << setw(width) << outputbase(numeric_limits<__int128>::min()) << setw(width) << outputbase(numeric_limits<__int128>::max()) << '\n';
cout << "unsigned __int128:\t" << setw(width) << outputbase(numeric_limits<unsigned __int128>::min()) << setw(width) << outputbase(numeric_limits<unsigned __int128>::max()) << '\n';
#endif
cout << "intmax_t:\t\t" << setw(width) << INTMAX_MIN << setw(width) << INTMAX_MAX << '\n';
cout << "uintmax_t:\t\t" << setw(width) << 0 << setw(width) << UINTMAX_MAX << '\n';
cout << "float:\t\t\t" << setw(width) << floattostring(FLT_MIN) << setw(width) << floattostring(FLT_MAX) << '\n';
cout << "double:\t\t\t" << setw(width) << floattostring(DBL_MIN) << setw(width) << floattostring(DBL_MAX) << '\n';
cout << "long double:\t\t" << setw(width) << floattostring(LDBL_MIN) << setw(width) << floattostring(LDBL_MAX) << '\n';
cout << "\n\n";
cout << "Data Type\t\tDecimal digits\tMaximum Decimal digits\tMantissa bits\tMaximum integer\n\n";
cout << "float:\t\t\t" << FLT_DIG << "\t\t" << /* FLT_DECIMAL_DIG */ numeric_limits<float>::max_digits10 << "\t\t\t" << FLT_MANT_DIG << "\t\t" << floattostring(maxint<float>()) << '\n';
cout << "double:\t\t\t" << DBL_DIG << "\t\t" << /* DBL_DECIMAL_DIG */ numeric_limits<double>::max_digits10 << "\t\t\t" << DBL_MANT_DIG << "\t\t" << floattostring(maxint<double>()) << '\n';
cout << "long double:\t\t" << LDBL_DIG << "\t\t" << /* LDBL_DECIMAL_DIG */ numeric_limits<long double>::max_digits10 << "\t\t\t" << LDBL_MANT_DIG << "\t\t" << floattostring(maxint<long double>()) << '\n';
cout << '\n';
return 0;
}
EOF
trap 'rm /tmp/types{.cpp,}' EXIT
"$CXX" -std=gnu++11 -Wall -g -O3 /tmp/types.cpp -o /tmp/types
/tmp/types