-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Conversation
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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.
For the casts used with 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. |
When I was halfway that already crossed my mind :) 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. |
+1
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). |
I'm using clang-9 and gcc 7.5.0 on WSL and gcc 9.3.0 from mingw-w64 on Windows for testing.
Compiling this with
Which means that fpclassify (and isnan/isinf/...) effectively take a |
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.