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.
This commit is contained in:
Tom Lane 2005-08-25 05:01:43 +00:00
parent ca4cf09232
commit 2613b74785

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.150 2005/08/25 03:53:22 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.151 2005/08/25 05:01:43 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -2262,7 +2262,7 @@ interval_mul(PG_FUNCTION_ARGS)
{ {
Interval *span = PG_GETARG_INTERVAL_P(0); Interval *span = PG_GETARG_INTERVAL_P(0);
float8 factor = PG_GETARG_FLOAT8(1); float8 factor = PG_GETARG_FLOAT8(1);
double month_remainder, day_remainder; double month_remainder, day_remainder, month_remainder_days;
Interval *result; Interval *result;
result = (Interval *) palloc(sizeof(Interval)); result = (Interval *) palloc(sizeof(Interval));
@ -2276,17 +2276,15 @@ interval_mul(PG_FUNCTION_ARGS)
/* Cascade fractions to lower units */ /* Cascade fractions to lower units */
/* fractional months full days into days */ /* fractional months full days into days */
result->day += month_remainder * DAYS_PER_MONTH; month_remainder_days = month_remainder * DAYS_PER_MONTH;
result->day += month_remainder_days;
/* fractional months partial days into time */ /* fractional months partial days into time */
day_remainder += (month_remainder * DAYS_PER_MONTH) - day_remainder += month_remainder_days - (int) month_remainder_days;
(int)(month_remainder * DAYS_PER_MONTH);
#ifdef HAVE_INT64_TIMESTAMP #ifdef HAVE_INT64_TIMESTAMP
result->time = rint(span->time * factor + result->time = rint(span->time * factor + day_remainder * USECS_PER_DAY);
day_remainder * USECS_PER_DAY);
#else #else
result->time = JROUND(span->time * factor + result->time = JROUND(span->time * factor + day_remainder * SECS_PER_DAY);
day_remainder * SECS_PER_DAY);
#endif #endif
result = DatumGetIntervalP(DirectFunctionCall1(interval_justify_hours, result = DatumGetIntervalP(DirectFunctionCall1(interval_justify_hours,
@ -2309,7 +2307,7 @@ interval_div(PG_FUNCTION_ARGS)
{ {
Interval *span = PG_GETARG_INTERVAL_P(0); Interval *span = PG_GETARG_INTERVAL_P(0);
float8 factor = PG_GETARG_FLOAT8(1); float8 factor = PG_GETARG_FLOAT8(1);
double month_remainder, day_remainder; double month_remainder, day_remainder, month_remainder_days;
Interval *result; Interval *result;
result = (Interval *) palloc(sizeof(Interval)); result = (Interval *) palloc(sizeof(Interval));
@ -2329,10 +2327,10 @@ interval_div(PG_FUNCTION_ARGS)
/* Cascade fractions to lower units */ /* Cascade fractions to lower units */
/* fractional months full days into days */ /* fractional months full days into days */
result->day += month_remainder * DAYS_PER_MONTH; month_remainder_days = month_remainder * DAYS_PER_MONTH;
result->day += month_remainder_days;
/* fractional months partial days into time */ /* fractional months partial days into time */
day_remainder += (month_remainder * DAYS_PER_MONTH) - day_remainder += month_remainder_days - (int) month_remainder_days;
(int)(month_remainder * DAYS_PER_MONTH);
#ifdef HAVE_INT64_TIMESTAMP #ifdef HAVE_INT64_TIMESTAMP
result->time += rint(day_remainder * USECS_PER_DAY); result->time += rint(day_remainder * USECS_PER_DAY);