From 380bd04c1658795c77b3b808ee766347188fab5f Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 21 Oct 2004 19:28:36 +0000
Subject: [PATCH] Standardize on using the Min, Max, and Abs macros that are in
 our c.h file, getting rid of numerous ad-hoc versions that have popped up in
 various places.  Shortens code and avoids conflict with Windows min() and
 max() macros.

---
 contrib/cube/cube.c                 |  6 ++----
 contrib/intarray/_int.h             |  4 ----
 contrib/intarray/_int_gist.c        |  2 +-
 contrib/intarray/_int_tool.c        |  2 +-
 contrib/intarray/_intbig_gist.c     |  2 +-
 contrib/ltree/_ltree_gist.c         |  2 +-
 contrib/ltree/ltree.h               |  9 ---------
 contrib/ltree/ltree_gist.c          |  4 ++--
 contrib/ltree/ltree_op.c            |  4 ++--
 contrib/miscutil/misc_utils.c       | 13 +++++--------
 contrib/miscutil/misc_utils.h       | 11 ++---------
 contrib/miscutil/misc_utils.sql.in  |  4 ++--
 contrib/seg/seg.c                   | 10 ++++------
 contrib/string/string_io.c          |  4 +---
 contrib/tsearch/gistidx.c           |  2 +-
 contrib/tsearch/gistidx.h           |  5 -----
 contrib/tsearch2/gistidx.c          |  2 +-
 contrib/tsearch2/gistidx.h          |  2 --
 contrib/tsearch2/rank.c             |  2 +-
 src/backend/postmaster/postmaster.c |  4 ++--
 src/interfaces/libpq/fe-exec.c      |  9 ++-------
 src/test/regress/regress.c          |  8 +++-----
 22 files changed, 34 insertions(+), 77 deletions(-)

diff --git a/contrib/cube/cube.c b/contrib/cube/cube.c
index 82a09729177..6b61d9e3f63 100644
--- a/contrib/cube/cube.c
+++ b/contrib/cube/cube.c
@@ -15,8 +15,6 @@
 
 #include "cubedata.h"
 
-#define abs(a)			((a) <	(0) ? (-a) : (a))
-
 extern int	cube_yyparse();
 extern void cube_yyerror(const char *message);
 extern void cube_scanner_init(const char *str);
@@ -683,7 +681,7 @@ cube_size(NDBOX * a)
 
 	*result = 1.0;
 	for (i = 0, j = a->dim; i < a->dim; i++, j++)
-		*result = (*result) * abs((a->x[j] - a->x[i]));
+		*result = (*result) * Abs((a->x[j] - a->x[i]));
 
 	return (result);
 }
@@ -700,7 +698,7 @@ rt_cube_size(NDBOX * a, double *size)
 	{
 		*size = 1.0;
 		for (i = 0, j = a->dim; i < a->dim; i++, j++)
-			*size = (*size) * abs((a->x[j] - a->x[i]));
+			*size = (*size) * Abs((a->x[j] - a->x[i]));
 	}
 	return;
 }
diff --git a/contrib/intarray/_int.h b/contrib/intarray/_int.h
index 3e702ca1afe..2231bdb095b 100644
--- a/contrib/intarray/_int.h
+++ b/contrib/intarray/_int.h
@@ -14,10 +14,6 @@
 /* number ranges for compression */
 #define MAXNUMRANGE 100
 
-#define max(a,b)		((a) >	(b) ? (a) : (b))
-#define min(a,b)		((a) <= (b) ? (a) : (b))
-#define abs(a)			((a) <	(0) ? -(a) : (a))
-
 /* dimension of array */
 #define NDIM 1
 
diff --git a/contrib/intarray/_int_gist.c b/contrib/intarray/_int_gist.c
index e4b923523b1..2d5e82a4596 100644
--- a/contrib/intarray/_int_gist.c
+++ b/contrib/intarray/_int_gist.c
@@ -425,7 +425,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
 		union_d = inner_int_union(datum_r, datum_alpha);
 		rt__int_size(union_d, &size_beta);
 		pfree(union_d);
