From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29054 invoked by alias); 3 May 2006 16:18:02 -0000 Received: (qmail 29039 invoked by uid 22791); 3 May 2006 16:18:01 -0000 X-Spam-Check-By: sourceware.org Received: from wr-out-0506.google.com (HELO wr-out-0506.google.com) (64.233.184.236) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 03 May 2006 16:17:57 +0000 Received: by wr-out-0506.google.com with SMTP id 68so164527wri for ; Wed, 03 May 2006 09:17:56 -0700 (PDT) Received: by 10.54.122.10 with SMTP id u10mr2009555wrc; Wed, 03 May 2006 09:17:56 -0700 (PDT) Received: from ?192.168.2.51? ( [141.157.202.6]) by mx.gmail.com with ESMTP id 6sm683903wrh.2006.05.03.09.17.50; Wed, 03 May 2006 09:17:55 -0700 (PDT) Message-ID: <4458D7A2.8060504@gmail.com> Date: Wed, 03 May 2006 16:18:00 -0000 From: Jim Cromie User-Agent: Thunderbird 1.5.0.2 (X11/20060420) MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: compile-time conversion of floating-point expressions to long longs Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-05/txt/msg00039.txt.bz2 Im working on a program that cannot use floating-point, so it must do shift and integer multiply, with the 2 constants being determined by the need to maintain maximum precision. Id like to bury the math in macros, and to have it all computed at compile-time, and reduced to constants. GCC only solutions are fine. The obvious way apparently involves floating point.. (all those f* instructions - pun not intended) int main(int c, char** v) { float f = 3.14159 * 2 * 100; long long run = (long long) f; printf("float %g cast-to-long %lld \n", f, run); } $ fp-comp float 628.318 cast-to-long 628 int main(int c, char** v) { 80483b4: 55 push %ebp 80483b5: 89 e5 mov %esp,%ebp 80483b7: 83 ec 18 sub $0x18,%esp 80483ba: 83 e4 f0 and $0xfffffff0,%esp 80483bd: b8 00 00 00 00 mov $0x0,%eax 80483c2: 83 c0 0f add $0xf,%eax 80483c5: 83 c0 0f add $0xf,%eax 80483c8: c1 e8 04 shr $0x4,%eax 80483cb: c1 e0 04 shl $0x4,%eax 80483ce: 29 c4 sub %eax,%esp float f = 3.14159 * 2 * 100; 80483d0: b8 5a 14 1d 44 mov $0x441d145a,%eax 80483d5: 89 45 f4 mov %eax,0xfffffff4(%ebp) long long run = (long long) f; 80483d8: d9 45 f4 flds 0xfffffff4(%ebp) 80483db: d9 7d ee fnstcw 0xffffffee(%ebp) 80483de: 66 8b 45 ee mov 0xffffffee(%ebp),%ax 80483e2: b4 0c mov $0xc,%ah 80483e4: 66 89 45 ec mov %ax,0xffffffec(%ebp) 80483e8: d9 6d ec fldcw 0xffffffec(%ebp) 80483eb: df 7d f8 fistpll 0xfffffff8(%ebp) 80483ee: d9 6d ee fldcw 0xffffffee(%ebp) The actual conversion Im after is cycles -> nanoseconds, where cycles is pulled from a Time Stamp Counter, or similar. Are there any macros that can tear into a floating point number and pull out the exponent and mantissa ? Arch-specific is ok, as long as they exist.