home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!idacrd!desj@ccr-p.ida.org
- From: desj@ccr-p.ida.org (David desJardins)
- Newsgroups: comp.programming
- Subject: Re: Detecting Integer Overflow/Underflow
- Message-ID: <1573@idacrd.UUCP>
- Date: 23 Aug 92 18:30:13 GMT
- References: <HUDGENS.92Aug22113851@sun13.SCRI.FSU.EDU> <1572@idacrd.UUCP> <1992Aug23.133620.6154@gmuvax2.gmu.edu>
- Sender: desj@idacrd.UUCP
- Organization: IDA Center for Communications Research, Princeton
- Lines: 52
-
- Steve Coile <scoile@gmuvax2.gmu.edu> writes:
- >Could someone please, PLEASE explain what is going on with
- >
- > ((a ^ sum) & (a ^ b)) ^ b
- >
- >What is the justification for doing this (i.e. how in the WORLD would someone
- >figure this out that THIS is the way to do it?????)
-
- It's close to being trial and error. You know what boolean function you
- want, and you try different sequences of operations to find a minimal-
- cost representation. As I said, I think this is in general NP-hard, so
- it's not going to be possible to state a simple algorithm.
-
- It does help to use boolean logic; i.e.,
-
- F = (a & b) | ((a | b) & ~sum)
- ; Start with the "obvious" representation of the carry
- ; function in terms of {a, b, sum}. Carry if a and b are
- ; both 1, or if at least one of them is 1 and the sum is 0.
-
- = (a & b) | ((a ^ b) & ~sum)
- ; We want to replace | operations by & and ^, which are the
- ; basic addition and multiplication of mod-2 arithmetic.
- ; This expression is equivalent to the first, because when a
- ; and b are both 1, (a & b) makes the whole expression 1, so
- ; it doesn't matter that we have changed the second term.
-
- = (a & b) ^ ((a ^ b) & ~sum)
- ; Continue replacing. The two operands of | are never both
- ; 1, so we can replace | by ^.
-
- = (a & b) ^ ((a ^ b) & (one ^ sum))
- ; ~x is the same as (one ^ x), where one is a word of all 1s.
-
- = (a & b) ^ (a & one) ^ (b & one) ^ (a & sum) ^ (b & sum)
- ; Now we can use the ordinary distributive law to expand the
- ; product fully.
-
- = (a & b) ^ a ^ b ^ (a & sum) ^ (b & sum)
- ; (x & one) is the same as x.
-
- = a ^ (a & b) ^ (sum & a) ^ (sum & b) ^ b
- ; Write the terms in a different order.
-
- = (a ^ sum) & (a ^ b) ^ b
- ; Now recognize that the first four terms can be factored
- ; into a product.
-
- Then you look for a while for a representation with three terms and fail
- to find one. So you conclude that this is the best you can do.
-
- David desJardins
-