From dc4d4c52e19171cf11268e6327aeb44b1e88b172 Mon Sep 17 00:00:00 2001
From: David Christensen <david.christensen@crunchydata.com>
Date: Thu, 31 Aug 2023 12:09:55 -0400
Subject: [PATCH] Switch to using only 16-bit to avoid 128-bit requirement

---
 src/include/port/pg_bitutils.h | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/src/include/port/pg_bitutils.h b/src/include/port/pg_bitutils.h
index 84a5fdead9..2d7311ebb5 100644
--- a/src/include/port/pg_bitutils.h
+++ b/src/include/port/pg_bitutils.h
@@ -349,36 +349,28 @@ pg_rotate_left32(uint32 word, int n)
  * asm-tuned things in port maybe?  general-purpose solution should be ok
  * though.
  */
-static inline uint64 pg_fastinverse(uint32 divisor)
+static inline uint32 pg_fastinverse(uint16 divisor)
 {
-	return UINT64_C(0xFFFFFFFFFFFFFFFF) / divisor + 1;
+	return UINT32_C(0xFFFFFFFF) / divisor + 1;
 }
 
 /*
- * pg_fastdiv - calculates the quotient of a 32-bit number against a constant
+ * pg_fastdiv - calculates the quotient of a 16-bit number against a constant
  * divisor without using the division operator
  */
-static inline uint32 pg_fastdiv(uint32 n, uint32 divisor, uint64 fastinv)
+static inline uint16 pg_fastdiv(uint16 n, uint16 divisor, uint32 fastinv)
 {
-#ifdef HAVE_INT128
-	return (((uint128)(fastinv - 1) * n)) >> 64;
-#else
-	return n / divisor;
-#endif
+	return (((uint64)(fastinv - 1) * n)) >> 32;
 }
 
 /*
- * pg_fastmod - calculates the modulus of a 32-bit number against a constant
+ * pg_fastmod - calculates the modulus of a 16-bit number against a constant
  * divisor without using the division operator
  */
-static inline uint32 pg_fastmod(uint32 n, uint32 divisor, uint64 fastinv)
+static inline uint16 pg_fastmod(uint16 n, uint16 divisor, uint32 fastinv)
 {
-#ifdef HAVE_INT128
-	uint64_t lowbits = fastinv * n;
-	return ((uint128)lowbits * divisor) >> 64;
-#else
-	return n % divisor;
-#endif
+	uint32 lowbits = fastinv * n;
+	return ((uint64)lowbits * divisor) >> 32;
 }
 
 #endif							/* PG_BITUTILS_H */
-- 
2.39.2

