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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/**
* slibc — Yet another C library
* Copyright © 2015 Mattias Andrée (m@maandree.se)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
/**
* Used to determine the signness of `char`.
*/
volatile char nul = 0;
/**
* Used to determine the signness of `wchar_t`.
*
* Note that `wchar_t` is set implicitly by the
* help of the compiler so that our libc does
* not taint the result.
*/
volatile typeof(L'\0') wnul = 0;
/**
* On my amd64 machine, the usual result is:
* int_fast8_t = int32_t
* int_fast16_t = int32_t
* int_fast32_t = int32_t
* int_fast64_t = int64_t
*/
/**
* Measure the performance of a intrinsic integer type,
* and if it is faster than the previous candidate (or
* is the first candidate,) store it as the, yet, most
* performant candidate.
*
* @param VAR A volatile register variable of the
* type that shall be tested.
*/
#define FAST_TEST(VAR) \
n = 0, *VAR##p = 0; \
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start)) \
return perror(""), -1; \
for (;;) \
{ \
for (m = 1000; m--;) \
VAR = 50, VAR += 50, VAR -= 50, VAR *= 50, VAR /= 50, VAR <<= 1, VAR >>= 1, *VAR##p |= 1; \
n++; \
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end)) \
return perror(""), -1; \
if (end.tv_sec > start.tv_sec + 2) \
break; \
if (end.tv_sec > start.tv_sec + 1) \
if (end.tv_nsec >= start.tv_nsec) \
break; \
} \
time_elapsed = (long long int)(end.tv_sec) * 1000000000LL; \
time_elapsed += (long long int)(end.tv_nsec); \
time_elapsed -= (long long int)(start.tv_sec) * 1000000000LL; \
time_elapsed -= (long long int)(start.tv_nsec); \
adjn = (double)n, adjn /= (double)time_elapsed / 1000000000.; \
fprintf(stderr, "int%zu_t:%s %i000 op:s in %lli.%09lli s = %lf op:s/s\n", \
8 * sizeof(VAR), 8 * sizeof(VAR) < 10 ? " " : "", 8 * n, \
time_elapsed / 1000000000LL, time_elapsed % 1000000000LL, adjn * 8000.); \
if ((!best) || (adjn > bestn)) \
bestn = adjn, best = 8 * sizeof(VAR); \
last = this
/**
* Determine whether the performance of `typeof(VAR)` should be determined,
* when finding the most performant, compatiable intrinsic type of a `int_fastN_t`.
*
* @param VAR An variable of the type (or the type itself) that is a candidate.
* @return Determine whether the type shall be tested.
*/
#define TEST(VAR) (this = 8 * (int)sizeof(VAR), ((this >= bits) && (this > last)))
#if defined(__GNUC__)
__attribute__((optimize("-O0")))
#endif
/**
* Find the (in general) fastest intrinsic integer type,
* that has at least a specified number of bits.
*
* @param bits The minimum number of bits the type may have.
* @return The number of bits the most performance
* compatible integer type has.
*/
static int fast(int bits)
{
int last = 0, this = 0, best = 0, n, m;
float bestn = 0;
float adjn;
struct timespec start, end;
long long int time_elapsed;
register volatile char hhi;
register volatile short int hi;
register volatile int i;
register volatile long int li;
register volatile long long int lli;
volatile char hhip[1];
volatile short int hip[1];
volatile int ip[1];
volatile long int lip[1];
volatile long long int llip[1];
if (TEST(hhi)) { FAST_TEST(hhi); }
if (TEST(hi)) { FAST_TEST(hi); }
if (TEST(i)) { FAST_TEST(i); }
if (TEST(li)) { FAST_TEST(li); }
if (best) {
if (TEST(lli)) { FAST_TEST(lli); }
} else
best = 64;
fprintf(stderr, "int_fast%i_t = int%i_t\n\n", bits, best);
return best;
}
#undef TEST
/**
* For a 16-bit integer with the value 0x0102, print,
* to a string, the bytes it is constructed by one the
* host machine, in storage order.
*
* @return The bytes in the integer juxtaposed.
*/
static char* byteorder_16(void)
{
static char buf[16 / 4 + 1];
union
{
short int all;
struct
{
char a; char b;
};
} test = { .all = 0x0102 };
sprintf(buf, "%02i%02i", test.a, test.b);
return buf;
}
/**
* For a 32-bit integer with the value 0x01020304, print,
* to a string, the bytes it is constructed by one the
* host machine, in storage order.
*
* @return The bytes in the integer juxtaposed.
*/
static char* byteorder_32(void)
{
static char buf[32 / 4 + 1];
if (8 * sizeof(int) == 32)
{
union
{
int all;
struct
{
char a; char b; char c; char d;
};
} test = { .all = 0x01020304 };
sprintf(buf, "%02i%02i%02i%02i",
test.a, test.b, test.c, test.d);
}
else
{
union
{
long int all;
struct
{
char a; char b; char c; char d;
};
} test = { .all = 0x01020304L };
sprintf(buf, "%02i%02i%02i%02i",
test.a, test.b, test.c, test.d);
}
return buf;
}
/**
* For a 16-bit integer with the value 0x0102030405060708,
* print, to a string, the bytes it is constructed by one
* the host machine, in storage order.
*
* @return The bytes in the integer juxtaposed.
*/
static char* byteorder_64(void)
{
static char buf[64 / 4 + 1];
union
{
long long int all;
struct
{
char a; char b; char c; char d;
char e; char f; char g; char h;
};
} test = { .all = 0x0102030405060708LL };
sprintf(buf, "%02i%02i%02i%02i%02i%02i%02i%02i",
test.a, test.b, test.c, test.d,
test.e, test.f, test.g, test.h);
return buf;
}
/**
* @param argc The number of command line arguments, should
* be either 1 (print integer width information)
* or 2 (otherwise).
* @param argv Command line arguments, if `argc == 2`, `argv[2]`
* is interpreted as follows:
* - "fast8" Print the number of bits in `int_fast8_t` and `uint_fast8_t`.
* - "fast16" Print the number of bits in `int_fast16_t` and `uint_fast16_t`.
* - "fast32" Print the number of bits in `int_fast32_t` and `uint_fast32_t`.
* - "fast64" Print the number of bits in `int_fast64_t` and `uint_fast64_t`.
* - "char-signed" Print 1 if `char` is signed, and `0` if it is unsigned.
* - "wchar-signed" Print 1 if `wchar_t` is signed, and `0` if it is unsigned.
* @return 0 on success, 1 on error (including printing error.)
*/
int main(int argc, char* argv[])
{
int r = 0;
if (argc < 2)
{
/* Print the number of bits in the intrinsic integers types. */
r |= printf("CHAR_BIT %zu\n", 8 * sizeof(char));
r |= printf("SHORT_BIT %zu\n", 8 * sizeof(short int));
r |= printf("INT_BIT %zu\n", 8 * sizeof(int));
r |= printf("LONG_BIT %zu\n", 8 * sizeof(long int));
r |= printf("LONG_LONG_BIT %zu\n", 8 * sizeof(long long int));
r |= printf("PTR_BIT %zu\n", 8 * sizeof(void*));
r |= printf("WCHAR_BIT %zu\n", 8 * sizeof(L'\0'));
/* Print byte orders. */
r |= printf("INT16_BYTEORDER 0x%s\n", byteorder_16());
r |= printf("INT32_BYTEORDER 0x%sL\n", byteorder_32());
r |= printf("INT64_BYTEORDER 0x%sLL\n", byteorder_64());
/* Print the intrinsic type for specific numbers of bits. */
r |= printf("INT%zu %s\n", 8 * sizeof(char), "char");
r |= printf("INT%zu %s\n", 8 * sizeof(short int), "short int");
r |= printf("INT%zu %s\n", 8 * sizeof(int), "int");
r |= printf("INT%zu %s\n", 8 * sizeof(long int), "long int");
r |= printf("INT%zu %s\n", 8 * sizeof(long long int), "long long int");
return r < 0 ? 1 : 0;
}
else if (argc == 2)
{
if (!strcmp(argv[1], "fast8")) r = fast(8);
else if (!strcmp(argv[1], "fast16")) r = fast(16);
else if (!strcmp(argv[1], "fast32")) r = fast(32);
else if (!strcmp(argv[1], "fast64")) r = fast(64);
else if (!strcmp(argv[1], "char-signed"))
{
nul = ~nul;
r = !(nul > 0);
return printf("%i\n", r) < 0 ? 1 : 0;
}
else if (!strcmp(argv[1], "wchar-signed"))
{
wnul = ~wnul;
r = !(wnul > 0);
return printf("%i\n", r) < 0 ? 1 : 0;
}
if (r <= 0)
return 1;
return printf("%i\n", r) < 0 ? 1 : 0;
}
return 1;
}
|