From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12731 invoked by alias); 8 Aug 2008 15:01:44 -0000 Received: (qmail 12722 invoked by uid 22791); 8 Aug 2008 15:01:43 -0000 X-Spam-Check-By: sourceware.org Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 08 Aug 2008 15:01:04 +0000 X-IronPort-AV: E=Sophos;i="4.31,327,1215381600"; d="scan'208";a="13832879" Received: from stedding.loria.fr ([152.81.8.164]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 08 Aug 2008 17:01:01 +0200 Received: from glisse (helo=localhost) by stedding.loria.fr with local-esmtp (Exim 4.69) (envelope-from ) id 1KRTSf-0008Be-N9 for gcc-help@gcc.gnu.org; Fri, 08 Aug 2008 17:01:01 +0200 Date: Fri, 08 Aug 2008 15:25:00 -0000 From: Marc Glisse To: gcc-help@gcc.gnu.org Subject: Compile-time computations Message-ID: User-Agent: Alpine 1.10 (DEB 962 2008-03-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII 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: 2008-08/txt/msg00074.txt.bz2 Hello, for builtin types, the compiler can optimize some functions when it detects that an argument is a compile-time constant. For instance it can turn a multiplication (or division) of an unsigned int by a power of 2 into a shift. If I create my own type (I am doing C++, but it would be similar in C), I lose all these optimizations. Gcc provides __builtin_constant_p which in many cases (assuming at least -O1) can detect compile-time constants. Knowing it, I would like to perform some computation at compile-time that will make the evaluation fast. For instance, if I detect a multiplication by a power of 2, I need to compute the log2 of this number to pass it as an argument to shift. And I really want the computation of the log2 to happen at compile-time, otherwise this branch becomes slower than the regular multiplication. I can compute the log2 and ask gcc whether the result is a compile-time constant, so I am safe. But in practice, the operations that carry constants are fairly limited. In order to compile the log2, I have to use a switch statement with a list of all possible powers of 2, because a for/while loop gives a result that is not considered a constant (for log2 in particular, probably as a result of the gmp-mpfr integration, (int)(log2(5)) is considered a constant by the latest gcc version, but that is really a very special case). If I do modular arithmetic and want to compute the inverse of a number to replace a division by a multiplication, it essentially means computing a gcd and I can't do it without a loop or recursion (or do I really have to unroll it by hand to a large depth?). Is there a way to tell gcc that it would be _really_ nice if it could evaluate some function at compile-time, even if that means, as in the case of a log2 computation, going through a loop up to 64 times? Note that I could have a type static_int and use C++ template meta-programming to perform all the compile-time computations I want (log2 and gcd are trivial, sqrt is already a bit more fun), but it means I have to write n/static_int<2>() instead of n/2 to halve a number, and I want to avoid that. Besides, there are other overloading issues with this technique. Thank you for any hint, -- Marc Glisse