public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Warn about conversion from floating point to integers
@ 2022-08-20  9:21 Andrea Bocci
  2022-08-22 17:32 ` Segher Boessenkool
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Bocci @ 2022-08-20  9:21 UTC (permalink / raw)
  To: gcc-help

Hi,

is there a way to ask GCC to produce a warning when there is a conversion
from a floating point type (float or double) to an integer type (char,
short, int, long, etc.), but not when there is a conversion from double to
float ?

The reason is that we often do computations in double precision and store
the result in single precision, so I'd rather not warn about that.

-Wconversion warns about both kinds and more.
-Wfloat-conversion warns about both kinds.

Thank you for any advice,
.Andrea

-- 

Dr. Andrea Bocci
Applied Physicist, CMS Experiment
<https://cms.cern/>EP-CMD, CERN
<https://home.cern/>168990 (+41 75 411 8990)
CERN 40/1-B01 <https://maps.cern.ch/mapsearch/mapsearch.htm?n=['40/1-B01']>
E27810

Esplanade des particules, 1, CH-1211 Geneve 23, Switzerland
<https://goo.gl/maps/AMjHdhXzYk2itSu7A>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-20  9:21 Warn about conversion from floating point to integers Andrea Bocci
@ 2022-08-22 17:32 ` Segher Boessenkool
  2022-08-22 19:27   ` Andrea Bocci
  0 siblings, 1 reply; 7+ messages in thread
From: Segher Boessenkool @ 2022-08-22 17:32 UTC (permalink / raw)
  To: Andrea Bocci; +Cc: gcc-help

Hi!

On Sat, Aug 20, 2022 at 11:21:50AM +0200, Andrea Bocci wrote:
> is there a way to ask GCC to produce a warning when there is a conversion
> from a floating point type (float or double) to an integer type (char,
> short, int, long, etc.), but not when there is a conversion from double to
> float ?
> 
> The reason is that we often do computations in double precision and store
> the result in single precision, so I'd rather not warn about that.
> 
> -Wconversion warns about both kinds and more.
> -Wfloat-conversion warns about both kinds.

There is no separate option.  But, you can just grep the error messages?

fc.c: In function 'f':
fc.c:1:28: warning: conversion from 'double' to 'float' may change value [-Wfloat-conversion]
    1 | float f(double x) { return x; }
      |                            ^
fc.c: In function 'g':
fc.c:2:26: warning: conversion from 'double' to 'int' may change value [-Wfloat-conversion]
    2 | int g(double x) { return x; }
      |                          ^

Not ideal of course, but this should be workable?  (You may want to set
LANG=C to get parsable warning messages).

If you want a separate -W option for this, please file a PR?
See <https://gcc.gnu.org/bugs.html>.  Thanks!


Segher

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-22 17:32 ` Segher Boessenkool
@ 2022-08-22 19:27   ` Andrea Bocci
  2022-08-22 19:43     ` Jonathan Wakely
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Bocci @ 2022-08-22 19:27 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: gcc-help

On Mon, 22 Aug 2022 at 19:33, Segher Boessenkool <segher@kernel.crashing.org>
wrote:

> Hi!
>
> On Sat, Aug 20, 2022 at 11:21:50AM +0200, Andrea Bocci wrote:
> > is there a way to ask GCC to produce a warning when there is a conversion
> > from a floating point type (float or double) to an integer type (char,
> > short, int, long, etc.), but not when there is a conversion from double
> to
> > float ?
> >
> > The reason is that we often do computations in double precision and store
> > the result in single precision, so I'd rather not warn about that.
> >
> > -Wconversion warns about both kinds and more.
> > -Wfloat-conversion warns about both kinds.
>
> There is no separate option.  But, you can just grep the error messages?
>
> fc.c: In function 'f':
> fc.c:1:28: warning: conversion from 'double' to 'float' may change value
> [-Wfloat-conversion]
>     1 | float f(double x) { return x; }
>       |                            ^
> fc.c: In function 'g':
> fc.c:2:26: warning: conversion from 'double' to 'int' may change value
> [-Wfloat-conversion]
>     2 | int g(double x) { return x; }
>       |                          ^
>
> Not ideal of course, but this should be workable?  (You may want to set
> LANG=C to get parsable warning messages).
>
> If you want a separate -W option for this, please file a PR?
> See <https://gcc.gnu.org/bugs.html>.  Thanks!
>


Hi Segher,
thank you for your message !

From a test build of our code base with -Wfloat-conversion we got over 700k
warnings from 65k unique locations, the vast majority being about "warning:
conversion from 'double' to 'float' may change value".
So, while we can run an ad-hoc build with -Wfloat-conversion and
post-process the compiler messages, it's not something we can really keep
enabled all the time.

Making a PR for this might be a tad complicated, but I agree it would be
the best way forward... so thanks for the suggestion :-)

