From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2668 invoked by alias); 4 Mar 2011 15:43:03 -0000 Received: (qmail 2610 invoked by uid 22791); 4 Mar 2011 15:43:01 -0000 X-SWARE-Spam-Status: No, hits=-2.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 04 Mar 2011 15:42:57 +0000 From: "joseph at codesourcery dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/47990] Missed promotion of double precision constants to single precision for -funsafe-math-optimizations X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: joseph at codesourcery dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Date: Fri, 04 Mar 2011 15:43:00 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2011-03/txt/msg00414.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47990 --- Comment #1 from joseph at codesourcery dot com 2011-03-04 15:42:39 UTC --- On Fri, 4 Mar 2011, rguenth at gcc dot gnu.org wrote: > In 482.sphinx3 we have code like > > float foo (float x, float y) > { > return ((int)(x/y + 0.5)) * y; > } > > where the addition of 0.5 is performed in double precision. With > -funsafe-math-optimizations we can demote 0.5 to single precision > (its exactly representable) also because the result of the addition > does not take part of further floating point computation but is > immediately converted to int. > > The unsafe part of this optimization occurs when x/y is FLT_MAX > and we'd truncate to a 64bit integer type. For 32bit integers > it would probably be safe to do this optimization unconditionally. No, that's not safe unconditionally; consider x/y == 0x800001p0f, for example (so adding 0.5f and converting to float results in rounding up to the next integer in the default round-to-nearest mode, whereas conversion from floating point to integer in C always rounds towards zero).