From d91a039299c5ee75e84889dd69a3ff9c3efc4b01 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 4 Oct 2023 13:35:11 +0200 Subject: [PATCH] totalorder* tests: Refactor. * tests/test-totalorder.c (positive_nan, negative_nan): New functions, extracted from main. (main): Use them when initializing the array. --- ChangeLog | 7 +++++++ tests/test-totalorder.c | 32 ++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index e9de2bc1d0..31e02bbf4a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2023-10-04 Bruno Haible + + totalorder* tests: Refactor. + * tests/test-totalorder.c (positive_nan, negative_nan): New functions, + extracted from main. + (main): Use them when initializing the array. + 2023-10-04 Bruno Haible totalorderl: Optimize. diff --git a/tests/test-totalorder.c b/tests/test-totalorder.c index 1fc1b84568..b4a121c2a2 100644 --- a/tests/test-totalorder.c +++ b/tests/test-totalorder.c @@ -31,25 +31,37 @@ # define TOTALORDER_TYPE double #endif +/* Return a quiet NaN with sign bit == 0. */ +static TOTALORDER_TYPE +positive_nan () +{ + /* 'volatile' works around a GCC bug: + */ + TOTALORDER_TYPE volatile nan = TOTALORDER_NAN (); + return (signbit (nan) ? - nan : nan); +} + +/* Return a quiet NaN with sign bit == 1. */ +static TOTALORDER_TYPE +negative_nan () +{ + /* 'volatile' works around a GCC bug: + */ + TOTALORDER_TYPE volatile nan = TOTALORDER_NAN (); + return (signbit (nan) ? nan : - nan); +} + int main () { TOTALORDER_TYPE x[] = { - -TOTALORDER_NAN (), -TOTALORDER_INF (), -1e37, -1, -1e-5, + negative_nan (), -TOTALORDER_INF (), -1e37, -1, -1e-5, TOTALORDER_MINUS_ZERO, 0, - 1e-5, 1, 1e37, TOTALORDER_INF (), TOTALORDER_NAN () + 1e-5, 1, 1e37, TOTALORDER_INF (), positive_nan () }; int n = sizeof x / sizeof *x; - /* TOTALORDER_NAN () yields a NaN of unknown sign, so fix the - first NaN to be negative and the last NaN to be nonnegative. - Do this via volatile accesses, to work around GCC bug 111655. */ - TOTALORDER_TYPE volatile a = x[0], z = x[n - 1]; - if (! signbit (a)) a = -a; - if ( signbit (z)) z = -z; - x[0] = a, x[n - 1] = z; - for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) ASSERT (!!TOTALORDER (&x[i], &x[j]) == (i <= j)); -- 2.39.5