public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Complex integer division
@ 2001-11-05 14:23 Joseph S. Myers
  2001-11-05 15:31 ` Bernd Schmidt
  0 siblings, 1 reply; 5+ messages in thread
From: Joseph S. Myers @ 2001-11-05 14:23 UTC (permalink / raw)
  To: gcc

GCC supports complex integer types as an extension (whereas C99 only 
allows complex floating point types).

How ought division of such numbers to work?  That is, when can / and %
applied to such numbers yield undefined behavior, and exactly which values
should the results have?  Presumably (a/b)*b + a%b equals a, as for normal
integer division, and the algebraic value of a%b divided by b should have
real and imaginary parts both strictly between 1 and -1 (or some similar
constraint), but is there a more precise specification?  For normal
integer division, C99 precisely defines the results.

PR c/2995, though it claims to be a problem with casts, in fact reflects a 
problem with how GCC handles complex integer division: using the normal 
mathematical formulas without regard for how C integers behave.  A simple 
testcase which will divide by zero but clearly shouldn't is:

	int
	main (void)
	{
	  _Complex int x = 1;
	  x = x / 65536;
	  return 0;
	}

I don't think this is a reasonable way to do complex integer division; the 
only corner case for normal integer division that causes problems is the 
most negative integer modulo -1 (which ought to be 0 rather than an 
exception), and making a lot of cases simply not work for dividing complex 
integers seems bad.  (C99 in fact requires that for floating point 
division the usual mathematical formulas - the only way GCC supports - 
should not be used by default because of undue overflow and underflow; 
CX_LIMITED_RANGE defaults to "off".)

Suggestions on the right way to fix this?

-- 
Joseph S. Myers
jsm28@cam.ac.uk

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

* Re: Complex integer division
  2001-11-05 14:23 Complex integer division Joseph S. Myers
@ 2001-11-05 15:31 ` Bernd Schmidt
  2001-11-07 12:21   ` Joseph S. Myers
  2001-11-07 19:06   ` Joe Buck
  0 siblings, 2 replies; 5+ messages in thread
From: Bernd Schmidt @ 2001-11-05 15:31 UTC (permalink / raw)
  To: Joseph S. Myers; +Cc: gcc

On Sat, 17 Nov 2001, Joseph S. Myers wrote:
> Suggestions on the right way to fix this?

Deprecate complex ints?  Or is anyone using them?


Bernd

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

* Re: Complex integer division
  2001-11-05 15:31 ` Bernd Schmidt
@ 2001-11-07 12:21   ` Joseph S. Myers
  2001-11-07 19:06   ` Joe Buck
  1 sibling, 0 replies; 5+ messages in thread
From: Joseph S. Myers @ 2001-11-07 12:21 UTC (permalink / raw)
  To: Bernd Schmidt; +Cc: gcc

On Sun, 18 Nov 2001, Bernd Schmidt wrote:

> On Sat, 17 Nov 2001, Joseph S. Myers wrote:
> > Suggestions on the right way to fix this?
> 
> Deprecate complex ints?  Or is anyone using them?

I'd be happy to deprecate and then get rid of them - what does anyone else
think?

There are other problems with them.  For example, debug/1621 describes
long-standing problems with debug information for complex numbers, which
need compiler and debugger fixes.  The "right way" to fix this would be to
generate the debugging information for complex types supported by the
current DWARF drafts, and support it in the debugger, but this only
supports complex floating-point types (as in C99), not complex integer
types.

-- 
Joseph S. Myers
jsm28@cam.ac.uk

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

* Re: Complex integer division
  2001-11-05 15:31 ` Bernd Schmidt
  2001-11-07 12:21   ` Joseph S. Myers
@ 2001-11-07 19:06   ` Joe Buck
  2001-11-08 12:58     ` Joseph S. Myers
  1 sibling, 1 reply; 5+ messages in thread
From: Joe Buck @ 2001-11-07 19:06 UTC (permalink / raw)
  To: Bernd Schmidt; +Cc: Joseph S. Myers, gcc


> On Sat, 17 Nov 2001, Joseph S. Myers wrote:
> > Suggestions on the right way to fix this?
> 
> Deprecate complex ints?  Or is anyone using them?

The only problematic operation is division, as complex ints are closed
(modulo overflow) for +, -, and *.  So deprecate only the problematic
operation.

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

* Re: Complex integer division
  2001-11-07 19:06   ` Joe Buck
@ 2001-11-08 12:58     ` Joseph S. Myers
  0 siblings, 0 replies; 5+ messages in thread
From: Joseph S. Myers @ 2001-11-08 12:58 UTC (permalink / raw)
  To: Joe Buck; +Cc: Bernd Schmidt, gcc

On Mon, 19 Nov 2001, Joe Buck wrote:

> The only problematic operation is division, as complex ints are closed
> (modulo overflow) for +, -, and *.  So deprecate only the problematic
> operation.

Multiplication could cause problems as well, since GCC might optimize
intermediate computations on the basis that signed integer overflow is
undefined and so fail to produce the correct answer when that is within
range.

-- 
Joseph S. Myers
jsm28@cam.ac.uk

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

end of thread, other threads:[~2001-11-19 20:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-05 14:23 Complex integer division Joseph S. Myers
2001-11-05 15:31 ` Bernd Schmidt
2001-11-07 12:21   ` Joseph S. Myers
2001-11-07 19:06   ` Joe Buck
2001-11-08 12:58     ` Joseph S. Myers

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