public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* RFA: Generalise IN_RANGE
@ 2007-10-12 17:11 Richard Sandiford
  2007-10-20  7:22 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Sandiford @ 2007-10-12 17:11 UTC (permalink / raw)
  To: gcc-patches

I'm working on some patches that use the IN_RANGE macro, and I hit
a problem with the current definition:

#define IN_RANGE(VALUE, LOWER, UPPER) \
  ((unsigned HOST_WIDE_INT)((VALUE) - (LOWER)) <= ((UPPER) - (LOWER)))

This gives a warning if VALUE, LOWER and UPPER are all signed variables
(thus including caess where "VALUE >= LOWER && VALUE <= UPPER" wouldn't
warn).

One fix would be to cast both sides to "unsigned HOST_WIDE_INT",
but given that overflow in signed subtraction is undefined, I think
it would be more correct to cast each term individually.  I don't
think this should have any negative consequences in terms of when
the macro can be applied, since we already require LOWER <= UPPER
(with "<=" implicitly being in the natural type of LOWER and UPPER,
rather than "unsigned HOST_WIDE_INT").  And I don't think it would
have any real optimisation impact either, since gcc seems pretty
good at handling these sorts of cast.

Bootstrapped & regression-tested on x86_64-linux-gnu and regression-tested
on mipsisa32-elf.  OK to install?

Richard


gcc/
	* system.h (IN_RANGE): Cast each argument individually.

Index: gcc/system.h
===================================================================
--- gcc/system.h	2007-10-12 17:52:01.000000000 +0100
+++ gcc/system.h	2007-10-12 17:58:48.000000000 +0100
@@ -253,7 +253,8 @@ #define ICE_EXIT_CODE 4
    UPPER.  However the bounds themselves can be either positive or
    negative.  */
 #define IN_RANGE(VALUE, LOWER, UPPER) \
-  ((unsigned HOST_WIDE_INT)((VALUE) - (LOWER)) <= ((UPPER) - (LOWER)))
+  ((unsigned HOST_WIDE_INT) (VALUE) - (unsigned HOST_WIDE_INT) (LOWER) \
+   <= (unsigned HOST_WIDE_INT) (UPPER) - (unsigned HOST_WIDE_INT) (LOWER))
 
 /* Infrastructure for defining missing _MAX and _MIN macros.  Note that
    macros defined with these cannot be used in #if.  */

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

* Re: RFA: Generalise IN_RANGE
  2007-10-12 17:11 RFA: Generalise IN_RANGE Richard Sandiford
@ 2007-10-20  7:22 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2007-10-20  7:22 UTC (permalink / raw)
  To: Richard Sandiford; +Cc: gcc-patches

Richard Sandiford <rsandifo@nildram.co.uk> writes:

> gcc/
> 	* system.h (IN_RANGE): Cast each argument individually.

This is OK.

Thanks.

Ian

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

end of thread, other threads:[~2007-10-20  1:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-12 17:11 RFA: Generalise IN_RANGE Richard Sandiford
2007-10-20  7:22 ` Ian Lance Taylor

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).