Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #8055 > unrolled thread

Re: Arithmetic overflow checking

Started bytm <thomas.mertes@gmx.at>
First post2011-07-10 01:47 -0700
Last post2011-09-08 22:24 +0200
Articles 5 on this page of 105 — 32 participants

Back to article view | Back to comp.lang.c

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-10 01:47 -0700
    Re: Arithmetic overflow checking China Blue Dolls <chine.bleu@yahoo.com> - 2011-07-10 02:47 -0700
      Re: Arithmetic overflow checking pete <pfiland@mindspring.com> - 2011-07-10 06:04 -0400
        Re: Arithmetic overflow checking China Blue Dolls <chine.bleu@yahoo.com> - 2011-07-10 03:29 -0700
          Re: Arithmetic overflow checking Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-10 20:52 +0300
          Re: Arithmetic overflow checking pete <pfiland@mindspring.com> - 2011-07-10 23:29 -0400
      Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-10 04:44 -0700
        Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 11:33 +0100
          Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 04:17 -0700
            Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 12:33 +0100
              Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 05:24 -0700
                Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-12 21:45 -0400
            Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-12 05:25 -0700
              Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 10:21 -0700
                Re: Arithmetic overflow checking Thomas Boell <tboell@domain.invalid> - 2011-07-12 22:39 +0200
                  Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 14:15 -0700
                    Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 14:18 -0700
                      Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 23:54 -0700
              Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-12 19:14 +0100
                Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-13 00:20 -0700
          Re: Arithmetic overflow checking markspace <-@.> - 2011-07-12 09:26 -0700
            Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 10:52 -0600
              Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 10:48 -0700
                Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 12:36 -0600
                  Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 12:16 -0700
                    Re: Arithmetic overflow checking Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-12 14:09 -0600
                      Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-12 14:16 -0700
                Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-12 21:52 -0700
                  Re: Arithmetic overflow checking, not so hard after all Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-13 12:50 +0100
                    Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:10 -0700
                      Re: Arithmetic overflow checking, not so hard after all Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-07-14 01:34 +0100
                        Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-07-19 17:10 -0700
                          Re: Arithmetic overflow checking, not so hard after all Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-07-19 20:47 -0600
                            Re: Arithmetic overflow checking, not so hard after all Keith Thompson <kst-u@mib.org> - 2011-07-19 20:23 -0700
                              Re: Arithmetic overflow checking, not so hard after all "Greg A. Woods" <woods@robohack.org> - 2011-09-06 17:47 -0700
                            Re: Arithmetic overflow checking, not so hard after all Todd Carnes <toddcarnes@gmail.com> - 2011-07-20 03:48 +0000
                  Re: Arithmetic overflow checking, not so hard after all Keith Thompson <kst-u@mib.org> - 2011-07-13 09:27 -0700
                Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:55 -0500
            Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-12 16:54 +0000
              Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 11:35 -0700
            Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 10:13 -0700
            Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-12 21:53 -0400
          Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:41 -0500
            Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-15 10:56 -0700
              Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-15 21:27 -0500
        Re: Arithmetic overflow checking bugbear <bugbear@trim_papermule.co.uk_trim> - 2011-07-20 09:22 +0100
          Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-20 10:51 -0700
            Re: Arithmetic overflow checking gordonb.3urm7@burditt.org (Gordon Burditt) - 2011-07-20 15:39 -0500
            Re: Arithmetic overflow checking "BartC" <bc@freeuk.com> - 2011-07-21 12:12 +0100
      Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-10 09:28 -0400
        Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-10 06:52 -0700
          Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-10 14:47 -0700
          Re: Arithmetic overflow checking gordonb.u8vng@burditt.org (Gordon Burditt) - 2011-07-11 18:45 -0500
        Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:07 -0500
      Re: Arithmetic overflow checking Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-07-10 12:25 -0400
    Re: Arithmetic overflow checking Robert Wessel <robertwessel2@yahoo.com> - 2011-07-10 10:47 -0500
    Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 07:58 -0700
      Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-11 10:48 -0700
        Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 14:40 -0700
      Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-11 14:54 -0700
        Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 15:55 -0700
        Re: Arithmetic overflow checking Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-07-11 21:51 -0400
          Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-11 21:31 -0700
            Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-11 23:16 -0700
            Re: Arithmetic overflow checking James Kuyper <jameskuyper@verizon.net> - 2011-07-12 06:28 -0400
            Re: Arithmetic overflow checking David Thompson <dave.thompson2@verizon.net> - 2011-07-24 22:13 -0400
              Re: Arithmetic overflow checking Lew Pitcher <lpitcher@teksavvy.com> - 2011-07-25 10:24 -0400
    Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 09:05 +0200
      Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 02:22 -0700
        Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 11:34 +0200
          Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-12 03:04 -0700
            Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 19:55 -0700
              Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 21:54 -0700
              Re: Arithmetic overflow checking "MikeP" <mp011011@some.org> - 2011-07-14 23:28 -0500
                Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-19 17:16 -0700
          Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-12 03:33 -0700
          Re: Arithmetic overflow checking David Lamb <dalamb@cs.queensu.ca> - 2011-07-12 08:29 -0400
        Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-12 13:18 +0200
        Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 11:39 -0700
          Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-12 12:38 -0700
            Re: Arithmetic overflow checking markspace <-@.> - 2011-07-12 13:20 -0700
              Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 13:23 -0700
                Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-12 21:08 +0000
                  Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-12 14:48 -0700
                    Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-12 15:24 -0700
                      Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-12 16:09 -0700
                        Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-13 10:38 -0700
                          Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-13 11:00 -0700
                          Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-13 12:16 -0700
                            Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-13 13:10 -0700
                            Re: Arithmetic overflow checking markspace <-@.> - 2011-07-13 13:21 -0700
                              Re: Arithmetic overflow checking Keith Thompson <kst-u@mib.org> - 2011-07-13 13:41 -0700
                                Re: Arithmetic overflow checking Robert Wessel <robertwessel2@yahoo.com> - 2011-07-14 21:10 -0500
                                  Re: Arithmetic overflow checking "io_x" <a@b.c.invalid> - 2011-07-15 11:57 +0200
                                    Re: Arithmetic overflow checking Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-07-15 04:36 -0700
                        Re: Arithmetic overflow checking Niklas Holsti <niklas.holsti@tidorum.invalid> - 2011-08-13 21:54 +0300
        Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-12 20:03 -0700
          Re: Arithmetic overflow checking tm <thomas.mertes@gmx.at> - 2011-07-13 00:52 -0700
            Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-13 07:45 -0700
              Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:28 -0700
            Re: Arithmetic overflow checking "Greg A. Woods" <woods@robohack.org> - 2011-07-13 11:15 -0700
    Re: Arithmetic overflow checking Wolfgang Draxinger <wdraxinger@darkstargames.de> - 2011-09-08 21:02 +0200
      Re: Arithmetic overflow checking Wolfgang Draxinger <wdraxinger@darkstargames.de> - 2011-09-08 21:12 +0200
        Re: Arithmetic overflow checking Willem <willem@toad.stack.nl> - 2011-09-08 19:15 +0000
          Re: Arithmetic overflow checking Wolfgang Draxinger <wdraxinger@darkstargames.de> - 2011-09-08 22:24 +0200

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#8242

