UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
})
-#ifdef __clang__
-# define ABS(a) __builtin_llabs(a)
-#else
-# define ABS(a) __builtin_imaxabs(a)
-#endif
-assert_cc(sizeof(long long) == sizeof(intmax_t));
+#define ABS(a) _Generic((a), \
+ float: __builtin_fabsf((float) (a)), \
+ double: __builtin_fabs((double) (a)), \
+ long double: __builtin_fabsl((long double) (a)), \
+ unsigned long long: (a), \
+ unsigned long: (a), \
+ unsigned int: (a), \
+ default: __builtin_llabs((long long) (a)))
#define IS_UNSIGNED_INTEGER_TYPE(type) \
(__builtin_types_compatible_p(typeof(type), unsigned char) || \
/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#include <math.h>
-
/* We make an exception here to our usual "include system headers first" rule because we need one of these
* macros to disable a warning triggered by the glib headers. */
#include "macro-fundamental.h"
assert_se(r > 0);
assert_se(streq(x, "foo"));
assert_se(u64 == 815ULL);
- assert_se(fabs(dbl - 47.0) < 0.1);
+ assert_se(ABS(dbl - 47.0) < 0.1);
assert_se(streq(y, "/"));
r = sd_bus_message_peek_type(m, NULL, NULL);
d = va_arg(ap, double);
- assert_se(fabs(d - v.real) < 1e-10 ||
- fabs((d - v.real) / v.real) < 1e-10);
+ assert_se(ABS(d - v.real) < 1e-10 ||
+ ABS((d - v.real) / v.real) < 1e-10);
} else if (t == JSON_TOKEN_INTEGER) {
int64_t i;
/* has thisisaverylongproperty */
p = sd_json_variant_by_key(v, "thisisaverylongproperty");
- assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_REAL && fabs(sd_json_variant_real(p) - 1.27) < 0.001);
+ assert_se(p && sd_json_variant_type(p) == SD_JSON_VARIANT_REAL && ABS(sd_json_variant_real(p) - 1.27) < 0.001);
}
static void test_zeroes(sd_json_variant *v) {
assert_se(sd_json_variant_is_array(v));
assert_se(sd_json_variant_elements(v) == 11);
assert_se(!iszero_safe(sd_json_variant_real(sd_json_variant_by_index(v, 0))));
- assert_se(fabs(1.0 - (DBL_MIN / sd_json_variant_real(sd_json_variant_by_index(v, 0)))) <= delta);
+ assert_se(ABS(1.0 - (DBL_MIN / sd_json_variant_real(sd_json_variant_by_index(v, 0)))) <= delta);
assert_se(!iszero_safe(sd_json_variant_real(sd_json_variant_by_index(v, 1))));
- assert_se(fabs(1.0 - (DBL_MAX / sd_json_variant_real(sd_json_variant_by_index(v, 1)))) <= delta);
+ assert_se(ABS(1.0 - (DBL_MAX / sd_json_variant_real(sd_json_variant_by_index(v, 1)))) <= delta);
assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 2))); /* nan is not supported by json → null */
assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 3))); /* +inf is not supported by json → null */
assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 4))); /* -inf is not supported by json → null */
assert_se(sd_json_variant_is_null(sd_json_variant_by_index(v, 5)) ||
- fabs(1.0 - (HUGE_VAL / sd_json_variant_real(sd_json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
+ ABS(1.0 - (HUGE_VAL / sd_json_variant_real(sd_json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 6)) &&
sd_json_variant_is_integer(sd_json_variant_by_index(v, 6)) &&
sd_json_variant_integer(sd_json_variant_by_index(v, 6)) == 0);
assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 9)) &&
!sd_json_variant_is_integer(sd_json_variant_by_index(v, 9)));
assert_se(!iszero_safe(sd_json_variant_real(sd_json_variant_by_index(v, 9))));
- assert_se(fabs(1.0 - (DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 9)))) <= delta);
+ assert_se(ABS(1.0 - (DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 9)))) <= delta);
assert_se(sd_json_variant_is_real(sd_json_variant_by_index(v, 10)) &&
!sd_json_variant_is_integer(sd_json_variant_by_index(v, 10)));
assert_se(!iszero_safe(sd_json_variant_real(sd_json_variant_by_index(v, 10))));
- assert_se(fabs(1.0 - (-DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 10)))) <= delta);
+ assert_se(ABS(1.0 - (-DBL_MIN / 2 / sd_json_variant_real(sd_json_variant_by_index(v, 10)))) <= delta);
}
TEST(float) {
/* flags= */ 0,
&data) >= 0);
- assert_se(fabs(data.x1 - 0.5) < 0.01);
- assert_se(fabs(data.x2 + 0.5) < 0.01);
+ assert_se(ABS(data.x1 - 0.5) < 0.01);
+ assert_se(ABS(data.x2 + 0.5) < 0.01);
assert_se(isinf(data.x3));
assert_se(data.x3 > 0);
assert_se(isinf(data.x4));
assert(g >= 0 && g <= 1);
assert(b >= 0 && b <= 1);
- double max_color = fmax(r, fmax(g, b));
- double min_color = fmin(r, fmin(g, b));
+ double max_color = MAX(r, MAX(g, b));
+ double min_color = MIN(r, MIN(g, b));
double delta = max_color - min_color;
if (ret_v)
#include <linux/netfilter/nf_tables.h>
#include <locale.h>
-#include <math.h>
#include <sys/socket.h>
#include "capability-util.h"
ASSERT_ERROR(safe_atod("junk", &d), EINVAL);
ASSERT_OK_ZERO(safe_atod("0.2244", &d));
- assert_se(fabs(d - 0.2244) < 0.000001);
+ assert_se(ABS(d - 0.2244) < 0.000001);
ASSERT_ERROR(safe_atod("0,5", &d), EINVAL);
ASSERT_ERROR(safe_atod("", &d), EINVAL);
return (void) log_tests_skipped_errno(errno, "locale de_DE.utf8 not found");
ASSERT_OK_ZERO(safe_atod("0.2244", &d));
- assert_se(fabs(d - 0.2244) < 0.000001);
+ assert_se(ABS(d - 0.2244) < 0.000001);
ASSERT_ERROR(safe_atod("0,5", &d), EINVAL);
ASSERT_ERROR(safe_atod("", &d), EINVAL);
i, count[i], dev,
(int) (count[i] / scale), "x");
- assert_se(fabs(dev) < 6); /* 6 sigma is excessive, but this check should be enough to
+ assert_se(ABS(dev) < 6); /* 6 sigma is excessive, but this check should be enough to
* identify catastrophic failure while minimizing false
* positives. */
}
/* For small deltas, tell the kernel to gradually adjust the system clock to the NTP time, larger
* deltas are just directly set. */
- if (fabs(offset) < NTP_MAX_ADJUST) {
+ if (ABS(offset) < NTP_MAX_ADJUST) {
tmx = (struct timex) {
.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR,
.status = STA_PLL,
return false;
/* always accept offset if we are farther off than the round-trip delay */
- if (fabs(offset) > delay)
+ if (ABS(offset) > delay)
return false;
/* we need a few samples before looking at them */
return false;
/* do not accept anything worse than the maximum possible error of the best sample */
- if (fabs(offset) > m->samples[idx_min].delay)
+ if (ABS(offset) > m->samples[idx_min].delay)
return true;
/* compare the difference between the current offset to the previous offset and jitter */
- return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
+ return ABS(offset - m->samples[idx_cur].offset) > 3 * jitter;
}
static void manager_adjust_poll(Manager *m, double offset, bool spike) {
}
/* set to minimal poll interval */
- if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
+ if (!spike && ABS(offset) > NTP_ACCURACY_SEC) {
m->poll_interval_usec = m->poll_interval_min_usec;
return;
}
/* increase polling interval */
- if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
+ if (ABS(offset) < NTP_ACCURACY_SEC * 0.25) {
if (m->poll_interval_usec < m->poll_interval_max_usec)
m->poll_interval_usec *= 2;
return;
}
/* decrease polling interval */
- if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
+ if (spike || ABS(offset) > NTP_ACCURACY_SEC * 0.75) {
if (m->poll_interval_usec > m->poll_interval_min_usec)
m->poll_interval_usec /= 2;
return;