From 7330548cc5b5ebdbe5c8fa515f1eb2eebfc7f2c3 Mon Sep 17 00:00:00 2001
From: Chiranmoy Bhattacharya <chiranmoy.bhattacharya@fujitsu.com>
Date: Thu, 4 Sep 2025 15:33:47 +0530
Subject: [PATCH v6 2/3] SVE support for hex coding

---
 config/c-compiler.m4                   |  85 +++++++++
 configure                              | 104 +++++++++++
 configure.ac                           |   9 +
 meson.build                            |  81 +++++++++
 src/backend/utils/adt/encode_aarch64.c | 231 ++++++++++++++++++++++++-
 src/include/pg_config.h.in             |   3 +
 src/include/utils/builtins.h           |  10 +-
 7 files changed, 520 insertions(+), 3 deletions(-)

diff --git a/config/c-compiler.m4 b/config/c-compiler.m4
index da40bd6a647..73d12826698 100644
--- a/config/c-compiler.m4
+++ b/config/c-compiler.m4
@@ -798,3 +798,88 @@ if test x"$Ac_cachevar" = x"yes"; then
 fi
 undefine([Ac_cachevar])dnl
 ])# PGAC_SVE_POPCNT_INTRINSICS
+
+# PGAC_ARM_SVE_HEX_INTRINSICS
+# ------------------------------
+# Check if the compiler supports the SVE intrinsic required for hex coding:
+# svsub_x, svcmplt, svsel, svcmpgt, svtbl, svlsr_x, svand_z, svcreate2,
+# svptest_any, svnot_z, svorr_z, svcntb, svld1, svwhilelt_b8, svst2, svld2,
+# svget2, svst1 and svlsl_x.
+#
+# If the intrinsics are supported, sets pgac_arm_sve_hex_intrinsics.
+AC_DEFUN([PGAC_ARM_SVE_HEX_INTRINSICS],
+[define([Ac_cachevar], [AS_TR_SH([pgac_cv_arm_sve_hex_intrinsics])])dnl
+AC_CACHE_CHECK([for svtbl, svlsr_x, svand_z, svcreate2, etc], [Ac_cachevar],
+[AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <arm_sve.h>
+
+    char input@<:@64@:>@;
+    char output@<:@128@:>@;
+
+    #if defined(__has_attribute) && __has_attribute (target)
+        __attribute__((target("arch=armv8-a+sve")))
+    #endif
+    int get_hex_sve(svbool_t pred, svuint8_t vec, svuint8_t *res)
+    {
+      svuint8_t	digit = svsub_x(pred, vec, 48),
+                upper = svsub_x(pred, vec, 55),
+                lower = svsub_x(pred, vec, 87);
+      svbool_t	valid_digit = svcmplt(pred, digit, 10),
+                valid_upper = svcmplt(pred, upper, 16);
+      svuint8_t	letter = svsel(valid_upper, upper, lower);
+      svbool_t	valid_letter = svand_z(pred, svcmpgt(pred, letter, 9),
+                                            svcmplt(pred, letter, 16));
+      if (svptest_any(pred, svnot_z(pred, svorr_z(pred, valid_digit, valid_letter))))
+        return 0;
+      *res = svsel(valid_digit, digit, letter);
+      return 1;
+    }
+
+    #if defined(__has_attribute) && __has_attribute (target)
+        __attribute__((target("arch=armv8-a+sve")))
+    #endif
+    static int hex_coding_test(void)
+    {
+      int len = 64, vec_len = svcntb(), vec_len_x2 = svcntb() * 2;
+      const char	*hextbl = "0123456789abcdef";
+      svuint8_t	hextbl_vec = svld1(svwhilelt_b8(0, 16), (uint8_t *) hextbl);
+      char *src = input, *dst = output;
+
+      /* hex encode */
+      for (uint64_t i = 0; i < 64; i += vec_len, dst += 2 * vec_len, src += vec_len)
+      {
+        svbool_t  pred = svwhilelt_b8((uint64_t) i, (uint64_t) len);
+        svuint8_t bytes = svld1(pred, (uint8_t *) src),
+                  high = svlsr_x(pred, bytes, 4),
+                  low = svand_z(pred, bytes, 0xF);
+        svuint8x2_t merged = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+        svst2(pred, (uint8_t *) dst, merged);
+      }
+
+      /* hex decode */
+      len = 128;
+
+      for (int i; i < len; i += vec_len_x2)
+      {
+        svbool_t 	  pred = svwhilelt_b8((uint64_t) i / 2, (uint64_t) len / 2);
+        svuint8x2_t bytes = svld2(pred, (uint8_t *) src + i);
+        svuint8_t 	high = svget2(bytes, 0), low = svget2(bytes, 1);
+
+        if (svptest_any(pred, svorr_z(pred, svcmplt(pred, high, '0'), svcmplt(pred, low, '0'))))
+          break;
+        if (!get_hex_sve(pred, high, &high) || !get_hex_sve(pred, low, &low))
+          break;
+
+        svst1(pred, (uint8_t *) dst + i / 2, svorr_z(pred, svlsl_x(pred, high, 4), low));
+      }
+
+      /* return computed value, to prevent the above being optimized away */
+      return output@<:@0@:>@;
+    }],
+  [return hex_coding_test();])],
+  [Ac_cachevar=yes],
+  [Ac_cachevar=no])])
+if test x"$Ac_cachevar" = x"yes"; then
+  pgac_arm_sve_hex_intrinsics=yes
+fi
+undefine([Ac_cachevar])dnl
+])# PGAC_ARM_SVE_HEX_INTRINSICS
diff --git a/configure b/configure
index 39c68161cec..60354107f87 100755
--- a/configure
+++ b/configure
@@ -17735,6 +17735,110 @@ $as_echo "#define USE_SVE_POPCNT_WITH_RUNTIME_CHECK 1" >>confdefs.h
   fi
 fi
 
