From: Jim Cromie <jim.cromie@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: compile-time conversion of floating-point expressions to long longs
Date: Wed, 03 May 2006 16:18:00 -0000 [thread overview]
Message-ID: <4458D7A2.8060504@gmail.com> (raw)
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.
next reply other threads:[~2006-05-03 16:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-05-03 16:18 Jim Cromie [this message]
2006-05-03 22:57 Nelson H. F. Beebe
2006-05-07 17:42 ` Jim Cromie
2006-05-07 19:47 ` Jim Cromie
2006-05-08 11:53 Nelson H. F. Beebe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4458D7A2.8060504@gmail.com \
--to=jim.cromie@gmail.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).