Skip to contentSkip to conversation

ARC:fpu: add extra capability check before use of sqrt and fma builtins

PATCH Superseded [email protected] 2 participants 5 messages Dec 21, 2022 – Jan 17, 2023 Synopsys ARC architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARC:fpu: add extra capability check before use of sqrt and fma builtins

From: Pavel Kozlov <[email protected]>

Add extra check for compiler definitions to ensure that compiler provides sqrt and fma hw fpu instructions else use software implementation.

As divide/sqrt and FMA hw support from CPU side is optional, the compiler can be configured by options to generate hw FPU instructions, but without use of FDDIV, FDSQRT, FSDIV, FSSQRT, FDMADD and FSMADD instructions. In this case __builtin_sqrt and __builtin_sqrtf provided by compiler can't be used inside the glibc code, as these builtins are used in implementations of sqrt() and sqrtf() functions but at the same time these builtins unfold to sqrt() and sqrtf(). So it is possible to receive code like that:

0001c4b4 <__ieee754_sqrtf>:
1c4b4: 0001 0000 b 0 ;1c4b4 <__ieee754_sqrtf>

The same is also true for __builtin_fma and __builtin_fmaf.

---
 sysdeps/arc/fpu/math-use-builtins-fma.h  | 14 ++++++++++++--
 sysdeps/arc/fpu/math-use-builtins-sqrt.h | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
sysdeps/arc/fpu/math-use-builtins-fma.h +12 -2
index eede75aa41be..082badf48201 100644
@@ -1,4 +1,14 @@
-#define USE_FMA_BUILTIN 1
-#define USE_FMAF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_FMAL_BUILTIN 0
 #define USE_FMAF128_BUILTIN 0
sysdeps/arc/fpu/math-use-builtins-sqrt.h +12 -2
index e94c915ba66a..a449bc609295 100644
@@ -1,4 +1,14 @@
-#define USE_SQRT_BUILTIN 1
-#define USE_SQRTF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_SQRTL_BUILTIN 0
 #define USE_SQRTF128_BUILTIN 0
Signature · 7 lines
2.25.1


_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
#1 lore raw
Quoted 34 lines · Pavel.Kozlov--- via Libc-alpha

From: Pavel Kozlov <[email protected]>

Add extra check for compiler definitions to ensure that compiler provides sqrt and fma hw fpu instructions else use software implementation.

As divide/sqrt and FMA hw support from CPU side is optional, the compiler can be configured by options to generate hw FPU instructions, but without use of FDDIV, FDSQRT, FSDIV, FSSQRT, FDMADD and FSMADD instructions. In this case __builtin_sqrt and __builtin_sqrtf provided by compiler can't be used inside the glibc code, as these builtins are used in implementations of sqrt() and sqrtf() functions but at the same time these builtins unfold to sqrt() and sqrtf(). So it is possible to receive code like that:

0001c4b4 <__ieee754_sqrtf>:

   1c4b4:    0001 0000      b     0         ;1c4b4 <__ieee754_sqrtf>

The same is also true for __builtin_fma and __builtin_fmaf.

---
 sysdeps/arc/fpu/math-use-builtins-fma.h  | 14 ++++++++++++--
 sysdeps/arc/fpu/math-use-builtins-sqrt.h | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
sysdeps/arc/fpu/math-use-builtins-fma.h +4 -2
index eede75aa41be..082badf48201 100644
@@ -1,4 +1,14 @@
-#define USE_FMA_BUILTIN 1
-#define USE_FMAF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0

sysdeps/arc/fpu/math-use-builtins-fma.h line 14

Quoted 10 lines
sysdeps/arc/fpu/math-use-builtins-fma.h +8 -0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_FMAL_BUILTIN 0
 #define USE_FMAF128_BUILTIN 0

This is wrong, sqrt use macro do not belong for the fma switch file.

Quoted 11 lines
sysdeps/arc/fpu/math-use-builtins-sqrt.h +4 -2
index e94c915ba66a..a449bc609295 100644
@@ -1,4 +1,14 @@
-#define USE_SQRT_BUILTIN 1
-#define USE_SQRTF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0

sysdeps/arc/fpu/math-use-builtins-sqrt.h line 14

Quoted 10 lines
sysdeps/arc/fpu/math-use-builtins-sqrt.h +8 -0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_SQRTL_BUILTIN 0
 #define USE_SQRTF128_BUILTIN 0

This is ok.

_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc

#2 lore raw
Quoted 1 line

This is wrong, sqrt use macro do not belong for the fma switch file.

Thank you for the review and your notice. I was inattentive when moving changes from the branch I had. This has been fixed in v2 of the patch [1].
I've manually checked (by objdump -d output review) that now expected code for libm is generated in different cases (when compiler provides support for extra instructions and sets macroses __ARC_FPU_DP_DIV__, __ARC_FPU_SP_DIV__, __ARC_FPU_DP_FMA__, __ARC_FPU_SP_FMA__ and when not).

[1]
http://lists.infradead.org/pipermail/linux-snps-arc/2023-January/006771.html

--
Pavel

_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc

#3 lore raw

PATCH v2 1/1 ARC:fpu: add extra capability check before use of sqrt and fma builtins

In reply to [email protected]

From: Pavel Kozlov <[email protected]>

