This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RFA: Generalise IN_RANGE


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.  */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]