summaryrefslogtreecommitdiff
path: root/sysdeps/riscv/rv64/rvd/s_trunc.c
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2018-12-27 19:51:23 +0000
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2018-12-27 19:51:23 +0000
commit9a47d57c4784163dcf838c7461c53de29ca7a61f (patch)
tree80c130f3da27db25996b0a5f93270df554dcd8b4 /sysdeps/riscv/rv64/rvd/s_trunc.c
parente0b77a51a258fc7554aeb194ac6b219ee3078f0d (diff)
parent82dd75a7f436a19047325d62182590c9f9e23a78 (diff)
Merge branch 't/tls' into refs/top-bases/tschwinge/Roger_Whittaker
Diffstat (limited to 'sysdeps/riscv/rv64/rvd/s_trunc.c')
-rw-r--r--sysdeps/riscv/rv64/rvd/s_trunc.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/sysdeps/riscv/rv64/rvd/s_trunc.c b/sysdeps/riscv/rv64/rvd/s_trunc.c
new file mode 100644
index 0000000000..a0f78e6cd1
--- /dev/null
+++ b/sysdeps/riscv/rv64/rvd/s_trunc.c
@@ -0,0 +1,52 @@
+/* trunc(). RISC-V version.
+ Copyright (C) 2017-2018 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library. If not, see
+ <http://www.gnu.org/licenses/>. */
+
+#include <math.h>
+#include <math_private.h>
+#include <libm-alias-double.h>
+#include <stdint.h>
+
+double
+__trunc (double x)
+{
+ int flags = riscv_getflags ();
+ bool nan = isnan (x);
+ double mag = fabs (x);
+
+ if (nan)
+ return x + x;
+
+ if (mag < (1ULL << __DBL_MANT_DIG__))
+ {
+ int64_t i;
+ double new_x;
+
+ asm volatile ("fcvt.l.d %0, %1, rtz" : "=r" (i) : "f" (x));
+ asm volatile ("fcvt.d.l %0, %1, rtz" : "=f" (new_x) : "r" (i));
+
+ /* trunc(-0) == -0, and in general we'll always have the same
+ sign as our input. */
+ x = copysign (new_x, x);
+
+ riscv_setflags (flags);
+ }
+
+ return x;
+}
+
+libm_alias_double (__trunc, trunc)