#include #include #ifdef __MINGW32__ #include #endif #define NTESTS 24 static const float posnums[NTESTS] = { 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 98.0, 98.5, 99.0, 99.5, 100.0, 100.5, 101.0, 101.5 }; #ifndef __MINGW32__ #define FE_TONEAREST 0x0000 #define FE_DOWNWARD 0x0400 #define FE_UPWARD 0x0800 #define FE_TOWARDZERO 0x0c00 /* 7.6.3.2 The fesetround function establishes the rounding direction represented by its argument round. If the argument is not equal to the value of a rounding direction macro, the rounding direction is not changed. */ int fesetround (int mode) { unsigned short _cw; if ((mode & ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO)) != 0) return -1; __asm__ volatile ("fnstcw %0;": "=m" (_cw)); _cw &= ~(FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO); _cw |= mode; __asm__ volatile ("fldcw %0;" : : "m" (_cw)); return 0; } /* 7.6.3.1 The fegetround function returns the value of the rounding direction macro representing the current rounding direction. */ int fegetround (void) { unsigned short _cw; __asm__ ("fnstcw %0;" : "=m" (_cw)); return _cw & (FE_TONEAREST | FE_DOWNWARD | FE_UPWARD | FE_TOWARDZERO); } #endif int main (int argc, const char **argv) { float a, b; double d, e; int n; if (argc >= 2) { int nrm, n; n = sscanf (argv[1], "%i", &nrm); if (n == 1) switch (nrm) { case 0: case 0x400: case 0x800: case 0xc00: fesetround (nrm); } } printf ("Rmode $%08x\n", fegetround ()); for (n = 0; n < NTESTS; ++n) { a = d = posnums[n]; b = e = -posnums[n]; printf ("%g %g %g %g\n", a, b, d, e); a = lrintf (a); b = lrintf (b); d = lrint (d); e = lrint (e); printf ("%g %g %g %g\n", a, b, d, e); } return 0; }