From"Greg A. Woods" <woods@robohack.org>
Date2011-07-13 11:15 -0700
Message-ID<87r55u9hd3.fsf@once.weird.com>
In reply to#8229
tm <thomas.mertes@gmx.at> writes:
> 
> On 13 Jul., 05:03, "Greg A. Woods" <wo...@robohack.org> wrote:
> >
> > You may also be assuming that overflows aren't so common in C.  :-)
> >
> > Try re-compiling your system's entire userland with "gcc -ftrapv" and
> > see how much of it still works without aborting.
>
> It does work.

Your _entire_ system's userland?  Really?

I wonder if in NetBSD the shell wouldn't even die while running the
start-up scripts, and not because of a bug, but rather "intended"
overflows or wraparounds.

I dunno if I'm even brave enough to try it in a virtual machine!  ;-)

(of course I'd have to use a version of gcc with a working -ftrapv, and
that seems to be more difficult to find than I thought -- it's been a
while since I used that option for production code or even serious
testing)

-- 
						Greg A. Woods
						RoboHack

<woods@robohack.ca>     living on the edge      http://www.robohack.ca/

[toc] | [prev] | [next] | [standalone]


#10027

FromWolfgang Draxinger <wdraxinger@darkstargames.de>
Date2011-09-08 21:02 +0200
Message-ID<20110908210243.710b9d01@loki.yggdrasil.draxit.de>
In reply to#8055
Am Sun, 10 Jul 2011 01:47:25 -0700 (PDT)
schrieb tm <thomas.mertes@gmx.at>:

> A CPU could (in theory) easily recognize the overflow
> and generate an interrupt. This way normal computations
> (without overflow) would have no extra cost.

Interrupts are extremely expensive. They require the OS kernel into a
full blown context switch, registers must be saved and restored. And
finally the process needs to be signaled. Nobody sane in his mind would
it be done that way.

> AFAIK commonly used CPUs do not have this possibility. They
> have some FLAG, which is set when an overflow occurred.

And checking a flag is how difficult? It's a simple, cheap conditional
jump. And the execution prediction system works very efficient on
flags, because they're integral CPU state.

> But there is no possibility to cause an interrupt, when
> the overflow FLAG is set. So code, which checks for
> overflow, must check this flag after every computation.

So? Checks are not expensive. Heck, some architectures, like ARM,
execute instructions conditionally, depending on a flag. Reacting on a
overflow flag comes with NO EXTRA COST there.