Ciao,
.Andrea


-- 

Dr. Andrea Bocci
Applied Physicist, CMS Experiment
<https://cms.cern/>EP-CMD, CERN
<https://home.cern/>168990 (+41 75 411 8990)
CERN 40/1-B01 <https://maps.cern.ch/mapsearch/mapsearch.htm?n=['40/1-B01']>
E27810

Esplanade des particules, 1, CH-1211 Geneve 23, Switzerland
<https://goo.gl/maps/AMjHdhXzYk2itSu7A>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-22 19:27   ` Andrea Bocci
@ 2022-08-22 19:43     ` Jonathan Wakely
  2022-08-22 19:48       ` Andrea Bocci
  0 siblings, 1 reply; 7+ messages in thread
From: Jonathan Wakely @ 2022-08-22 19:43 UTC (permalink / raw)
  To: Andrea Bocci; +Cc: Segher Boessenkool, gcc-help

On Mon, 22 Aug 2022, 20:28 Andrea Bocci, <andrea.bocci@cern.ch> wrote:

> On Mon, 22 Aug 2022 at 19:33, Segher Boessenkool <
> segher@kernel.crashing.org>
> wrote:
> > If you want a separate -W option for this, please file a PR?
> > See <https://gcc.gnu.org/bugs.html>.
>

[snip]


> Making a PR for this might be a tad complicated, but I agree it would be
> the best way forward... so thanks for the suggestion :-)
>

For the avoidance of doubt, "PR" in this context means "problem report"
i.e. bug report, not "pull request".

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-22 19:43     ` Jonathan Wakely
@ 2022-08-22 19:48       ` Andrea Bocci
  2022-08-22 23:01         ` Segher Boessenkool
  0 siblings, 1 reply; 7+ messages in thread
From: Andrea Bocci @ 2022-08-22 19:48 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: Segher Boessenkool, gcc-help

Hi Jonathan.

For the avoidance of doubt, "PR" in this context means "problem report"
> i.e. bug report, not "pull request".
>

Ah... thanks for the clarification.
That is definitely something I can do :-)

Thanks,
.Andrea

-- 

Dr. Andrea Bocci
Applied Physicist, CMS Experiment
<https://cms.cern/>EP-CMD, CERN
<https://home.cern/>168990 (+41 75 411 8990)
CERN 40/1-B01 <https://maps.cern.ch/mapsearch/mapsearch.htm?n=['40/1-B01']>
E27810

Esplanade des particules, 1, CH-1211 Geneve 23, Switzerland
<https://goo.gl/maps/AMjHdhXzYk2itSu7A>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-22 19:48       ` Andrea Bocci
@ 2022-08-22 23:01         ` Segher Boessenkool
  2022-08-22 23:39           ` Jeff Law
  0 siblings, 1 reply; 7+ messages in thread
From: Segher Boessenkool @ 2022-08-22 23:01 UTC (permalink / raw)
  To: Andrea Bocci; +Cc: Jonathan Wakely, gcc-help

Hi!

On Mon, Aug 22, 2022 at 09:48:01PM +0200, Andrea Bocci wrote:
> For the avoidance of doubt, "PR" in this context means "problem report"
> > i.e. bug report, not "pull request".

Yeah, sorry for the confusion.  We talk about PRs in GCC a lot, since
time immemorial, long before github existed anyway.  We do not use
github's workflows, they are not suitable for us.

> Ah... thanks for the clarification.
> That is definitely something I can do :-)

:-)  Thanks in advance!


Segher

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Warn about conversion from floating point to integers
  2022-08-22 23:01         ` Segher Boessenkool
@ 2022-08-22 23:39           ` Jeff Law
  0 siblings, 0 replies; 7+ messages in thread
From: Jeff Law @ 2022-08-22 23:39 UTC (permalink / raw)
  To: gcc-help



On 8/22/2022 5:01 PM, Segher Boessenkool wrote:
> Hi!
>
> On Mon, Aug 22, 2022 at 09:48:01PM +0200, Andrea Bocci wrote:
>> For the avoidance of doubt, "PR" in this context means "problem report"
>>> i.e. bug report, not "pull request".
> Yeah, sorry for the confusion.  We talk about PRs in GCC a lot, since
> time immemorial, long before github existed anyway.  We do not use
> github's workflows, they are not suitable for us.
Likely goes all the way back to the days when we used PRMS/GNATS for bug 
tracking, so probably 25 years.

jeff


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-08-22 23:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-20  9:21 Warn about conversion from floating point to integers Andrea Bocci
2022-08-22 17:32 ` Segher Boessenkool
2022-08-22 19:27   ` Andrea Bocci
2022-08-22 19:43     ` Jonathan Wakely
2022-08-22 19:48       ` Andrea Bocci
2022-08-22 23:01         ` Segher Boessenkool
2022-08-22 23:39           ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).