-		costvector[i - 1].cost = abs((size_alpha - size_l) - (size_beta - size_r));
+		costvector[i - 1].cost = Abs((size_alpha - size_l) - (size_beta - size_r));
 	}
 	qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
 
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index d5f169f7c82..216a4105c7f 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -137,7 +137,7 @@ inner_int_inter(ArrayType *a, ArrayType *b)
 	nb = ARRNELEMS(b);
 	da = ARRPTR(a);
 	db = ARRPTR(b);
-	r = new_intArrayType(min(na, nb));
+	r = new_intArrayType(Min(na, nb));
 	dr = ARRPTR(r);
 
 	i = j = 0;
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index efd70758dc2..07a051dd50c 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -402,7 +402,7 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
 		_j = GETENTRY(entryvec, j);
 		size_alpha = hemdist(datum_l, _j);
 		size_beta = hemdist(datum_r, _j);
-		costvector[j - 1].cost = abs(size_alpha - size_beta);
+		costvector[j - 1].cost = Abs(size_alpha - size_beta);
 	}
 	qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
 
diff --git a/contrib/ltree/_ltree_gist.c b/contrib/ltree/_ltree_gist.c
index f0e01bbf471..50a38405c67 100644
--- a/contrib/ltree/_ltree_gist.c
+++ b/contrib/ltree/_ltree_gist.c
@@ -361,7 +361,7 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
 		_j = GETENTRY(entryvec, j);
 		size_alpha = hemdist(datum_l, _j);
 		size_beta = hemdist(datum_r, _j);
-		costvector[j - 1].cost = abs(size_alpha - size_beta);
+		costvector[j - 1].cost = Abs(size_alpha - size_beta);
 	}
 	qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
 
diff --git a/contrib/ltree/ltree.h b/contrib/ltree/ltree.h
index edcda22e4b9..c55c757016a 100644
--- a/contrib/ltree/ltree.h
+++ b/contrib/ltree/ltree.h
@@ -78,15 +78,6 @@ typedef struct
 
 #define LQUERY_HASNOT		0x01
 
-#ifndef max
-#define max(a,b)				((a) >	(b) ? (a) : (b))
-#endif
-#ifndef min
-#define min(a,b)				((a) <= (b) ? (a) : (b))
-#endif
-#ifndef abs
-#define abs(a)					((a) <	(0) ? -(a) : (a))
-#endif
 #define ISALNUM(x)	( isalnum((unsigned char)(x)) || (x) == '_' )
 
 /* full text query */
diff --git a/contrib/ltree/ltree_gist.c b/contrib/ltree/ltree_gist.c
index 7630b4f60c3..ec81ce1c183 100644
--- a/contrib/ltree/ltree_gist.c
+++ b/contrib/ltree/ltree_gist.c
@@ -259,7 +259,7 @@ ltree_penalty(PG_FUNCTION_ARGS)
 	cmpl = ltree_compare(LTG_GETLNODE(origval), LTG_GETLNODE(newval));
 	cmpr = ltree_compare(LTG_GETRNODE(newval), LTG_GETRNODE(origval));
 
-	*penalty = max(cmpl, 0) + max(cmpr, 0);
+	*penalty = Max(cmpl, 0) + Max(cmpr, 0);
 
 	PG_RETURN_POINTER(penalty);
 }
