473,511 Members | 15,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting multipication overflow

wij
Hi:
Is there better way of detecting multiplication overflow for type
long than by using double precision of lldiv to verify the result?
Thanks in advance.

I.J.Wang

Nov 15 '05 #1
7 2402
wi*@seed.net.tw wrote:

Hi:
Is there better way of detecting multiplication overflow for type
long than by using double precision of lldiv to verify the result?
Thanks in advance.

I.J.Wang


Divide LONG_MAX by one of the multiplicands
and see if the result is less than the other multiplicand.

--
pete
Nov 15 '05 #2
wi*@seed.net.tw a écrit :
Hi:
Is there better way of detecting multiplication overflow for type
long than by using double precision of lldiv to verify the result?
Thanks in advance.

I.J.Wang

The lcc-win32 compiler allows detection of overflow
automatically in 2 ways:
1) By a compile time switch. This will test each operation
for overflow in the whole program
2) Using the _overflow() intrinsic to test for overflow after
a specific operation, for instance:
int a,n;

n = n*59876;
if (_overflow())
printf("It did not work\n");

In another compiler systems the best thing is to use assembly:

.text
.globl _overflow
_overflow:
movl $0,%eax
seto %al
ret

This function will return 1 if the overflow flag is set,
zero otherwise. Note that a move is done to zero eax, NOT
xor eax,eax because that would destroy the flags.

If you use another CPU than the x86, you will have to read the
assembly language manual for your machine. If using a SPARC
for instance you could use BVS (Branch on oVerflow Set) to
alternatively set a register to either 0 or 1

jacob
http://www.cs.virginia.edu/~lcc-win32
Nov 15 '05 #3
In article <43***********************@news.wanadoo.fr> jacob navia <ja***@jacob.remcomp.fr> writes:
....
If you use another CPU than the x86, you will have to read the
assembly language manual for your machine. If using a SPARC
for instance you could use BVS (Branch on oVerflow Set) to
alternatively set a register to either 0 or 1


You apparently have not read the assembly language manual for the sparc
thoroughly.
1. BVS does (as the name implies) a branch when the overflow bit is set.
I think you mean SVS (or something like that) which sets a register
to 0 or 1 depending on the overflow bit.
2. If the compiler uses the instructions that do not set the overflow
bit, this will not help at all. "add" will not set it on addition,
"addcc" will set it on addition. I think most compilers do not use
"addcc" as it might trap.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 15 '05 #4
>In article <43***********************@news.wanadoo.fr> jacob navia
<ja***@jacob.remcomp.fr> writes:
The lcc-win32 compiler allows detection of overflow
automatically in 2 ways:
1) By a compile time switch. This will test each operation
for overflow in the whole program
This is actually a rather nice method. Of course, it turns out
that people want finer granularity of testing for integer overflow.
You might consider using C99's _Pragma() construct to turn this
automatic overflow-testing on and off. (_Pragma is better than
#pragma as "_Pragma"s can be hidden behind preprocessor macros.)
2) Using the _overflow() intrinsic to test for overflow after
a specific operation, for instance:
int a,n; [presumably here "n" is set to some value; "a" is irrelevant] n = n*59876;
if (_overflow())
printf("It did not work\n");
In another compiler systems the best thing is to use assembly:
.text
.globl _overflow
_overflow:
movl $0,%eax
seto %al
ret
This function will return 1 if the overflow flag is set,
zero otherwise. Note that a move is done to zero eax, NOT
xor eax,eax because that would destroy the flags.
This method will not work in general, for reasons I will mention in
a moment.
If you use another CPU than the x86, you will have to read the
assembly language manual for your machine. If using a SPARC
for instance you could use BVS (Branch on oVerflow Set) to
alternatively set a register to either 0 or 1

In article <Ip********@cwi.nl>, Dik T. Winter <Di********@cwi.nl> wrote:You apparently have not read the assembly language manual for the sparc
thoroughly.
1. BVS does (as the name implies) a branch when the overflow bit is set.
I think you mean SVS (or something like that) which sets a register
to 0 or 1 depending on the overflow bit.
The SPARC does not have an "svs", though it does have a "tvs" (trap
if overflow is set). One could use bvs, if it were not for a rather
different problem that you also mention:
2. If the compiler uses the instructions that do not set the overflow
bit, this will not help at all. "add" will not set it on addition,
"addcc" will set it on addition. I think most compilers do not use
"addcc" as it might trap.


Actually addcc never traps; it merely sets the condition codes
(both %icc and %xcc on V9; V8 and earlier have only %icc) based on
the result of the add. You may be thinking of "taddcc", although
that instruction is intended for Lisp systems.

It is, however, true that most compilers generate a simple "add"
or -- for multiply -- one of three instructions or instruction
sequences, none of which set the "v" flag in %icc and/or %xcc.
So this method will not work on SPARC systems.

