From 0bcd3eeb4adbf1b8039f4f7f536d66f59ccf461f Mon Sep 17 00:00:00 2001 From: kettenis Date: Wed, 28 Apr 2021 15:38:59 +0000 Subject: [PATCH] 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@ --- lib/libc/arch/riscv64/gen/flt_rounds.c | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/libc/arch/riscv64/gen/flt_rounds.c 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 +#include + +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); -- 2.20.1