Skip to content

Commit f14aad5

Browse files
committed
Remove unnecessary uses of Abs()
Use C standard abs() or fabs() instead. 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 0fe954c commit f14aad5

26 files changed

+54
-49
lines changed

contrib/btree_gist/btree_date.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ gdb_date_dist(const void *a, const void *b, FmgrInfo *flinfo)
9595
DateADTGetDatum(*((const DateADT *) a)),
9696
DateADTGetDatum(*((const DateADT *) b)));
9797

98-
return (float8) Abs(DatumGetInt32(diff));
98+
return (float8) abs(DatumGetInt32(diff));
9999
}
100100

101101

@@ -123,7 +123,7 @@ date_dist(PG_FUNCTION_ARGS)
123123
PG_GETARG_DATUM(0),
124124
PG_GETARG_DATUM(1));
125125

126-
PG_RETURN_INT32(Abs(DatumGetInt32(diff)));
126+
PG_RETURN_INT32(abs(DatumGetInt32(diff)));
127127
}
128128

129129

contrib/btree_gist/btree_float8.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ gbt_float8_dist(const void *a, const void *b, FmgrInfo *flinfo)
7979
r = arg1 - arg2;
8080
if (unlikely(isinf(r)) && !isinf(arg1) && !isinf(arg2))
8181
float_overflow_error();
82-
return Abs(r);
82+
return fabs(r);
8383
}
8484

8585

@@ -110,7 +110,7 @@ float8_dist(PG_FUNCTION_ARGS)
110110
if (unlikely(isinf(r)) && !isinf(a) && !isinf(b))
111111
float_overflow_error();
112112

113-
PG_RETURN_FLOAT8(Abs(r));
113+
PG_RETURN_FLOAT8(fabs(r));
114114
}
115115

