tests: kernel: fpu_sharing: fix double-promotions

Double promotion warnings are generated with the flag -Wdouble-promotion

Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
This commit is contained in:
Ryan McClelland 2023-07-19 11:33:40 -07:00 committed by Carles Cufí
commit 9ce16f8465

View file

@ -36,15 +36,19 @@
#include "float_context.h" #include "float_context.h"
#include "test_common.h" #include "test_common.h"
#ifdef CONFIG_CPU_HAS_FPU_DOUBLE_PRECISION
#define FP_TYPE double
#define FP_CONSTANT(x) x
#else
#define FP_TYPE float
#define FP_CONSTANT(x) x##f
#endif
/* /*
* PI_NUM_ITERATIONS: This macro is defined in the project's Makefile and * PI_NUM_ITERATIONS: This macro is defined in the project's Makefile and
* is configurable from the command line. * is configurable from the command line.
*/ */
#ifdef CONFIG_CPU_HAS_FPU_DOUBLE_PRECISION static FP_TYPE reference_pi = FP_CONSTANT(0.0);
static double reference_pi = 0.0f;
#else
static float reference_pi = 0.0f;
#endif
/* /*
* Test counters are "volatile" because GCC wasn't properly updating * Test counters are "volatile" because GCC wasn't properly updating
@ -68,37 +72,31 @@ static K_SEM_DEFINE(test_exit_sem, 0, 1);
*/ */
static void calculate_pi_low(void) static void calculate_pi_low(void)
{ {
#ifdef CONFIG_CPU_HAS_FPU_DOUBLE_PRECISION volatile FP_TYPE pi; /* volatile to avoid optimizing out of loop */
volatile double pi; /* volatile to avoid optimizing out of loop */ FP_TYPE divisor = FP_CONSTANT(3.0);
double divisor = 3.0f; FP_TYPE sign = FP_CONSTANT(-1.0);
double sign = -1.0f;
#else
volatile float pi; /* volatile to avoid optimizing out of loop */
float divisor = 3.0f;
float sign = -1.0f;
#endif
unsigned int ix; unsigned int ix;
/* Loop until the test finishes, or an error is detected. */ /* Loop until the test finishes, or an error is detected. */
for (calc_pi_low_count = 0; !test_exited; calc_pi_low_count++) { for (calc_pi_low_count = 0; !test_exited; calc_pi_low_count++) {
sign = -1.0f; sign = FP_CONSTANT(-1.0);
pi = 1.0f; pi = FP_CONSTANT(1.0);
divisor = 3.0f; divisor = FP_CONSTANT(3.0);
for (ix = 0; ix < PI_NUM_ITERATIONS; ix++) { for (ix = 0; ix < PI_NUM_ITERATIONS; ix++) {
pi += sign / divisor; pi += sign / divisor;
divisor += 2.0f; divisor += FP_CONSTANT(2.0);
sign *= -1.0f; sign *= FP_CONSTANT(-1.0);
} }
pi *= 4.0f; pi *= FP_CONSTANT(4.0);
if (reference_pi == 0.0f) { if (reference_pi == FP_CONSTANT(0.0)) {
reference_pi = pi; reference_pi = pi;
} else if (reference_pi != pi) { } else if (reference_pi != pi) {
printf("Computed pi %1.6f, reference pi %1.6f\n", printf("Computed pi %1.6f, reference pi %1.6f\n",
pi, reference_pi); (double)pi, (double)reference_pi);
} }
zassert_equal(reference_pi, pi, zassert_equal(reference_pi, pi,
@ -113,15 +111,9 @@ static void calculate_pi_low(void)
*/ */
static void calculate_pi_high(void) static void calculate_pi_high(void)
{ {
#ifdef CONFIG_CPU_HAS_FPU_DOUBLE_PRECISION volatile FP_TYPE pi; /* volatile to avoid optimizing out of loop */
volatile double pi; /* volatile to avoid optimizing out of loop */ FP_TYPE divisor = FP_CONSTANT(3.0);
double divisor = 3.0f; FP_TYPE sign = FP_CONSTANT(-1.0);
double sign = -1.0f;
#else
volatile float pi; /* volatile to avoid optimizing out of loop */
float divisor = 3.0f;
float sign = -1.0f;
#endif
unsigned int ix; unsigned int ix;
/* Run the test until the specified maximum test count is reached */ /* Run the test until the specified maximum test count is reached */
@ -129,14 +121,14 @@ static void calculate_pi_high(void)
calc_pi_high_count <= MAX_TESTS; calc_pi_high_count <= MAX_TESTS;
calc_pi_high_count++) { calc_pi_high_count++) {
sign = -1.0f; sign = FP_CONSTANT(-1.0);
pi = 1.0f; pi = FP_CONSTANT(1.0);
divisor = 3.0f; divisor = FP_CONSTANT(3.0);
for (ix = 0; ix < PI_NUM_ITERATIONS; ix++) { for (ix = 0; ix < PI_NUM_ITERATIONS; ix++) {
pi += sign / divisor; pi += sign / divisor;
divisor += 2.0f; divisor += FP_CONSTANT(2.0);
sign *= -1.0f; sign *= FP_CONSTANT(-1.0);
} }
/* /*
@ -151,13 +143,13 @@ static void calculate_pi_high(void)
*/ */
k_sleep(K_MSEC(10)); k_sleep(K_MSEC(10));
pi *= 4.0f; pi *= FP_CONSTANT(4.0);
if (reference_pi == 0.0f) { if (reference_pi == FP_CONSTANT(0.0)) {
reference_pi = pi; reference_pi = pi;
} else if (reference_pi != pi) { } else if (reference_pi != pi) {
printf("Computed pi %1.6f, reference pi %1.6f\n", printf("Computed pi %1.6f, reference pi %1.6f\n",
pi, reference_pi); (double)pi, (double)reference_pi);
} }
zassert_equal(reference_pi, pi, zassert_equal(reference_pi, pi,
@ -167,7 +159,7 @@ static void calculate_pi_high(void)
if ((calc_pi_high_count % 100) == 50) { if ((calc_pi_high_count % 100) == 50) {
printf("Pi calculation OK after %u (high) +" printf("Pi calculation OK after %u (high) +"
" %u (low) tests (computed %1.6lf)\n", " %u (low) tests (computed %1.6lf)\n",
calc_pi_high_count, calc_pi_low_count, pi); calc_pi_high_count, calc_pi_low_count, (double)pi);
} }
} }