1 // RUN: %clang_cc1 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both 2 // RUN: %clang_cc1 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple i686 %s -verify=expected,both 3 // RUN: %clang_cc1 -Wno-string-plus-int -verify=ref,both %s -Wno-constant-evaluated 4 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both 5 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter -triple i686 %s -verify=expected,both 6 // RUN: %clang_cc1 -std=c++20 -Wno-string-plus-int -verify=ref,both %s -Wno-constant-evaluated 7 // RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -fexperimental-new-constant-interpreter %s -verify=expected,both 8 // RUN: %clang_cc1 -triple avr -std=c++20 -Wno-string-plus-int -verify=ref,both %s -Wno-constant-evaluated 9 10 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 11 #define LITTLE_END 1 12 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 13 #define LITTLE_END 0 14 #else 15 #error "huh?" 16 #endif 17 18 extern "C" { 19 typedef decltype(sizeof(int)) size_t; 20 extern size_t wcslen(const wchar_t *p); 21 } 22 23 namespace strcmp { 24 constexpr char kFoobar[6] = {'f','o','o','b','a','r'}; 25 constexpr char kFoobazfoobar[12] = {'f','o','o','b','a','z','f','o','o','b','a','r'}; 26 27 static_assert(__builtin_strcmp("", "") == 0, ""); 28 static_assert(__builtin_strcmp("abab", "abab") == 0, ""); 29 static_assert(__builtin_strcmp("abab", "abba") == -1, ""); 30 static_assert(__builtin_strcmp("abab", "abaa") == 1, ""); 31 static_assert(__builtin_strcmp("ababa", "abab") == 1, ""); 32 static_assert(__builtin_strcmp("abab", "ababa") == -1, ""); 33 static_assert(__builtin_strcmp("a\203", "a") == 1, ""); 34 static_assert(__builtin_strcmp("a\203", "a\003") == 1, ""); 35 static_assert(__builtin_strcmp("abab\0banana", "abab") == 0, ""); 36 static_assert(__builtin_strcmp("abab", "abab\0banana") == 0, ""); 37 static_assert(__builtin_strcmp("abab\0banana", "abab\0canada") == 0, ""); 38 static_assert(__builtin_strcmp(0, "abab") == 0, ""); // both-error {{not an integral constant}} \ 39 // both-note {{dereferenced null}} 40 static_assert(__builtin_strcmp("abab", 0) == 0, ""); // both-error {{not an integral constant}} \ 41 // both-note {{dereferenced null}} 42 43 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar) == -1, ""); 44 static_assert(__builtin_strcmp(kFoobar, kFoobazfoobar + 6) == 0, ""); // both-error {{not an integral constant}} \ 45 // both-note {{dereferenced one-past-the-end}} 46 47 /// Used to assert because we're passing a dummy pointer to 48 /// __builtin_strcmp() when evaluating the return statement. 49 constexpr bool char_memchr_mutable() { 50 char buffer[] = "mutable"; 51 return __builtin_strcmp(buffer, "mutable") == 0; 52 } 53 static_assert(char_memchr_mutable(), ""); 54 55 static_assert(__builtin_strncmp("abaa", "abba", 5) == -1); 56 static_assert(__builtin_strncmp("abaa", "abba", 4) == -1); 57 static_assert(__builtin_strncmp("abaa", "abba", 3) == -1); 58 static_assert(__builtin_strncmp("abaa", "abba", 2) == 0); 59 static_assert(__builtin_strncmp("abaa", "abba", 1) == 0); 60 static_assert(__builtin_strncmp("abaa", "abba", 0) == 0); 61 static_assert(__builtin_strncmp(0, 0, 0) == 0); 62 static_assert(__builtin_strncmp("abab\0banana", "abab\0canada", 100) == 0); 63 } 64 65 /// Copied from constant-expression-cxx11.cpp 66 namespace strlen { 67 constexpr const char *a = "foo\0quux"; 68 constexpr char b[] = "foo\0quux"; 69 constexpr int f() { return 'u'; } 70 constexpr char c[] = { 'f', 'o', 'o', 0, 'q', f(), 'u', 'x', 0 }; 71 72 static_assert(__builtin_strlen("foo") == 3, ""); 73 static_assert(__builtin_strlen("foo\0quux") == 3, ""); 74 static_assert(__builtin_strlen("foo\0quux" + 4) == 4, ""); 75 76 constexpr bool check(const char *p) { 77 return __builtin_strlen(p) == 3 && 78 __builtin_strlen(p + 1) == 2 && 79 __builtin_strlen(p + 2) == 1 && 80 __builtin_strlen(p + 3) == 0 && 81 __builtin_strlen(p + 4) == 4 && 82 __builtin_strlen(p + 5) == 3 && 83 __builtin_strlen(p + 6) == 2 && 84 __builtin_strlen(p + 7) == 1 && 85 __builtin_strlen(p + 8) == 0; 86 } 87 88 static_assert(check(a), ""); 89 static_assert(check(b), ""); 90 static_assert(check(c), ""); 91 92 constexpr int over1 = __builtin_strlen(a + 9); // both-error {{constant expression}} \ 93 // both-note {{one-past-the-end}} 94 constexpr int over2 = __builtin_strlen(b + 9); // both-error {{constant expression}} \ 95 // both-note {{one-past-the-end}} 96 constexpr int over3 = __builtin_strlen(c + 9); // both-error {{constant expression}} \ 97 // both-note {{one-past-the-end}} 98 99 constexpr int under1 = __builtin_strlen(a - 1); // both-error {{constant expression}} \ 100 // both-note {{cannot refer to element -1}} 101 constexpr int under2 = __builtin_strlen(b - 1); // both-error {{constant expression}} \ 102 // both-note {{cannot refer to element -1}} 103 constexpr int under3 = __builtin_strlen(c - 1); // both-error {{constant expression}} \ 104 // both-note {{cannot refer to element -1}} 105 106 constexpr char d[] = { 'f', 'o', 'o' }; // no nul terminator. 107 constexpr int bad = __builtin_strlen(d); // both-error {{constant expression}} \ 108 // both-note {{one-past-the-end}} 109 110 constexpr int wn = __builtin_wcslen(L"hello"); 111 static_assert(wn == 5); 112 constexpr int wm = wcslen(L"hello"); // both-error {{constant expression}} \ 113 // both-note {{non-constexpr function 'wcslen' cannot be used in a constant expression}} 114 115 int arr[3]; // both-note {{here}} 116 int wk = arr[wcslen(L"hello")]; // both-warning {{array index 5}} 117 } 118 119 namespace nan { 120 constexpr double NaN1 = __builtin_nan(""); 121 122 /// The current interpreter does not accept this, but it should. 123 constexpr float NaN2 = __builtin_nans([](){return "0xAE98";}()); // ref-error {{must be initialized by a constant expression}} 124 #if __cplusplus < 201703L 125 // expected-error@-2 {{must be initialized by a constant expression}} 126 #endif 127 128 constexpr double NaN3 = __builtin_nan("foo"); // both-error {{must be initialized by a constant expression}} 129 constexpr float NaN4 = __builtin_nanf(""); 130 //constexpr long double NaN5 = __builtin_nanf128(""); 131 132 /// FIXME: This should be accepted by the current interpreter as well. 133 constexpr char f[] = {'0', 'x', 'A', 'E', '\0'}; 134 constexpr double NaN6 = __builtin_nan(f); // ref-error {{must be initialized by a constant expression}} 135 136 /// FIXME: Current interpreter misses diagnostics. 137 constexpr char f2[] = {'0', 'x', 'A', 'E'}; /// No trailing 0 byte. 138 constexpr double NaN7 = __builtin_nan(f2); // both-error {{must be initialized by a constant expression}} \ 139 // expected-note {{read of dereferenced one-past-the-end pointer}} 140 static_assert(!__builtin_issignaling(__builtin_nan("")), ""); 141 static_assert(__builtin_issignaling(__builtin_nans("")), ""); 142 } 143 144 namespace fmin { 145 constexpr float f1 = __builtin_fmin(1.0, 2.0f); 146 static_assert(f1 == 1.0f, ""); 147 148 constexpr float min = __builtin_fmin(__builtin_nan(""), 1); 149 static_assert(min == 1, ""); 150 constexpr float min2 = __builtin_fmin(1, __builtin_nan("")); 151 static_assert(min2 == 1, ""); 152 constexpr float min3 = __builtin_fmin(__builtin_inf(), __builtin_nan("")); 153 static_assert(min3 == __builtin_inf(), ""); 154 } 155 156 namespace inf { 157 static_assert(__builtin_isinf(__builtin_inf()), ""); 158 static_assert(!__builtin_isinf(1.0), ""); 159 160 static_assert(__builtin_isfinite(1.0), ""); 161 static_assert(!__builtin_isfinite(__builtin_inf()), ""); 162 163 static_assert(__builtin_isnormal(1.0), ""); 164 static_assert(!__builtin_isnormal(__builtin_inf()), ""); 165 166 #ifndef __AVR__ 167 static_assert(__builtin_issubnormal(0x1p-1070), ""); 168 #endif 169 static_assert(!__builtin_issubnormal(__builtin_inf()), ""); 170 171 static_assert(__builtin_iszero(0.0), ""); 172 static_assert(!__builtin_iszero(__builtin_inf()), ""); 173 174 static_assert(__builtin_issignaling(__builtin_nans("")), ""); 175 static_assert(!__builtin_issignaling(__builtin_inf()), ""); 176 } 177 178 namespace isfpclass { 179 char isfpclass_inf_pos_0[__builtin_isfpclass(__builtin_inf(), 0x0200) ? 1 : -1]; // fcPosInf 180 char isfpclass_inf_pos_1[!__builtin_isfpclass(__builtin_inff(), 0x0004) ? 1 : -1]; // fcNegInf 181 char isfpclass_inf_pos_2[__builtin_isfpclass(__builtin_infl(), 0x0207) ? 1 : -1]; // fcSNan|fcQNan|fcNegInf|fcPosInf 182 char isfpclass_inf_pos_3[!__builtin_isfpclass(__builtin_inf(), 0x01F8) ? 1 : -1]; // fcFinite 183 char isfpclass_pos_0 [__builtin_isfpclass(1.0, 0x0100) ? 1 : -1]; // fcPosNormal 184 char isfpclass_pos_1 [!__builtin_isfpclass(1.0f, 0x0008) ? 1 : -1]; // fcNegNormal 185 char isfpclass_pos_2 [__builtin_isfpclass(1.0L, 0x01F8) ? 1 : -1]; // fcFinite 186 char isfpclass_pos_3 [!__builtin_isfpclass(1.0, 0x0003) ? 1 : -1]; // fcSNan|fcQNan 187 #ifndef __AVR__ 188 char isfpclass_pdenorm_0[__builtin_isfpclass(1.0e-40f, 0x0080) ? 1 : -1]; // fcPosSubnormal 189 char isfpclass_pdenorm_1[__builtin_isfpclass(1.0e-310, 0x01F8) ? 1 : -1]; // fcFinite 190 char isfpclass_pdenorm_2[!__builtin_isfpclass(1.0e-40f, 0x003C) ? 1 : -1]; // fcNegative 191 char isfpclass_pdenorm_3[!__builtin_isfpclass(1.0e-310, 0x0207) ? 1 : -1]; // ~fcFinite 192 #endif 193 char isfpclass_pzero_0 [__builtin_isfpclass(0.0f, 0x0060) ? 1 : -1]; // fcZero 194 char isfpclass_pzero_1 [__builtin_isfpclass(0.0, 0x01F8) ? 1 : -1]; // fcFinite 195 char isfpclass_pzero_2 [!__builtin_isfpclass(0.0L, 0x0020) ? 1 : -1]; // fcNegZero 196 char isfpclass_pzero_3 [!__builtin_isfpclass(0.0, 0x0003) ? 1 : -1]; // fcNan 197 char isfpclass_nzero_0 [__builtin_isfpclass(-0.0f, 0x0060) ? 1 : -1]; // fcZero 198 char isfpclass_nzero_1 [__builtin_isfpclass(-0.0, 0x01F8) ? 1 : -1]; // fcFinite 199 char isfpclass_nzero_2 [!__builtin_isfpclass(-0.0L, 0x0040) ? 1 : -1]; // fcPosZero 200 char isfpclass_nzero_3 [!__builtin_isfpclass(-0.0, 0x0003) ? 1 : -1]; // fcNan 201 char isfpclass_ndenorm_0[__builtin_isfpclass(-1.0e-40f, 0x0010) ? 1 : -1]; // fcNegSubnormal 202 char isfpclass_ndenorm_2[!__builtin_isfpclass(-1.0e-40f, 0x03C0) ? 1 : -1]; // fcPositive 203 #ifndef __AVR__ 204 char isfpclass_ndenorm_1[__builtin_isfpclass(-1.0e-310, 0x01F8) ? 1 : -1]; // fcFinite 205 char isfpclass_ndenorm_3[!__builtin_isfpclass(-1.0e-310, 0x0207) ? 1 : -1]; // ~fcFinite 206 #endif 207 char isfpclass_neg_0 [__builtin_isfpclass(-1.0, 0x0008) ? 1 : -1]; // fcNegNormal 208 char isfpclass_neg_1 [!__builtin_isfpclass(-1.0f, 0x00100) ? 1 : -1]; // fcPosNormal 209 char isfpclass_neg_2 [__builtin_isfpclass(-1.0L, 0x01F8) ? 1 : -1]; // fcFinite 210 char isfpclass_neg_3 [!__builtin_isfpclass(-1.0, 0x0003) ? 1 : -1]; // fcSNan|fcQNan 211 char isfpclass_inf_neg_0[__builtin_isfpclass(-__builtin_inf(), 0x0004) ? 1 : -1]; // fcNegInf 212 char isfpclass_inf_neg_1[!__builtin_isfpclass(-__builtin_inff(), 0x0200) ? 1 : -1]; // fcPosInf 213 char isfpclass_inf_neg_2[__builtin_isfpclass(-__builtin_infl(), 0x0207) ? 1 : -1]; // ~fcFinite 214 char isfpclass_inf_neg_3[!__builtin_isfpclass(-__builtin_inf(), 0x03C0) ? 1 : -1]; // fcPositive 215 char isfpclass_qnan_0 [__builtin_isfpclass(__builtin_nan(""), 0x0002) ? 1 : -1]; // fcQNan 216 char isfpclass_qnan_1 [!__builtin_isfpclass(__builtin_nanf(""), 0x0001) ? 1 : -1]; // fcSNan 217 char isfpclass_qnan_2 [__builtin_isfpclass(__builtin_nanl(""), 0x0207) ? 1 : -1]; // ~fcFinite 218 char isfpclass_qnan_3 [!__builtin_isfpclass(__builtin_nan(""), 0x01F8) ? 1 : -1]; // fcFinite 219 char isfpclass_snan_0 [__builtin_isfpclass(__builtin_nansf(""), 0x0001) ? 1 : -1]; // fcSNan 220 char isfpclass_snan_1 [!__builtin_isfpclass(__builtin_nans(""), 0x0002) ? 1 : -1]; // fcQNan 221 char isfpclass_snan_2 [__builtin_isfpclass(__builtin_nansl(""), 0x0207) ? 1 : -1]; // ~fcFinite 222 char isfpclass_snan_3 [!__builtin_isfpclass(__builtin_nans(""), 0x01F8) ? 1 : -1]; // fcFinite 223 } 224 225 namespace signbit { 226 static_assert( 227 !__builtin_signbit(1.0) && __builtin_signbit(-1.0) && !__builtin_signbit(0.0) && __builtin_signbit(-0.0) && 228 !__builtin_signbitf(1.0f) && __builtin_signbitf(-1.0f) && !__builtin_signbitf(0.0f) && __builtin_signbitf(-0.0f) && 229 !__builtin_signbitl(1.0L) && __builtin_signbitf(-1.0L) && !__builtin_signbitf(0.0L) && __builtin_signbitf(-0.0L) && 230 !__builtin_signbit(1.0f) && __builtin_signbit(-1.0f) && !__builtin_signbit(0.0f) && __builtin_signbit(-0.0f) && 231 !__builtin_signbit(1.0L) && __builtin_signbit(-1.0L) && !__builtin_signbit(0.0L) && __builtin_signbit(-0.0L) && 232 true, "" 233 ); 234 } 235 236 namespace floating_comparison { 237 #define LESS(X, Y) \ 238 !__builtin_isgreater(X, Y) && __builtin_isgreater(Y, X) && \ 239 !__builtin_isgreaterequal(X, Y) && __builtin_isgreaterequal(Y, X) && \ 240 __builtin_isless(X, Y) && !__builtin_isless(Y, X) && \ 241 __builtin_islessequal(X, Y) && !__builtin_islessequal(Y, X) && \ 242 __builtin_islessgreater(X, Y) && __builtin_islessgreater(Y, X) && \ 243 !__builtin_isunordered(X, Y) && !__builtin_isunordered(Y, X) 244 #define EQUAL(X, Y) \ 245 !__builtin_isgreater(X, Y) && !__builtin_isgreater(Y, X) && \ 246 __builtin_isgreaterequal(X, Y) && __builtin_isgreaterequal(Y, X) && \ 247 !__builtin_isless(X, Y) && !__builtin_isless(Y, X) && \ 248 __builtin_islessequal(X, Y) && __builtin_islessequal(Y, X) && \ 249 !__builtin_islessgreater(X, Y) && !__builtin_islessgreater(Y, X) && \ 250 !__builtin_isunordered(X, Y) && !__builtin_isunordered(Y, X) 251 #define UNORDERED(X, Y) \ 252 !__builtin_isgreater(X, Y) && !__builtin_isgreater(Y, X) && \ 253 !__builtin_isgreaterequal(X, Y) && !__builtin_isgreaterequal(Y, X) && \ 254 !__builtin_isless(X, Y) && !__builtin_isless(Y, X) && \ 255 !__builtin_islessequal(X, Y) && !__builtin_islessequal(Y, X) && \ 256 !__builtin_islessgreater(X, Y) && !__builtin_islessgreater(Y, X) && \ 257 __builtin_isunordered(X, Y) && __builtin_isunordered(Y, X) 258 259 static_assert(LESS(0.0, 1.0)); 260 static_assert(LESS(0.0, __builtin_inf())); 261 static_assert(LESS(0.0f, 1.0f)); 262 static_assert(LESS(0.0f, __builtin_inff())); 263 static_assert(LESS(0.0L, 1.0L)); 264 static_assert(LESS(0.0L, __builtin_infl())); 265 266 static_assert(EQUAL(1.0, 1.0)); 267 static_assert(EQUAL(0.0, -0.0)); 268 static_assert(EQUAL(1.0f, 1.0f)); 269 static_assert(EQUAL(0.0f, -0.0f)); 270 static_assert(EQUAL(1.0L, 1.0L)); 271 static_assert(EQUAL(0.0L, -0.0L)); 272 273 static_assert(UNORDERED(__builtin_nan(""), 1.0)); 274 static_assert(UNORDERED(__builtin_nan(""), __builtin_inf())); 275 static_assert(UNORDERED(__builtin_nanf(""), 1.0f)); 276 static_assert(UNORDERED(__builtin_nanf(""), __builtin_inff())); 277 static_assert(UNORDERED(__builtin_nanl(""), 1.0L)); 278 static_assert(UNORDERED(__builtin_nanl(""), __builtin_infl())); 279 } 280 281 namespace fpclassify { 282 char classify_nan [__builtin_fpclassify(+1, -1, -1, -1, -1, __builtin_nan(""))]; 283 char classify_snan [__builtin_fpclassify(+1, -1, -1, -1, -1, __builtin_nans(""))]; 284 char classify_inf [__builtin_fpclassify(-1, +1, -1, -1, -1, __builtin_inf())]; 285 char classify_neg_inf [__builtin_fpclassify(-1, +1, -1, -1, -1, -__builtin_inf())]; 286 char classify_normal [__builtin_fpclassify(-1, -1, +1, -1, -1, 1.539)]; 287 #ifndef __AVR__ 288 char classify_normal2 [__builtin_fpclassify(-1, -1, +1, -1, -1, 1e-307)]; 289 char classify_denorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1e-308)]; 290 char classify_denorm2 [__builtin_fpclassify(-1, -1, -1, +1, -1, -1e-308)]; 291 #endif 292 char classify_zero [__builtin_fpclassify(-1, -1, -1, -1, +1, 0.0)]; 293 char classify_neg_zero[__builtin_fpclassify(-1, -1, -1, -1, +1, -0.0)]; 294 char classify_subnorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1.0e-38f)]; 295 } 296 297 namespace abs { 298 static_assert(__builtin_abs(14) == 14, ""); 299 static_assert(__builtin_labs(14L) == 14L, ""); 300 static_assert(__builtin_llabs(14LL) == 14LL, ""); 301 static_assert(__builtin_abs(-14) == 14, ""); 302 static_assert(__builtin_labs(-0x14L) == 0x14L, ""); 303 static_assert(__builtin_llabs(-0x141414141414LL) == 0x141414141414LL, ""); 304 #define BITSIZE(x) (sizeof(x) * 8) 305 constexpr int abs4 = __builtin_abs(1 << (BITSIZE(int) - 1)); // both-error {{must be initialized by a constant expression}} 306 constexpr long abs6 = __builtin_labs(1L << (BITSIZE(long) - 1)); // both-error {{must be initialized by a constant expression}} 307 constexpr long long abs8 = __builtin_llabs(1LL << (BITSIZE(long long) - 1)); // both-error {{must be initialized by a constant expression}} 308 #undef BITSIZE 309 } // namespace abs 310 311 namespace fabs { 312 static_assert(__builtin_fabs(-14.0) == 14.0, ""); 313 } 314 315 namespace std { 316 struct source_location { 317 struct __impl { 318 unsigned int _M_line; 319 const char *_M_file_name; 320 signed char _M_column; 321 const char *_M_function_name; 322 }; 323 using BuiltinT = decltype(__builtin_source_location()); // OK. 324 }; 325 } 326 327 namespace SourceLocation { 328 constexpr auto A = __builtin_source_location(); 329 static_assert(A->_M_line == __LINE__ -1, ""); 330 static_assert(A->_M_column == 22, ""); 331 static_assert(__builtin_strcmp(A->_M_function_name, "") == 0, ""); 332 static_assert(__builtin_strcmp(A->_M_file_name, __FILE__) == 0, ""); 333 334 static_assert(__builtin_LINE() == __LINE__, ""); 335 336 struct Foo { 337 int a = __builtin_LINE(); 338 }; 339 340 static_assert(Foo{}.a == __LINE__, ""); 341 342 struct AA { 343 int n = __builtin_LINE(); 344 }; 345 struct B { 346 AA a = {}; 347 }; 348 constexpr void f() { 349 constexpr B c = {}; 350 static_assert(c.a.n == __LINE__ - 1, ""); 351 } 352 } 353 354 #define BITSIZE(x) (sizeof(x) * 8) 355 namespace popcount { 356 static_assert(__builtin_popcount(~0u) == __CHAR_BIT__ * sizeof(unsigned int), ""); 357 static_assert(__builtin_popcount(0) == 0, ""); 358 static_assert(__builtin_popcountl(~0ul) == __CHAR_BIT__ * sizeof(unsigned long), ""); 359 static_assert(__builtin_popcountl(0) == 0, ""); 360 static_assert(__builtin_popcountll(~0ull) == __CHAR_BIT__ * sizeof(unsigned long long), ""); 361 static_assert(__builtin_popcountll(0) == 0, ""); 362 static_assert(__builtin_popcountg((unsigned char)~0) == __CHAR_BIT__ * sizeof(unsigned char), ""); 363 static_assert(__builtin_popcountg((unsigned char)0) == 0, ""); 364 static_assert(__builtin_popcountg((unsigned short)~0) == __CHAR_BIT__ * sizeof(unsigned short), ""); 365 static_assert(__builtin_popcountg((unsigned short)0) == 0, ""); 366 static_assert(__builtin_popcountg(~0u) == __CHAR_BIT__ * sizeof(unsigned int), ""); 367 static_assert(__builtin_popcountg(0u) == 0, ""); 368 static_assert(__builtin_popcountg(~0ul) == __CHAR_BIT__ * sizeof(unsigned long), ""); 369 static_assert(__builtin_popcountg(0ul) == 0, ""); 370 static_assert(__builtin_popcountg(~0ull) == __CHAR_BIT__ * sizeof(unsigned long long), ""); 371 static_assert(__builtin_popcountg(0ull) == 0, ""); 372 #ifdef __SIZEOF_INT128__ 373 static_assert(__builtin_popcountg(~(unsigned __int128)0) == __CHAR_BIT__ * sizeof(unsigned __int128), ""); 374 static_assert(__builtin_popcountg((unsigned __int128)0) == 0, ""); 375 #endif 376 #ifndef __AVR__ 377 static_assert(__builtin_popcountg(~(unsigned _BitInt(128))0) == __CHAR_BIT__ * sizeof(unsigned _BitInt(128)), ""); 378 static_assert(__builtin_popcountg((unsigned _BitInt(128))0) == 0, ""); 379 #endif 380 381 /// From test/Sema/constant-builtins-2.c 382 char popcount1[__builtin_popcount(0) == 0 ? 1 : -1]; 383 char popcount2[__builtin_popcount(0xF0F0) == 8 ? 1 : -1]; 384 char popcount3[__builtin_popcount(~0) == BITSIZE(int) ? 1 : -1]; 385 char popcount4[__builtin_popcount(~0L) == BITSIZE(int) ? 1 : -1]; 386 char popcount5[__builtin_popcountl(0L) == 0 ? 1 : -1]; 387 char popcount6[__builtin_popcountl(0xF0F0L) == 8 ? 1 : -1]; 388 char popcount7[__builtin_popcountl(~0L) == BITSIZE(long) ? 1 : -1]; 389 char popcount8[__builtin_popcountll(0LL) == 0 ? 1 : -1]; 390 char popcount9[__builtin_popcountll(0xF0F0LL) == 8 ? 1 : -1]; 391 char popcount10[__builtin_popcountll(~0LL) == BITSIZE(long long) ? 1 : -1]; 392 char popcount11[__builtin_popcountg(0U) == 0 ? 1 : -1]; 393 char popcount12[__builtin_popcountg(0xF0F0U) == 8 ? 1 : -1]; 394 char popcount13[__builtin_popcountg(~0U) == BITSIZE(int) ? 1 : -1]; 395 char popcount14[__builtin_popcountg(~0UL) == BITSIZE(long) ? 1 : -1]; 396 char popcount15[__builtin_popcountg(~0ULL) == BITSIZE(long long) ? 1 : -1]; 397 #ifdef __SIZEOF_INT128__ 398 char popcount16[__builtin_popcountg(~(unsigned __int128)0) == BITSIZE(__int128) ? 1 : -1]; 399 #endif 400 #ifndef __AVR__ 401 char popcount17[__builtin_popcountg(~(unsigned _BitInt(128))0) == BITSIZE(_BitInt(128)) ? 1 : -1]; 402 #endif 403 } 404 405 namespace parity { 406 /// From test/Sema/constant-builtins-2.c 407 char parity1[__builtin_parity(0) == 0 ? 1 : -1]; 408 char parity2[__builtin_parity(0xb821) == 0 ? 1 : -1]; 409 char parity3[__builtin_parity(0xb822) == 0 ? 1 : -1]; 410 char parity4[__builtin_parity(0xb823) == 1 ? 1 : -1]; 411 char parity5[__builtin_parity(0xb824) == 0 ? 1 : -1]; 412 char parity6[__builtin_parity(0xb825) == 1 ? 1 : -1]; 413 char parity7[__builtin_parity(0xb826) == 1 ? 1 : -1]; 414 char parity8[__builtin_parity(~0) == 0 ? 1 : -1]; 415 char parity9[__builtin_parityl(1L << (BITSIZE(long) - 1)) == 1 ? 1 : -1]; 416 char parity10[__builtin_parityll(1LL << (BITSIZE(long long) - 1)) == 1 ? 1 : -1]; 417 } 418 419 namespace clrsb { 420 char clrsb1[__builtin_clrsb(0) == BITSIZE(int) - 1 ? 1 : -1]; 421 char clrsb2[__builtin_clrsbl(0L) == BITSIZE(long) - 1 ? 1 : -1]; 422 char clrsb3[__builtin_clrsbll(0LL) == BITSIZE(long long) - 1 ? 1 : -1]; 423 char clrsb4[__builtin_clrsb(~0) == BITSIZE(int) - 1 ? 1 : -1]; 424 char clrsb5[__builtin_clrsbl(~0L) == BITSIZE(long) - 1 ? 1 : -1]; 425 char clrsb6[__builtin_clrsbll(~0LL) == BITSIZE(long long) - 1 ? 1 : -1]; 426 char clrsb7[__builtin_clrsb(1) == BITSIZE(int) - 2 ? 1 : -1]; 427 char clrsb8[__builtin_clrsb(~1) == BITSIZE(int) - 2 ? 1 : -1]; 428 char clrsb9[__builtin_clrsb(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1]; 429 char clrsb10[__builtin_clrsb(~(1 << (BITSIZE(int) - 1))) == 0 ? 1 : -1]; 430 char clrsb11[__builtin_clrsb(0xf) == BITSIZE(int) - 5 ? 1 : -1]; 431 char clrsb12[__builtin_clrsb(~0x1f) == BITSIZE(int) - 6 ? 1 : -1]; 432 } 433 434 namespace bitreverse { 435 char bitreverse1[__builtin_bitreverse8(0x01) == 0x80 ? 1 : -1]; 436 char bitreverse2[__builtin_bitreverse16(0x3C48) == 0x123C ? 1 : -1]; 437 char bitreverse3[__builtin_bitreverse32(0x12345678) == 0x1E6A2C48 ? 1 : -1]; 438 char bitreverse4[__builtin_bitreverse64(0x0123456789ABCDEFULL) == 0xF7B3D591E6A2C480 ? 1 : -1]; 439 } 440 441 namespace expect { 442 constexpr int a() { 443 return 12; 444 } 445 static_assert(__builtin_expect(a(),1) == 12, ""); 446 static_assert(__builtin_expect_with_probability(a(), 1, 1.0) == 12, ""); 447 } 448 449 namespace rotateleft { 450 char rotateleft1[__builtin_rotateleft8(0x01, 5) == 0x20 ? 1 : -1]; 451 char rotateleft2[__builtin_rotateleft16(0x3210, 11) == 0x8190 ? 1 : -1]; 452 char rotateleft3[__builtin_rotateleft32(0x76543210, 22) == 0x841D950C ? 1 : -1]; 453 char rotateleft4[__builtin_rotateleft64(0xFEDCBA9876543210ULL, 55) == 0x87F6E5D4C3B2A19ULL ? 1 : -1]; 454 } 455 456 namespace rotateright { 457 char rotateright1[__builtin_rotateright8(0x01, 5) == 0x08 ? 1 : -1]; 458 char rotateright2[__builtin_rotateright16(0x3210, 11) == 0x4206 ? 1 : -1]; 459 char rotateright3[__builtin_rotateright32(0x76543210, 22) == 0x50C841D9 ? 1 : -1]; 460 char rotateright4[__builtin_rotateright64(0xFEDCBA9876543210ULL, 55) == 0xB97530ECA86421FDULL ? 1 : -1]; 461 } 462 463 namespace ffs { 464 char ffs1[__builtin_ffs(0) == 0 ? 1 : -1]; 465 char ffs2[__builtin_ffs(1) == 1 ? 1 : -1]; 466 char ffs3[__builtin_ffs(0xfbe71) == 1 ? 1 : -1]; 467 char ffs4[__builtin_ffs(0xfbe70) == 5 ? 1 : -1]; 468 char ffs5[__builtin_ffs(1U << (BITSIZE(int) - 1)) == BITSIZE(int) ? 1 : -1]; 469 char ffs6[__builtin_ffsl(0x10L) == 5 ? 1 : -1]; 470 char ffs7[__builtin_ffsll(0x100LL) == 9 ? 1 : -1]; 471 } 472 473 namespace EhReturnDataRegno { 474 void test11(int X) { 475 switch (X) { 476 case __builtin_eh_return_data_regno(0): // constant foldable. 477 break; 478 } 479 __builtin_eh_return_data_regno(X); // both-error {{argument to '__builtin_eh_return_data_regno' must be a constant integer}} 480 } 481 } 482 483 /// From test/SemaCXX/builtins.cpp 484 namespace test_launder { 485 #define TEST_TYPE(Ptr, Type) \ 486 static_assert(__is_same(decltype(__builtin_launder(Ptr)), Type), "expected same type") 487 488 struct Dummy {}; 489 490 using FnType = int(char); 491 using MemFnType = int (Dummy::*)(char); 492 using ConstMemFnType = int (Dummy::*)() const; 493 494 void foo() {} 495 496 void test_builtin_launder_diags(void *vp, const void *cvp, FnType *fnp, 497 MemFnType mfp, ConstMemFnType cmfp, int (&Arr)[5]) { 498 __builtin_launder(vp); // both-error {{void pointer argument to '__builtin_launder' is not allowed}} 499 __builtin_launder(cvp); // both-error {{void pointer argument to '__builtin_launder' is not allowed}} 500 __builtin_launder(fnp); // both-error {{function pointer argument to '__builtin_launder' is not allowed}} 501 __builtin_launder(mfp); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}} 502 __builtin_launder(cmfp); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}} 503 (void)__builtin_launder(&fnp); 504 __builtin_launder(42); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}} 505 __builtin_launder(nullptr); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}} 506 __builtin_launder(foo); // both-error {{function pointer argument to '__builtin_launder' is not allowed}} 507 (void)__builtin_launder(Arr); 508 } 509 510 void test_builtin_launder(char *p, const volatile int *ip, const float *&fp, 511 double *__restrict dp) { 512 int x; 513 __builtin_launder(x); // both-error {{non-pointer argument to '__builtin_launder' is not allowed}} 514 515 TEST_TYPE(p, char*); 516 TEST_TYPE(ip, const volatile int*); 517 TEST_TYPE(fp, const float*); 518 TEST_TYPE(dp, double *__restrict); 519 520 char *d = __builtin_launder(p); 521 const volatile int *id = __builtin_launder(ip); 522 int *id2 = __builtin_launder(ip); // both-error {{cannot initialize a variable of type 'int *' with an rvalue of type 'const volatile int *'}} 523 const float* fd = __builtin_launder(fp); 524 } 525 526 void test_launder_return_type(const int (&ArrayRef)[101], int (&MArrRef)[42][13], 527 void (**&FuncPtrRef)()) { 528 TEST_TYPE(ArrayRef, const int *); 529 TEST_TYPE(MArrRef, int(*)[13]); 530 TEST_TYPE(FuncPtrRef, void (**)()); 531 } 532 533 template <class Tp> 534 constexpr Tp *test_constexpr_launder(Tp *tp) { 535 return __builtin_launder(tp); 536 } 537 constexpr int const_int = 42; 538 constexpr int const_int2 = 101; 539 constexpr const int *const_ptr = test_constexpr_launder(&const_int); 540 static_assert(&const_int == const_ptr, ""); 541 static_assert(const_ptr != test_constexpr_launder(&const_int2), ""); 542 543 void test_non_constexpr() { 544 constexpr int i = 42; // both-note {{address of non-static constexpr variable 'i' may differ on each invocation}} 545 constexpr const int *ip = __builtin_launder(&i); // both-error {{constexpr variable 'ip' must be initialized by a constant expression}} 546 // both-note@-1 {{pointer to 'i' is not a constant expression}} 547 } 548 549 constexpr bool test_in_constexpr(const int &i) { 550 return (__builtin_launder(&i) == &i); 551 } 552 553 static_assert(test_in_constexpr(const_int), ""); 554 void f() { 555 constexpr int i = 42; 556 static_assert(test_in_constexpr(i), ""); 557 } 558 559 struct Incomplete; // both-note {{forward declaration}} 560 struct IncompleteMember { 561 Incomplete &i; 562 }; 563 void test_incomplete(Incomplete *i, IncompleteMember *im) { 564 // both-error@+1 {{incomplete type 'Incomplete' where a complete type is required}} 565 __builtin_launder(i); 566 __builtin_launder(&i); // OK 567 __builtin_launder(im); // OK 568 } 569 570 void test_noexcept(int *i) { 571 static_assert(noexcept(__builtin_launder(i)), ""); 572 } 573 #undef TEST_TYPE 574 } // end namespace test_launder 575 576 577 /// FIXME: The commented out tests here use a IntAP value and fail. 578 /// This currently means we will leak the IntAP value since nothing cleans it up. 579 namespace clz { 580 char clz1[__builtin_clz(1) == BITSIZE(int) - 1 ? 1 : -1]; 581 char clz2[__builtin_clz(7) == BITSIZE(int) - 3 ? 1 : -1]; 582 char clz3[__builtin_clz(1 << (BITSIZE(int) - 1)) == 0 ? 1 : -1]; 583 int clz4 = __builtin_clz(0); 584 char clz5[__builtin_clzl(0xFL) == BITSIZE(long) - 4 ? 1 : -1]; 585 char clz6[__builtin_clzll(0xFFLL) == BITSIZE(long long) - 8 ? 1 : -1]; 586 char clz7[__builtin_clzs(0x1) == BITSIZE(short) - 1 ? 1 : -1]; 587 char clz8[__builtin_clzs(0xf) == BITSIZE(short) - 4 ? 1 : -1]; 588 char clz9[__builtin_clzs(0xfff) == BITSIZE(short) - 12 ? 1 : -1]; 589 590 int clz10 = __builtin_clzg((unsigned char)0); 591 char clz11[__builtin_clzg((unsigned char)0, 42) == 42 ? 1 : -1]; 592 char clz12[__builtin_clzg((unsigned char)0x1) == BITSIZE(char) - 1 ? 1 : -1]; 593 char clz13[__builtin_clzg((unsigned char)0x1, 42) == BITSIZE(char) - 1 ? 1 : -1]; 594 char clz14[__builtin_clzg((unsigned char)0xf) == BITSIZE(char) - 4 ? 1 : -1]; 595 char clz15[__builtin_clzg((unsigned char)0xf, 42) == BITSIZE(char) - 4 ? 1 : -1]; 596 char clz16[__builtin_clzg((unsigned char)(1 << (BITSIZE(char) - 1))) == 0 ? 1 : -1]; 597 char clz17[__builtin_clzg((unsigned char)(1 << (BITSIZE(char) - 1)), 42) == 0 ? 1 : -1]; 598 int clz18 = __builtin_clzg((unsigned short)0); 599 char clz19[__builtin_clzg((unsigned short)0, 42) == 42 ? 1 : -1]; 600 char clz20[__builtin_clzg((unsigned short)0x1) == BITSIZE(short) - 1 ? 1 : -1]; 601 char clz21[__builtin_clzg((unsigned short)0x1, 42) == BITSIZE(short) - 1 ? 1 : -1]; 602 char clz22[__builtin_clzg((unsigned short)0xf) == BITSIZE(short) - 4 ? 1 : -1]; 603 char clz23[__builtin_clzg((unsigned short)0xf, 42) == BITSIZE(short) - 4 ? 1 : -1]; 604 char clz24[__builtin_clzg((unsigned short)(1 << (BITSIZE(short) - 1))) == 0 ? 1 : -1]; 605 char clz25[__builtin_clzg((unsigned short)(1 << (BITSIZE(short) - 1)), 42) == 0 ? 1 : -1]; 606 int clz26 = __builtin_clzg(0U); 607 char clz27[__builtin_clzg(0U, 42) == 42 ? 1 : -1]; 608 char clz28[__builtin_clzg(0x1U) == BITSIZE(int) - 1 ? 1 : -1]; 609 char clz29[__builtin_clzg(0x1U, 42) == BITSIZE(int) - 1 ? 1 : -1]; 610 char clz30[__builtin_clzg(0xfU) == BITSIZE(int) - 4 ? 1 : -1]; 611 char clz31[__builtin_clzg(0xfU, 42) == BITSIZE(int) - 4 ? 1 : -1]; 612 char clz32[__builtin_clzg(1U << (BITSIZE(int) - 1)) == 0 ? 1 : -1]; 613 char clz33[__builtin_clzg(1U << (BITSIZE(int) - 1), 42) == 0 ? 1 : -1]; 614 int clz34 = __builtin_clzg(0UL); 615 char clz35[__builtin_clzg(0UL, 42) == 42 ? 1 : -1]; 616 char clz36[__builtin_clzg(0x1UL) == BITSIZE(long) - 1 ? 1 : -1]; 617 char clz37[__builtin_clzg(0x1UL, 42) == BITSIZE(long) - 1 ? 1 : -1]; 618 char clz38[__builtin_clzg(0xfUL) == BITSIZE(long) - 4 ? 1 : -1]; 619 char clz39[__builtin_clzg(0xfUL, 42) == BITSIZE(long) - 4 ? 1 : -1]; 620 char clz40[__builtin_clzg(1UL << (BITSIZE(long) - 1)) == 0 ? 1 : -1]; 621 char clz41[__builtin_clzg(1UL << (BITSIZE(long) - 1), 42) == 0 ? 1 : -1]; 622 int clz42 = __builtin_clzg(0ULL); 623 char clz43[__builtin_clzg(0ULL, 42) == 42 ? 1 : -1]; 624 char clz44[__builtin_clzg(0x1ULL) == BITSIZE(long long) - 1 ? 1 : -1]; 625 char clz45[__builtin_clzg(0x1ULL, 42) == BITSIZE(long long) - 1 ? 1 : -1]; 626 char clz46[__builtin_clzg(0xfULL) == BITSIZE(long long) - 4 ? 1 : -1]; 627 char clz47[__builtin_clzg(0xfULL, 42) == BITSIZE(long long) - 4 ? 1 : -1]; 628 char clz48[__builtin_clzg(1ULL << (BITSIZE(long long) - 1)) == 0 ? 1 : -1]; 629 char clz49[__builtin_clzg(1ULL << (BITSIZE(long long) - 1), 42) == 0 ? 1 : -1]; 630 #ifdef __SIZEOF_INT128__ 631 // int clz50 = __builtin_clzg((unsigned __int128)0); 632 char clz51[__builtin_clzg((unsigned __int128)0, 42) == 42 ? 1 : -1]; 633 char clz52[__builtin_clzg((unsigned __int128)0x1) == BITSIZE(__int128) - 1 ? 1 : -1]; 634 char clz53[__builtin_clzg((unsigned __int128)0x1, 42) == BITSIZE(__int128) - 1 ? 1 : -1]; 635 char clz54[__builtin_clzg((unsigned __int128)0xf) == BITSIZE(__int128) - 4 ? 1 : -1]; 636 char clz55[__builtin_clzg((unsigned __int128)0xf, 42) == BITSIZE(__int128) - 4 ? 1 : -1]; 637 #endif 638 #ifndef __AVR__ 639 // int clz58 = __builtin_clzg((unsigned _BitInt(128))0); 640 char clz59[__builtin_clzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1]; 641 char clz60[__builtin_clzg((unsigned _BitInt(128))0x1) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1]; 642 char clz61[__builtin_clzg((unsigned _BitInt(128))0x1, 42) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1]; 643 char clz62[__builtin_clzg((unsigned _BitInt(128))0xf) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1]; 644 char clz63[__builtin_clzg((unsigned _BitInt(128))0xf, 42) == BITSIZE(_BitInt(128)) - 4 ? 1 : -1]; 645 #endif 646 } 647 648 namespace ctz { 649 char ctz1[__builtin_ctz(1) == 0 ? 1 : -1]; 650 char ctz2[__builtin_ctz(8) == 3 ? 1 : -1]; 651 char ctz3[__builtin_ctz(1 << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1]; 652 int ctz4 = __builtin_ctz(0); 653 char ctz5[__builtin_ctzl(0x10L) == 4 ? 1 : -1]; 654 char ctz6[__builtin_ctzll(0x100LL) == 8 ? 1 : -1]; 655 char ctz7[__builtin_ctzs(1 << (BITSIZE(short) - 1)) == BITSIZE(short) - 1 ? 1 : -1]; 656 int ctz8 = __builtin_ctzg((unsigned char)0); 657 char ctz9[__builtin_ctzg((unsigned char)0, 42) == 42 ? 1 : -1]; 658 char ctz10[__builtin_ctzg((unsigned char)0x1) == 0 ? 1 : -1]; 659 char ctz11[__builtin_ctzg((unsigned char)0x1, 42) == 0 ? 1 : -1]; 660 char ctz12[__builtin_ctzg((unsigned char)0x10) == 4 ? 1 : -1]; 661 char ctz13[__builtin_ctzg((unsigned char)0x10, 42) == 4 ? 1 : -1]; 662 char ctz14[__builtin_ctzg((unsigned char)(1 << (BITSIZE(char) - 1))) == BITSIZE(char) - 1 ? 1 : -1]; 663 char ctz15[__builtin_ctzg((unsigned char)(1 << (BITSIZE(char) - 1)), 42) == BITSIZE(char) - 1 ? 1 : -1]; 664 int ctz16 = __builtin_ctzg((unsigned short)0); 665 char ctz17[__builtin_ctzg((unsigned short)0, 42) == 42 ? 1 : -1]; 666 char ctz18[__builtin_ctzg((unsigned short)0x1) == 0 ? 1 : -1]; 667 char ctz19[__builtin_ctzg((unsigned short)0x1, 42) == 0 ? 1 : -1]; 668 char ctz20[__builtin_ctzg((unsigned short)0x10) == 4 ? 1 : -1]; 669 char ctz21[__builtin_ctzg((unsigned short)0x10, 42) == 4 ? 1 : -1]; 670 char ctz22[__builtin_ctzg((unsigned short)(1 << (BITSIZE(short) - 1))) == BITSIZE(short) - 1 ? 1 : -1]; 671 char ctz23[__builtin_ctzg((unsigned short)(1 << (BITSIZE(short) - 1)), 42) == BITSIZE(short) - 1 ? 1 : -1]; 672 int ctz24 = __builtin_ctzg(0U); 673 char ctz25[__builtin_ctzg(0U, 42) == 42 ? 1 : -1]; 674 char ctz26[__builtin_ctzg(0x1U) == 0 ? 1 : -1]; 675 char ctz27[__builtin_ctzg(0x1U, 42) == 0 ? 1 : -1]; 676 char ctz28[__builtin_ctzg(0x10U) == 4 ? 1 : -1]; 677 char ctz29[__builtin_ctzg(0x10U, 42) == 4 ? 1 : -1]; 678 char ctz30[__builtin_ctzg(1U << (BITSIZE(int) - 1)) == BITSIZE(int) - 1 ? 1 : -1]; 679 char ctz31[__builtin_ctzg(1U << (BITSIZE(int) - 1), 42) == BITSIZE(int) - 1 ? 1 : -1]; 680 int ctz32 = __builtin_ctzg(0UL); 681 char ctz33[__builtin_ctzg(0UL, 42) == 42 ? 1 : -1]; 682 char ctz34[__builtin_ctzg(0x1UL) == 0 ? 1 : -1]; 683 char ctz35[__builtin_ctzg(0x1UL, 42) == 0 ? 1 : -1]; 684 char ctz36[__builtin_ctzg(0x10UL) == 4 ? 1 : -1]; 685 char ctz37[__builtin_ctzg(0x10UL, 42) == 4 ? 1 : -1]; 686 char ctz38[__builtin_ctzg(1UL << (BITSIZE(long) - 1)) == BITSIZE(long) - 1 ? 1 : -1]; 687 char ctz39[__builtin_ctzg(1UL << (BITSIZE(long) - 1), 42) == BITSIZE(long) - 1 ? 1 : -1]; 688 int ctz40 = __builtin_ctzg(0ULL); 689 char ctz41[__builtin_ctzg(0ULL, 42) == 42 ? 1 : -1]; 690 char ctz42[__builtin_ctzg(0x1ULL) == 0 ? 1 : -1]; 691 char ctz43[__builtin_ctzg(0x1ULL, 42) == 0 ? 1 : -1]; 692 char ctz44[__builtin_ctzg(0x10ULL) == 4 ? 1 : -1]; 693 char ctz45[__builtin_ctzg(0x10ULL, 42) == 4 ? 1 : -1]; 694 char ctz46[__builtin_ctzg(1ULL << (BITSIZE(long long) - 1)) == BITSIZE(long long) - 1 ? 1 : -1]; 695 char ctz47[__builtin_ctzg(1ULL << (BITSIZE(long long) - 1), 42) == BITSIZE(long long) - 1 ? 1 : -1]; 696 #ifdef __SIZEOF_INT128__ 697 // int ctz48 = __builtin_ctzg((unsigned __int128)0); 698 char ctz49[__builtin_ctzg((unsigned __int128)0, 42) == 42 ? 1 : -1]; 699 char ctz50[__builtin_ctzg((unsigned __int128)0x1) == 0 ? 1 : -1]; 700 char ctz51[__builtin_ctzg((unsigned __int128)0x1, 42) == 0 ? 1 : -1]; 701 char ctz52[__builtin_ctzg((unsigned __int128)0x10) == 4 ? 1 : -1]; 702 char ctz53[__builtin_ctzg((unsigned __int128)0x10, 42) == 4 ? 1 : -1]; 703 char ctz54[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1)) == BITSIZE(__int128) - 1 ? 1 : -1]; 704 char ctz55[__builtin_ctzg((unsigned __int128)1 << (BITSIZE(__int128) - 1), 42) == BITSIZE(__int128) - 1 ? 1 : -1]; 705 #endif 706 #ifndef __AVR__ 707 // int ctz56 = __builtin_ctzg((unsigned _BitInt(128))0); 708 char ctz57[__builtin_ctzg((unsigned _BitInt(128))0, 42) == 42 ? 1 : -1]; 709 char ctz58[__builtin_ctzg((unsigned _BitInt(128))0x1) == 0 ? 1 : -1]; 710 char ctz59[__builtin_ctzg((unsigned _BitInt(128))0x1, 42) == 0 ? 1 : -1]; 711 char ctz60[__builtin_ctzg((unsigned _BitInt(128))0x10) == 4 ? 1 : -1]; 712 char ctz61[__builtin_ctzg((unsigned _BitInt(128))0x10, 42) == 4 ? 1 : -1]; 713 char ctz62[__builtin_ctzg((unsigned _BitInt(128))1 << (BITSIZE(_BitInt(128)) - 1)) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1]; 714 char ctz63[__builtin_ctzg((unsigned _BitInt(128))1 << (BITSIZE(_BitInt(128)) - 1), 42) == BITSIZE(_BitInt(128)) - 1 ? 1 : -1]; 715 #endif 716 } 717 718 namespace bswap { 719 extern int f(void); 720 int h3 = __builtin_bswap16(0x1234) == 0x3412 ? 1 : f(); 721 int h4 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f(); 722 int h5 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f(); 723 } 724 725 #define CFSTR __builtin___CFStringMakeConstantString 726 void test7(void) { 727 const void *X; 728 #if !defined(_AIX) 729 X = CFSTR("\242"); // both-warning {{input conversion stopped}} 730 X = CFSTR("\0"); // no-warning 731 X = CFSTR(242); // both-error {{cannot initialize a parameter of type 'const char *' with an rvalue of type 'int'}} 732 X = CFSTR("foo", "bar"); // both-error {{too many arguments to function call}} 733 #endif 734 } 735 736 /// The actual value on my machine is 22, but I have a feeling this will be different 737 /// on other targets, so just checking for != 0 here. Light testing is fine since 738 /// the actual implementation uses analyze_os_log::computeOSLogBufferLayout(), which 739 /// is tested elsewhere. 740 static_assert(__builtin_os_log_format_buffer_size("%{mask.xyz}s", "abc") != 0, ""); 741 742 /// Copied from test/Sema/constant_builtins_vector.cpp. 743 /// Some tests are missing since we run this for multiple targets, 744 /// some of which do not support _BitInt. 745 #ifndef __AVR__ 746 typedef _BitInt(128) BitInt128; 747 typedef double vector4double __attribute__((__vector_size__(32))); 748 typedef float vector4float __attribute__((__vector_size__(16))); 749 typedef long long vector4long __attribute__((__vector_size__(32))); 750 typedef int vector4int __attribute__((__vector_size__(16))); 751 typedef short vector4short __attribute__((__vector_size__(8))); 752 typedef char vector4char __attribute__((__vector_size__(4))); 753 typedef BitInt128 vector4BitInt128 __attribute__((__vector_size__(64))); 754 typedef double vector8double __attribute__((__vector_size__(64))); 755 typedef float vector8float __attribute__((__vector_size__(32))); 756 typedef long long vector8long __attribute__((__vector_size__(64))); 757 typedef int vector8int __attribute__((__vector_size__(32))); 758 typedef short vector8short __attribute__((__vector_size__(16))); 759 typedef char vector8char __attribute__((__vector_size__(8))); 760 typedef BitInt128 vector8BitInt128 __attribute__((__vector_size__(128))); 761 762 namespace convertvector { 763 constexpr vector4double from_vector4double_to_vector4double_var = 764 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4double); 765 constexpr vector4float from_vector4double_to_vector4float_var = 766 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4float); 767 constexpr vector4long from_vector4double_to_vector4long_var = 768 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4long); 769 constexpr vector4int from_vector4double_to_vector4int_var = 770 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4int); 771 constexpr vector4short from_vector4double_to_vector4short_var = 772 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4short); 773 constexpr vector4char from_vector4double_to_vector4char_var = 774 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4char); 775 constexpr vector4BitInt128 from_vector4double_to_vector4BitInt128_var = 776 __builtin_convertvector((vector4double){0, 1, 2, 3}, vector4BitInt128); 777 constexpr vector4double from_vector4float_to_vector4double_var = 778 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4double); 779 constexpr vector4float from_vector4float_to_vector4float_var = 780 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4float); 781 constexpr vector4long from_vector4float_to_vector4long_var = 782 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4long); 783 constexpr vector4int from_vector4float_to_vector4int_var = 784 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4int); 785 constexpr vector4short from_vector4float_to_vector4short_var = 786 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4short); 787 constexpr vector4char from_vector4float_to_vector4char_var = 788 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4char); 789 constexpr vector4BitInt128 from_vector4float_to_vector4BitInt128_var = 790 __builtin_convertvector((vector4float){0, 1, 2, 3}, vector4BitInt128); 791 constexpr vector4double from_vector4long_to_vector4double_var = 792 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4double); 793 constexpr vector4float from_vector4long_to_vector4float_var = 794 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4float); 795 constexpr vector4long from_vector4long_to_vector4long_var = 796 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4long); 797 constexpr vector4int from_vector4long_to_vector4int_var = 798 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4int); 799 constexpr vector4short from_vector4long_to_vector4short_var = 800 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4short); 801 constexpr vector4char from_vector4long_to_vector4char_var = 802 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4char); 803 constexpr vector4BitInt128 from_vector4long_to_vector4BitInt128_var = 804 __builtin_convertvector((vector4long){0, 1, 2, 3}, vector4BitInt128); 805 constexpr vector4double from_vector4int_to_vector4double_var = 806 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4double); 807 constexpr vector4float from_vector4int_to_vector4float_var = 808 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4float); 809 constexpr vector4long from_vector4int_to_vector4long_var = 810 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4long); 811 constexpr vector4int from_vector4int_to_vector4int_var = 812 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4int); 813 constexpr vector4short from_vector4int_to_vector4short_var = 814 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4short); 815 constexpr vector4char from_vector4int_to_vector4char_var = 816 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4char); 817 constexpr vector4BitInt128 from_vector4int_to_vector4BitInt128_var = 818 __builtin_convertvector((vector4int){0, 1, 2, 3}, vector4BitInt128); 819 constexpr vector4double from_vector4short_to_vector4double_var = 820 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4double); 821 constexpr vector4float from_vector4short_to_vector4float_var = 822 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4float); 823 constexpr vector4long from_vector4short_to_vector4long_var = 824 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4long); 825 constexpr vector4int from_vector4short_to_vector4int_var = 826 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4int); 827 constexpr vector4short from_vector4short_to_vector4short_var = 828 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4short); 829 constexpr vector4char from_vector4short_to_vector4char_var = 830 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4char); 831 constexpr vector4BitInt128 from_vector4short_to_vector4BitInt128_var = 832 __builtin_convertvector((vector4short){0, 1, 2, 3}, vector4BitInt128); 833 constexpr vector4double from_vector4char_to_vector4double_var = 834 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4double); 835 constexpr vector4float from_vector4char_to_vector4float_var = 836 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4float); 837 constexpr vector4long from_vector4char_to_vector4long_var = 838 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4long); 839 constexpr vector4int from_vector4char_to_vector4int_var = 840 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4int); 841 constexpr vector4short from_vector4char_to_vector4short_var = 842 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4short); 843 constexpr vector4char from_vector4char_to_vector4char_var = 844 __builtin_convertvector((vector4char){0, 1, 2, 3}, vector4char); 845 constexpr vector8double from_vector8double_to_vector8double_var = 846 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 847 vector8double); 848 constexpr vector8float from_vector8double_to_vector8float_var = 849 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 850 vector8float); 851 constexpr vector8long from_vector8double_to_vector8long_var = 852 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 853 vector8long); 854 constexpr vector8int from_vector8double_to_vector8int_var = 855 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 856 vector8int); 857 constexpr vector8short from_vector8double_to_vector8short_var = 858 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 859 vector8short); 860 constexpr vector8char from_vector8double_to_vector8char_var = 861 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 862 vector8char); 863 constexpr vector8BitInt128 from_vector8double_to_vector8BitInt128_var = 864 __builtin_convertvector((vector8double){0, 1, 2, 3, 4, 5, 6, 7}, 865 vector8BitInt128); 866 constexpr vector8double from_vector8float_to_vector8double_var = 867 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 868 vector8double); 869 constexpr vector8float from_vector8float_to_vector8float_var = 870 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 871 vector8float); 872 constexpr vector8long from_vector8float_to_vector8long_var = 873 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 874 vector8long); 875 constexpr vector8int from_vector8float_to_vector8int_var = 876 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, vector8int); 877 constexpr vector8short from_vector8float_to_vector8short_var = 878 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 879 vector8short); 880 constexpr vector8char from_vector8float_to_vector8char_var = 881 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 882 vector8char); 883 constexpr vector8BitInt128 from_vector8float_to_vector8BitInt128_var = 884 __builtin_convertvector((vector8float){0, 1, 2, 3, 4, 5, 6, 7}, 885 vector8BitInt128); 886 constexpr vector8double from_vector8long_to_vector8double_var = 887 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, 888 vector8double); 889 constexpr vector8float from_vector8long_to_vector8float_var = 890 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, 891 vector8float); 892 constexpr vector8long from_vector8long_to_vector8long_var = 893 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8long); 894 constexpr vector8int from_vector8long_to_vector8int_var = 895 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8int); 896 constexpr vector8short from_vector8long_to_vector8short_var = 897 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, 898 vector8short); 899 constexpr vector8char from_vector8long_to_vector8char_var = 900 __builtin_convertvector((vector8long){0, 1, 2, 3, 4, 5, 6, 7}, vector8char); 901 constexpr vector8double from_vector8int_to_vector8double_var = 902 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, 903 vector8double); 904 constexpr vector8float from_vector8int_to_vector8float_var = 905 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8float); 906 constexpr vector8long from_vector8int_to_vector8long_var = 907 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8long); 908 constexpr vector8int from_vector8int_to_vector8int_var = 909 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8int); 910 constexpr vector8short from_vector8int_to_vector8short_var = 911 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8short); 912 constexpr vector8char from_vector8int_to_vector8char_var = 913 __builtin_convertvector((vector8int){0, 1, 2, 3, 4, 5, 6, 7}, vector8char); 914 constexpr vector8double from_vector8short_to_vector8double_var = 915 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, 916 vector8double); 917 constexpr vector8float from_vector8short_to_vector8float_var = 918 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, 919 vector8float); 920 constexpr vector8long from_vector8short_to_vector8long_var = 921 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, 922 vector8long); 923 constexpr vector8int from_vector8short_to_vector8int_var = 924 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, vector8int); 925 constexpr vector8short from_vector8short_to_vector8short_var = 926 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, 927 vector8short); 928 constexpr vector8char from_vector8short_to_vector8char_var = 929 __builtin_convertvector((vector8short){0, 1, 2, 3, 4, 5, 6, 7}, 930 vector8char); 931 932 constexpr vector8double from_vector8char_to_vector8double_var = 933 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, 934 vector8double); 935 constexpr vector8float from_vector8char_to_vector8float_var = 936 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, 937 vector8float); 938 constexpr vector8long from_vector8char_to_vector8long_var = 939 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8long); 940 constexpr vector8int from_vector8char_to_vector8int_var = 941 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8int); 942 constexpr vector8short from_vector8char_to_vector8short_var = 943 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, 944 vector8short); 945 constexpr vector8char from_vector8char_to_vector8char_var = 946 __builtin_convertvector((vector8char){0, 1, 2, 3, 4, 5, 6, 7}, vector8char); 947 constexpr vector8double from_vector8BitInt128_to_vector8double_var = 948 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 949 vector8double); 950 constexpr vector8float from_vector8BitInt128_to_vector8float_var = 951 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 952 vector8float); 953 constexpr vector8long from_vector8BitInt128_to_vector8long_var = 954 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 955 vector8long); 956 constexpr vector8int from_vector8BitInt128_to_vector8int_var = 957 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 958 vector8int); 959 constexpr vector8short from_vector8BitInt128_to_vector8short_var = 960 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 961 vector8short); 962 constexpr vector8char from_vector8BitInt128_to_vector8char_var = 963 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 964 vector8char); 965 constexpr vector8BitInt128 from_vector8BitInt128_to_vector8BitInt128_var = 966 __builtin_convertvector((vector8BitInt128){0, 1, 2, 3, 4, 5, 6, 7}, 967 vector8BitInt128); 968 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[0] == 0, ""); 969 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[1] == 1, ""); 970 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[2] == 2, ""); 971 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[3] == 3, ""); 972 static_assert(from_vector8BitInt128_to_vector8BitInt128_var[4] == 4, ""); 973 } 974 975 namespace shufflevector { 976 constexpr vector4char vector4charConst1 = {0, 1, 2, 3}; 977 constexpr vector4char vector4charConst2 = {4, 5, 6, 7}; 978 constexpr vector8char vector8intConst = {8, 9, 10, 11, 12, 13, 14, 15}; 979 constexpr vector4char vectorShuffle1 = 980 __builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 1, 2, 3); 981 constexpr vector4char vectorShuffle2 = 982 __builtin_shufflevector(vector4charConst1, vector4charConst2, 4, 5, 6, 7); 983 constexpr vector4char vectorShuffle3 = 984 __builtin_shufflevector(vector4charConst1, vector4charConst2, 0, 2, 4, 6); 985 constexpr vector8char vectorShuffle4 = __builtin_shufflevector( 986 vector8intConst, vector8intConst, 0, 2, 4, 6, 8, 10, 12, 14); 987 constexpr vector4char vectorShuffle5 = 988 __builtin_shufflevector(vector8intConst, vector8intConst, 0, 2, 4, 6); 989 constexpr vector8char vectorShuffle6 = __builtin_shufflevector( 990 vector4charConst1, vector4charConst2, 0, 2, 4, 6, 1, 3, 5, 7); 991 992 static_assert(vectorShuffle6[0] == 0, ""); 993 static_assert(vectorShuffle6[1] == 2, ""); 994 static_assert(vectorShuffle6[2] == 4, ""); 995 static_assert(vectorShuffle6[3] == 6, ""); 996 static_assert(vectorShuffle6[4] == 1, ""); 997 static_assert(vectorShuffle6[5] == 3, ""); 998 static_assert(vectorShuffle6[6] == 5, ""); 999 static_assert(vectorShuffle6[7] == 7, ""); 1000 1001 constexpr vector4char vectorShuffleFail1 = __builtin_shufflevector( // both-error {{must be initialized by a constant expression}}\ 1002 // both-error {{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}} 1003 vector4charConst1, 1004 vector4charConst2, -1, -1, -1, -1); 1005 } 1006 1007 #endif 1008 1009 namespace FunctionStart { 1010 void a(void) {} 1011 static_assert(__builtin_function_start(a) == a, ""); // both-error {{not an integral constant expression}} \ 1012 // ref-note {{comparison against opaque constant address '&__builtin_function_start(a)'}} \ 1013 // expected-note {{comparison of addresses of potentially overlapping literals has unspecified value}} 1014 } 1015 1016 namespace BuiltinInImplicitCtor { 1017 constexpr struct { 1018 int a = __builtin_isnan(1.0); 1019 } Foo; 1020 static_assert(Foo.a == 0, ""); 1021 } 1022 1023 typedef double vector4double __attribute__((__vector_size__(32))); 1024 typedef float vector4float __attribute__((__vector_size__(16))); 1025 typedef long long vector4long __attribute__((__vector_size__(32))); 1026 typedef int vector4int __attribute__((__vector_size__(16))); 1027 typedef unsigned long long vector4ulong __attribute__((__vector_size__(32))); 1028 typedef unsigned int vector4uint __attribute__((__vector_size__(16))); 1029 typedef short vector4short __attribute__((__vector_size__(8))); 1030 typedef char vector4char __attribute__((__vector_size__(4))); 1031 typedef double vector8double __attribute__((__vector_size__(64))); 1032 typedef float vector8float __attribute__((__vector_size__(32))); 1033 typedef long long vector8long __attribute__((__vector_size__(64))); 1034 typedef int vector8int __attribute__((__vector_size__(32))); 1035 typedef short vector8short __attribute__((__vector_size__(16))); 1036 typedef char vector8char __attribute__((__vector_size__(8))); 1037 1038 namespace RecuceAdd { 1039 static_assert(__builtin_reduce_add((vector4char){}) == 0); 1040 static_assert(__builtin_reduce_add((vector4char){1, 2, 3, 4}) == 10); 1041 static_assert(__builtin_reduce_add((vector4short){10, 20, 30, 40}) == 100); 1042 static_assert(__builtin_reduce_add((vector4int){100, 200, 300, 400}) == 1000); 1043 static_assert(__builtin_reduce_add((vector4long){1000, 2000, 3000, 4000}) == 10000); 1044 constexpr int reduceAddInt1 = __builtin_reduce_add((vector4int){~(1 << (sizeof(int) * 8 - 1)), 0, 0, 1}); 1045 // both-error@-1 {{must be initialized by a constant expression}} \ 1046 // both-note@-1 {{outside the range of representable values of type 'int'}} 1047 constexpr long long reduceAddLong1 = __builtin_reduce_add((vector4long){~(1LL << (sizeof(long long) * 8 - 1)), 0, 0, 1}); 1048 // both-error@-1 {{must be initialized by a constant expression}} \ 1049 // both-note@-1 {{outside the range of representable values of type 'long long'}} 1050 constexpr int reduceAddInt2 = __builtin_reduce_add((vector4int){(1 << (sizeof(int) * 8 - 1)), 0, 0, -1}); 1051 // both-error@-1 {{must be initialized by a constant expression}} \ 1052 // both-note@-1 {{outside the range of representable values of type 'int'}} 1053 constexpr long long reduceAddLong2 = __builtin_reduce_add((vector4long){(1LL << (sizeof(long long) * 8 - 1)), 0, 0, -1}); 1054 // both-error@-1 {{must be initialized by a constant expression}} \ 1055 // both-note@-1 {{outside the range of representable values of type 'long long'}} 1056 static_assert(__builtin_reduce_add((vector4uint){~0U, 0, 0, 1}) == 0); 1057 static_assert(__builtin_reduce_add((vector4ulong){~0ULL, 0, 0, 1}) == 0); 1058 1059 1060 #ifdef __SIZEOF_INT128__ 1061 typedef __int128 v4i128 __attribute__((__vector_size__(128 * 2))); 1062 constexpr __int128 reduceAddInt3 = __builtin_reduce_add((v4i128){}); 1063 static_assert(reduceAddInt3 == 0); 1064 #endif 1065 } 1066 1067 namespace ReduceMul { 1068 static_assert(__builtin_reduce_mul((vector4char){}) == 0); 1069 static_assert(__builtin_reduce_mul((vector4char){1, 2, 3, 4}) == 24); 1070 static_assert(__builtin_reduce_mul((vector4short){1, 2, 30, 40}) == 2400); 1071 #ifndef __AVR__ 1072 static_assert(__builtin_reduce_mul((vector4int){10, 20, 300, 400}) == 24'000'000); 1073 #endif 1074 static_assert(__builtin_reduce_mul((vector4long){1000L, 2000L, 3000L, 4000L}) == 24'000'000'000'000L); 1075 constexpr int reduceMulInt1 = __builtin_reduce_mul((vector4int){~(1 << (sizeof(int) * 8 - 1)), 1, 1, 2}); 1076 // both-error@-1 {{must be initialized by a constant expression}} \ 1077 // both-note@-1 {{outside the range of representable values of type 'int'}} 1078 constexpr long long reduceMulLong1 = __builtin_reduce_mul((vector4long){~(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2}); 1079 // both-error@-1 {{must be initialized by a constant expression}} \ 1080 // both-note@-1 {{outside the range of representable values of type 'long long'}} 1081 constexpr int reduceMulInt2 = __builtin_reduce_mul((vector4int){(1 << (sizeof(int) * 8 - 1)), 1, 1, 2}); 1082 // both-error@-1 {{must be initialized by a constant expression}} \ 1083 // both-note@-1 {{outside the range of representable values of type 'int'}} 1084 constexpr long long reduceMulLong2 = __builtin_reduce_mul((vector4long){(1LL << (sizeof(long long) * 8 - 1)), 1, 1, 2}); 1085 // both-error@-1 {{must be initialized by a constant expression}} \ 1086 // both-note@-1 {{outside the range of representable values of type 'long long'}} 1087 static_assert(__builtin_reduce_mul((vector4uint){~0U, 1, 1, 2}) == 1088 #ifdef __AVR__ 1089 0); 1090 #else 1091 (~0U - 1)); 1092 #endif 1093 static_assert(__builtin_reduce_mul((vector4ulong){~0ULL, 1, 1, 2}) == ~0ULL - 1); 1094 } 1095 1096 namespace ReduceAnd { 1097 static_assert(__builtin_reduce_and((vector4char){}) == 0); 1098 static_assert(__builtin_reduce_and((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == 0); 1099 static_assert(__builtin_reduce_and((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == 0); 1100 static_assert(__builtin_reduce_and((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == 0); 1101 #if __INT_WIDTH__ == 32 1102 static_assert(__builtin_reduce_and((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == 0L); 1103 static_assert(__builtin_reduce_and((vector4char){(char)-1, (char)~0x22, (char)~0x44, (char)~0x88}) == 0x11); 1104 static_assert(__builtin_reduce_and((vector4short){(short)~0x1111, (short)-1, (short)~0x4444, (short)~0x8888}) == 0x2222); 1105 static_assert(__builtin_reduce_and((vector4int){(int)~0x11111111, (int)~0x22222222, (int)-1, (int)~0x88888888}) == 0x44444444); 1106 static_assert(__builtin_reduce_and((vector4long){(long long)~0x1111111111111111L, (long long)~0x2222222222222222L, (long long)~0x4444444444444444L, (long long)-1}) == 0x8888888888888888L); 1107 static_assert(__builtin_reduce_and((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0U); 1108 static_assert(__builtin_reduce_and((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0L); 1109 #endif 1110 } 1111 1112 namespace ReduceOr { 1113 static_assert(__builtin_reduce_or((vector4char){}) == 0); 1114 static_assert(__builtin_reduce_or((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0xFF); 1115 static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0xFFFF); 1116 static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0xFFFFFFFF); 1117 #if __INT_WIDTH__ == 32 1118 static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL); 1119 static_assert(__builtin_reduce_or((vector4char){(char)0, (char)0x22, (char)0x44, (char)0x88}) == ~0x11); 1120 static_assert(__builtin_reduce_or((vector4short){(short)0x1111, (short)0, (short)0x4444, (short)0x8888}) == ~0x2222); 1121 static_assert(__builtin_reduce_or((vector4int){(int)0x11111111, (int)0x22222222, (int)0, (int)0x88888888}) == ~0x44444444); 1122 static_assert(__builtin_reduce_or((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0}) == ~0x8888888888888888L); 1123 static_assert(__builtin_reduce_or((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU); 1124 static_assert(__builtin_reduce_or((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFL); 1125 #endif 1126 } 1127 1128 namespace ReduceXor { 1129 static_assert(__builtin_reduce_xor((vector4char){}) == 0); 1130 static_assert(__builtin_reduce_xor((vector4char){(char)0x11, (char)0x22, (char)0x44, (char)0x88}) == (char)0xFF); 1131 static_assert(__builtin_reduce_xor((vector4short){(short)0x1111, (short)0x2222, (short)0x4444, (short)0x8888}) == (short)0xFFFF); 1132 #if __INT_WIDTH__ == 32 1133 static_assert(__builtin_reduce_xor((vector4int){(int)0x11111111, (int)0x22222222, (int)0x44444444, (int)0x88888888}) == (int)0xFFFFFFFF); 1134 static_assert(__builtin_reduce_xor((vector4long){(long long)0x1111111111111111L, (long long)0x2222222222222222L, (long long)0x4444444444444444L, (long long)0x8888888888888888L}) == (long long)0xFFFFFFFFFFFFFFFFL); 1135 static_assert(__builtin_reduce_xor((vector4uint){0x11111111U, 0x22222222U, 0x44444444U, 0x88888888U}) == 0xFFFFFFFFU); 1136 static_assert(__builtin_reduce_xor((vector4ulong){0x1111111111111111UL, 0x2222222222222222UL, 0x4444444444444444UL, 0x8888888888888888UL}) == 0xFFFFFFFFFFFFFFFFUL); 1137 #endif 1138 } 1139 1140 namespace ElementwisePopcount { 1141 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){1, 2, 3, 4})) == 5); 1142 #if __INT_WIDTH__ == 32 1143 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4int){0, 0xF0F0, ~0, ~0xF0F0})) == 16 * sizeof(int)); 1144 #endif 1145 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){1L, 2L, 3L, 4L})) == 5L); 1146 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4long){0L, 0xF0F0L, ~0L, ~0xF0F0L})) == 16 * sizeof(long long)); 1147 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){1U, 2U, 3U, 4U})) == 5U); 1148 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4uint){0U, 0xF0F0U, ~0U, ~0xF0F0U})) == 16 * sizeof(int)); 1149 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){1UL, 2UL, 3UL, 4UL})) == 5UL); 1150 static_assert(__builtin_reduce_add(__builtin_elementwise_popcount((vector4ulong){0ULL, 0xF0F0ULL, ~0ULL, ~0xF0F0ULL})) == 16 * sizeof(unsigned long long)); 1151 static_assert(__builtin_elementwise_popcount(0) == 0); 1152 static_assert(__builtin_elementwise_popcount(0xF0F0) == 8); 1153 static_assert(__builtin_elementwise_popcount(~0) == 8 * sizeof(int)); 1154 static_assert(__builtin_elementwise_popcount(0U) == 0); 1155 static_assert(__builtin_elementwise_popcount(0xF0F0U) == 8); 1156 static_assert(__builtin_elementwise_popcount(~0U) == 8 * sizeof(int)); 1157 static_assert(__builtin_elementwise_popcount(0L) == 0); 1158 static_assert(__builtin_elementwise_popcount(0xF0F0L) == 8); 1159 static_assert(__builtin_elementwise_popcount(~0LL) == 8 * sizeof(long long)); 1160 1161 #if __INT_WIDTH__ == 32 1162 static_assert(__builtin_bit_cast(unsigned, __builtin_elementwise_popcount((vector4char){1, 2, 3, 4})) == (LITTLE_END ? 0x01020101 : 0x01010201)); 1163 #endif 1164 } 1165 1166 namespace BuiltinMemcpy { 1167 constexpr int simple() { 1168 int a = 12; 1169 int b = 0; 1170 __builtin_memcpy(&b, &a, sizeof(a)); 1171 return b; 1172 } 1173 static_assert(simple() == 12); 1174 1175 constexpr bool arrayMemcpy() { 1176 char src[] = "abc"; 1177 char dst[4] = {}; 1178 __builtin_memcpy(dst, src, 4); 1179 return dst[0] == 'a' && dst[1] == 'b' && dst[2] == 'c' && dst[3] == '\0'; 1180 } 1181 static_assert(arrayMemcpy()); 1182 1183 extern struct Incomplete incomplete; 1184 constexpr struct Incomplete *null_incomplete = 0; 1185 static_assert(__builtin_memcpy(null_incomplete, null_incomplete, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \ 1186 // both-note {{source of 'memcpy' is nullptr}} 1187 1188 wchar_t global; 1189 constexpr wchar_t *null = 0; 1190 static_assert(__builtin_memcpy(&global, null, sizeof(wchar_t))); // both-error {{not an integral constant expression}} \ 1191 // both-note {{source of 'memcpy' is nullptr}} 1192 1193 constexpr int simpleMove() { 1194 int a = 12; 1195 int b = 0; 1196 __builtin_memmove(&b, &a, sizeof(a)); 1197 return b; 1198 } 1199 static_assert(simpleMove() == 12); 1200 1201 constexpr int memcpyTypeRem() { // both-error {{never produces a constant expression}} 1202 int a = 12; 1203 int b = 0; 1204 __builtin_memmove(&b, &a, 1); // both-note {{'memmove' not supported: size to copy (1) is not a multiple of size of element type 'int'}} \ 1205 // both-note {{not supported}} 1206 return b; 1207 } 1208 static_assert(memcpyTypeRem() == 12); // both-error {{not an integral constant expression}} \ 1209 // both-note {{in call to}} 1210 1211 template<typename T> 1212 constexpr T result(T (&arr)[4]) { 1213 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3]; 1214 } 1215 1216 constexpr int test_memcpy(int a, int b, int n) { 1217 int arr[4] = {1, 2, 3, 4}; 1218 __builtin_memcpy(arr + a, arr + b, n); // both-note {{overlapping memory regions}} 1219 return result(arr); 1220 } 1221 1222 static_assert(test_memcpy(1, 2, sizeof(int)) == 1334); 1223 static_assert(test_memcpy(0, 1, sizeof(int) * 2) == 2334); // both-error {{not an integral constant expression}} \ 1224 // both-note {{in call}} 1225 1226 /// Both memcpy and memmove must support pointers. 1227 constexpr bool moveptr() { 1228 int a = 0; 1229 void *x = &a; 1230 void *z = nullptr; 1231 1232 __builtin_memmove(&z, &x, sizeof(void*)); 1233 return z == x; 1234 } 1235 static_assert(moveptr()); 1236 1237 constexpr bool cpyptr() { 1238 int a = 0; 1239 void *x = &a; 1240 void *z = nullptr; 1241 1242 __builtin_memcpy(&z, &x, sizeof(void*)); 1243 return z == x; 1244 } 1245 static_assert(cpyptr()); 1246 1247 #ifndef __AVR__ 1248 constexpr int test_memmove(int a, int b, int n) { 1249 int arr[4] = {1, 2, 3, 4}; 1250 __builtin_memmove(arr + a, arr + b, n); // both-note {{destination is not a contiguous array of at least 3 elements of type 'int'}} 1251 return result(arr); 1252 } 1253 static_assert(test_memmove(2, 0, 12) == 4234); // both-error {{constant}} \ 1254 // both-note {{in call}} 1255 #endif 1256 1257 struct Trivial { char k; short s; constexpr bool ok() { return k == 3 && s == 4; } }; 1258 constexpr bool test_trivial() { 1259 Trivial arr[3] = {{1, 2}, {3, 4}, {5, 6}}; 1260 __builtin_memcpy(arr, arr+1, sizeof(Trivial)); 1261 __builtin_memmove(arr+1, arr, 2 * sizeof(Trivial)); 1262 1263 return arr[0].ok() && arr[1].ok() && arr[2].ok(); 1264 } 1265 static_assert(test_trivial()); 1266 1267 // Check that an incomplete array is rejected. 1268 constexpr int test_incomplete_array_type() { // both-error {{never produces a constant}} 1269 extern int arr[]; 1270 __builtin_memmove(arr, arr, 4 * sizeof(arr[0])); 1271 // both-note@-1 2{{'memmove' not supported: source is not a contiguous array of at least 4 elements of type 'int'}} 1272 return arr[0] * 1000 + arr[1] * 100 + arr[2] * 10 + arr[3]; 1273 } 1274 static_assert(test_incomplete_array_type() == 1234); // both-error {{constant}} both-note {{in call}} 1275 } 1276 1277 namespace Memcmp { 1278 constexpr unsigned char ku00fe00[] = {0x00, 0xfe, 0x00}; 1279 constexpr unsigned char ku00feff[] = {0x00, 0xfe, 0xff}; 1280 constexpr signed char ks00fe00[] = {0, -2, 0}; 1281 constexpr signed char ks00feff[] = {0, -2, -1}; 1282 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 2) == 0); 1283 static_assert(__builtin_memcmp(ku00feff, ks00fe00, 99) == 1); 1284 static_assert(__builtin_memcmp(ku00fe00, ks00feff, 99) == -1); 1285 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 2) == 0); 1286 static_assert(__builtin_memcmp(ks00feff, ku00fe00, 99) == 1); 1287 static_assert(__builtin_memcmp(ks00fe00, ku00feff, 99) == -1); 1288 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 2) == 0); 1289 static_assert(__builtin_memcmp(ks00feff, ks00fe00, 99) == 1); 1290 static_assert(__builtin_memcmp(ks00fe00, ks00feff, 99) == -1); 1291 1292 struct Bool3Tuple { bool bb[3]; }; 1293 constexpr Bool3Tuple kb000100 = {{false, true, false}}; 1294 static_assert(sizeof(bool) != 1u || __builtin_memcmp(ks00fe00, kb000100.bb, 1) == 0); // both-error {{constant}} \ 1295 // both-note {{not supported}} 1296 1297 constexpr char a = 'a'; 1298 constexpr char b = 'a'; 1299 static_assert(__builtin_memcmp(&a, &b, 1) == 0); 1300 1301 extern struct Incomplete incomplete; 1302 static_assert(__builtin_memcmp(&incomplete, "", 0u) == 0); 1303 static_assert(__builtin_memcmp("", &incomplete, 0u) == 0); 1304 static_assert(__builtin_memcmp(&incomplete, "", 1u) == 42); // both-error {{not an integral constant}} \ 1305 // both-note {{not supported}} 1306 static_assert(__builtin_memcmp("", &incomplete, 1u) == 42); // both-error {{not an integral constant}} \ 1307 // both-note {{not supported}} 1308 1309 static_assert(__builtin_memcmp(u8"abab\0banana", u8"abab\0banana", 100) == 0); // both-error {{not an integral constant}} \ 1310 // both-note {{dereferenced one-past-the-end}} 1311 1312 static_assert(__builtin_bcmp("abaa", "abba", 3) != 0); 1313 static_assert(__builtin_bcmp("abaa", "abba", 2) == 0); 1314 static_assert(__builtin_bcmp("a\203", "a", 2) != 0); 1315 static_assert(__builtin_bcmp("a\203", "a\003", 2) != 0); 1316 static_assert(__builtin_bcmp(0, 0, 0) == 0); 1317 static_assert(__builtin_bcmp("abab\0banana", "abab\0banana", 100) == 0); // both-error {{not an integral constant}}\ 1318 // both-note {{dereferenced one-past-the-end}} 1319 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 100) != 0); // FIXME: Should we reject this? 1320 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 7) != 0); 1321 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 6) != 0); 1322 static_assert(__builtin_bcmp("abab\0banana", "abab\0canada", 5) == 0); 1323 1324 1325 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 3) == -1); 1326 static_assert(__builtin_wmemcmp(L"abaa", L"abba", 2) == 0); 1327 static_assert(__builtin_wmemcmp(0, 0, 0) == 0); 1328 #if __WCHAR_WIDTH__ == 32 1329 static_assert(__builtin_wmemcmp(L"a\x83838383", L"aa", 2) == 1330 (wchar_t)-1U >> 31); 1331 #endif 1332 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0banana", 100) == 0); // both-error {{not an integral constant}} \ 1333 // both-note {{dereferenced one-past-the-end}} 1334 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 100) == -1); // FIXME: Should we reject this? 1335 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 7) == -1); 1336 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 6) == -1); 1337 static_assert(__builtin_wmemcmp(L"abab\0banana", L"abab\0canada", 5) == 0); 1338 } 1339