473,654 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer overflow detection would be a nice addition.

I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in calculations.
When multiplying two numbers, the CPU automatically returns a double width result.

c & c++ give programmers these bloody ridiculous integer types of undetermined size and no really optimal way to even build a variable width number library without resorting to platform specific assembler code.

I know that c & c++ have built a lot of their success on the many many many CPUs other than x86 that they target, but really, this inability to write portable code that deals with large numbers relaibly (& quickly) is a bit of a pain.. And lets not forget the pre and post increment operators owe their existance to assembly language level features of the first CPU a c compiler was developed for (at least, so Ive been led to believe).

The 6502 processor in my Vic20 had these basic features more than 20 years ago. Its not like carry flags and double width multiplaction results are novel or new ideas.

/rant
Oct 15 '08 #1
6 4989
"Chris Becke" <ch*********@gm ail.comwrites:
I *know* my CPU has opcodes that can do this - when adding (or
subtracting) there is a carry flag that can be invoked to make the
result essentially 1 bit longer than the data size used in
calculations. When multiplying two numbers, the CPU automatically
returns a double width result.

c & c++ give programmers these bloody ridiculous integer types of
undetermined size and no really optimal way to even build a variable
width number library without resorting to platform specific
assembler code.

I know that c & c++ have built a lot of their success on the many
many many CPUs other than x86 that they target, but really, this
inability to write portable code that deals with large numbers
relaibly (& quickly) is a bit of a pain.. And lets not forget the
pre and post increment operators owe their existance to assembly
language level features of the first CPU a c compiler was developed
for (at least, so Ive been led to believe).

The 6502 processor in my Vic20 had these basic features more than 20
years ago. Its not like carry flags and double width multiplaction
results are novel or new ideas.

/rant
Basically, you've got two choices:

- use gmp or some other bignum library. With C++ operator overloading
this may even be pleasant.

- switch to Common Lisp: bignums are natively supported in all
implementations . Some implementations also support long-float,
where you can specify the size of the mantissa.
C/USER[35](! 100) ; factorial of 100.
933262154439441 526816992388562 667004907159682 643816214685929 638952175999932 299156089414639 761565182862536 979208272237582 511852109168640 000000000000000 00000000

--
__Pascal Bourguignon__
Oct 15 '08 #2
Chris Becke wrote:
I *know* my CPU has opcodes that can do this -
when adding (or subtracting) there is a carry flag
that can be invoked to make the result essentially
1 bit longer than the data size used in calculations.
When multiplying two numbers, the CPU automatically
returns a double width result.
I'm not an expert on this, but as far as I know there is not such
facility in C++. In C, however (and therefore in most C++ compilers,
even though formally this is not required), there is some support for
floating point exceptions, in the header fenv.h. Check out:

http://qnxclub.net/files/articles/un...fs/fenv.h.html

However, this support is not event-driven, so you have to query for
exceptions. I know this is not optimal, you may consider writing guards
on some exception to wrap pieces of code that you don't want to generate
NaNs, underflows, overflows, and so on.

Best wishes,

Zeppe
Oct 15 '08 #3
Chris Becke wrote:
I *know* my CPU has opcodes that can do this
Then write your own inline asm to do that. It will be exactly as
portable as anything else related to that.
Oct 15 '08 #4
The lcc-win C compiler (not C++) detects overflow
if the user asks for it.
I agree that it should be part of the language.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Oct 15 '08 #5

I agree, but only to a point.

One option you have is to use Ada, which has overflow detection built
in. You can declare subtypes with different ranges, and the overflow
check is based on that. Performance isn't normally badly hit because
the compiler can determine when the checks are needed and when they
aren't.

The main problem with Ada is that it's nowhere near as widely used as
C++, meaning few third party libraries, limited compiler choices etc.

Within C++, you can use big integer classes or other integer-type
wrapper classes that either never overflow (unless you go really nuts)
or which have overflow checks built in. Libraries like this can be
used pretty much as easily as simple integers due to operator
overloading.

There are big integer classes available

