Thread: [HACKERS] random_fract and aix compiler bug

[HACKERS] random_fract and aix compiler bug

From
"Zeugswetter Andreas SB SD"
Date:
Hi Peter,

While fighting with a compiler bug here, I came across the following code,
that loops here (on AIX 4.3.2) due to a compiler bug,
presumably with the z < MAX_RANDOM_VALUE (it even warns at compile time):

static double
random_fract(void)
{
        long            z;

        /* random() can produce endpoint values, try again if so */
        do
        {
                z = random();
        } while (!(z > 0 && z < MAX_RANDOM_VALUE));
        return (double) z / (double) MAX_RANDOM_VALUE;
}

Since you where the last to commit on analyze.c I send you this mail,
since my mails currently don't pass some filter on postgresql.org

I have attached a patch as a workaround for this silly compiler bug.
Please consider to apply, Thanks

Andreas

Attachment

Re: [HACKERS] random_fract and aix compiler bug

From
Tom Lane
Date:
"Zeugswetter Andreas SB SD" <ZeugswetterA@spardat.at> writes:

!     } while (!(z > 0 && z < MAX_RANDOM_VALUE));

!     } while (z == 0 || z == MAX_RANDOM_VALUE);

This seems fairly ugly, since it eliminates the original coding's
positive guarantee that the final result is in 0 < x < 1.  Does your
compiler manage not to choke if we add a cast instead?

    } while (!(z > 0 && z < (long) MAX_RANDOM_VALUE));

            regards, tom lane

Re: [HACKERS] random_fract and aix compiler bug

From
Andrew Dunstan
Date:
Tom Lane wrote:

>"Zeugswetter Andreas SB SD" <ZeugswetterA@spardat.at> writes:
>
>!     } while (!(z > 0 && z < MAX_RANDOM_VALUE));
>
>!     } while (z == 0 || z == MAX_RANDOM_VALUE);
>
>This seems fairly ugly, since it eliminates the original coding's
>positive guarantee that the final result is in 0 < x < 1.  Does your
>compiler manage not to choke if we add a cast instead?
>
>    } while (!(z > 0 && z < (long) MAX_RANDOM_VALUE));
>
>
>

Or put an "L" suffix on the value in pg_config_manual.h, so it gets
picked up everywhere?

cheers

andrew


Re: [HACKERS] random_fract and aix compiler bug

From
"Zeugswetter Andreas SB SD"
Date:
> !     } while (!(z > 0 && z < MAX_RANDOM_VALUE));
>
> !     } while (z == 0 || z == MAX_RANDOM_VALUE);
>
> This seems fairly ugly, since it eliminates the original coding's
> positive guarantee that the final result is in 0 < x < 1.  Does your

yes, ugly :-(

> compiler manage not to choke if we add a cast instead?
>
>     } while (!(z > 0 && z < (long) MAX_RANDOM_VALUE));
>

this does unfortunately not help, have also tried all sorts of other casts :-(

Would we be happy with the following, which would work ?

        } while (z <= 0 || z == MAX_RANDOM_VALUE);

Andreas

Re: [HACKERS] random_fract and aix compiler bug

From
Tom Lane
Date:
"Zeugswetter Andreas SB SD" <ZeugswetterA@spardat.at> writes:
> Would we be happy with the following, which would work ?
>         } while (z <= 0 || z == MAX_RANDOM_VALUE);

I suppose this doesn't?
          } while (z <= 0 || z >= MAX_RANDOM_VALUE);

            regards, tom lane

Re: [HACKERS] random_fract and aix compiler bug

From
"Zeugswetter Andreas SB SD"
Date:
> > Would we be happy with the following, which would work ?
> >         } while (z <= 0 || z == MAX_RANDOM_VALUE);
>
> I suppose this doesn't?
>           } while (z <= 0 || z >= MAX_RANDOM_VALUE);

Ah, yes ! That also works and is a lot nicer. Can you please apply ?

Together with the patch in Bruce's queue, the only minor open issue is now
with the -i switch for postmaster in the getaddrinfo_all area
(on this machine a direct nameservice request answers "host not found"),
and the geometry and horology regression expected files.

Thanks
Andreas

Re: [HACKERS] random_fract and aix compiler bug

From
Tom Lane
Date:
"Zeugswetter Andreas SB SD" <ZeugswetterA@spardat.at> writes:
>> } while (z <= 0 || z >= MAX_RANDOM_VALUE);

> Ah, yes ! That also works and is a lot nicer. Can you please apply ?

Done.

            regards, tom lane