@@ -537,7 +537,7 @@ gist_tqcmp(ltree * t, lquery * q)
 	while (an > 0 && bn > 0)
 	{
 		bl = LQL_FIRST(ql);
-		if ((res = strncmp(al->name, bl->name, min(al->len, bl->len))) == 0)
+		if ((res = strncmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
 		{
 			if (al->len != bl->len)
 				return al->len - bl->len;
diff --git a/contrib/ltree/ltree_op.c b/contrib/ltree/ltree_op.c
index 4dcb558fe48..f10f7df4127 100644
--- a/contrib/ltree/ltree_op.c
+++ b/contrib/ltree/ltree_op.c
@@ -55,7 +55,7 @@ ltree_compare(const ltree * a, const ltree * b)
 
 	while (an > 0 && bn > 0)
 	{
-		if ((res = strncmp(al->name, bl->name, min(al->len, bl->len))) == 0)
+		if ((res = strncmp(al->name, bl->name, Min(al->len, bl->len))) == 0)
 		{
 			if (al->len != bl->len)
 				return (al->len - bl->len) * 10 * (an + 1);
@@ -443,7 +443,7 @@ lca_inner(ltree ** a, int len)
 			l2 = LTREE_FIRST(*ptr);
 			tmp = num;
 			num = 0;
-			for (i = 0; i < min(tmp, (*ptr)->numlevel - 1); i++)
+			for (i = 0; i < Min(tmp, (*ptr)->numlevel - 1); i++)
 			{
 				if (l1->len == l2->len && strncmp(l1->name, l2->name, l1->len) == 0)
 					num = i + 1;
diff --git a/contrib/miscutil/misc_utils.c b/contrib/miscutil/misc_utils.c
index fb121e83803..ce8776fa3cb 100644
--- a/contrib/miscutil/misc_utils.c
+++ b/contrib/miscutil/misc_utils.c
@@ -30,9 +30,6 @@
 
 #include "misc_utils.h"
 
-#undef MIN
-#define MIN(x,y)	((x)<=(y) ? (x) : (y))
-
 
 int
 backend_pid()
@@ -48,15 +45,15 @@ unlisten(char *relname)
 }
 
 int
-max(int x, int y)
+int4max(int x, int y)
 {
-	return ((x > y) ? x : y);
+	return Max(x, y);
 }
 
 int
-min(int x, int y)
+int4min(int x, int y)
 {
-	return ((x < y) ? x : y);
+	return Min(x, y);
 }
 
 /*
@@ -84,7 +81,7 @@ active_listeners(text *relname)
 	if (relname && (VARSIZE(relname) > VARHDRSZ))
 	{
 		MemSet(listen_name, 0, NAMEDATALEN);
-		len = MIN(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
+		len = Min(VARSIZE(relname) - VARHDRSZ, NAMEDATALEN - 1);
 		memcpy(listen_name, VARDATA(relname), len);
 		ScanKeyInit(&key,
 					Anum_pg_listener_relname,
diff --git a/contrib/miscutil/misc_utils.h b/contrib/miscutil/misc_utils.h
index bc69ba86157..3de5ac70018 100644
--- a/contrib/miscutil/misc_utils.h
+++ b/contrib/miscutil/misc_utils.h
@@ -1,17 +1,10 @@
 #ifndef MISC_UTILS_H
 #define MISC_UTILS_H
 
-#ifdef max
-#undef max
-#endif
-#ifdef min
-#undef min
-#endif
-
 int			backend_pid(void);
 int			unlisten(char *relname);
-int			max(int x, int y);
-int			min(int x, int y);
+int			int4max(int x, int y);
+int			int4min(int x, int y);
 int			active_listeners(text *relname);
 
 #endif
diff --git a/contrib/miscutil/misc_utils.sql.in b/contrib/miscutil/misc_utils.sql.in
index b7e0a8b3671..5f777a1e5b6 100644
--- a/contrib/miscutil/misc_utils.sql.in
+++ b/contrib/miscutil/misc_utils.sql.in
@@ -36,14 +36,14 @@ LANGUAGE 'SQL';
 --
 CREATE OR REPLACE FUNCTION min(int4,int4)
 RETURNS int4
-AS 'MODULE_PATHNAME'
+AS 'MODULE_PATHNAME', 'int4min'
 LANGUAGE 'C';
 
 -- max(x,y)
 --
 CREATE OR REPLACE FUNCTION max(int4,int4)
 RETURNS int4
-AS 'MODULE_PATHNAME'
+AS 'MODULE_PATHNAME', 'int4max'
 LANGUAGE 'C';
 
 -- Return the number of active listeners on a relation
diff --git a/contrib/seg/seg.c b/contrib/seg/seg.c
index ce857aac479..0de714854d0 100644
--- a/contrib/seg/seg.c
+++ b/contrib/seg/seg.c
@@ -14,8 +14,6 @@
 
 #include "segdata.h"
 
-#define abs(a)			((a) <	(0) ? (-a) : (a))
-
 /*
 #define GIST_DEBUG
 #define GIST_QUERY_DEBUG
@@ -717,7 +715,7 @@ rt_seg_size(SEG * a, float *size)
 	if (a == (SEG *) NULL || a->upper <= a->lower)
 		*size = 0.0;
 	else
-		*size = (float) abs(a->upper - a->lower);
+		*size = (float) Abs(a->upper - a->lower);
 
 	return;
 }
@@ -729,7 +727,7 @@ seg_size(SEG * a)
 
 	result = (float *) palloc(sizeof(float));
 
-	*result = (float) abs(a->upper - a->lower);
+	*result = (float) Abs(a->upper - a->lower);
 
 	return (result);
 }
@@ -948,7 +946,7 @@ restore(char *result, float val, int n)
 	}
 	else
 	{
-		if (abs(exp) <= 4)
+		if (Abs(exp) <= 4)
 		{
 			/*
 			 * remove the decimal point from the mantyssa and write the
@@ -1036,7 +1034,7 @@ restore(char *result, float val, int n)
 			}
 		}
 
-		/* do nothing for abs(exp) > 4; %e must be OK */
+		/* do nothing for Abs(exp) > 4; %e must be OK */
 		/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
 
 		/* ... this is not done yet. */
diff --git a/contrib/string/string_io.c b/contrib/string/string_io.c
index 059dfa08947..a6f49e9afe7 100644
--- a/contrib/string/string_io.c
+++ b/contrib/string/string_io.c
@@ -20,8 +20,6 @@
 /* define this if you want to see iso-8859 characters */
 #define ISO8859
 
-#undef MIN
-#define MIN(x, y)	((x) < (y) ? (x) : (y))
 #define VALUE(char) ((char) - '0')
 #define DIGIT(val)	((val) + '0')
 #define ISOCTAL(c)	(((c) >= '0') && ((c) <= '7'))
@@ -229,7 +227,7 @@ string_input(unsigned char *str, int size, int hdrsize, int *rtn_size)
 	else
 		/* result has variable length with maximum size */
 	if (size < 0)
-		size = MIN(len, -size) + 1;
+		size = Min(len, -size) + 1;
 
 	result = (char *) palloc(hdrsize + size);
 	memset(result, 0, hdrsize + size);
diff --git a/contrib/tsearch/gistidx.c b/contrib/tsearch/gistidx.c
index 46d516904b7..25b0d8d1e9f 100644
--- a/contrib/tsearch/gistidx.c
+++ b/contrib/tsearch/gistidx.c
@@ -700,7 +700,7 @@ gtxtidx_picksplit(PG_FUNCTION_ARGS)
 			}
 
 		}
-		costvector[j - 1].cost = abs(size_alpha - size_beta);
+		costvector[j - 1].cost = Abs(size_alpha - size_beta);
 	}
 	qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
 
diff --git a/contrib/tsearch/gistidx.h b/contrib/tsearch/gistidx.h
index 98e974456e9..90ce6a9263e 100644
--- a/contrib/tsearch/gistidx.h
+++ b/contrib/tsearch/gistidx.h
@@ -32,11 +32,6 @@ typedef char *BITVECP;
 #define SETBIT(x,i)   GETBYTE(x,i) |=  ( 0x01 << ( (i) % BITBYTE ) )
 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
 
-#define abs(a)			((a) <	(0) ? -(a) : (a))
-#ifdef min
-#undef min
-#endif
-#define min(a,b)			((a) <	(b) ? (a) : (b))
 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
 
diff --git a/contrib/tsearch2/gistidx.c b/contrib/tsearch2/gistidx.c
index f0a9d8828d9..bf1705036e4 100644
--- a/contrib/tsearch2/gistidx.c
+++ b/contrib/tsearch2/gistidx.c
@@ -634,7 +634,7 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
 		costvector[j - 1].pos = j;
 		size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]));
 		size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]));
