Use C library functions instead of Abs() for int64
authorPeter Eisentraut <[email protected]>
Mon, 10 Oct 2022 06:51:07 +0000 (08:51 +0200)
committerPeter Eisentraut <[email protected]>
Mon, 10 Oct 2022 07:01:17 +0000 (09:01 +0200)
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 <[email protected]>
Reviewed-by: Tom Lane <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/4beb42b5-216b-bce8-d452-d924d5794c63%40enterprisedb.com

contrib/btree_gist/btree_cash.c
contrib/btree_gist/btree_int8.c
src/backend/utils/adt/datetime.c
src/backend/utils/adt/dbsize.c
src/include/c.h

index 6ac97e2b12a806a74b0c1025bc26cb621e2bd648..546b948ea400a413174d5c3120d5c04296703702 100644 (file)
@@ -106,7 +106,7 @@ cash_dist(PG_FUNCTION_ARGS)
                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                 errmsg("money out of range")));
 
-   ra = Abs(r);
+   ra = i64abs(r);
 
    PG_RETURN_CASH(ra);
 }
index df2b0d174b965fc96baf1cb1467372aa999f6229..7c63a5b6dc1f84e2eb1178ace39b7a3440b1aac8 100644 (file)
@@ -106,7 +106,7 @@ int8_dist(PG_FUNCTION_ARGS)
                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
                 errmsg("bigint out of range")));
 
-   ra = Abs(r);
+   ra = i64abs(r);
 
    PG_RETURN_INT64(ra);
 }
index a8b025f43fa176719aaa0b37dfc6d962d5535eb7..4e9935e01dde78b8d81bd082c8881e9cfe0ed692 100644 (file)
@@ -4468,7 +4468,7 @@ AddVerboseIntPart(char *cp, int64 value, const char *units,
    if (*is_zero)
    {
        *is_before = (value < 0);
-       value = Abs(value);
+       value = i64abs(value);
    }
    else if (*is_before)
        value = -value;
@@ -4569,8 +4569,8 @@ EncodeInterval(struct pg_itm *itm, int style, char *str)
 
                    sprintf(cp, "%c%d-%d %c%lld %c%lld:%02d:",
                            year_sign, abs(year), abs(mon),
-                           day_sign, (long long) Abs(mday),
-                           sec_sign, (long long) Abs(hour), abs(min));
+                           day_sign, (long long) i64abs(mday),
+                           sec_sign, (long long) i64abs(hour), abs(min));
                    cp += strlen(cp);
                    cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
                    *cp = '\0';
@@ -4642,7 +4642,7 @@ EncodeInterval(struct pg_itm *itm, int style, char *str)
                sprintf(cp, "%s%s%02lld:%02d:",
                        is_zero ? "" : " ",
                        (minus ? "-" : (is_before ? "+" : "")),
-                       (long long) Abs(hour), abs(min));
+                       (long long) i64abs(hour), abs(min));
                cp += strlen(cp);
                cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
                *cp = '\0';
index 34efa121b404aabd93494072561d7fdecd03ec35..0a9b93f26345735aec1b2ea698dd6ba348ec1ac2 100644 (file)
@@ -564,7 +564,7 @@ pg_size_pretty(PG_FUNCTION_ARGS)
        uint8       bits;
 
        /* use this unit if there are no more units or we're below the limit */
-       if (unit[1].name == NULL || Abs(size) < unit->limit)
+       if (unit[1].name == NULL || i64abs(size) < unit->limit)
        {
            if (unit->round)
                size = half_rounded(size);
index c8f72e44d894f3d83d2dd53fc19ce10a3847807d..405d53cb56b9c78e327dcd297676813af1eaa579 100644 (file)
@@ -1282,6 +1282,15 @@ extern int   fdatasync(int fildes);
 #define strtou64(str, endptr, base) ((uint64) strtoull(str, endptr, base))
 #endif
 
+/*
+ * Similarly, wrappers around labs()/llabs() matching our int64.
+ */
+#ifdef HAVE_LONG_INT_64
+#define i64abs(i) labs(i)
+#else
+#define i64abs(i) llabs(i)
+#endif
+
 /*
  * Use "extern PGDLLIMPORT ..." to declare variables that are defined
  * in the core backend and need to be accessible by loadable modules.