> Needless to say: Normal computations (without overflow)
> are slowed down by this checks.

No, they are not. Modern pipelined CPUs have codepath prediction and
out-of-order-operations, where some codepaths are executed proactively
and their results are kept on-hold. The instructions reacting upon the
overflow flag check may have been finished, even before the offending
operation may have been.

> Because of this slow down most compilers and virtual
> machines (AFAIK inluding the JVM) have no overflow
> checking.

Almost every new language designed implement overflow checks. Also
every generic container library does so. Overflow checks cost NOTHING
these days.

> In other words: A missing hardware feature:
> 
>   Trigger interupt when overflow flag is set.
> 
> Causes compilers and JVMs to omit overflow checks.

Did you ever think about the kind of infrastructure that's required for
this? This goes down into the bowels of the OS kernel. Interrupts
always cause context switches, which are expensive. Checking a flag
comes practically for free. Especially if the execution prediction can
determine, that a certain codepath depends on a certain flag only.

To give you some figures: Processing an interrupt takes Linux about 1 to
10µs. On a 3GHz CPU that amounts to 30000 clockcycles, and most
modern, pipelined CPUs finish at least 1 instruction per clock cycle;
on a Intel Pentium or Core for the majority of instructions have a up
to 8 finished instructions per clock cycle. In the time your proposed
overflow-check interrupt is processed you can do about 200000 flag
checks.


Wolfgang

[toc] | [prev] | [next] | [standalone]


#10030

FromWolfgang Draxinger <wdraxinger@darkstargames.de>
Date2011-09-08 21:12 +0200
Message-ID<20110908211243.7932aa0c@loki.yggdrasil.draxit.de>
In reply to#10027
Am Thu, 8 Sep 2011 21:02:43 +0200
schrieb Wolfgang Draxinger <wdraxinger@darkstargames.de>:

> To give you some figures: Processing an interrupt takes Linux about 1
> to 10µs. On a 3GHz CPU that amounts to 30000 clockcycles, and most
> modern, pipelined CPUs finish at least 1 instruction per clock cycle;
> on a Intel Pentium or Core for the majority of instructions have a up
> to 8 finished instructions per clock cycle. In the time your proposed
> overflow-check interrupt is processed you can do about 200000 flag
> checks.

Well, I was off by a factor of ~10. The typical Linux ISR takes about
2000 clock cycles. Still going such a long way is still a lot slower
than checking the flag in under a cycle.


Wolfgang

[toc] | [prev] | [next] | [standalone]


#10031

FromWillem <willem@toad.stack.nl>
Date2011-09-08 19:15 +0000
Message-ID<slrnj6i521.1t7i.willem@toad.stack.nl>
In reply to#10030
Wolfgang Draxinger wrote:
) Am Thu, 8 Sep 2011 21:02:43 +0200
) schrieb Wolfgang Draxinger <wdraxinger@darkstargames.de>:
)
)> To give you some figures: Processing an interrupt takes Linux about 1
)> to 10??s. On a 3GHz CPU that amounts to 30000 clockcycles, and most
)> modern, pipelined CPUs finish at least 1 instruction per clock cycle;
)> on a Intel Pentium or Core for the majority of instructions have a up
)> to 8 finished instructions per clock cycle. In the time your proposed
)> overflow-check interrupt is processed you can do about 200000 flag
)> checks.
)
) Well, I was off by a factor of ~10. The typical Linux ISR takes about
) 2000 clock cycles. Still going such a long way is still a lot slower
) than checking the flag in under a cycle.

So if an overflow condition typically occurs less than 1 in 10000 times,
the interrupt solution is better.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

[toc] | [prev] | [next] | [standalone]


#10034

FromWolfgang Draxinger <wdraxinger@darkstargames.de>
Date2011-09-08 22:24 +0200
Message-ID<20110908222442.3eda3e88@loki.yggdrasil.draxit.de>
In reply to#10031
Am Thu, 8 Sep 2011 19:15:13 +0000 (UTC)
schrieb Willem <willem@toad.stack.nl>:

> So if an overflow condition typically occurs less than 1 in 10000
> times, the interrupt solution is better.

Theoretically yes, practically no!

It depends on the architecture. On ARM it doesn't matter at all, since
ALL instructions are conditional.

On x86 architecture due to pipelining and OOE the differences are not
measureable as well (the overflow case code executes in parallel and
may have been finished, before the code, creating the overflow executed
at all). Also the overflow condition will probably not do any
arithmetic, in contrast to the arithmetic that does overflow. So these
are separate functional units at work, so pipelining will execute them
in parallel.

Really, there no cost to speak of in doing overflow checks.


Wolfgang

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

Back to top | Article view | comp.lang.c


csiph-web