Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix warnings from -Wfloat-conversion #5805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

stinos
Copy link
Contributor

@stinos stinos commented Mar 26, 2020

Continuing #5773: enable that flag and fix the issues it shows. Last commit for mingw is a bit drastic so could be left out; because what it means is that before this change, things like isnan would forward to a function with signature isnan(float) even if the argument is a double. Seems very wrong, so maybe there's a better fix.

stinos added 3 commits March 26, 2020 17:00
Either make the conversion explicit or use correct types, and enable
the compiler to emit warnings for it.

Warnings are not enabled for ports using libm (nrf, qemu-arm, samd)
becasue that just has too much warnings.  Also not enabled for
esp32 because external code like esp-idf/components/driver/mcpwm.c
has warnings, and not for esp8266 because it's compiler does not
support -Wfloat-conversion.
@@ -375,7 +375,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
if (val_type == FLOAT32 || val_type == FLOAT64) {
mp_float_t v = mp_obj_get_float(val);
if (val_type == FLOAT32) {
((float *)p)[index] = v;
((float *)p)[index] = (float) v;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does uncrustify still not remove the space in the cast here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does AFAIK but this commit is from before changing he configuration. Also everything still needs rebasing.

@@ -13,7 +13,7 @@ INC += -I$(TOP)
INC += -I$(BUILD)

CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion
CFLAGS = $(INC) -Wall -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)
CFLAGS = $(INC) -Wall -Wfloat-conversion -Werror -std=c99 -nostdlib $(CFLAGS_CORTEX_M4) $(COPT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port doesn't enable floats, so not sure this makes sense to add.

@@ -20,7 +20,7 @@ include ../../py/mkenv.mk
CROSS_COMPILE ?= arm-none-eabi-

CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -march=armv7e-m -mabi=aapcs -mcpu=cortex-m4 -msoft-float -mfloat-abi=soft -fsingle-precision-constant -Wdouble-promotion
CFLAGS = -Wall -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os
CFLAGS = -Wall -Wfloat-conversion -Wpointer-arith -Werror -std=gnu99 -nostdlib $(CFLAGS_CORTEX_M4) -Os
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port doesn't enable floats.

@@ -19,7 +19,7 @@ INC += -I$(XC16)/include
INC += -I$(XC16)/support/$(PARTFAMILY)/h

CFLAGS_PIC16BIT = -mcpu=$(PART) -mlarge-code
CFLAGS = $(INC) -Wall -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT)
CFLAGS = $(INC) -Wall -Wfloat-conversion -Werror -std=gnu99 -nostdlib $(CFLAGS_PIC16BIT) $(COPT)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This port doesn't enable floats, and not sure the PIC compiler would support this flag (?).

#define isnan __isnan
#define isinf(x) (__fpclassify(x) == FP_INFINITE)
#define fpclassify __fpclassify
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this. As you say there must be a better way to fix it.... does anyone else have problems with math.h under mingw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry about this too much, I'm thinking it has to be a build option or so and will figure that out later.

@@ -100,15 +102,15 @@ static inline int fp_isless1(float x) {

static const FPTYPE g_pos_pow[] = {
#if FPDECEXP > 32
1e256, 1e128, 1e64,
MICROPY_FLOAT_CONST(1e256), MICROPY_FLOAT_CONST(1e128), MICROPY_FLOAT_CONST(1e64),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can only be double because they are such large numbers, but I guess for consistency it makes sense to have these macro wrappers.

@dpgeorge
Copy link
Member

For the casts used with mp_obj_get_float() and m_obj_new_float(): instead of casting it may be cleaner (?) to use new functions/macros along the following lines:

float mp_obj_get_float_to_f(mp_obj_t o);
double mp_obj_get_float_to_d(mp_obj_t o);
mp_obj_t mp_obj_new_float_from_f(float f);
mp_obj_t mp_obj_new_float_from_d(double d);

Such functions are actually part of the dynamic native modules ABI, because a native .mpy does not know if the target that it will run on uses single or double precision floats, and using the above functions as the ABI allows a single .mpy to run on both types of systems.

Using such functions (even if they are just macros with a cast) would make the intent of the code clearer, IMO. That would also impact the changes made by #5773.

@stinos
Copy link
Contributor Author

stinos commented Mar 30, 2020

instead of casting it may be cleaner (?) to use new functions/macros along the following lines:

When I was halfway that already crossed my mind :)
Would be cleaner, easier to spot and so on. Only advantages I think.

I'm a bit busy now but I'll make a new PR with that included, mingw stuff stripped and we'll see how that looks.

@dpgeorge
Copy link
Member

When I was halfway that already crossed my mind :)
Would be cleaner, easier to spot and so on. Only advantages I think.

+1

I'm a bit busy now but I'll make a new PR with that included, mingw stuff stripped and we'll see how that looks.

Feel free to revert that earlier commit as part of the set of commits in this PR, if it makes sense (I don't mind either way).

@stinos
Copy link
Contributor Author

stinos commented Mar 31, 2020

I'm using clang-9 and gcc 7.5.0 on WSL and gcc 9.3.0 from mingw-w64 on Windows for testing.
This:

#include <math.h>
int main(int argc, char** argv) {
  double x = 35.0;
  int i = fpclassify(x);
  return i;
}

Compiling this with clang -Wfloat-conversion is ok. However compiling with gcc -Wfloat-conversion on ming-w64 or with gcc -Wfloat-conversion -Os (the -Os is crucial) on WSL yields

./floattest.c:5:22: warning: conversion to ‘float’ from ‘double’ may alter its value [-Wfloat-conversion]
   int i = fpclassify(x);
                      ^

Which means that fpclassify (and isnan/isinf/...) effectively take a float as argument for those cases. Which as far as I can tell is not standard-compliant. We can cast that away, and I've seen other codebases do that, but is the result of fpclassify(somedouble) always the same as fpclassify((float)somedouble)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants