-
Notifications
You must be signed in to change notification settings - Fork 0
/
tclang-lshift2sign.c
163 lines (130 loc) · 2.98 KB
/
tclang-lshift2sign.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
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
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
/*
* compile with -DMAKE_SAFE to get working code from Clang 4.2
*
* this though is OK:
* gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
*/
int main(void);
int
main()
{
{
unsigned int u;
u = ~0U;
printf("u = %u\n", u); /* UINT_MAX */
}
{
unsigned int u;
u = 1U << ((sizeof(u) * CHAR_BIT) - 1);
printf("u = %u\n", u);
}
{
unsigned int u;
u = ~(~0U >> 1); /* effectively sets MSB */
printf("u = %u\n", u);
}
{
signed int i;
i = (int) (((unsigned int) -1) >> 1); /* INT_MAX */
printf("i = %i\n", i);
}
{
signed int i;
i = (int) (1U << ((sizeof(i) * CHAR_BIT) - 1));
printf("i = %d\n", i);
}
{
signed int i;
i = (int) ~(~0U >> 1); /* INT_MIN */
printf("i = %d\n", i);
}
{
signed int i;
i = (int) ((unsigned int) 1 << ((sizeof(i) * CHAR_BIT) - 1));
printf("i = %d\n", i);
}
{
unsigned long int lu;
lu = 1LU << ((sizeof(lu) * CHAR_BIT) - 1);
printf("lu = %lu\n", lu);
}
{
unsigned long int lu;
lu = ~(~0LU >> 1);
printf("lu = %lu\n", lu);
}
{
unsigned long long int llu;
llu = 1LLU << ((sizeof(llu) * CHAR_BIT) - 1);
printf("llu = %llu\n", llu);
}
{
unsigned long long int llu;
llu = ~(~0LLU >> 1);
printf("llu = %llu\n", llu);
}
{
unsigned int u;
#if defined(MAKE_SAFE) && defined(__clang_major__) && (__clang_major__ >= 4)
/* assuming I32 */
u = 0x80000000;
#else
/*
* code generated for this expression by clang 4.2.0 blows
* chunks on x86_64:
*
* Program received signal EXC_BAD_INSTRUCTION, Illegal instruction/operand.
*
* as shown above this seems to be due to the literal '1' being
* a signed int.
*
* $ clang -v
* Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
* Target: x86_64-apple-darwin12.4.0
* Thread model: posix
*/
u = (unsigned int) (1 << ((sizeof(u) * CHAR_BIT) - 1));
#endif
printf("u = %u\n", u);
}
{
signed int i = 1;
#if defined(MAKE_SAFE) && defined(__clang_major__) && (__clang_major__ >= 4)
/* assuming I32 */
i = (int) 0x80000000;
#else
i = (int) (1 << ((sizeof(i) * CHAR_BIT) - 1));
#endif
printf("i = %d\n", i);
}
{
unsigned long int lu;
#if defined(MAKE_SAFE) && defined(__clang_major__) && (__clang_major__ >= 4)
/* assuming __LP64__ */
lu = 0x8000000000000000;
#else
lu = (unsigned long) (1L << ((sizeof(lu) * CHAR_BIT) - 1));
#endif
printf("lu = %lu\n", lu);
}
{
unsigned long long int llu;
#if defined(MAKE_SAFE) && defined(__clang_major__) && (__clang_major__ >= 4)
/* assuming __LP64__ */
llu = 0x8000000000000000;
#else
llu = (unsigned long long) (1LL << ((sizeof(llu) * CHAR_BIT) - 1));
#endif
printf("llu = %llu\n", llu);
}
exit(0);
}
/*
* Local Variables:
* eval: (make-local-variable 'compile-command)
* compile-command: (let ((fn (file-name-sans-extension (file-name-nondirectory (buffer-file-name))))) (concat "rm -f " fn "; " (default-value 'compile-command) " " fn " && ./" fn))
* End:
*/