libm/newlib: disable optimization for sincos

Disable sincos optimization for all functions in this file,
otherwise gcc would generate infinite calls.
Refer to gcc bug 46926.
-fno-builtin-sin or -fno-builtin-cos can disable sincos optimization,
but these two options do not work inside optimize pragma in-file.
Thus we just enforce -O0 when compiling this file.

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
This commit is contained in:
p-szafonimateusz 2024-05-24 09:25:26 +02:00 committed by Xiang Xiao
parent 23b2f96c9d
commit c932fe3045
4 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,73 @@
From b88abc9c31c450c44c384feaf2e2653c8c4c69e4 Mon Sep 17 00:00:00 2001
From: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
Date: Fri, 24 May 2024 09:19:03 +0200
Subject: [PATCH] newlib: disable optmisation for sincos
Change-Id: Ie571e357485384655f67cdc9af2be1c60cacfeee
Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
---
newlib/libm/math/w_sincos.c | 9 +++++++++
newlib/libm/math/wf_sincos.c | 10 ++++++++++
2 files changed, 19 insertions(+)
diff --git a/newlib/newlib/newlib/libm/math/w_sincos.c newlib/newlib/newlib/libm/math/w_sincos.c
index 491efa418..6f85c27ab 100644
--- a/newlib/newlib/newlib/libm/math/w_sincos.c
+++ newlib/newlib/newlib/libm/math/w_sincos.c
@@ -4,8 +4,19 @@
#include "fdlibm.h"
#include <errno.h>
+/* Disable sincos optimization for all functions in this file,
+ * otherwise gcc would generate infinite calls.
+ * Refer to gcc PR46926.
+ * -fno-builtin-sin or -fno-builtin-cos can disable sincos optimization,
+ * but these two options do not work inside optimize pragma in-file.
+ * Thus we just enforce -O0 when compiling this file.
+ */
+
#ifndef _DOUBLE_IS_32BITS
+#ifdef __GNUC__
+__attribute__((optimize("O0")))
+#endif
#ifdef __STDC__
void sincos(double x, double *sinx, double *cosx)
#else
diff --git a/newlib/newlib/newlib/libm/math/wf_sincos.c newlib/newlib/newlib/libm/math/wf_sincos.c
index 69eb922c9..2e9b5ca62 100644
--- a/newlib/newlib/newlib/libm/math/wf_sincos.c
+++ newlib/newlib/newlib/libm/math/wf_sincos.c
@@ -3,8 +3,19 @@
#include "fdlibm.h"
#if __OBSOLETE_MATH
+/* Disable sincos optimization for all functions in this file,
+ * otherwise gcc would generate infinite calls.
+ * Refer to gcc PR46926.
+ * -fno-builtin-sin or -fno-builtin-cos can disable sincos optimization,
+ * but these two options do not work inside optimize pragma in-file.
+ * Thus we just enforce -O0 when compiling this file.
+ */
+
#include <errno.h>
+#ifdef __GNUC__
+__attribute__((optimize("O0")))
+#endif
#ifdef __STDC__
void sincosf(float x, float *sinx, float *cosx)
#else
@@ -20,6 +29,9 @@
#ifdef _DOUBLE_IS_32BITS
+#ifdef __GNUC__
+__attribute__((optimize("O0")))
+#endif
#ifdef __STDC__
void sincos(double x, double *sinx, double *cosx)
#else
--
2.44.0

View File

@ -42,7 +42,10 @@ if(CONFIG_LIBM_NEWLIB)
&& patch -p1 -d ${CMAKE_CURRENT_LIST_DIR} <
${CMAKE_CURRENT_LIST_DIR}/0002-newlib-libm-remove-include-reent.h.patch
&& patch -p1 -d ${CMAKE_CURRENT_LIST_DIR} <
${CMAKE_CURRENT_LIST_DIR}/0003-newlib-fix-compilation-for-x86.patch)
${CMAKE_CURRENT_LIST_DIR}/0003-newlib-fix-compilation-for-x86.patch &&
patch -p1 -d ${CMAKE_CURRENT_LIST_DIR} <
${CMAKE_CURRENT_LIST_DIR}/0004-newlib-disable-optmisation-for-sincos.patch
)
FetchContent_GetProperties(newlib_fetch)

View File

@ -40,6 +40,7 @@ newlib/newlib: $(NEWLIB_BASENAME)-$(NEWLIB_VERSION).tar.gz
$(Q) patch -p0 < newlib/0001-newlib-libm-fix-__RCSID-build-error.patch
$(Q) patch -p0 < newlib/0002-newlib-libm-remove-include-reent.h.patch
$(Q) patch -p0 < newlib/0003-newlib-fix-compilation-for-x86.patch
$(Q) patch -p0 < newlib/0004-newlib-disable-optmisation-for-sincos.patch
$(Q) touch $@
endif

View File

@ -24,10 +24,19 @@
#include <math.h>
/* Disable sincos optimization for all functions in this file,
* otherwise gcc would generate infinite calls.
* Refer to gcc PR46926.
* -fno-builtin-sin or -fno-builtin-cos can disable sincos optimization,
* but these two options do not work inside optimize pragma in-file.
* Thus we just enforce -O0 when compiling this file.
*/
/****************************************************************************
* Public Functions
****************************************************************************/
nooptimiziation_function
void sincosl(long double x, long double *s, long double *c)
{
*s = sinl(x);