Add extra check for compiler definitions to ensure that compiler provides sqrt and fma hw fpu instructions else use software implementation.

As divide/sqrt and FMA hw support from CPU side is optional, the compiler can be configured by options to generate hw FPU instructions, but without use of FDDIV, FDSQRT, FSDIV, FSSQRT, FDMADD and FSMADD instructions. In this case __builtin_sqrt and __builtin_sqrtf provided by compiler can't be used inside the glibc code, as these builtins are used in implementations of sqrt() and sqrtf() functions but at the same time these builtins unfold to sqrt() and sqrtf(). So it is possible to receive code like that:

0001c4b4 <__ieee754_sqrtf>:
1c4b4: 0001 0000 b 0 ;1c4b4 <__ieee754_sqrtf>

The same is also true for __builtin_fma and __builtin_fmaf.

---
Changes in v2:
- Fixed macros definitions for FMA

 sysdeps/arc/fpu/math-use-builtins-fma.h  | 14 ++++++++++++--
 sysdeps/arc/fpu/math-use-builtins-sqrt.h | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
sysdeps/arc/fpu/math-use-builtins-fma.h +12 -2
index eede75aa41be..2acd8113ce2c 100644
@@ -1,4 +1,14 @@
-#define USE_FMA_BUILTIN 1
-#define USE_FMAF_BUILTIN 1
+#if defined __ARC_FPU_DP_FMA__
+# define USE_FMA_BUILTIN 1
+#else
+# define USE_FMA_BUILTIN 0
+#endif
+
+#if defined __ARC_FPU_SP_FMA__
+# define USE_FMAF_BUILTIN 1
+#else
+# define USE_FMAF_BUILTIN 0
+#endif
+
 #define USE_FMAL_BUILTIN 0
 #define USE_FMAF128_BUILTIN 0
sysdeps/arc/fpu/math-use-builtins-sqrt.h +12 -2
index e94c915ba66a..a449bc609295 100644
@@ -1,4 +1,14 @@
-#define USE_SQRT_BUILTIN 1
-#define USE_SQRTF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_SQRTL_BUILTIN 0
 #define USE_SQRTF128_BUILTIN 0
Signature · 7 lines
2.25.1


_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
Reviewed-by on v2 1/1
#4 lore raw
Quoted 14 lines · Pavel.Kozlov--- via Libc-alpha

From: Pavel Kozlov <[email protected]>

Add extra check for compiler definitions to ensure that compiler provides sqrt and fma hw fpu instructions else use software implementation.

As divide/sqrt and FMA hw support from CPU side is optional, the compiler can be configured by options to generate hw FPU instructions, but without use of FDDIV, FDSQRT, FSDIV, FSSQRT, FDMADD and FSMADD instructions. In this case __builtin_sqrt and __builtin_sqrtf provided by compiler can't be used inside the glibc code, as these builtins are used in implementations of sqrt() and sqrtf() functions but at the same time these builtins unfold to sqrt() and sqrtf(). So it is possible to receive code like that:

Quoted 4 lines

0001c4b4 <__ieee754_sqrtf>:

   1c4b4:    0001 0000      b     0         ;1c4b4 <__ieee754_sqrtf>

The same is also true for __builtin_fma and __builtin_fmaf.

LGTM, thanks. You might need to check with Carlos O'Donnel to see it you could install this for 2.36.

Quoted 41 lines

---
Changes in v2:
- Fixed macros definitions for FMA

 sysdeps/arc/fpu/math-use-builtins-fma.h  | 14 ++++++++++++--
 sysdeps/arc/fpu/math-use-builtins-sqrt.h | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 4 deletions(-)
sysdeps/arc/fpu/math-use-builtins-fma.h +12 -2
index eede75aa41be..2acd8113ce2c 100644
@@ -1,4 +1,14 @@
-#define USE_FMA_BUILTIN 1
-#define USE_FMAF_BUILTIN 1
+#if defined __ARC_FPU_DP_FMA__
+# define USE_FMA_BUILTIN 1
+#else
+# define USE_FMA_BUILTIN 0
+#endif
+
+#if defined __ARC_FPU_SP_FMA__
+# define USE_FMAF_BUILTIN 1
+#else
+# define USE_FMAF_BUILTIN 0
+#endif
+
 #define USE_FMAL_BUILTIN 0
 #define USE_FMAF128_BUILTIN 0
sysdeps/arc/fpu/math-use-builtins-sqrt.h +4 -2
index e94c915ba66a..a449bc609295 100644
@@ -1,4 +1,14 @@
-#define USE_SQRT_BUILTIN 1
-#define USE_SQRTF_BUILTIN 1
+#if defined __ARC_FPU_DP_DIV__
+# define USE_SQRT_BUILTIN 1
+#else
+# define USE_SQRT_BUILTIN 0

sysdeps/arc/fpu/math-use-builtins-sqrt.h line 14

Quoted 10 lines
sysdeps/arc/fpu/math-use-builtins-sqrt.h +8 -0
+#endif
+
+#if defined __ARC_FPU_SP_DIV__
+# define USE_SQRTF_BUILTIN 1
+#else
+# define USE_SQRTF_BUILTIN 0
+#endif
+
 #define USE_SQRTL_BUILTIN 0
 #define USE_SQRTF128_BUILTIN 0

_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc