summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorMark Kettenis <kettenis@cvs.openbsd.org>2021-04-28 15:39:00 +0000
committerMark Kettenis <kettenis@cvs.openbsd.org>2021-04-28 15:39:00 +0000
commit3b4fec21b230aca43d97aa17f27d315aa8c09b3b (patch)
tree76941a2bf337b7cc490e7649f0858ab9e1605658 /lib/libc
parent619a2dcfa1a98c1f8ec844dbb61d0174971cb56d (diff)
Implement __flt_rounds() for RISC-V. RISC-V is "interesting" since it
implements a variation on the traditional "to nearest" rounding mode that rounds away from zero when tied. The upcoming C2x includes support for that and LLVM already implements this so provide an implementation that matches our system compiler. ok drahn@
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/arch/riscv64/gen/flt_rounds.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/libc/arch/riscv64/gen/flt_rounds.c b/lib/libc/arch/riscv64/gen/flt_rounds.c
new file mode 100644
index 00000000000..b89de97487c
--- /dev/null
+++ b/lib/libc/arch/riscv64/gen/flt_rounds.c
@@ -0,0 +1,30 @@
+/* $OpenBSD: flt_rounds.c,v 1.1 2021/04/28 15:38:59 kettenis Exp $ */
+
+/*
+ * Written by Mark Kettenis based on the hppa version written by
+ * Miodrag Vallat. Public domain.
+ */
+
+#include <sys/types.h>
+#include <float.h>
+
+static const int map[] = {
+ 1, /* round to nearest, ties to even */
+ 0, /* round to zero */
+ 3, /* round to negative infinity */
+ 2, /* round to positive infinity */
+ 4, /* round to nearest, ties away from zero */
+ -1, /* invalid */
+ -1, /* invalid */
+ -1 /* invalid */
+};
+
+int
+__flt_rounds(void)
+{
+ uint32_t frm;
+
+ __asm volatile ("frrm %0" : "=r"(frm));
+ return map[frm];
+}
+DEF_STRONG(__flt_rounds);