From: mfranz Date: Fri, 16 Jun 2006 09:29:54 +0000 (+0000) Subject: add float_to_int() rounding function from Cockpit/hud_opts.hxx. The original X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=52b8f924aacff76fa530edaaddb0407631897b74;p=simgear.git add float_to_int() rounding function from Cockpit/hud_opts.hxx. The original file said "(c) FlightGear Project" and "probably written by Norman Vine". --- diff --git a/simgear/math/fastmath.cxx b/simgear/math/fastmath.cxx index f7379bd5..41b2739d 100644 --- a/simgear/math/fastmath.cxx +++ b/simgear/math/fastmath.cxx @@ -2,7 +2,7 @@ * \file fastmath.cxx * fast mathematics routines. * - * Refferences: + * References: * * A Fast, Compact Approximation of the Exponential Function * Nicol N. Schraudolph diff --git a/simgear/math/fastmath.hxx b/simgear/math/fastmath.hxx index 97851e33..f78fddc5 100644 --- a/simgear/math/fastmath.hxx +++ b/simgear/math/fastmath.hxx @@ -112,5 +112,36 @@ inline int fast_sgn(float f) return 1+(((*(int*)&f)>>31)<<1); } + + +/** + * Quick rounding function. + */ +#if defined(i386) +#define USE_X86_ASM +#endif + +#if defined(USE_X86_ASM) +static __inline__ int float_to_int(float f) +{ + int r; + __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st"); + return r; +} +#elif defined(__MSC__) && defined(__WIN32__) +static __inline int float_to_int(float f) +{ + int r; + _asm { + fld f + fistp r + } + return r; +} +#else +#define float_to_int(F) ((int) ((F) < 0.0f ? (F)-0.5f : (F)+0.5f)) +#endif + + #endif // !_SG_FMATH_HXX