-		costvector[j - 1].cost = abs(size_alpha - size_beta);
+		costvector[j - 1].cost = Abs(size_alpha - size_beta);
 	}
 	qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
 
diff --git a/contrib/tsearch2/gistidx.h b/contrib/tsearch2/gistidx.h
index d081c746826..6c8898da867 100644
--- a/contrib/tsearch2/gistidx.h
+++ b/contrib/tsearch2/gistidx.h
@@ -33,8 +33,6 @@ typedef char *BITVECP;
 #define SETBIT(x,i)   GETBYTE(x,i) |=  ( 0x01 << ( (i) % BITBYTE ) )
 #define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
 
-#define abs(a)			((a) <	(0) ? -(a) : (a))
-#define min(a,b)			((a) <	(b) ? (a) : (b))
 #define HASHVAL(val) (((unsigned int)(val)) % SIGLENBIT)
 #define HASH(sign, val) SETBIT((sign), HASHVAL(val))
 
diff --git a/contrib/tsearch2/rank.c b/contrib/tsearch2/rank.c
index a79444250da..3845ddf4921 100644
--- a/contrib/tsearch2/rank.c
+++ b/contrib/tsearch2/rank.c
@@ -165,7 +165,7 @@ calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
 			{
 				for (p = 0; p < lenct; p++)
 				{
-					dist = abs(post[l].pos - ct[p].pos);
+					dist = Abs(post[l].pos - ct[p].pos);
 					if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
 					{
 						float		curw;
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index e3361738ed5..a70ff10860a 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.434 2004/10/15 04:54:31 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.435 2004/10/21 19:28:35 tgl Exp $
  *
  * NOTES
  *
@@ -3702,7 +3702,7 @@ win32_waitpid(int *exitstatus)
 
 	for (offset = 0; offset < win32_numChildren; offset += MAXIMUM_WAIT_OBJECTS)
 	{
-		unsigned long num = min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
+		unsigned long num = Min(MAXIMUM_WAIT_OBJECTS, win32_numChildren - offset);
 
 		ret = WaitForMultipleObjects(num, &win32_childHNDArray[offset], FALSE, 0);
 		switch (ret)
diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
index fff9746e33b..00f48ec4064 100644
--- a/src/interfaces/libpq/fe-exec.c
+++ b/src/interfaces/libpq/fe-exec.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.164 2004/10/18 22:00:42 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.165 2004/10/21 19:28:36 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -111,14 +111,9 @@ static PGresult *PQexecFinish(PGconn *conn);
  * ----------------
  */
 
-#ifdef MAX
-#undef MAX
-#endif
-#define MAX(a,b)  ((a) > (b) ? (a) : (b))
-
 #define PGRESULT_DATA_BLOCKSIZE		2048
 #define PGRESULT_ALIGN_BOUNDARY		MAXIMUM_ALIGNOF		/* from configure */
-#define PGRESULT_BLOCK_OVERHEAD		MAX(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
+#define PGRESULT_BLOCK_OVERHEAD		Max(sizeof(PGresult_data), PGRESULT_ALIGN_BOUNDARY)
 #define PGRESULT_SEP_ALLOC_THRESHOLD	(PGRESULT_DATA_BLOCKSIZE / 2)
 
 
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index c0afa59658a..97f2ea713f4 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -1,5 +1,5 @@
 /*
- * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.61 2004/10/07 15:21:58 momjian Exp $
+ * $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.62 2004/10/21 19:28:36 tgl Exp $
  */
 
 #include "postgres.h"
@@ -272,8 +272,6 @@ pt_in_widget(PG_FUNCTION_ARGS)
 	PG_RETURN_BOOL(point_dt(point, &widget->center) < widget->radius);
 }
 
-#define ABS(X) ((X) >= 0 ? (X) : -(X))
-
 PG_FUNCTION_INFO_V1(boxarea);
 
 Datum
@@ -283,8 +281,8 @@ boxarea(PG_FUNCTION_ARGS)
 	double		width,
 				height;
 
-	width = ABS(box->high.x - box->low.x);
-	height = ABS(box->high.y - box->low.y);
+	width = Abs(box->high.x - box->low.x);
+	height = Abs(box->high.y - box->low.y);
 	PG_RETURN_FLOAT8(width * height);
 }
 
-- 
GitLab