[Bug c/56956] ftrapv traps on valid abs-like code

2015-10-22 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956

--- Comment #8 from Richard Biener  ---
Yeah, unfortunately that doesn't look correct :(

I've always wanted to have an ABS_EXPR variant that returns an unsigned value
(so with no undefined behavior).  Maybe just overload ABS_EXPR in this way.
You'd need to assure expansion doesn't use absv optabs, that is, trapping
behavior / undefinedness would depend on the ABS_EXPR result type.  The
argument type would still be signed.


[Bug c/56956] ftrapv traps on valid abs-like code

2015-10-22 Thread mpolacek at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956

Marek Polacek  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2015-10-22
 CC||mpolacek at gcc dot gnu.org
 Ever confirmed|0   |1

--- Comment #7 from Marek Polacek  ---
Confirmed. 

#define INT_MIN (-__INT_MAX__ - 1L)

unsigned int
foo (unsigned int x)
{
  return x <= __INT_MAX__ ? x : -x;
}

int
main ()
{
  return foo (INT_MIN);
}

We transform foo to
return (unsigned int) ABS_EXPR <(signed int) x>;


[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread jasonwucj at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956



Chung-Ju Wu jasonwucj at gmail dot com changed:



   What|Removed |Added



 CC||jasonwucj at gmail dot com



--- Comment #1 from Chung-Ju Wu jasonwucj at gmail dot com 2013-04-15 
00:58:14 UTC ---

I think 'x' can not present negative value.


[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread sunfish at google dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956



--- Comment #2 from Dan Gohman sunfish at google dot com 2013-04-15 03:53:10 
UTC ---

(In reply to comment #1)

 I think 'x' can not present negative value.



The unary minus operator is defined for unsigned types. It doesn't produce

signed overflow.


[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread sunfish at google dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956



--- Comment #3 from Dan Gohman sunfish at google dot com 2013-04-15 03:54:32 
UTC ---

Pulling the unary minus out into a separate statement, like this:



  uint64_t y = -x;

  return x = INT64_MAX ? x : -y;



causes the program to execute correctly.


[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread jasonwucj at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956



--- Comment #4 from Chung-Ju Wu jasonwucj at gmail dot com 2013-04-15 
04:18:13 UTC ---

(In reply to comment #2)

 (In reply to comment #1)

  I think 'x' can not present negative value.

 

 The unary minus operator is defined for unsigned types. It doesn't produce

 signed overflow.



According to C99 6.5.3.3 Point 3 and C99 6.5 Point 5:



The result of the unary '-' operator is the negative of its operand.



... if the result is ... not in the range of representable

 values for its type... the behavior is undefined.



So my understanding is that the evaluation expression '-x' is not

a representable value of 'uint64_t', which is undefined behavior,

resulting abort if -ftrav is issued.



Perhaps my understanding is incorrect? :(


[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread sunfish at google dot com

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956

--- Comment #5 from Dan Gohman sunfish at google dot com 2013-04-15 05:12:30 
UTC ---
(In reply to comment #4)
 (In reply to comment #2)
  (In reply to comment #1)
   I think 'x' can not present negative value.
  
  The unary minus operator is defined for unsigned types. It doesn't produce
  signed overflow.
 
 According to C99 6.5.3.3 Point 3 and C99 6.5 Point 5:
 
 The result of the unary '-' operator is the negative of its operand.
 
 ... if the result is ... not in the range of representable
  values for its type... the behavior is undefined.
 
 So my understanding is that the evaluation expression '-x' is not
 a representable value of 'uint64_t', which is undefined behavior,
 resulting abort if -ftrav is issued.
 
 Perhaps my understanding is incorrect? :(

Yes; unsigned types are an exception to the rule:

C99 6.2.5p9 says A computation involving unsigned operands can never overflow,
because a result that cannot be represented by the resulting unsigned integer
type is reduced modulo the number that is one greater than the largest value
that can be represented by the resulting type.

The wording is a little vague, but it means that negative results are converted
to unsigned values by conceptually adding the maximum unsigned value plus one
until the value is in range.

[Bug c/56956] ftrapv traps on valid abs-like code

2013-04-14 Thread sunfish at google dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56956



--- Comment #6 from Dan Gohman sunfish at google dot com 2013-04-15 05:14:27 
UTC ---

(In reply to comment #3)

 Pulling the unary minus out into a separate statement, like this:

 

   uint64_t y = -x;

   return x = INT64_MAX ? x : -y;

 

 causes the program to execute correctly.



Actually, I meant to write this:



  uint64_t y = -x;

  return x = INT64_MAX ? x : y;



and it still executes correctly, with no trap.