tree-wide: Use our own macros instead of fabs()/fmax()/fmin()
To make this work, ABS() is made generic so it also works on floats and doubles. While at it, fold the __ABS_INTEGER indirection and the assert_cc(sizeof(long long) == sizeof(intmax_t)) away. The previous form switched between __builtin_llabs (clang) and __builtin_imaxabs (gcc), with the assert keeping the two paths behaviorally identical on every platform we build for. imaxabs was originally chosen because intmax_t is conceptually the widest signed integer type the platform exposes, but the _Generic ABS already casts to (long long) before the call, so the extra width imaxabs could in theory carry was being narrowed away immediately anyway. With both paths collapsed to __builtin_llabs((long long) (a)), the size relationship between long long and intmax_t is no longer relevant. Also add explicit unsigned long long / unsigned long / unsigned int cases that pass the argument through unchanged. The previous default branch cast unsigned values to (long long); for values above LLONG_MAX this reinterprets them as negative, and __builtin_llabs(LLONG_MIN) is UB. Unsigned values are already non-negative, so passing them through is both correct and avoids the narrowing. Smaller unsigned types (unsigned char, unsigned short) still go through the default branch but promote to int first and fit in long long losslessly. Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <linux/netfilter/nf_tables.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include "capability-util.h"
|
||||
@@ -660,7 +659,7 @@ TEST(safe_atod) {
|
||||
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);
|
||||
@@ -672,7 +671,7 @@ TEST(safe_atod) {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user