[libc-commits] [libc] [libc][NFC] refactor fmin and fmax (PR #86718)

Job Henandez Lara via libc-commits libc-commits at lists.llvm.org
Tue Mar 26 13:18:16 PDT 2024


https://github.com/Jobhdez updated https://github.com/llvm/llvm-project/pull/86718

>From 5af9f466e79415d183ba4845a421419b9002a530 Mon Sep 17 00:00:00 2001
From: Job Henandez Lara <hj93 at protonmail.com>
Date: Tue, 26 Mar 2024 12:22:26 -0700
Subject: [PATCH 1/3] refactor fmin and fmax

Hello,

So, I worked on the fmaximum and fminimum functions recently and the reviewers suggested the structure:

```
if (bitsx ...)
  return ...;
if (bitsy ..)
  return
...
return ...;
```
So I went ahead and did the same for fmin and fmax. I hope this isnt an issue for you all. thanks.
---
 libc/src/__support/FPUtil/BasicOperations.h | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 405755f8b57d9b..917b5ebc22be26 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -30,36 +30,32 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T fmin(T x, T y) {
   const FPBits<T> bitx(x), bity(y);
 
-  if (bitx.is_nan()) {
+  if (bitx.is_nan()) 
     return y;
-  } else if (bity.is_nan()) {
+  if (bity.is_nan()) 
     return x;
-  } else if (bitx.sign() != bity.sign()) {
+  if (bitx.sign() != bity.sign()) 
     // To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
     // y has different signs and both are not NaNs, we return the number
     // with negative sign.
     return (bitx.is_neg()) ? x : y;
-  } else {
-    return (x < y ? x : y);
-  }
+  return (x < y ? x : y);
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T fmax(T x, T y) {
   FPBits<T> bitx(x), bity(y);
 
-  if (bitx.is_nan()) {
+  if (bitx.is_nan()) 
     return y;
-  } else if (bity.is_nan()) {
+  if (bity.is_nan()) 
     return x;
-  } else if (bitx.sign() != bity.sign()) {
+  if (bitx.sign() != bity.sign()) 
     // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
     // y has different signs and both are not NaNs, we return the number
     // with positive sign.
     return (bitx.is_neg() ? y : x);
-  } else {
-    return (x > y ? x : y);
-  }
+  return (x > y ? x : y);
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>

>From 185b49c9a29e847ee1b8e19b3ae95686c94adec1 Mon Sep 17 00:00:00 2001
From: Job Hernandez <h93 at protonmail.com>
Date: Tue, 26 Mar 2024 12:32:57 -0700
Subject: [PATCH 2/3] format code

---
 libc/src/__support/FPUtil/BasicOperations.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index 917b5ebc22be26..b93796d6cd32d9 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -30,11 +30,11 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T fmin(T x, T y) {
   const FPBits<T> bitx(x), bity(y);
 
-  if (bitx.is_nan()) 
+  if (bitx.is_nan())
     return y;
-  if (bity.is_nan()) 
+  if (bity.is_nan())
     return x;
-  if (bitx.sign() != bity.sign()) 
+  if (bitx.sign() != bity.sign())
     // To make sure that fmin(+0, -0) == -0 == fmin(-0, +0), whenever x and
     // y has different signs and both are not NaNs, we return the number
     // with negative sign.
@@ -46,11 +46,11 @@ template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
 LIBC_INLINE T fmax(T x, T y) {
   FPBits<T> bitx(x), bity(y);
 
-  if (bitx.is_nan()) 
+  if (bitx.is_nan())
     return y;
-  if (bity.is_nan()) 
+  if (bity.is_nan())
     return x;
-  if (bitx.sign() != bity.sign()) 
+  if (bitx.sign() != bity.sign())
     // To make sure that fmax(+0, -0) == +0 == fmax(-0, +0), whenever x and
     // y has different signs and both are not NaNs, we return the number
     // with positive sign.

>From 3257d9bdcce10dcd36f2bdbb118e9ed4d010e071 Mon Sep 17 00:00:00 2001
From: Job Hernandez <h93 at protonmail.com>
Date: Tue, 26 Mar 2024 13:18:04 -0700
Subject: [PATCH 3/3] address review

---
 libc/src/__support/FPUtil/BasicOperations.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/FPUtil/BasicOperations.h b/libc/src/__support/FPUtil/BasicOperations.h
index b93796d6cd32d9..c5809cdf456fec 100644
--- a/libc/src/__support/FPUtil/BasicOperations.h
+++ b/libc/src/__support/FPUtil/BasicOperations.h
@@ -39,7 +39,7 @@ LIBC_INLINE T fmin(T x, T y) {
     // y has different signs and both are not NaNs, we return the number
     // with negative sign.
     return (bitx.is_neg()) ? x : y;
-  return (x < y ? x : y);
+  return x < y ? x : y;
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
@@ -55,7 +55,7 @@ LIBC_INLINE T fmax(T x, T y) {
     // y has different signs and both are not NaNs, we return the number
     // with positive sign.
     return (bitx.is_neg() ? y : x);
-  return (x > y ? x : y);
+  return x > y ? x : y;
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>



More information about the libc-commits mailing list