first commit of the new libm!
[musl] / src / math / nearbyint.c
diff --git a/src/math/nearbyint.c b/src/math/nearbyint.c
new file mode 100644 (file)
index 0000000..781769f
--- /dev/null
@@ -0,0 +1,20 @@
+#include <fenv.h>
+#include "libm.h"
+
+/*
+rint may raise inexact (and it should not alter the fenv otherwise)
+nearbyint must not raise inexact
+
+(according to ieee754r section 7.9 both functions should raise invalid
+when the input is signaling nan, but c99 does not define snan so saving
+and restoring the entire fenv should be fine)
+*/
+
+double nearbyint(double x) {
+       fenv_t e;
+
+       fegetenv(&e);
+       x = rint(x);
+       fesetenv(&e);
+       return x;
+}