forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdint_h.c
115 lines (86 loc) · 2.58 KB
/
stdint_h.c
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
/*
# stdint.h
Contains several types of ints, including fixed size
and optimal for speed types
C99
All macros with numbers are defined for N = 8, 16, 32, 64.
*/
#include "common.h"
int main() {
#if __STDC_VERSION__ >= 199901L
/* Fixed size integers. */
{
/* Exactly 32 bits. */
assert(sizeof(int32_t) == 4);
/* All have unsigned versions prefixed by 'u'. */
assert(sizeof(uint32_t) == 4);
/* At least 32 bits. */
assert(sizeof(int_least32_t) >= 4);
/* Fastest operations with at least 32 bits. */
assert(sizeof(int_least32_t) >= 4);
/*
# Fast types
# int_fast32_t
TODO What is the motivation for those types?
Is there an architecture where this fits well?
http://stackoverflow.com/questions/9239558/what-is-the-difference-between-intxx-t-and-int-fastxx-t
*/
{
int_fast32_t i = 0;
}
}
/*
# intmax_t
# uintmax_t
int with max possible width
There is no floating point analogue:
http://stackoverflow.com/questions/17189423/how-to-get-the-largest-precision-floating-point-data-type-of-implemenation-and-i
This does not have to be `long long`, since the standard explicitly allows extended types:
http://stackoverflow.com/a/30322474/895245
GCC 4.8 has __int128, but it currently does not count:
http://stackoverflow.com/questions/21265462/why-in-g-stdintmax-t-is-not-a-int128-t
*/
{
printf("sizeof(intmax_t) = %zu\n", sizeof(intmax_t));
assert(sizeof(intmax_t) >= sizeof(long long));
assert(sizeof(uintmax_t) >= sizeof(unsigned long long));
}
/*
# intptr_t
An integer type large enough to hold a pointer.
Could be larger than the minimum however.
# uintptr_t
Unsigned version.
TODO example of real life application?
*/
{
assert(sizeof(void*) == sizeof(intptr_t));
assert(sizeof(void*) == sizeof(uintptr_t));
}
/*
# Bound for types
# INT32_MAX
# INT32_MIN
Bounds for the fixed size integers.
*/
{
{
int32_t i;
assert(INT32_MIN <= i);
assert(INT32_MAX >= i);
assert(INT32_MIN == 0x80000000);
assert(INT32_MAX == 0x7FFFFFFF);
}
/*
# fast types
TODO
*/
{
int_fast32_t i;
assert(INT_FAST32_MIN <= i);
assert(INT_FAST32_MAX >= i);
}
}
#endif
return EXIT_SUCCESS;
}