Is PG built on any C compilers where int division floors?

Started by Chapman Flackover 7 years ago4 messages
#1Chapman Flack
chap@anastigmatix.net

C99 finally pinned down what / does on signed ints, truncating toward zero.

Before that, it could truncate toward zero, or floor toward -inf.

Is PostgreSQL built on any compilers/platforms that have the floor
behavior?

-Chap

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chapman Flack (#1)
Re: Is PG built on any C compilers where int division floors?

Chapman Flack <chap@anastigmatix.net> writes:

C99 finally pinned down what / does on signed ints, truncating toward zero.
Before that, it could truncate toward zero, or floor toward -inf.
Is PostgreSQL built on any compilers/platforms that have the floor
behavior?

I'm not sure if we still have any buildfarm animals that act that way[1]gaur/pademelon might, but it's turned off and 300 miles away, so I can't check right now.,
but the project policy is that we target C90 not C99. So wiring in any
assumptions about this would seem to be contrary to policy.

regards, tom lane

[1]: gaur/pademelon might, but it's turned off and 300 miles away, so I can't check right now.
I can't check right now.

#3Chapman Flack
chap@anastigmatix.net
In reply to: Tom Lane (#2)
Re: Is PG built on any C compilers where int division floors?

On 06/24/18 23:38, Tom Lane wrote:

Chapman Flack <chap@anastigmatix.net> writes:

C99 finally pinned down what / does on signed ints, truncating toward zero.
Before that, it could truncate toward zero, or floor toward -inf.
Is PostgreSQL built on any compilers/platforms that have the floor
behavior?

I'm not sure if we still have any buildfarm animals that act that way[1],
but the project policy is that we target C90 not C99. So wiring in any

On a related note, does PG itself specify the behavior of its own signed int
/ and % (and div() and mod() functions)? Should it? I've been looking at

https://www.postgresql.org/docs/11/static/functions-math.html

and that doesn't specify. (There's a column of examples for the operators,
where the operands are positive. Changing nothing else, it might be more
revealing to toss in a negative-operand example: do we have div(-9,4) = -2
with mod(-9,4) = -1, or div(-9,4) = -3 with mod(-9,4) = +3 ?)

I don't see it specified in the standard either (2006 draft) - doesn't even
pay the issue enough notice to say it's implementation-defined.

Do PG's operators just do what the underlying C compiler generates?

Also, the PG manual doesn't seem to say whether >> is arithmetic or logical.
(I just tried it here and it was arithmetic; I assume it's consistent, but
would it be worth saying?)

-Chap

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chapman Flack (#3)
Re: Is PG built on any C compilers where int division floors?

Chapman Flack <chap@anastigmatix.net> writes:

Do PG's operators just do what the underlying C compiler generates?

Yes. This is generally true for both the int and float operators.

regards, tom lane