Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!209.197.12.242.MISMATCH!nx01.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!news1.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.posted.palinacquisition!news.posted.palinacquisition.POSTED!not-for-mail NNTP-Posting-Date: Fri, 08 Jul 2011 21:49:11 -0500 Date: Fri, 08 Jul 2011 19:49:11 -0700 From: Peter Duniho User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Arithmetic overflow checking References: <015aeb15-57db-48ab-9cd4-77f8448b632f@w24g2000yqw.googlegroups.com> <2rydnez7l-H5BYnTnZ2dnUVZ_vGdnZ2d@earthlink.com> <9LWdnZH2hdfmyYvTnZ2dnUVZ_vidnZ2d@posted.palinacquisition> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 65 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 50.46.118.188 X-Trace: sv3-tJ09jdzkd2uNnf7DkETlqNjeB9q83orXwINJj279jxGkrhdcvZ7VJUNmCspmPw6YCVkfVq730axKyus!nH0Nmps9VNDEm2kTONh259oGTYDsqhGfZ7EQ5SgwaKgm5/eI1G3E6X0W1yIWPojToSeD57Odv1Gf!1bNL6ohsNm7QjbEcn3YVTGLwpeEhOsoIoea8zlgBXHw= X-Complaints-To: abuse@iinet.com X-DMCA-Complaints-To: abuse@iinet.com X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3766 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6005 On 7/8/11 6:17 PM, markspace wrote: > On 7/8/2011 5:40 PM, Eric Sosman wrote: > >>> Not a single instruction to AddWithTrap, but a it does have a global >>> state register, and a test/branch instrution, so you just pair up an ADD >>> followed by a JO (Jump if Overflow) and that's it. Two easy instructions >>> paired together, and it works the same for MUL and SUB too. (DIV can't >>> overflow; think about it). >> >> I Am Curious, Ultraviolet: Even for Integer.MIN_VALUE / -1? > > > Hmm, that was the description I read in the docs, or thought I read. I'm > not really sure what the hardware will do in that case. One way to find > out would be to try it. :) It had better trigger the same overflow as for other arithmetic operations. And in C# (which is using the hardware features), it does: using System; namespace TestDivOverflow { class Program { static void Main(string[] args) { try { checked { int i = int.MinValue, j = i / -1; } } catch (Exception e) { Console.WriteLine(e.Message); } } } } Output: Arithmetic operation resulted in an overflow. Note that .NET is taking advantage of the hardware exception; it doesn't have to check the overflow bit, because the CPU generates an interrupt when the overflow occurs, when set up properly. Here's the (unoptimized) assembly for the line of code in the "checked" block: 00000041 mov dword ptr [ebp-40h],80000000h 00000048 mov eax,dword ptr [ebp-40h] 0000004b or ecx,0FFFFFFFFh 0000004e cdq 0000004f idiv eax,ecx 00000051 mov dword ptr [ebp-44h],eax Java _could_ do the same, but of course you'd have to, one way or the other, wrap "checked" areas of code with the toggle of the interrupt-trapping so that only those areas of code you want to generate the overflow check actually get it. Pete