Wrapping a normal integer is more likely to be done for strict typing
reasons - it's easy enough to write a template wrapper class, but
getting the checks right (without exploiting a platform-specific
overflow flag) can be fiddly. What's more, the overflow checking
probably needs to make a 2s complement assumption - I wouldn't be
surprised if this is non-portable in itself. There used to be machines
that used sign-value for integers, for instance - I have no idea if
that kind of thing is still in use.

Oct 16 '08 #6
On Oct 16, 3:54 am, Stephen Horne <sh006d3...@blu eyonder.co.ukwr ote:
One option you have is to use Ada, which has overflow
detection built in. You can declare subtypes with different
ranges, and the overflow check is based on that. Performance
isn't normally badly hit because the compiler can determine
when the checks are needed and when they aren't.
I don't think the original poster was looking for Ada-like
overflow checking (although it would be nice as well). He
specifically mentionned the problem of implementing variable
width numbers, and his problem seemed to be that there are
always (or almost always) specific hardware features which would
help here, but which cannot be accessed from C++: every machine
I've worked on has a carry bit, for example, and the vast
majority will calculate a double word result when multiplying
words. Thus, if you're implementing an extended + operator on
an 80x86, your main loop would almost certainly contain
something like:

mov eax, [esi]
adc eax, [ebx]
mov [edi], eax

That adc instruction (add with carry) makes things go a lot
faster, and there's no way to get the C++ compiler to generate
it. (I suppose a really good optimizer could recognize some
pattern in your code, deduce that this was the target semantics,
and generate it. But I've never seen an optimizer that good.)
The case of multiplication is even worse: when multiplying two
32 bit values, you need the full 64 bit results; the hardware
multiply on an 80x86 gives you this, but the compiler won't let
you get at the top 32 bits.

As an extreme example of this sort of problem, I once maintained
a product which used BCD arithmetic; there was a module of
somewhere around 1000 LOC in C which implemented the four basic
operators for 13 digit BCD values. The person who ported this
application to the Siemens BS2000 mainframe (IBM 360
architecture) rewrote this module in assembler. With about 10
machine instructions and no loops for each operator: the
hardware had hardwired 8 byte BCD arthimetic. In this case, the
assembler was not only faster (by several orders of magnitude),
it was also significantlly shorter and simpler to understand and
maintain.
The main problem with Ada is that it's nowhere near as widely
used as C++, meaning few third party libraries, limited
compiler choices etc.
Within C++, you can use big integer classes or other
integer-type wrapper classes that either never overflow
(unless you go really nuts) or which have overflow checks
built in. Libraries like this can be used pretty much as
easily as simple integers due to operator overloading.
The problem is that either you write the actual code for such a
class in assembler, or you take a serious performance hit.
Because the hardware has special instructions for this, the
assembler is typically easier to understand and maintain than
the C++ would. As for portability, however...

I rather agree with the original poster. It would be nice if
C++ offered some way to access this funtionality. Practically,
however, I don't really know what the syntax would look like.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 16 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4330
by: John Black | last post by:
Hi, If I have many integer calculation in my code, what's the best way to detect integer overflow? unsigned int i1 = 0xFFFFFF00, i2 = 0xFFFF; then in statement unsigned int i3 = i1 + i2; there is overflow and the result is not what I want. If such sum calculation scatters around my code, I wonder what's the best way to catch it?
8
377
by: Ashutosh Iddya | last post by:
Hi , I am performing an integer count of a particular operation in my program. After a sufficiently large value an overflow occurs. At the moment I have gone around the problem by declaring it as a double, even that has its limits. Is there a method of preventing this overflow or some method of recovering from it. Any help in this regard would be greatly appreciated. Thanking you.
25
6241
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do something;
9
8617
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is FFFFFFDB, which should be -37. Thanks.
40
2788
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at: http://www.cert.org/secure-coding/ The purpose of this library is to provide a collection of utility functions that can assist software developers in writing C programs that are free from common integer problems such as integer overflow, integer...
13
3208
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
232
13242
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
4
9943
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum (augend + addend); if (sum < augend)
42
6999
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C type 'long' is used. That way native C speed can be reached. Now I want to experiment with raising a Seed7 exception (which is emulated with setjmp(), longjmp() in C) for integer
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.