116116
/**************************************************

contrib/btree_gist/btree_int2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int2_dist(PG_FUNCTION_ARGS)
105105
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
106106
errmsg("smallint out of range")));
107107

108-
ra = Abs(r);
108+
ra = abs(r);
109109

110110
PG_RETURN_INT16(ra);
111111
}

contrib/btree_gist/btree_int4.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int4_dist(PG_FUNCTION_ARGS)
106106
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
107107
errmsg("integer out of range")));
108108

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

111111
PG_RETURN_INT32(ra);
112112
}

contrib/btree_gist/btree_interval.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ intr2num(const Interval *i)
8383
static float8
8484
gbt_intv_dist(const void *a, const void *b, FmgrInfo *flinfo)
8585
{
86-
return (float8) Abs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
86+
return fabs(intr2num((const Interval *) a) - intr2num((const Interval *) b));
8787
}
8888

8989
/*

contrib/btree_gist/btree_time.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ gbt_time_dist(const void *a, const void *b, FmgrInfo *flinfo)
118118
i = DatumGetIntervalP(DirectFunctionCall2(time_mi_time,
119119
TimeADTGetDatumFast(*aa),
120120
TimeADTGetDatumFast(*bb)));
121-
return (float8) Abs(INTERVAL_TO_SEC(i));
121+
return fabs(INTERVAL_TO_SEC(i));
122122
}
123123

124124

contrib/btree_gist/btree_ts.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ gbt_ts_dist(const void *a, const void *b, FmgrInfo *flinfo)
123123
i = DatumGetIntervalP(DirectFunctionCall2(timestamp_mi,
124124
TimestampGetDatumFast(*aa),
125125
TimestampGetDatumFast(*bb)));
126-
return (float8) Abs(INTERVAL_TO_SEC(i));
126+
return fabs(INTERVAL_TO_SEC(i));
127127
}
128128

129129

contrib/btree_gist/btree_utils_num.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ typedef struct
8787
(ivp)->day * (24.0 * SECS_PER_HOUR) + \
8888
(ivp)->month * (30.0 * SECS_PER_DAY))
8989

90-
#define GET_FLOAT_DISTANCE(t, arg1, arg2) Abs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
90+
#define GET_FLOAT_DISTANCE(t, arg1, arg2) fabs( ((float8) *((const t *) (arg1))) - ((float8) *((const t *) (arg2))) )
9191

9292

9393
extern Interval *abs_interval(Interval *a);

contrib/btree_gist/btree_utils_var.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ gbt_var_penalty(float *res, const GISTENTRY *o, const GISTENTRY *n,
426426
tmp[1] = (unsigned char) (((VARSIZE(uk.lower) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.lower)[ul]));
427427
tmp[2] = (unsigned char) (((VARSIZE(ok.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(ok.upper)[ul]));
428428
tmp[3] = (unsigned char) (((VARSIZE(uk.upper) - VARHDRSZ) <= ul) ? 0 : (VARDATA(uk.upper)[ul]));
429-
dres = Abs(tmp[0] - tmp[1]) + Abs(tmp[3] - tmp[2]);
429+
dres = abs(tmp[0] - tmp[1]) + abs(tmp[3] - tmp[2]);
430430
dres /= 256.0;
431431
}
432432

contrib/cube/cube.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ rt_cube_size(NDBOX *a, double *size)
925925
{
926926
result = 1.0;
927927
for (i = 0; i < DIM(a); i++)
928-
result *= Abs(UR_COORD(a, i) - LL_COORD(a, i));
928+
result *= fabs(UR_COORD(a, i) - LL_COORD(a, i));
929929
}
930930
*size = result;
931931
}

contrib/intarray/_int_gist.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "postgres.h"
55

66
#include <limits.h>
7+
#include <math.h>
78

89
#include "_int.h"
910
#include "access/gist.h"
@@ -539,7 +540,7 @@ g_int_picksplit(PG_FUNCTION_ARGS)
539540
union_d = inner_int_union(datum_r, datum_alpha);
540541
rt__int_size(union_d, &size_beta);
541542
pfree(union_d);
542-
costvector[i - 1].cost = Abs((size_alpha - size_l) - (size_beta - size_r));
543+
costvector[i - 1].cost = fabs((size_alpha - size_l) - (size_beta - size_r));
543544
}
544545
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
545546

contrib/intarray/_intbig_gist.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
#include "postgres.h"
55

6+
#include <math.h>
7+
68
#include "_int.h"
79
#include "access/gist.h"
810
#include "access/reloptions.h"
@@ -389,7 +391,7 @@ g_intbig_picksplit(PG_FUNCTION_ARGS)
389391
_j = GETENTRY(entryvec, j);
390392
size_alpha = hemdist(datum_l, _j, siglen);
391393
size_beta = hemdist(datum_r, _j, siglen);
392-
costvector[j - 1].cost = Abs(size_alpha - size_beta);
394+
costvector[j - 1].cost = abs(size_alpha - size_beta);
393395
}
394396
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
395397

contrib/ltree/_ltree_gist.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
#include "postgres.h"
99

10+
#include <math.h>
11+
1012
#include "access/gist.h"
1113
#include "access/reloptions.h"
1214
#include "access/stratnum.h"
@@ -315,7 +317,7 @@ _ltree_picksplit(PG_FUNCTION_ARGS)
315317
_j = GETENTRY(entryvec, j);
316318
size_alpha = hemdist(datum_l, _j, siglen);
317319
size_beta = hemdist(datum_r, _j, siglen);
318-
costvector[j - 1].cost = Abs(size_alpha - size_beta);
320+
costvector[j - 1].cost = abs(size_alpha - size_beta);
319321
}
320322
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
321323

contrib/seg/seg.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ restore(char *result, float val, int n)
952952
}
953953
else
954954
{
955-
if (Abs(exp) <= 4)
955+
if (abs(exp) <= 4)
956956
{
957957
/*
958958
* remove the decimal point from the mantissa and write the digits
@@ -1039,7 +1039,7 @@ restore(char *result, float val, int n)
10391039
}
10401040
}
10411041

1042-
/* do nothing for Abs(exp) > 4; %e must be OK */
1042+
/* do nothing for abs(exp) > 4; %e must be OK */
10431043
/* just get rid of zeroes after [eE]- and +zeroes after [Ee]. */
10441044

10451045
/* ... this is not done yet. */

src/backend/access/gist/gistproc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,8 @@ gist_box_picksplit(PG_FUNCTION_ARGS)
797797
for (i = 0; i < commonEntriesCount; i++)
798798
{
799799
box = DatumGetBoxP(entryvec->vector[commonEntries[i].index].key);
800-
commonEntries[i].delta = Abs(float8_mi(box_penalty(leftBox, box),
801-
box_penalty(rightBox, box)));
800+
commonEntries[i].delta = fabs(float8_mi(box_penalty(leftBox, box),
801+
box_penalty(rightBox, box)));
802802
}
803803

804804
/*

src/backend/optimizer/geqo/geqo_erx.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ gimme_edge(PlannerInfo *root, Gene gene1, Gene gene2, Edge *edge_table)
162162

163163
for (i = 0; i < edges; i++)
164164
{
165-
if ((Gene) Abs(edge_table[city1].edge_list[i]) == city2)
165+
if ((Gene) abs(edge_table[city1].edge_list[i]) == city2)
166166
{
167167

168168
/* mark shared edges as negative */
@@ -249,14 +249,14 @@ remove_gene(PlannerInfo *root, Gene gene, Edge edge, Edge *edge_table)
249249

