473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4975
"Chris Becke" <ch*********@gmail.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.
93326215443944152681699238856266700490715968264381 62146859296389521759999322991560894146397615651828 62536979208272237582511852109168640000000000000000 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...@blueyonder.co.ukwrote:
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 objektorientierter Datenverarbeitung
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
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;...
8
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...
25
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...
9
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...
40
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:...
13
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...
232
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...
4
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...
42
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.