int printf(const char *, ...);
void p(char *str, int x) {
printf("%s: %d\n", str, x);
}
void q(char *str, double x) {
printf("%s: %f\n", str, x);
}
void r(char *str, void *ptr) {
printf("%s: %p\n", str, ptr);
}
int random(void);
int finite(double);
int main(void) {
int N = random();
#define P(n,args) p(#n #args, __builtin_##n args)
#define Q(n,args) q(#n #args, __builtin_##n args)
#define R(n,args) r(#n #args, __builtin_##n args)
#define V(n,args) p(#n #args, (__builtin_##n args, 0))
P(types_compatible_p, (int, float));
P(choose_expr, (0, 10, 20));
P(constant_p, (sizeof(10)));
P(expect, (N == 12, 0));
V(prefetch, (&N));
V(prefetch, (&N, 1));
V(prefetch, (&N, 1, 0));
Q(huge_val, ());
Q(huge_valf, ());
Q(huge_vall, ());
Q(inf, ());
Q(inff, ());
Q(infl, ());
P(fpclassify, (0, 1, 2, 3, 4, 1.0));
P(fpclassify, (0, 1, 2, 3, 4, 1.0f));
P(fpclassify, (0, 1, 2, 3, 4, 1.0l));
Q(nan, (""));
Q(nanf, (""));
Q(nanl, (""));
Q(nans, (""));
Q(nan, ("10"));
Q(nanf, ("10"));
Q(nanl, ("10"));
Q(nans, ("10"));
P(isgreater, (1., 2.));
P(isgreaterequal, (1., 2.));
P(isless, (1., 2.));
P(islessequal, (1., 2.));
P(islessgreater, (1., 2.));
P(isunordered, (1., 2.));
P(isinf, (1.));
P(isinf_sign, (1.));
P(isnan, (1.));
P(abs, (N));
P(clz, (N));
P(clzl, (N));
P(clzll, (N));
P(ctz, (N));
P(ctzl, (N));
P(ctzll, (N));
P(ffs, (N));
P(ffsl, (N));
P(ffsll, (N));
P(parity, (N));
P(parityl, (N));
P(parityll, (N));
P(popcount, (N));
P(popcountl, (N));
P(popcountll, (N));
Q(powi, (1.2f, N));
Q(powif, (1.2f, N));
Q(powil, (1.2f, N));
int a, b, n = random(); char s0[10], s1[] = "Hello";
V(strcat, (s0, s1));
V(strcmp, (s0, s1));
V(strdup, (s0));
V(strncat, (s0, s1, n));
V(strndup, (s0, n));
V(strchr, (s0, s1[0]));
V(strrchr, (s0, s1[0]));
V(strcpy, (s0, s1));
V(strncpy, (s0, s1, n));
V(sprintf, (s0, "%s", s1));
V(snprintf, (s0, n, "%s", s1));
V(__memset_chk, (s0, 0, sizeof s0, n));
V(__memcpy_chk, (s0, s1, sizeof s0, n));
V(__memmove_chk, (s0, s1, sizeof s0, n));
V(__mempcpy_chk, (s0, s1, sizeof s0, n));
V(__strncpy_chk, (s0, s1, sizeof s0, n));
V(__strcpy_chk, (s0, s1, n));
s0[0] = 0;
V(__strcat_chk, (s0, s1, n));
P(object_size, (s0, 0));
P(object_size, (s0, 1));
P(object_size, (s0, 2));
P(object_size, (s0, 3));
P(bswap16, (N));
P(bswap32, (N));
P(bswap64, (N));
P(bitreverse8, (N));
P(bitreverse16, (N));
P(bitreverse32, (N));
P(bitreverse64, (N));
V(trap, ());
R(extract_return_addr, (&N));
P(signbit, (1.0));
R(launder, (&N));
return 0;
}
void foo(void) {
__builtin_strcat(0, 0);
}
void bar(void) {
float f;
double d;
long double ld;
f = __builtin_huge_valf(); d = __builtin_huge_val(); ld = __builtin_huge_vall(); f = __builtin_nanf(""); d = __builtin_nan(""); ld = __builtin_nanl(""); f = __builtin_nanf("0xAE98"); d = __builtin_nan("0xAE98"); ld = __builtin_nanl("0xAE98"); f = __builtin_nansf(""); d = __builtin_nans(""); ld = __builtin_nansl(""); f = __builtin_nansf("0xAE98"); d = __builtin_nans("0xAE98"); ld = __builtin_nansl("0xAE98");
}
void test_conditional_bzero(void) {
char dst[20];
int _sz = 20, len = 20;
return (_sz
? ((_sz >= len)
? __builtin_bzero(dst, len)
: foo())
: __builtin_bzero(dst, len));
}
void test_float_builtins(__fp16 *H, float F, double D, long double LD) {
volatile int res;
res = __builtin_isinf(*H);
res = __builtin_isinf(F);
res = __builtin_isinf(D);
res = __builtin_isinf(LD);
res = __builtin_isinf_sign(*H);
res = __builtin_isinf_sign(F);
res = __builtin_isinf_sign(D);
res = __builtin_isinf_sign(LD);
res = __builtin_isfinite(*H);
res = __builtin_isfinite(F);
res = finite(D);
res = __builtin_isnormal(*H);
res = __builtin_isnormal(F);
res = __builtin_flt_rounds();
}
void test_float_builtin_ops(float F, double D, long double LD) {
volatile float resf;
volatile double resd;
volatile long double resld;
volatile long int resli;
volatile long long int reslli;
resf = __builtin_fmodf(F,F);
resd = __builtin_fmod(D,D);
resld = __builtin_fmodl(LD,LD);
resf = __builtin_fabsf(F);
resd = __builtin_fabs(D);
resld = __builtin_fabsl(LD);
resf = __builtin_canonicalizef(F);
resd = __builtin_canonicalize(D);
resld = __builtin_canonicalizel(LD);
resf = __builtin_fminf(F, F);
resd = __builtin_fmin(D, D);
resld = __builtin_fminl(LD, LD);
resf = __builtin_fmaxf(F, F);
resd = __builtin_fmax(D, D);
resld = __builtin_fmaxl(LD, LD);
resf = __builtin_fabsf(F);
resd = __builtin_fabs(D);
resld = __builtin_fabsl(LD);
resf = __builtin_copysignf(F, F);
resd = __builtin_copysign(D, D);
resld = __builtin_copysignl(LD, LD);
resf = __builtin_ceilf(F);
resd = __builtin_ceil(D);
resld = __builtin_ceill(LD);
resf = __builtin_floorf(F);
resd = __builtin_floor(D);
resld = __builtin_floorl(LD);
resf = __builtin_sqrtf(F);
resd = __builtin_sqrt(D);
resld = __builtin_sqrtl(LD);
resf = __builtin_truncf(F);
resd = __builtin_trunc(D);
resld = __builtin_truncl(LD);
resf = __builtin_rintf(F);
resd = __builtin_rint(D);
resld = __builtin_rintl(LD);
resf = __builtin_nearbyintf(F);
resd = __builtin_nearbyint(D);
resld = __builtin_nearbyintl(LD);
resf = __builtin_roundf(F);
resd = __builtin_round(D);
resld = __builtin_roundl(LD);
resli = __builtin_lroundf (F);
resli = __builtin_lround (D);
resli = __builtin_lroundl (LD);
resli = __builtin_lrintf (F);
resli = __builtin_lrint (D);
resli = __builtin_lrintl (LD);
}
#ifdef __x86_64__
void test_builtin_longjmp(void **buffer) {
__builtin_longjmp(buffer, 1);
}
#endif
void test_memory_builtins(int n) {
void * p = __builtin_malloc(n);
__builtin_free(p);
p = __builtin_calloc(1, n);
p = __builtin_realloc(p, n);
__builtin_free(p);
}
long long test_builtin_readcyclecounter(void) {
return __builtin_readcyclecounter();
}
void test_builtin_launder(int *p) {
int *d = __builtin_launder(p);
}
void test___warn_memset_zero_len(void) {
__warn_memset_zero_len();
}
#ifdef __x86_64__
void test_builtin_os_log(void *buf, int i, const char *data) {
volatile int len;
len = __builtin_os_log_format_buffer_size("%d %{public}s %{private}.16P", i, data, data);
__builtin_os_log_format(buf, "%d %{public}s %{private}.16P", i, data, data);
__builtin_os_log_format(buf, "%{xyz public}s", data);
__builtin_os_log_format(buf, "%{ public xyz}s", data);
__builtin_os_log_format(buf, "%{ public1}s", data);
__builtin_os_log_format(buf, "%{ xyz, public }s", "abc");
__builtin_os_log_format(buf, "%{ xyz, private }s", "abc");
__builtin_os_log_format(buf, "%{ xyz, sensitive }s", "abc");
__builtin_os_log_format(buf, "%{ private, public, private, public}s", "abc");
__builtin_os_log_format(buf, "%{ private, sensitive, private, public}s",
"abc");
len = __builtin_os_log_format_buffer_size("%{mask.xyz}s", "abc");
__builtin_os_log_format(buf, "%{mask.xyz, public}s", "abc");
__builtin_os_log_format(buf, "%{ mask.xyz, private }d", 11);
__builtin_os_log_format(buf, "%{ mask. xyz }s", "abc");
__builtin_os_log_format(buf, "%{ mask.xy z }s", "abc");
}
typedef int wchar_t;
void test_builtin_os_log_wide(void *buf, const char *data, wchar_t *str) {
volatile int len;
len = __builtin_os_log_format_buffer_size("%S", str);
__builtin_os_log_format(buf, "%S", str);
}
void test_builtin_os_log_precision_width(void *buf, const char *data,
int precision, int width) {
volatile int len;
len = __builtin_os_log_format_buffer_size("Hello %*.*s World", precision, width, data);
__builtin_os_log_format(buf, "Hello %*.*s World", precision, width, data);
}
void test_builtin_os_log_invalid(void *buf, int data) {
volatile int len;
len = __builtin_os_log_format_buffer_size("invalid specifier %: %d even a trailing one%", data);
__builtin_os_log_format(buf, "invalid specifier %: %d even a trailing one%", data);
}
void test_builtin_os_log_percent(void *buf, const char *data1, const char *data2) {
volatile int len;
len = __builtin_os_log_format_buffer_size("%s %% %s", data1, data2);
__builtin_os_log_format(buf, "%s %% %s", data1, data2);
}
void test_builtin_os_log_merge_helper0(void *buf, int i, double d) {
__builtin_os_log_format(buf, "%d %f", i, d);
}
void test_builtin_os_log_merge_helper1(void *buf, unsigned u, long long ll) {
__builtin_os_log_format(buf, "%u %lld", u, ll);
}
void test_builtin_os_log_errno(void) {
char buf[__builtin_os_log_format_buffer_size("%m")];
__builtin_os_log_format(buf, "%m");
}
void test_builtin_os_log_long_double(void *buf, long double ld) {
__builtin_os_log_format(buf, "%Lf", ld);
}
#endif