250250
for (i = 0; i < edge.unused_edges; i++)
251251
{
252-
possess_edge = (int) Abs(edge.edge_list[i]);
252+
possess_edge = abs(edge.edge_list[i]);
253253
genes_remaining = edge_table[possess_edge].unused_edges;
254254

255255
/* find the input gene in all edge_lists and delete it */
256256
for (j = 0; j < genes_remaining; j++)
257257
{
258258

259-
if ((Gene) Abs(edge_table[possess_edge].edge_list[j]) == gene)
259+
if ((Gene) abs(edge_table[possess_edge].edge_list[j]) == gene)
260260
{
261261

262262
edge_table[possess_edge].unused_edges--;
@@ -307,7 +307,7 @@ gimme_gene(PlannerInfo *root, Edge edge, Edge *edge_table)
307307
* converting to absolute values
308308
*/
309309
if (friend < 0)
310-
return (Gene) Abs(friend);
310+
return (Gene) abs(friend);
311311

312312

313313
/*

src/backend/partitioning/partbounds.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3206,7 +3206,7 @@ check_new_partition_bound(char *relname, Relation parent,
32063206
* datums list.
32073207
*/
32083208
PartitionRangeDatum *datum =
3209-
list_nth(spec->upperdatums, Abs(cmpval) - 1);
3209+
list_nth(spec->upperdatums, abs(cmpval) - 1);
32103210

32113211
/*
32123212
* The new partition overlaps with the
@@ -3232,7 +3232,7 @@ check_new_partition_bound(char *relname, Relation parent,
32323232
* if we have equality, point to the first one.
32333233
*/
32343234
datum = cmpval == 0 ? linitial(spec->lowerdatums) :
3235-
list_nth(spec->lowerdatums, Abs(cmpval) - 1);
3235+
list_nth(spec->lowerdatums, abs(cmpval) - 1);
32363236
overlap = true;
32373237
overlap_location = datum->location;
32383238
with = boundinfo->indexes[offset + 1];

src/backend/utils/adt/datetime.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
448448
Assert(precision >= 0);
449449

450450
if (fillzeros)
451-
cp = pg_ultostr_zeropad(cp, Abs(sec), 2);
451+
cp = pg_ultostr_zeropad(cp, abs(sec), 2);
452452
else
453-
cp = pg_ultostr(cp, Abs(sec));
453+
cp = pg_ultostr(cp, abs(sec));
454454

455455
/* fsec_t is just an int32 */
456456
if (fsec != 0)
457457
{
458-
int32 value = Abs(fsec);
458+
int32 value = abs(fsec);
459459
char *end = &cp[precision + 1];
460460
bool gotnonzero = false;
461461

@@ -490,7 +490,7 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
490490
* which will generate a correct answer in the minimum valid width.
491491
*/
492492
if (value)
493-
return pg_ultostr(cp, Abs(fsec));
493+
return pg_ultostr(cp, abs(fsec));
494494

495495
return end;
496496
}

src/backend/utils/adt/numeric.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -8870,7 +8870,7 @@ div_var_fast(const NumericVar *var1, const NumericVar *var2,
88708870
if (qdigit != 0)
88718871
{
88728872
/* Do we need to normalize now? */
8873-
maxdiv += Abs(qdigit);
8873+
maxdiv += abs(qdigit);
88748874
if (maxdiv > (INT_MAX - INT_MAX / NBASE - 1) / (NBASE - 1))
88758875
{
88768876
/*
@@ -8923,7 +8923,7 @@ div_var_fast(const NumericVar *var1, const NumericVar *var2,
89238923
fquotient = fdividend * fdivisorinverse;
89248924
qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
89258925
(((int) fquotient) - 1); /* truncate towards -infinity */
8926-
maxdiv += Abs(qdigit);
8926+
maxdiv += abs(qdigit);
89278927
}
89288928

89298929
/*
@@ -9107,7 +9107,7 @@ div_var_int(const NumericVar *var, int ival, int ival_weight,
91079107
* become as large as divisor * NBASE - 1, and so it requires a 64-bit
91089108
* integer if this exceeds UINT_MAX.
91099109
*/
9110-
divisor = Abs(ival);
9110+
divisor = abs(ival);
91119111

91129112
if (divisor <= UINT_MAX / NBASE)
91139113
{
@@ -9948,7 +9948,7 @@ exp_var(const NumericVar *arg, NumericVar *result, int rscale)
99489948

99499949
/* Guard against overflow/underflow */
99509950
/* If you change this limit, see also power_var()'s limit */
9951-
if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
9951+
if (fabs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
99529952
{
99539953
if (val > 0)
99549954
ereport(ERROR,
@@ -9966,15 +9966,15 @@ exp_var(const NumericVar *arg, NumericVar *result, int rscale)
99669966
* Reduce x to the range -0.01 <= x <= 0.01 (approximately) by dividing by
99679967
* 2^ndiv2, to improve the convergence rate of the Taylor series.
99689968
*
9969-
* Note that the overflow check above ensures that Abs(x) < 6000, which
9969+
* Note that the overflow check above ensures that fabs(x) < 6000, which
99709970
* means that ndiv2 <= 20 here.
99719971
*/
9972-
if (Abs(val) > 0.01)
9972+
if (fabs(val) > 0.01)
99739973
{
99749974
ndiv2 = 1;
99759975
val /= 2;
99769976

9977-
while (Abs(val) > 0.01)
9977+
while (fabs(val) > 0.01)
99789978
{
99799979
ndiv2++;
99809980
val /= 2;
@@ -10116,7 +10116,7 @@ estimate_ln_dweight(const NumericVar *var)
1011610116
*----------
1011710117
*/
1011810118
ln_var = log((double) digits) + dweight * 2.302585092994046;
10119-
ln_dweight = (int) log10(Abs(ln_var));
10119+
ln_dweight = (int) log10(fabs(ln_var));
1012010120
}
1012110121
else
1012210122
{
@@ -10427,7 +10427,7 @@ power_var(const NumericVar *base, const NumericVar *exp, NumericVar *result)
1042710427
val = numericvar_to_double_no_overflow(&ln_num);
1042810428

1042910429
/* initial overflow/underflow test with fuzz factor */
10430-
if (Abs(val) > NUMERIC_MAX_RESULT_SCALE * 3.01)
10430+
if (fabs(val) > NUMERIC_MAX_RESULT_SCALE * 3.01)
1043110431
{
1043210432
if (val > 0)
1043310433
ereport(ERROR,
@@ -10583,7 +10583,7 @@ power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
1058310583
* Now we can proceed with the multiplications.
1058410584
*/
1058510585
neg = (exp < 0);
10586-
mask = Abs(exp);
10586+
mask = abs(exp);
1058710587

1058810588
init_var(&base_prod);
1058910589
set_var_from_var(base, &base_prod);

src/backend/utils/adt/rangetypes_gist.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,8 @@ range_gist_picksplit(PG_FUNCTION_ARGS)
743743
emptyCount = total_count - nonEmptyCount;
744744

745745
if (infCount > 0 && nonInfCount > 0 &&
746-
(Abs(infCount - nonInfCount) <=
747-
Abs(emptyCount - nonEmptyCount)))
746+
(abs(infCount - nonInfCount) <=
747+
abs(emptyCount - nonEmptyCount)))
748748
{
749749
classes_groups[CLS_NORMAL] = SPLIT_RIGHT;
750750
classes_groups[CLS_CONTAIN_EMPTY] = SPLIT_RIGHT;

src/backend/utils/adt/selfuncs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7835,7 +7835,7 @@ brincostestimate(PlannerInfo *root, IndexPath *path, double loop_count,
78357835
double varCorrelation = 0.0;
78367836

78377837
if (sslot.nnumbers > 0)
7838-
varCorrelation = Abs(sslot.numbers[0]);
7838+
varCorrelation = fabs(sslot.numbers[0]);
78397839

78407840
if (varCorrelation > *indexCorrelation)
78417841
*indexCorrelation = varCorrelation;

src/backend/utils/adt/timestamp.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ interval_mul(PG_FUNCTION_ARGS)
32903290
* cascade from months and days. It might still be >24 if the combination
32913291
* of cascade and the seconds factor operation itself.
32923292
*/
3293-
if (Abs(sec_remainder) >= SECS_PER_DAY)
3293+
if (fabs(sec_remainder) >= SECS_PER_DAY)
32943294
{
32953295
result->day += (int) (sec_remainder / SECS_PER_DAY);
32963296
sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;
@@ -3347,7 +3347,7 @@ interval_div(PG_FUNCTION_ARGS)
33473347
sec_remainder = (orig_day / factor - result->day +
33483348
month_remainder_days - (int) month_remainder_days) * SECS_PER_DAY;
33493349
sec_remainder = TSROUND(sec_remainder);
3350-
if (Abs(sec_remainder) >= SECS_PER_DAY)
3350+
if (fabs(sec_remainder) >= SECS_PER_DAY)
33513351
{
33523352
result->day += (int) (sec_remainder / SECS_PER_DAY);
33533353
sec_remainder -= (int) (sec_remainder / SECS_PER_DAY) * SECS_PER_DAY;

src/backend/utils/adt/tsgistidx.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ gtsvector_picksplit(PG_FUNCTION_ARGS)
700700
costvector[j - 1].pos = j;
701701
size_alpha = hemdistcache(&(cache[seed_1]), &(cache[j]), siglen);
702702
size_beta = hemdistcache(&(cache[seed_2]), &(cache[j]), siglen);
703-
costvector[j - 1].cost = Abs(size_alpha - size_beta);
703+
costvector[j - 1].cost = abs(size_alpha - size_beta);
704704
}
705705
qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
706706

0 commit comments

Comments
 (0)