Skip to content

Commit 2613b74

Browse files
committed
Factor out the common subexpression month_remainder * DAYS_PER_MONTH
in interval_mul and interval_div. This avoids an optimization bug in A Certain Company's compiler (and given their explanation, I wouldn't be surprised if other compilers blow it too). Besides the code seems more clear this way --- in the original formulation, you had to mentally recognize the common subexpression in order to understand what was going on.
1 parent ca4cf09 commit 2613b74

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.150 2005/08/25 03:53:22 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.151 2005/08/25 05:01:43 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2262,7 +2262,7 @@ interval_mul(PG_FUNCTION_ARGS)
22622262
{
22632263
Interval *span = PG_GETARG_INTERVAL_P(0);
22642264
float8 factor = PG_GETARG_FLOAT8(1);
2265-
double month_remainder, day_remainder;
2265+
double month_remainder, day_remainder, month_remainder_days;
22662266
Interval *result;
22672267

22682268
result = (Interval *) palloc(sizeof(Interval));
@@ -2276,17 +2276,15 @@ interval_mul(PG_FUNCTION_ARGS)
22762276

22772277
/* Cascade fractions to lower units */
22782278
/* fractional months full days into days */
2279-
result->day += month_remainder * DAYS_PER_MONTH;
2279+
month_remainder_days = month_remainder * DAYS_PER_MONTH;
2280+
result->day += month_remainder_days;
22802281
/* fractional months partial days into time */
2281-
day_remainder += (month_remainder * DAYS_PER_MONTH) -
2282-
(int)(month_remainder * DAYS_PER_MONTH);
2282+
day_remainder += month_remainder_days - (int) month_remainder_days;
22832283

22842284
#ifdef HAVE_INT64_TIMESTAMP
2285-
result->time = rint(span->time * factor +
2286-
day_remainder * USECS_PER_DAY);
2285+
result->time = rint(span->time * factor + day_remainder * USECS_PER_DAY);
22872286
#else
2288-
result->time = JROUND(span->time * factor +
2289-
day_remainder * SECS_PER_DAY);
2287+
result->time = JROUND(span->time * factor + day_remainder * SECS_PER_DAY);
22902288
#endif
22912289

22922290
result = DatumGetIntervalP(DirectFunctionCall1(interval_justify_hours,
@@ -2309,7 +2307,7 @@ interval_div(PG_FUNCTION_ARGS)
23092307
{
23102308
Interval *span = PG_GETARG_INTERVAL_P(0);
23112309
float8 factor = PG_GETARG_FLOAT8(1);
2312-
double month_remainder, day_remainder;
2310+
double month_remainder, day_remainder, month_remainder_days;
23132311
Interval *result;
23142312

23152313
result = (Interval *) palloc(sizeof(Interval));
@@ -2329,10 +2327,10 @@ interval_div(PG_FUNCTION_ARGS)
23292327

23302328
/* Cascade fractions to lower units */
23312329
/* fractional months full days into days */
2332-
result->day += month_remainder * DAYS_PER_MONTH;
2330+
month_remainder_days = month_remainder * DAYS_PER_MONTH;
2331+
result->day += month_remainder_days;
23332332
/* fractional months partial days into time */
2334-
day_remainder += (month_remainder * DAYS_PER_MONTH) -
2335-
(int)(month_remainder * DAYS_PER_MONTH);
2333+
day_remainder += month_remainder_days - (int) month_remainder_days;
23362334

23372335
#ifdef HAVE_INT64_TIMESTAMP
23382336
result->time += rint(day_remainder * USECS_PER_DAY);

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