agora inbox for pgsql-hackers@postgresql.org  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
Subject: [PATCH v1 1/3] Refactor dsqrt().
Date: Sun, 19 Mar 2023 14:37:54 -0700

This moves most of the logic for dsqrt() to a helper function in
float.h so that it can be reused elsewhere.
---
 src/backend/utils/adt/float.c | 16 +---------------
 src/include/utils/float.h     | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 9b51da2382..4f655c281a 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -1445,21 +1445,7 @@ dtrunc(PG_FUNCTION_ARGS)
 Datum
 dsqrt(PG_FUNCTION_ARGS)
 {
-	float8		arg1 = PG_GETARG_FLOAT8(0);
-	float8		result;
-
-	if (arg1 < 0)
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
-				 errmsg("cannot take square root of a negative number")));
-
-	result = sqrt(arg1);
-	if (unlikely(isinf(result)) && !isinf(arg1))
-		float_overflow_error();
-	if (unlikely(result == 0.0) && arg1 != 0.0)
-		float_underflow_error();
-
-	PG_RETURN_FLOAT8(result);
+	PG_RETURN_FLOAT8(float8_sqrt(PG_GETARG_FLOAT8(0)));
 }
 
 
diff --git a/src/include/utils/float.h b/src/include/utils/float.h
index 7529899d63..b4e50cccaf 100644
--- a/src/include/utils/float.h
+++ b/src/include/utils/float.h
@@ -250,6 +250,25 @@ float8_div(const float8 val1, const float8 val2)
 	return result;
 }
 
+static inline float8
+float8_sqrt(const float8 val)
+{
+	float8		result;
+
+	if (unlikely(val < 0))
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
+				 errmsg("cannot take square root of a negative number")));
+
+	result = sqrt(val);
+	if (unlikely(isinf(result)) && !isinf(val))
+		float_overflow_error();
+	if (unlikely(result == 0.0) && val != 0.0)
+		float_underflow_error();
+
+	return result;
+}
+
 /*
  * Routines for NaN-aware comparisons
  *
-- 
2.25.1


--cWoXeonUoKmBZSoM
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
	filename="v1-0002-Introduce-basic-vector-support.patch"



view thread (2+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com
  Subject: Re: [PATCH v1 1/3] Refactor dsqrt().
  In-Reply-To: <no-message-id-630344@localhost>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox