From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18622 invoked by alias); 2 Nov 2011 04:31:57 -0000 Received: (qmail 18612 invoked by uid 22791); 2 Nov 2011 04:31:56 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-qw0-f47.google.com (HELO mail-qw0-f47.google.com) (209.85.216.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Nov 2011 04:31:43 +0000 Received: by qam2 with SMTP id 2so7132838qam.20 for ; Tue, 01 Nov 2011 21:31:42 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.19.131 with SMTP id a3mr343464qcb.24.1320208302323; Tue, 01 Nov 2011 21:31:42 -0700 (PDT) Received: by 10.229.251.195 with HTTP; Tue, 1 Nov 2011 21:31:42 -0700 (PDT) Date: Wed, 02 Nov 2011 04:31:00 -0000 Message-ID: Subject: Compile-time floating-point expressions and subsequent detection of possible overflows etc -- during the compile-time stage. From: leon zadorin To: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-11/txt/msg00011.txt.bz2 Under certain conditions GCC is able to perform compile-time folding of floating point expressions. This is great. Sometimes, however, the resulting (or indeed intermediate) expressions may evaluate to a non-finite value (e.g. NaN, Inf) -- at compile-time. During a traditional run-time execution of floating point arithmetic there are various ways to detect the case of values going into something like an overflow (individual guarding of each possibly-violating expression via 'isfinite()'; or via reading the FPU status register, which hardware itself may flag appropriately; or via the 'fetestexcept' et al wrappers). I was wondering if there was a way to ask GCC to optionally emit some kind of a diagnostic (e.g. a warning of some kind) when it does floating point calculations at compile-time and when such compile-time calculations yield in a non-finite number. For example in an oversimplified case like this: float const x(::std::numeric_limits::max()); float const y(x * 2); It would be nice for GCC to emit a warning if it ended up setting 'y' to 'Inf'... or similar. Testing for such compile-time results during runtime would always be less efficient (and at times, when not using individual expression-guarding but rather FPU register reading, not possible...) Forcing to always individually-guard may also be extremely defficient (e.g. if 99% of cases are not overflowing then, statistically, it may not be the best way of doing this). Expecting for the compile-time 'Inf', or similar, to be propagated eventually to some runtime-detectable result is also not a solution as comparative (e.g. x < y) math will not work either and not be detected (as 'trigger' for y to overflow had happened at compile-time). Best regards Leon.