Skip to content

Commit 357cfef

Browse files
committed
Use C library functions instead of Abs() for int64
Instead of Abs() for int64, use the C standard functions labs() or llabs() as appropriate. Define a small wrapper around them that matches our definition of int64. (labs() is C90, llabs() is C99.) Reviewed-by: Zhang Mingli <zmlpostgres@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/4beb42b5-216b-bce8-d452-d924d5794c63%40enterprisedb.com
1 parent 06dbd61 commit 357cfef

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

contrib/btree_gist/btree_cash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ cash_dist(PG_FUNCTION_ARGS)
106106
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107107
errmsg("money out of range")));
108108

109-
ra = Abs(r);
109+
ra = i64abs(r);
110110

111111
PG_RETURN_CASH(ra);
112112
}

contrib/btree_gist/btree_int8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int8_dist(PG_FUNCTION_ARGS)
106106
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107107
errmsg("bigint out of range")));
108108

109-
ra = Abs(r);
109+
ra = i64abs(r);
110110

111111
PG_RETURN_INT64(ra);
112112
}

src/backend/utils/adt/datetime.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4468,7 +4468,7 @@ AddVerboseIntPart(char *cp, int64 value, const char *units,
44684468
if (*is_zero)
44694469
{
44704470
*is_before = (value < 0);
4471-
value = Abs(value);
4471+
value = i64abs(value);
44724472
}
44734473
else if (*is_before)
44744474
value = -value;
@@ -4569,8 +4569,8 @@ EncodeInterval(struct pg_itm *itm, int style, char *str)
45694569

45704570
sprintf(cp, "%c%d-%d %c%lld %c%lld:%02d:",
45714571
year_sign, abs(year), abs(mon),
4572-
day_sign, (long long) Abs(mday),
4573-
sec_sign, (long long) Abs(hour), abs(min));
4572+
day_sign, (long long) i64abs(mday),
4573+
sec_sign, (long long) i64abs(hour), abs(min));
45744574
cp += strlen(cp);
45754575
cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
45764576
*cp = '\0';
@@ -4642,7 +4642,7 @@ EncodeInterval(struct pg_itm *itm, int style, char *str)
46424642
sprintf(cp, "%s%s%02lld:%02d:",
46434643
is_zero ? "" : " ",
46444644
(minus ? "-" : (is_before ? "+" : "")),
4645-
(long long) Abs(hour), abs(min));
4645+
(long long) i64abs(hour), abs(min));
46464646
cp += strlen(cp);
46474647
cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
46484648
*cp = '\0';

src/backend/utils/adt/dbsize.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ pg_size_pretty(PG_FUNCTION_ARGS)
564564
uint8 bits;
565565

566566
/* use this unit if there are no more units or we're below the limit */
567-
if (unit[1].name == NULL || Abs(size) < unit->limit)
567+
if (unit[1].name == NULL || i64abs(size) < unit->limit)
568568
{
569569
if (unit->round)
570570
size = half_rounded(size);

src/include/c.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,15 @@ extern int fdatasync(int fildes);
12821282
#define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
12831283
#endif
12841284

1285+
/*
1286+
* Similarly, wrappers around labs()/llabs() matching our int64.
1287+
*/
1288+
#ifdef HAVE_LONG_INT_64
1289+
#define i64abs(i) labs(i)
1290+
#else
1291+
#define i64abs(i) llabs(i)
1292+
#endif
1293+
12851294
/*
12861295
* Use "extern PGDLLIMPORT ..." to declare variables that are defined
12871296
* in the core backend and need to be accessible by loadable modules.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy