similarity index 61%
rename from sysdeps/i386/fpu/s_trunc.S
rename to sysdeps/i386/fpu/s_trunc.c
@@ -1,5 +1,5 @@
-/* Truncate double value.
- Copyright (C) 1997-2024 Free Software Foundation, Inc.
+/* Round to integer, toward zero. i386 version.
+ Copyright (C) 2024 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
@@ -16,22 +16,23 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
-#include <machine/asm.h>
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <fenv_private.h>
#include <libm-alias-double.h>
-ENTRY(__trunc)
- fldl 4(%esp)
- subl $32, %esp
- cfi_adjust_cfa_offset (32)
- fnstenv 4(%esp)
- movl $0xc00, %edx
- orl 4(%esp), %edx
- movl %edx, (%esp)
- fldcw (%esp)
- frndint
- fldenv 4(%esp)
- addl $32, %esp
- cfi_adjust_cfa_offset (-32)
- ret
-END(__trunc)
+double
+__trunc (double x)
+{
+ fenv_t fenv;
+ double r;
+
+ libc_feholdexcept_setround_387 (&fenv, FE_TOWARDZERO);
+ asm volatile ("frndint" : "=t" (r) : "0" (x));
+ /* Preserve "invalid" exceptions from sNaN input. */
+ fenv.__status_word |= libc_fetestexcept_387 (FE_INVALID);
+ libc_fesetenv_387 (&fenv);
+
+ return r;
+}
libm_alias_double (__trunc, trunc)
similarity index 61%
rename from sysdeps/i386/fpu/s_truncf.S
rename to sysdeps/i386/fpu/s_truncf.c
@@ -1,5 +1,5 @@
-/* Truncate float value.
- Copyright (C) 1997-2024 Free Software Foundation, Inc.
+/* Round to integer, toward zero. i386 version.
+ Copyright (C) 2024 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
@@ -16,22 +16,23 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
-#include <machine/asm.h>
+#define NO_MATH_REDIRECT
+#include <math.h>
+#include <fenv_private.h>
#include <libm-alias-float.h>
-ENTRY(__truncf)
- flds 4(%esp)
- subl $32, %esp
- cfi_adjust_cfa_offset (32)
- fnstenv 4(%esp)
- movl $0xc00, %edx
- orl 4(%esp), %edx
- movl %edx, (%esp)
- fldcw (%esp)
- frndint
- fldenv 4(%esp)
- addl $32, %esp
- cfi_adjust_cfa_offset (-32)
- ret
-END(__truncf)
+float
+__truncf (float x)
+{
+ fenv_t fenv;
+ float r;
+
+ libc_feholdexcept_setround_387 (&fenv, FE_TOWARDZERO);
+ asm volatile ("frndint" : "=t" (r) : "0" (x));
+ /* Preserve "invalid" exceptions from sNaN input. */
+ fenv.__status_word |= libc_fetestexcept_387 (FE_INVALID);
+ libc_fesetenv_387 (&fenv);
+
+ return r;
+}
libm_alias_float (__trunc, trunc)