+# Check for ARM SVE intrinsics for hex coding
+#
+if test x"$host_cpu" = x"aarch64"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for svtbl, svlsr_x, svand_z, svcreate2, etc" >&5
+$as_echo_n "checking for svtbl, svlsr_x, svand_z, svcreate2, etc... " >&6; }
+if ${pgac_cv_arm_sve_hex_intrinsics+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <arm_sve.h>
+
+    char input[64];
+    char output[128];
+
+    #if defined(__has_attribute) && __has_attribute (target)
+        __attribute__((target("arch=armv8-a+sve")))
+    #endif
+    int get_hex_sve(svbool_t pred, svuint8_t vec, svuint8_t *res)
+    {
+      svuint8_t	digit = svsub_x(pred, vec, 48),
+                upper = svsub_x(pred, vec, 55),
+                lower = svsub_x(pred, vec, 87);
+      svbool_t	valid_digit = svcmplt(pred, digit, 10),
+                valid_upper = svcmplt(pred, upper, 16);
+      svuint8_t	letter = svsel(valid_upper, upper, lower);
+      svbool_t	valid_letter = svand_z(pred, svcmpgt(pred, letter, 9),
+                                            svcmplt(pred, letter, 16));
+      if (svptest_any(pred, svnot_z(pred, svorr_z(pred, valid_digit, valid_letter))))
+        return 0;
+      *res = svsel(valid_digit, digit, letter);
+      return 1;
+    }
+
+    #if defined(__has_attribute) && __has_attribute (target)
+        __attribute__((target("arch=armv8-a+sve")))
+    #endif
+    static int hex_coding_test(void)
+    {
+      int len = 64, vec_len = svcntb(), vec_len_x2 = svcntb() * 2;
+      const char	*hextbl = "0123456789abcdef";
+      svuint8_t	hextbl_vec = svld1(svwhilelt_b8(0, 16), (uint8_t *) hextbl);
+      char *src = input, *dst = output;
+
+      /* hex encode */
+      for (uint64_t i = 0; i < 64; i += vec_len, dst += 2 * vec_len, src += vec_len)
+      {
+        svbool_t  pred = svwhilelt_b8((uint64_t) i, (uint64_t) len);
+        svuint8_t bytes = svld1(pred, (uint8_t *) src),
+                  high = svlsr_x(pred, bytes, 4),
+                  low = svand_z(pred, bytes, 0xF);
+        svuint8x2_t merged = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+        svst2(pred, (uint8_t *) dst, merged);
+      }
+
+      /* hex decode */
+      len = 128;
+
+      for (int i; i < len; i += vec_len_x2)
+      {
+        svbool_t 	  pred = svwhilelt_b8((uint64_t) i / 2, (uint64_t) len / 2);
+        svuint8x2_t bytes = svld2(pred, (uint8_t *) src + i);
+        svuint8_t 	high = svget2(bytes, 0), low = svget2(bytes, 1);
+
+        if (svptest_any(pred, svorr_z(pred, svcmplt(pred, high, '0'), svcmplt(pred, low, '0'))))
+          break;
+        if (!get_hex_sve(pred, high, &high) || !get_hex_sve(pred, low, &low))
+          break;
+
+        svst1(pred, (uint8_t *) dst + i / 2, svorr_z(pred, svlsl_x(pred, high, 4), low));
+      }
+
+      /* return computed value, to prevent the above being optimized away */
+      return output[0];
+    }
+int
+main ()
+{
+return hex_coding_test();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  pgac_cv_arm_sve_hex_intrinsics=yes
+else
+  pgac_cv_arm_sve_hex_intrinsics=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_arm_sve_hex_intrinsics" >&5
+$as_echo "$pgac_cv_arm_sve_hex_intrinsics" >&6; }
+if test x"$pgac_cv_arm_sve_hex_intrinsics" = x"yes"; then
+  pgac_arm_sve_hex_intrinsics=yes
+fi
+
+  if test x"$pgac_arm_sve_hex_intrinsics" = x"yes"; then
+
+$as_echo "#define USE_SVE_HEX_WITH_RUNTIME_CHECK 1" >>confdefs.h
+
+  fi
+fi
+
 # Check for Intel SSE 4.2 intrinsics to do CRC calculations.
 #
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for _mm_crc32_u8 and _mm_crc32_u32" >&5
diff --git a/configure.ac b/configure.ac
index 066e3976c0a..6ca57b8c4a7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2136,6 +2136,15 @@ if test x"$host_cpu" = x"aarch64"; then
   fi
 fi
 
+# Check for ARM SVE intrinsics for hex coding
+#
+if test x"$host_cpu" = x"aarch64"; then
+  PGAC_ARM_SVE_HEX_INTRINSICS()
+  if test x"$pgac_arm_sve_hex_intrinsics" = x"yes"; then
+    AC_DEFINE(USE_SVE_HEX_WITH_RUNTIME_CHECK, 1, [Define to 1 to use ARM SVE intrinsic for hex coding.])
+  fi
+fi
+
 # Check for Intel SSE 4.2 intrinsics to do CRC calculations.
 #
 PGAC_SSE42_CRC32_INTRINSICS()
diff --git a/meson.build b/meson.build
index ab8101d67b2..ea5392cfc78 100644
--- a/meson.build
+++ b/meson.build
@@ -2372,6 +2372,87 @@ int main(void)
 endif
 
 
+###############################################################
+# Check the availability of SVE intrinsics for hex coding.
+###############################################################
+
+if host_cpu == 'aarch64'
+
+  prog = '''
+#include <arm_sve.h>
+
+char input[64];
+char output[128];
+
+#if defined(__has_attribute) && __has_attribute (target)
+    __attribute__((target("arch=armv8-a+sve")))
+#endif
+int get_hex_sve(svbool_t pred, svuint8_t vec, svuint8_t *res)
+{
+	svuint8_t	digit = svsub_x(pred, vec, 48),
+				    upper = svsub_x(pred, vec, 55),
+				    lower = svsub_x(pred, vec, 87);
+	svbool_t	valid_digit = svcmplt(pred, digit, 10),
+            valid_upper = svcmplt(pred, upper, 16);
+	svuint8_t	letter = svsel(valid_upper, upper, lower);
+	svbool_t	valid_letter = svand_z(pred, svcmpgt(pred, letter, 9),
+											                 svcmplt(pred, letter, 16));
+	if (svptest_any(pred, svnot_z(pred, svorr_z(pred, valid_digit, valid_letter))))
+		return 0;
+	*res = svsel(valid_digit, digit, letter);
+	return 1;
+}
+
+#if defined(__has_attribute) && __has_attribute (target)
+    __attribute__((target("arch=armv8-a+sve")))
+#endif
+int main(void)
+{
+    int len = 64, vec_len = svcntb(), vec_len_x2 = svcntb() * 2;
+    const char	hextbl[] = "0123456789abcdef";
+    svuint8_t	hextbl_vec = svld1(svwhilelt_b8(0, 16), (uint8_t *) hextbl);
+    char *src = input, *dst = output;
+
+    /* hex encode */
+    for (uint64_t i = 0; i < 64; i += vec_len, dst += 2 * vec_len, src += vec_len)
+    {
+      svbool_t  pred = svwhilelt_b8((uint64_t) i, (uint64_t) len);
+      svuint8_t bytes = svld1(pred, (uint8_t *) src),
+                high = svlsr_x(pred, bytes, 4),
+                low = svand_z(pred, bytes, 0xF);
+      svuint8x2_t merged = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+      svst2(pred, (uint8_t *) dst, merged);
+    }
+
+    /* hex decode */
+    len = 128;
+
+    for (int i; i < len; i += vec_len_x2)
+    {
+      svbool_t 	  pred = svwhilelt_b8((uint64_t) i / 2, (uint64_t) len / 2);
+      svuint8x2_t bytes = svld2(pred, (uint8_t *) src + i);
+      svuint8_t 	high = svget2(bytes, 0), low = svget2(bytes, 1);
+
+      if (svptest_any(pred, svorr_z(pred, svcmplt(pred, high, '0'), svcmplt(pred, low, '0'))))
+        break;
+      if (!get_hex_sve(pred, high, &high) || !get_hex_sve(pred, low, &low))
+        break;
+
+      svst1(pred, (uint8_t *) dst + i / 2, svorr_z(pred, svlsl_x(pred, high, 4), low));
+    }
+
+    /* return computed value, to prevent the above being optimized away */
+    return output[0];
+}
+'''
+
+  if cc.links(prog, name: 'SVE hex coding', args: test_c_args)
+    cdata.set('USE_SVE_HEX_WITH_RUNTIME_CHECK', 1)
+  endif
+
+endif
+
+
 ###############################################################
 # Select CRC-32C implementation.
 #
diff --git a/src/backend/utils/adt/encode_aarch64.c b/src/backend/utils/adt/encode_aarch64.c
index 7b0412dc255..4f22fb1d6c7 100644
--- a/src/backend/utils/adt/encode_aarch64.c
+++ b/src/backend/utils/adt/encode_aarch64.c
@@ -20,8 +20,229 @@
 
 #include <arm_neon.h>
 
+#ifdef USE_SVE_HEX_WITH_RUNTIME_CHECK
+#include <arm_sve.h>
+
+#if defined(HAVE_ELF_AUX_INFO) || defined(HAVE_GETAUXVAL)
+#include <sys/auxv.h>
+#endif
+
+/*
+ * These are the NEON implementations of the hex encode/decode functions.
+ */
+static uint64 hex_encode_neon(const char *src, size_t len, char *dst);
+static uint64 hex_decode_neon(const char *src, size_t len, char *dst);
+static uint64 hex_decode_safe_neon(const char *src, size_t len, char *dst, Node *escontext);
+
+/*
+ * These are the SVE implementations of the hex encode/decode functions.
+ */
+static uint64 hex_encode_sve(const char *src, size_t len, char *dst);
+static uint64 hex_decode_sve(const char *src, size_t len, char *dst);
+static uint64 hex_decode_safe_sve(const char *src, size_t len, char *dst, Node *escontext);
+
+/*
+ * The function pointers are initially set to "choose" functions.  These
+ * functions will first set the pointers to the right implementations (based on
+ * what the current CPU supports) and then will call the pointer to fulfill the
+ * caller's request.
+ */
+
+static uint64 hex_encode_choose(const char *src, size_t len, char *dst);
+static uint64 hex_decode_choose(const char *src, size_t len, char *dst);
+static uint64 hex_decode_safe_choose(const char *src, size_t len, char *dst, Node *escontext);
+uint64 		(*hex_encode_optimized) (const char *src, size_t len, char *dst) = hex_encode_choose;
+uint64 		(*hex_decode_optimized) (const char *src, size_t len, char *dst) = hex_decode_choose;
+uint64 		(*hex_decode_safe_optimized) (const char *src, size_t len, char *dst, Node *escontext) = hex_decode_safe_choose;
+
+static inline bool
+check_sve_support(void)
+{
+#ifdef HAVE_ELF_AUX_INFO
+	unsigned long value;
+
+	return elf_aux_info(AT_HWCAP, &value, sizeof(value)) == 0 &&
+		(value & HWCAP_SVE) != 0;
+#elif defined(HAVE_GETAUXVAL)
+	return (getauxval(AT_HWCAP) & HWCAP_SVE) != 0;
+#else
+	return false;
+#endif
+}
+
+static inline void
+choose_hex_functions(void)
+{
+	if (check_sve_support())
+	{
+		hex_encode_optimized = hex_encode_sve;
+		hex_decode_optimized = hex_decode_sve;
+		hex_decode_safe_optimized = hex_decode_safe_sve;
+	}
+	else
+	{
+		hex_encode_optimized = hex_encode_neon;
+		hex_decode_optimized = hex_decode_neon;
+		hex_decode_safe_optimized = hex_decode_safe_neon;
+	}
+}
+
+static uint64
+hex_encode_choose(const char *src, size_t len, char *dst)
+{
+	choose_hex_functions();
+	return hex_encode_optimized(src, len, dst);
+}
+
+static uint64
+hex_decode_choose(const char *src, size_t len, char *dst)
+{
+	choose_hex_functions();
+	return hex_decode_optimized(src, len, dst);
+}
+
+static uint64
+hex_decode_safe_choose(const char *src, size_t len, char *dst, Node *escontext)
+{
+	choose_hex_functions();
+	return hex_decode_safe_optimized(src, len, dst, escontext);
+}
+
+pg_attribute_target("arch=armv8-a+sve")
+uint64
+hex_encode_sve(const char *src, size_t len, char *dst)
+{
+	const char	hextbl[] = "0123456789abcdef";
+	uint32 		vec_len = svcntb();
+	svuint8_t	hextbl_vec = svld1(svwhilelt_b8(0, 16), (uint8 *) hextbl);
+	svbool_t	pred_true = svptrue_b8();
+	size_t		chunks_len = len & ~(2 * vec_len - 1); /* process 2 * vec_len bytes per iteration */
+
+	for (size_t i = 0; i < chunks_len; i += 2 * vec_len)
+	{
+		svuint8_t	bytes = svld1(pred_true, (uint8 *) src);
+		svuint8_t	high = svlsr_x(pred_true, bytes, 4);
+		svuint8_t	low = svand_z(pred_true, bytes, 0xF);
+		svuint8x2_t	zipped = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+		svst2(pred_true, (uint8 *) dst, zipped);
+
+		dst += 2 * vec_len;
+		src += vec_len;
+
+		/* unrolled */
+		bytes = svld1(pred_true, (uint8 *) src);
+		high = svlsr_x(pred_true, bytes, 4);
+		low = svand_z(pred_true, bytes, 0xF);
+		zipped = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+		svst2(pred_true, (uint8 *) dst, zipped);
+
+		dst += 2 * vec_len;
+		src += vec_len;
+	}
+
+	/* process remaining tail bytes */
+	for (size_t i = chunks_len; i < len; i += vec_len)
+	{
+		svbool_t	pred = svwhilelt_b8((uint64) i, (uint64) len);
+		svuint8_t	bytes = svld1(pred, (uint8 *) src);
+		svuint8_t	high = svlsr_x(pred, bytes, 4);
+		svuint8_t	low = svand_z(pred, bytes, 0xF);
+		svuint8x2_t	zipped = svcreate2(svtbl(hextbl_vec, high), svtbl(hextbl_vec, low));
+		svst2(pred, (uint8 *) dst, zipped);
+
+		dst += 2 * vec_len;
+		src += vec_len;
+	}
+
+	return (uint64) len * 2;
+}
+
+pg_attribute_target("arch=armv8-a+sve")
+static inline bool
+get_hex_sve(svbool_t pred, svuint8_t vec, svuint8_t *res)
+{
+	svuint8_t	digit = svsub_x(pred, vec, 48);
+	svuint8_t	upper = svsub_x(pred, vec, 55);
+	svuint8_t	lower = svsub_x(pred, vec, 87);
+
+	svbool_t	valid_digit = svcmplt(pred, digit, 10);
+	svbool_t	valid_upper = svcmplt(pred, upper, 16);
+
+	svuint8_t	letter = svsel(valid_upper, upper, lower);
+	svbool_t	valid_letter = svand_z(pred, svcmpgt(pred, letter, 9),
+											 svcmplt(pred, letter, 16));
+
+	if (svptest_any(pred, svnot_z(pred, svorr_z(pred, valid_digit, valid_letter))))
+		return false;
+
+	*res = svsel(valid_digit, digit, letter);
+	return true;
+}
+
+uint64
+hex_decode_sve(const char *src, size_t len, char *dst)
+{
+	return hex_decode_safe_sve(src, len, dst, NULL);
+}
+
+pg_attribute_target("arch=armv8-a+sve")
+uint64
+hex_decode_safe_sve(const char *src, size_t len, char *dst, Node *escontext)
+{
+	uint32		vec_len = svcntb();
+	size_t		i = 0;
+	size_t		chunks_len = len & ~(2 * vec_len - 1); /* process 2 * vec_len byte chunk each iteration */
+	svbool_t 	pred = svptrue_b8();
+	const char *p = dst;
+	bool		fallback = false;
+
+	while (i < chunks_len)
+	{
+		svuint8x2_t bytes = svld2(pred, (uint8 *) src);
+		svuint8_t 	high = svget2(bytes, 0);
+		svuint8_t  	low = svget2(bytes, 1);
+
+		if (svptest_any(pred, svorr_z(pred, svcmplt(pred, high, '0'), svcmplt(pred, low, '0'))))
+		{
+			fallback = true;
+			break;
+		}
+
+		if (!get_hex_sve(pred, high, &high) || !get_hex_sve(pred, low, &low))
+		{
+			fallback = true;
+			break;
+		}
+
+		svst1(pred, (uint8 *) dst, svorr_z(pred, svlsl_x(pred, high, 4), low));
+
+		i += 2 * vec_len;
+		src += 2 * vec_len;
+		dst += vec_len;
+	}
+
+	if (len > i && !fallback) /* can use neon for smaller chunks */
+		return dst - p + hex_decode_safe_neon(src, len - i, dst, escontext);
+
+	if (fallback) /* fallback */
+		return dst - p + hex_decode_safe_scalar(src, len - i, dst, escontext);
+
+	return dst - p;
+}
+
+#endif	/* USE_SVE_HEX_WITH_RUNTIME_CHECK */
+
+/*
+ * If the compiler supports SVE, rename the NEON versions because the
+ * optimized versions are now referenced via function pointers.
+ */
+
 uint64
+#ifdef USE_SVE_HEX_WITH_RUNTIME_CHECK
+static hex_encode_neon(const char *src, size_t len, char *dst)
+#else
 hex_encode_optimized(const char *src, size_t len, char *dst)
+#endif
 {
 	const char		hextbl[] = "0123456789abcdef";
 	uint8x16_t		hextbl_vec = vld1q_u8((uint8 *) hextbl);
@@ -63,8 +284,6 @@ hex_encode_optimized(const char *src, size_t len, char *dst)
 		dst += 2 * vec_len;
 	}
 
-
-
 	if (len > chunks_len)
 		hex_encode_scalar(src, len - chunks_len, dst);
 
@@ -148,13 +367,21 @@ get_hex_neon(uint8x16_t vec, uint8x16_t *res)
 }
 
 uint64
