From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,acba876b1e3c9639 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!w5g2000hsg.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: GNAT Optimization of Constant Expressions Date: 18 May 2007 15:51:27 -0700 Organization: http://groups.google.com Message-ID: <1179528686.946797.189500@w5g2000hsg.googlegroups.com> References: <1179355028.624745.258370@q75g2000hsh.googlegroups.com> <464cb4cd$1_3@news.bluewin.ch> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1179528699 5328 127.0.0.1 (18 May 2007 22:51:39 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 18 May 2007 22:51:39 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: w5g2000hsg.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news1.google.com comp.lang.ada:15848 Date: 2007-05-18T15:51:27-07:00 List-Id: On May 18, 10:40 am, "Randy Brukardt" wrote: > I'm not a numerical analysist, I only play at one when implementing > compilers... ;-) but let me give my understanding. > > If the machine does not keep extra precision between operations (that is, > the floating point registers are the same size as the in-memory > representation), and abs X is < 1.0, then > (X - 0.5) - 0.5 > will have one more bit of precision than > X - 1.0 > will. That occurs because in order to add or subtract, the smaller value has > to be shifted to match the larger value, which will cause bits to fall off > the end of the smaller value. 1.0 requires more shifting than 0.5 does. I tried it with an imaginary machine that supports only an 8-bit mantissa, so that floating-point numbers have the form 1.abcdefgh * (2**exponent). Suppose X has an exponent of -4, so that the value of X is essentially 0.0001abcdefgh. Now, when we subtract 1.0, and we line things up, the efgh bits will fall off, giving 0.0001abcd - 1.00000000 with the result being negative (0.1110a'b'c'd' + .00000001); I'm using ' to indicate the complement of a bit. To normalize the result, it needs to be shifted one bit to the left; the question is, what bit gets shifted in on the right? Is it related to "e", or is it always 0, or what? I don't know that much about the details of floating- point hardware, so I don't know the answer to this. If we subtract 0.5, then only fgh will fall off: 0.0001abcde - 0.100000000 (since there will be 8 bits of mantissa after the first 1) with the result being negative (0.0110a'b'c'd'e' + 0.000000001). As before, when we normalize this result, some other bit will be shifted in on the right. Then, since this is negative, when we subtract 0.5 again we're really adding 0.5; when we line things up, whatever bit just got shifted in, will be shifted out again: (0.0110a'b'c'd'e' + 0.000000001) + 0.10000 0 0 0 0 giving 0.1110a'b'c'd'e' + 0.000000001. So I suspect Randy is right, but I'm not sure. If the hardware keeps just one extra mantissa bit internally when it does the subtraction, and uses the extra bit when it shifts left to normalize the result, then I think the two expressions will always produce the same result. I could have easily screwed up the math in the above. Hopefully I didn't. -- Adam