mirror of
https://github.com/systemd/systemd.git
synced 2025-03-28 02:50:16 +03:00
Merge pull request #24051 from yuwata/json-fpclassify
json: use fpclassify() macro and refuse -ffinite-math-only
This commit is contained in:
commit
a5d0919c9b
@ -390,6 +390,11 @@ possible_common_cc_flags = [
|
||||
|
||||
c_args = get_option('c_args')
|
||||
|
||||
# Our json library does not support -ffinite-math-only, which is enabled by -Ofast or -ffast-math.
|
||||
if (('-Ofast' in c_args or '-ffast-math' in c_args or '-ffinite-math-only' in c_args) and not '-fno-finite-math-only' in c_args)
|
||||
error('-Ofast, -ffast-math, or -ffinite-math-only is specified in c_args.')
|
||||
endif
|
||||
|
||||
# Disable -Wmaybe-uninitialized when compiling with -Os/-O1/-O3/etc. There are
|
||||
# too many false positives with gcc >= 8. Effectively, we only test with -O0
|
||||
# and -O2; this should be enough to catch most important cases without too much
|
||||
|
@ -87,10 +87,6 @@
|
||||
_Pragma("GCC diagnostic push")
|
||||
#endif
|
||||
|
||||
#define DISABLE_WARNING_FLOAT_EQUAL \
|
||||
_Pragma("GCC diagnostic push"); \
|
||||
_Pragma("GCC diagnostic ignored \"-Wfloat-equal\"")
|
||||
|
||||
#define DISABLE_WARNING_TYPE_LIMITS \
|
||||
_Pragma("GCC diagnostic push"); \
|
||||
_Pragma("GCC diagnostic ignored \"-Wtype-limits\"")
|
||||
|
14
src/basic/math-util.h
Normal file
14
src/basic/math-util.h
Normal file
@ -0,0 +1,14 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
/* On some optimization level, iszero(x) is converted to (x == 0.0), and emits warning -Wfloat-equal.
|
||||
* The argument must be a floating point, i.e. one of float, double, or long double. */
|
||||
#define iszero_safe(x) (fpclassify(x) == FP_ZERO)
|
||||
|
||||
/* To avoid x == y and triggering compile warning -Wfloat-equal. This retuns false if one of the argument is
|
||||
* NaN or infinity. One of the argument must be a floating point. */
|
||||
#define fp_equal(x, y) iszero_safe((x) - (y))
|
@ -129,6 +129,7 @@ basic_sources = files(
|
||||
'login-util.c',
|
||||
'login-util.h',
|
||||
'macro.h',
|
||||
'math-util.h',
|
||||
'memfd-util.c',
|
||||
'memfd-util.h',
|
||||
'memory-util.c',
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
@ -18,6 +17,7 @@
|
||||
#include "json-internal.h"
|
||||
#include "json.h"
|
||||
#include "macro.h"
|
||||
#include "math-util.h"
|
||||
#include "memory-util.h"
|
||||
#include "string-table.h"
|
||||
#include "string-util.h"
|
||||
@ -253,9 +253,7 @@ static JsonVariant *json_variant_formalize(JsonVariant *v) {
|
||||
return json_variant_unsigned(v) == 0 ? JSON_VARIANT_MAGIC_ZERO_UNSIGNED : v;
|
||||
|
||||
case JSON_VARIANT_REAL:
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
return json_variant_real(v) == 0.0 ? JSON_VARIANT_MAGIC_ZERO_REAL : v;
|
||||
REENABLE_WARNING;
|
||||
return iszero_safe(json_variant_real(v)) ? JSON_VARIANT_MAGIC_ZERO_REAL : v;
|
||||
|
||||
case JSON_VARIANT_STRING:
|
||||
return isempty(json_variant_string(v)) ? JSON_VARIANT_MAGIC_EMPTY_STRING : v;
|
||||
@ -352,17 +350,17 @@ int json_variant_new_real(JsonVariant **ret, double d) {
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
if (d == 0.0) {
|
||||
*ret = JSON_VARIANT_MAGIC_ZERO_REAL;
|
||||
return 0;
|
||||
}
|
||||
REENABLE_WARNING;
|
||||
|
||||
/* JSON doesn't know NaN, +Infinity or -Infinity. Let's silently convert to 'null'. */
|
||||
if (isnan(d) || isinf(d)) {
|
||||
r = fpclassify(d);
|
||||
switch (r) {
|
||||
case FP_NAN:
|
||||
case FP_INFINITE:
|
||||
/* JSON doesn't know NaN, +Infinity or -Infinity. Let's silently convert to 'null'. */
|
||||
*ret = JSON_VARIANT_MAGIC_NULL;
|
||||
return 0;
|
||||
|
||||
case FP_ZERO:
|
||||
*ret = JSON_VARIANT_MAGIC_ZERO_REAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = json_variant_new(&v, JSON_VARIANT_REAL, sizeof(d));
|
||||
@ -914,10 +912,8 @@ int64_t json_variant_integer(JsonVariant *v) {
|
||||
|
||||
converted = (int64_t) v->value.real;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
if ((double) converted == v->value.real)
|
||||
if (fp_equal((double) converted, v->value.real))
|
||||
return converted;
|
||||
REENABLE_WARNING;
|
||||
|
||||
log_debug("Real %g requested as integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
return 0;
|
||||
@ -961,10 +957,8 @@ uint64_t json_variant_unsigned(JsonVariant *v) {
|
||||
|
||||
converted = (uint64_t) v->value.real;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
if ((double) converted == v->value.real)
|
||||
if (fp_equal((double) converted, v->value.real))
|
||||
return converted;
|
||||
REENABLE_WARNING;
|
||||
|
||||
log_debug("Real %g requested as unsigned integer, and cannot be converted losslessly, returning 0.", v->value.real);
|
||||
return 0;
|
||||
@ -1153,15 +1147,11 @@ bool json_variant_has_type(JsonVariant *v, JsonVariantType type) {
|
||||
if (rt == JSON_VARIANT_UNSIGNED && type == JSON_VARIANT_REAL)
|
||||
return (uint64_t) (double) v->value.unsig == v->value.unsig;
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
|
||||
/* Any real that can be converted losslessly to an integer and back may also be considered an integer */
|
||||
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_INTEGER)
|
||||
return (double) (int64_t) v->value.real == v->value.real;
|
||||
return fp_equal((double) (int64_t) v->value.real, v->value.real);
|
||||
if (rt == JSON_VARIANT_REAL && type == JSON_VARIANT_UNSIGNED)
|
||||
return (double) (uint64_t) v->value.real == v->value.real;
|
||||
|
||||
REENABLE_WARNING;
|
||||
return fp_equal((double) (uint64_t) v->value.real, v->value.real);
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -1314,9 +1304,7 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) {
|
||||
return json_variant_unsigned(a) == json_variant_unsigned(b);
|
||||
|
||||
case JSON_VARIANT_REAL:
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
return json_variant_real(a) == json_variant_real(b);
|
||||
REENABLE_WARNING;
|
||||
return fp_equal(json_variant_real(a), json_variant_real(b));
|
||||
|
||||
case JSON_VARIANT_BOOLEAN:
|
||||
return json_variant_boolean(a) == json_variant_boolean(b);
|
||||
|
@ -209,6 +209,10 @@ tests += [
|
||||
|
||||
[files('test-macro.c')],
|
||||
|
||||
[files('test-math-util.c'),
|
||||
[],
|
||||
[libm]],
|
||||
|
||||
[files('test-mkdir.c')],
|
||||
|
||||
[files('test-json.c'),
|
||||
|
@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "escape.h"
|
||||
@ -9,6 +8,7 @@
|
||||
#include "fileio.h"
|
||||
#include "json-internal.h"
|
||||
#include "json.h"
|
||||
#include "math-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "tests.h"
|
||||
@ -50,8 +50,8 @@ static void test_tokenizer_one(const char *data, ...) {
|
||||
|
||||
d = va_arg(ap, double);
|
||||
|
||||
assert_se(fabsl(d - v.real) < 1e-10 ||
|
||||
fabsl((d - v.real) / v.real) < 1e-10);
|
||||
assert_se(fabs(d - v.real) < 1e-10 ||
|
||||
fabs((d - v.real) / v.real) < 1e-10);
|
||||
|
||||
} else if (t == JSON_TOKEN_INTEGER) {
|
||||
int64_t i;
|
||||
@ -221,7 +221,7 @@ static void test_2(JsonVariant *v) {
|
||||
|
||||
/* has thisisaverylongproperty */
|
||||
p = json_variant_by_key(v, "thisisaverylongproperty");
|
||||
assert_se(p && json_variant_type(p) == JSON_VARIANT_REAL && fabsl(json_variant_real(p) - 1.27) < 0.001);
|
||||
assert_se(p && json_variant_type(p) == JSON_VARIANT_REAL && fabs(json_variant_real(p) - 1.27) < 0.001);
|
||||
}
|
||||
|
||||
static void test_zeroes(JsonVariant *v) {
|
||||
@ -239,9 +239,7 @@ static void test_zeroes(JsonVariant *v) {
|
||||
assert_se(json_variant_integer(w) == 0);
|
||||
assert_se(json_variant_unsigned(w) == 0U);
|
||||
|
||||
DISABLE_WARNING_FLOAT_EQUAL;
|
||||
assert_se(json_variant_real(w) == 0.0L);
|
||||
REENABLE_WARNING;
|
||||
assert_se(iszero_safe(json_variant_real(w)));
|
||||
|
||||
assert_se(json_variant_is_integer(w));
|
||||
assert_se(json_variant_is_unsigned(w));
|
||||
@ -511,14 +509,14 @@ static void test_float_match(JsonVariant *v) {
|
||||
const double delta = 0.0001;
|
||||
|
||||
assert_se(json_variant_is_array(v));
|
||||
assert_se(json_variant_elements(v) == 9);
|
||||
assert_se(fabsl((double) 1.0 - ((double) DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
|
||||
assert_se(fabsl((double) 1.0 - ((double) DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
|
||||
assert_se(json_variant_elements(v) == 11);
|
||||
assert_se(fabs(1.0 - (DBL_MIN / json_variant_real(json_variant_by_index(v, 0)))) <= delta);
|
||||
assert_se(fabs(1.0 - (DBL_MAX / json_variant_real(json_variant_by_index(v, 1)))) <= delta);
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 2))); /* nan is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 3))); /* +inf is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 4))); /* -inf is not supported by json → null */
|
||||
assert_se(json_variant_is_null(json_variant_by_index(v, 5)) ||
|
||||
fabsl((double) 1.0 - ((double) HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
|
||||
fabs(1.0 - (HUGE_VAL / json_variant_real(json_variant_by_index(v, 5)))) <= delta); /* HUGE_VAL might be +inf, but might also be something else */
|
||||
assert_se(json_variant_is_real(json_variant_by_index(v, 6)) &&
|
||||
json_variant_is_integer(json_variant_by_index(v, 6)) &&
|
||||
json_variant_integer(json_variant_by_index(v, 6)) == 0);
|
||||
@ -528,6 +526,12 @@ static void test_float_match(JsonVariant *v) {
|
||||
assert_se(json_variant_is_real(json_variant_by_index(v, 8)) &&
|
||||
json_variant_is_integer(json_variant_by_index(v, 8)) &&
|
||||
json_variant_integer(json_variant_by_index(v, 8)) == -10);
|
||||
assert_se(json_variant_is_real(json_variant_by_index(v, 9)) &&
|
||||
!json_variant_is_integer(json_variant_by_index(v, 9)));
|
||||
assert_se(fabs(1.0 - (DBL_MIN / 2 / json_variant_real(json_variant_by_index(v, 9)))) <= delta);
|
||||
assert_se(json_variant_is_real(json_variant_by_index(v, 10)) &&
|
||||
!json_variant_is_integer(json_variant_by_index(v, 10)));
|
||||
assert_se(fabs(1.0 - (-DBL_MIN / 2 / json_variant_real(json_variant_by_index(v, 10)))) <= delta);
|
||||
}
|
||||
|
||||
TEST(float) {
|
||||
@ -543,7 +547,9 @@ TEST(float) {
|
||||
JSON_BUILD_REAL(HUGE_VAL),
|
||||
JSON_BUILD_REAL(0),
|
||||
JSON_BUILD_REAL(10),
|
||||
JSON_BUILD_REAL(-10))) >= 0);
|
||||
JSON_BUILD_REAL(-10),
|
||||
JSON_BUILD_REAL(DBL_MIN / 2),
|
||||
JSON_BUILD_REAL(-DBL_MIN / 2))) >= 0);
|
||||
|
||||
json_variant_dump(v, JSON_FORMAT_COLOR|JSON_FORMAT_PRETTY, NULL, NULL);
|
||||
|
||||
|
111
src/test/test-math-util.c
Normal file
111
src/test/test-math-util.c
Normal file
@ -0,0 +1,111 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#include "math-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
TEST(iszero_safe) {
|
||||
/* zeros */
|
||||
assert_se(iszero_safe(0.0));
|
||||
assert_se(iszero_safe(-0.0));
|
||||
assert_se(iszero_safe(0e0));
|
||||
assert_se(iszero_safe(-0e0));
|
||||
assert_se(iszero_safe(0e+0));
|
||||
assert_se(iszero_safe(0e-0));
|
||||
assert_se(iszero_safe(-0e-0));
|
||||
assert_se(iszero_safe(-0e000));
|
||||
assert_se(iszero_safe(0e000));
|
||||
|
||||
/* non-zero normal values */
|
||||
assert_se(!iszero_safe(42.0));
|
||||
assert_se(!iszero_safe(M_PI));
|
||||
assert_se(!iszero_safe(DBL_MAX));
|
||||
assert_se(!iszero_safe(-DBL_MAX));
|
||||
assert_se(!iszero_safe(DBL_MIN));
|
||||
assert_se(!iszero_safe(-DBL_MIN));
|
||||
assert_se(!iszero_safe(1 / DBL_MAX));
|
||||
|
||||
/* subnormal values */
|
||||
assert_se(!iszero_safe(DBL_MIN / 2));
|
||||
assert_se(!iszero_safe(-DBL_MIN / 42));
|
||||
assert_se(!iszero_safe(1 / DBL_MAX / 2));
|
||||
|
||||
/* too small values which cannot be in subnormal form */
|
||||
assert_se( iszero_safe(DBL_MIN / DBL_MAX));
|
||||
assert_se( iszero_safe(DBL_MIN / -DBL_MAX));
|
||||
assert_se( iszero_safe(-DBL_MIN / DBL_MAX));
|
||||
assert_se( iszero_safe(-DBL_MIN / -DBL_MAX));
|
||||
|
||||
/* NaN or infinity */
|
||||
assert_se(!iszero_safe(NAN));
|
||||
assert_se(!iszero_safe(INFINITY));
|
||||
assert_se(!iszero_safe(-INFINITY));
|
||||
assert_se(!iszero_safe(1 / NAN));
|
||||
|
||||
/* inverse of infinity */
|
||||
assert_se( iszero_safe(1 / INFINITY));
|
||||
assert_se( iszero_safe(1 / -INFINITY));
|
||||
assert_se( iszero_safe(-1 / INFINITY));
|
||||
assert_se( iszero_safe(-1 / -INFINITY));
|
||||
assert_se( iszero_safe(42 / -INFINITY));
|
||||
assert_se( iszero_safe(-42 / -INFINITY));
|
||||
assert_se( iszero_safe(DBL_MIN / INFINITY));
|
||||
assert_se( iszero_safe(DBL_MIN / -INFINITY));
|
||||
assert_se( iszero_safe(DBL_MAX / INFINITY / 2));
|
||||
assert_se( iszero_safe(DBL_MAX / -INFINITY * DBL_MAX));
|
||||
assert_se(!iszero_safe(DBL_MAX * 2 / INFINITY));
|
||||
|
||||
/* infinity / infinity is NaN */
|
||||
assert_se(!iszero_safe(INFINITY / INFINITY));
|
||||
assert_se(!iszero_safe(INFINITY * 2 / INFINITY));
|
||||
assert_se(!iszero_safe(INFINITY / DBL_MAX / INFINITY));
|
||||
}
|
||||
|
||||
TEST(fp_equal) {
|
||||
/* normal values */
|
||||
assert_se( fp_equal(0.0, -0e0));
|
||||
assert_se( fp_equal(3.0, 3));
|
||||
assert_se(!fp_equal(3.000001, 3));
|
||||
assert_se( fp_equal(M_PI, M_PI));
|
||||
assert_se(!fp_equal(M_PI, -M_PI));
|
||||
assert_se( fp_equal(DBL_MAX, DBL_MAX));
|
||||
assert_se(!fp_equal(DBL_MAX, -DBL_MAX));
|
||||
assert_se(!fp_equal(-DBL_MAX, DBL_MAX));
|
||||
assert_se( fp_equal(-DBL_MAX, -DBL_MAX));
|
||||
assert_se( fp_equal(DBL_MIN, DBL_MIN));
|
||||
assert_se(!fp_equal(DBL_MIN, -DBL_MIN));
|
||||
assert_se(!fp_equal(-DBL_MIN, DBL_MIN));
|
||||
assert_se( fp_equal(-DBL_MIN, -DBL_MIN));
|
||||
|
||||
/* subnormal values */
|
||||
assert_se( fp_equal(DBL_MIN / 10, DBL_MIN / 10));
|
||||
assert_se(!fp_equal(DBL_MIN / 10, -DBL_MIN / 10));
|
||||
assert_se(!fp_equal(-DBL_MIN / 10, DBL_MIN / 10));
|
||||
assert_se( fp_equal(-DBL_MIN / 10, -DBL_MIN / 10));
|
||||
assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN / 15));
|
||||
assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN / 15));
|
||||
|
||||
/* subnormal difference */
|
||||
assert_se(!fp_equal(DBL_MIN / 10, DBL_MIN + DBL_MIN / 10));
|
||||
assert_se( fp_equal(3.0, 3.0 + DBL_MIN / 2)); /* 3.0 + DBL_MIN / 2 is truncated to 3.0 */
|
||||
|
||||
/* too small values */
|
||||
assert_se( fp_equal(DBL_MIN / DBL_MAX, -DBL_MIN / DBL_MAX));
|
||||
|
||||
/* NaN or infinity */
|
||||
assert_se(!fp_equal(NAN, NAN));
|
||||
assert_se(!fp_equal(NAN, 0));
|
||||
assert_se(!fp_equal(NAN, INFINITY));
|
||||
assert_se(!fp_equal(INFINITY, INFINITY));
|
||||
assert_se(!fp_equal(INFINITY, -INFINITY));
|
||||
assert_se(!fp_equal(-INFINITY, INFINITY));
|
||||
assert_se(!fp_equal(-INFINITY, -INFINITY));
|
||||
|
||||
/* inverse of infinity */
|
||||
assert_se( fp_equal(0, 1 / INFINITY));
|
||||
assert_se( fp_equal(42 / INFINITY, 1 / -INFINITY));
|
||||
assert_se(!fp_equal(42 / INFINITY, INFINITY / INFINITY));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_DEBUG);
|
Loading…
x
Reference in New Issue
Block a user