Unfortunately, the overflow() function will also not work on x86
systems -- or rather, worse, it will *sometimes* work, but sometimes
fail, in a manner anywhere from "difficult" to "impossible" to
predict. The reason is that there is no requirement that the
compiler call overflow() immediately after the "obvious" instruction
(in this case imull). The code:

n = n * 59876;
if (_overflow())
...
use(n);

might -- as long as n is a local variable, thus "obviously" not
accessible from a function like _overflow() -- quite legitimately
compile to:

call _overflow
testl %eax,%eax
je .Laround
...
..Laround:
# multiply n by 59876 *now*, *after* calling overflow()
imull ... # or equivalent (gcc uses a sequence of "leal"s)
pushl [n] #
call use

As it happens, gcc (2.95.3, and not using -march=k6 or -march=i686)
does not move the multiply itself in a small sample routine I
created, but it does use leal instead of imull, and leal does not
set the "v" bit:

# at this point, "n" is in %ebx
leal (%ebx,%ebx,2),%eax
leal (%ebx,%eax,4),%eax
leal (%eax,%eax,8),%eax
sall $4,%eax
subl %ebx,%eax
leal (%ebx,%eax,8),%eax
leal 0(,%eax,4),%ebx
call _overflow
...

The "v" bit value will be left over from the "subl" instruction,
and will have little to do with whether the multiply really
overflowed. (But note that with 686 or k6 architecture or CPU
selected, the cost of the imull is reduced, so the instruction
selector chooses it instead of the leal-sequence.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 19 '05 #5
Well, if your compiler is moving instructions around
that will never work, of course.

In that case there is no other choice than to disable
those optimizations, or find a work-around specific
to the compiler, for instance maybe declaring the result
volatile, or similar stuff.

Since lcc-win32 never does that kind of optimizations
that does not apply to its environment.

The best option remains to test *ALL* operations
for overflow. In modern PCs that adds at most
0.0001% run time when done correctly. The correct
code to generate would be
add ...
jo ; jump if overflow. Predicted as NOT
; taken if it is a forward jump.

that would cost 1 extra cycle, something that would never add
much to your program's run time. Even the solution used in
lcc-win32, adds less than 1% to the run time speed.

jacob
Dec 19 '05 #6
jacob navia wrote:

Well, if your compiler is moving instructions around
that will never work, of course.

In that case there is no other choice than to disable
those optimizations, or find a work-around specific
to the compiler, for instance maybe declaring the result
volatile, or similar stuff.

Since lcc-win32 never does that kind of optimizations
that does not apply to its environment.

The best option remains to test *ALL* operations
for overflow. In modern PCs that adds at most
0.0001% run time when done correctly. The correct
code to generate would be
add ...
jo ; jump if overflow. Predicted as NOT
; taken if it is a forward jump.

that would cost 1 extra cycle, something that would never add
much to your program's run time. Even the solution used in
lcc-win32, adds less than 1% to the run time speed.


This is highly OT, since it refers only to X86 code generation.
However the x86 has the into instruction, which executes a software
interrupt when the overflow bit is set. This is not executed when
the overflow bit is clear. So all interrupts can be handled by a
single service routine, and only that routine (or the connector to
it) need be altered to ignore all overflows.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 19 '05 #7
Chuck F. wrote:

This is highly OT, since it refers only to X86 code generation. However
the x86 has the into instruction, which executes a software interrupt
when the overflow bit is set. This is not executed when the overflow
bit is clear. So all interrupts can be handled by a single service
routine, and only that routine (or the connector to it) need be altered
to ignore all overflows.


Mmmm That's VERY interesting!

Thanks Chuck!
Dec 19 '05 #8

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

Similar topics

3
2736
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
2
6051
by: Paul Emmons | last post by:
Can anyone suggest example code, or how to proceed, in adding or subtracting two long long integers (I'm working in gcc with Linux, where a long long is 64 bits) and determining whether an overflow...
25
6217
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...
10
3249
by: vashwath | last post by:
Hi all, Is there any free tool available for detecting array overflow? I found one which detects overflow of dynamic arrays. But I need a tool(or a special compiler) which detects static array...
7
2197
by: pocmatos | last post by:
Hi all, What the best way to detect under/over flow in a program with a lot of computations? For example: #include <iostream> #include <limits> using namespace std;
8
2385
by: Ole Nielsby | last post by:
Given two longs: long a = ...; long b = ...; What's the simplest/fastest way of performing a simple operation on longs and detecting overflow? long c = a + b; // but branch on overflow
6
3784
by: Andre Majorel | last post by:
How do you compute an off_t with overflow detection ? Ideally, the target language is C89/C90 and the target platform is reasonably recent versions of the major Unixen. If there is no practical...
0
7242
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
7138
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
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
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...
1
7075
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
5662
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,...
1
5063
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...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
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...

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.