On Thu, 18 Aug 2016, Martin Storsjö wrote:

---
Updated to check with fegetround to see which rounding mode to use.
---
mingw-w64-crt/math/llrint.c  | 11 ++++++++++-
mingw-w64-crt/math/llrintf.c | 11 ++++++++++-
2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-crt/math/llrint.c b/mingw-w64-crt/math/llrint.c
index 1fc11e8..80014b0 100644
--- a/mingw-w64-crt/math/llrint.c
+++ b/mingw-w64-crt/math/llrint.c
@@ -4,6 +4,7 @@
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */
#include <math.h>
+#include <fenv.h>

long long llrint (double x)
{
@@ -11,7 +12,15 @@ long long llrint (double x)
#if defined(_AMD64_) || defined(__x86_64__) || defined(_X86_) || 
defined(__i386__)
  __asm__ __volatile__ ("fistpll %0"  : "=m" (retval) : "t" (x) : "st");
#else
-  retval = (long long)x;
+  int mode = fegetround();
+  if (mode & FE_DOWNWARD)
+    retval = (long long)floor(x);
+  else if (mode & FE_UPWARD)
+    retval = (long long)ceil(x);
+  else if (mode & FE_TOWARDZERO)
+    retval = x >= 0 ? (long long)floor(x) : (long long)ceil(x);
+  else
+    retval = x >= 0 ? (long long)floor(x + 0.5) : (long long)ceil(x - 0.5);
#endif
  return retval;
}

Ok, the mode mask flags checks has to be revised a little, since FE_TOWARDZERO == FE_UPWARD | FE_DOWNWARD here. I'll return with a newer patch in a little while.

// Martin
------------------------------------------------------------------------------
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to