compile-time conversion of floating-point expressions to long longs

Jim Cromie jim.cromie@gmail.com
Wed May 3 16:18:00 GMT 2006



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.



More information about the Gcc-help mailing list