+#ifdef USE_SVE_HEX_WITH_RUNTIME_CHECK
+static hex_decode_neon(const char *src, size_t len, char *dst)
+#else
 hex_decode_optimized(const char *src, size_t len, char *dst)
+#endif
 {
 	return hex_decode_safe_optimized(src, len, dst, NULL);
 }
 
 uint64
+#ifdef USE_SVE_HEX_WITH_RUNTIME_CHECK
+static hex_decode_safe_neon(const char *src, size_t len, char *dst, Node *escontext)
+#else
 hex_decode_safe_optimized(const char *src, size_t len, char *dst, Node *escontext)
+#endif
 {
 	uint32		vec_len = sizeof(uint8x16_t);
 	size_t		i = 0;
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index c4dc5d72bdb..a6735bdd21f 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -678,6 +678,9 @@
 /* Define to 1 to use AVX-512 popcount instructions with a runtime check. */
 #undef USE_AVX512_POPCNT_WITH_RUNTIME_CHECK
 
+/* Define to 1 to use SVE instructions for hex coding with a runtime check. */
+#undef USE_SVE_HEX_WITH_RUNTIME_CHECK
+
 /* Define to 1 to build with Bonjour support. (--with-bonjour) */
 #undef USE_BONJOUR
 
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index a809fad2771..8a80a9ae51f 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -49,7 +49,15 @@ extern uint64 hex_decode_safe_scalar(const char *src, size_t len, char *dst,
 #define HEX_CODING_AARCH64 1
 #endif
 
-#ifdef HEX_CODING_AARCH64
+/*
+ * We can try to use an SVE-optimized hex encode/decode on systems supporting SVE.
+ * For that, we use a function pointer.
+ */
+#ifdef USE_SVE_HEX_WITH_RUNTIME_CHECK
+extern PGDLLIMPORT uint64 (*hex_encode_optimized) (const char *src, size_t len, char *dst);
+extern PGDLLIMPORT uint64 (*hex_decode_optimized) (const char *src, size_t len, char *dst);
+extern PGDLLIMPORT uint64 (*hex_decode_safe_optimized) (const char *src, size_t len, char *dst, Node *escontext);
+#elif HEX_CODING_AARCH64
 extern uint64 hex_encode_optimized(const char *src, size_t len, char *dst);
 extern uint64 hex_decode_optimized(const char *src, size_t len, char *dst);
 extern uint64 hex_decode_safe_optimized(const char *src, size_t len, char *dst, Node *escontext);
-- 
2.34.1

