[PATCH 02/27] Input: Add round_towards_zero helper

Daniel Stone daniel at fooishbar.org
Fri Jun 3 09:14:33 PDT 2011


Hi,

On Fri, Jun 03, 2011 at 06:04:34PM +0200, walter harms wrote:
> C99 defines FE_TOWARDZERO, so any compiler that support c99 should work
> with the example below.

Well ... there aren't many of those. :)

I had started with fesetround, but there were a couple of concerns:
    - code density
    - potentially missing a spot: you'd have to make sure every user was
      correct, rather than just doing it in one place
    - performance?
    - thread safety: if we ever merge the input thread, then doing
      fesetround() under people might make them deeply, deeply unhappy
      and result in bizzare, impossible-to-debug errors.  Is
      fesetround() thread-safe? Who knows, the manpage says nothing
      about threads.

> ntl: i guess you code will work also with older compilers

See corrections in your code, which I think illustrates my point
somewhat ...

> #include <stdio.h>
> #include <fenv.h>
> #include <math.h>
> 
> /* gcc fesetround.c -lm -std=c99 */
> int main()
> {
>   double ii;
    int old_rounding = fegetround();
> 
>   fesetround(FE_TOWARDZERO);
> 
>   for(ii=-2.0;ii<2.0;ii+=0.2)
>        printf("%f,nextint=%f\n",ii,nearbyint(ii));

    fesetround(old_rounding);
> 
>   return 0;
> }

Cheers,
Daniel


More information about the xorg-devel mailing list