Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:00:00 |
Calls: | 583 |
Files: | 1,139 |
Messages: | 111,529 |
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the
code.
What happens in this case?
The solution I can think of is emulation when evaluating constant expressions. But I don't if any compiler is doing this way.
Thiago Adams <thiago.adams@gmail.com> writes:
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the
code.
What happens in this case?
The solution I can think of is emulation when evaluating constant
expressions. But I don't if any compiler is doing this way.
For example, 65535u + 1u will evaluate to 0u if the target system
has 16-bit int, 65536u otherwise. (I picked an example that
doesn't depend on UINT_MAX or any other macros defined in the
standard library.)
Any compiler, cross- or not, is required to evaluate constant
expressions correctly for the target system. Whether they do so
by some sort of emulation is an implementation detail.
Even a non-cross compiler might not be implemented in exactly
the same language and configuration as the code it's compiling,
so evaluating constant expressions locally might not work correctly.
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the code. What happens in this case?
The solution I can think of is emulation when evaluating constant expressions. But I don't if any compiler is doing this way.
On 2025-08-29, Thiago Adams <thiago.adams@gmail.com> wrote:
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the code. >> What happens in this case?
A constant expression must be evaluated in the way that would happen
if it were translated to code on the target machine.
Thus, if necessary, the features of the target machine's arithmetic must
be simulated on the build machine.
(Modulo issues not relevant to the debate, like if the expression
has ambiguous evaluation orders that affect the result, or undefined >behaviors, they don't have to play out the same way under different
modes of processing in the same implementation.)
The solution I can think of is emulation when evaluating constant
expressions. But I don't if any compiler is doing this way.
They have to; if a constant-folding optimization produces a different
result (in an expression which has no issue) that is then an incorrect >optimization.
GCC uses arbitrary-precision libraries (GNU GMP for integer, and GNU
MPFR for floating-point), which are in part for this issue, I think.
On 2025-08-29, Thiago Adams <thiago.adams@gmail.com> wrote:
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the code. >> What happens in this case?
A constant expression must be evaluated in the way that would happen
if it were translated to code on the target machine.
Thus, if necessary, the features of the target machine's arithmetic must
be simulated on the build machine.
The solution I can think of is emulation when evaluating constant
expressions. But I don't if any compiler is doing this way.
They have to; if a constant-folding optimization produces a different
result (in an expression which has no issue) that is then an incorrect optimization.
On 2025-08-29 16:19, Kaz Kylheku wrote:
On 2025-08-29, Thiago Adams <thiago.adams@gmail.com> wrote:
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the code.
What happens in this case?
A constant expression must be evaluated in the way that would happen
if it were translated to code on the target machine.
Thus, if necessary, the features of the target machine's arithmetic must
be simulated on the build machine.
The solution I can think of is emulation when evaluating constant
expressions. But I don't if any compiler is doing this way.
They have to; if a constant-folding optimization produces a different
result (in an expression which has no issue) that is then an incorrect
optimization.
Emulation is necessary only if the value of the constant expression
changes which code is generated. If the value is simply used by the calculations, then the value can be calculated at run time on the target machine, as if done before the start of main().
Em 29/08/2025 16:54, Keith Thompson escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:yes this is the kind of sample I had in mind.
My curiosity is the following:
Given a constant expression, the result maybe be different in the
machine that compiles the code compared with the machine that runs the
code.
What happens in this case?
The solution I can think of is emulation when evaluating constant
expressions. But I don't if any compiler is doing this way.
For example, 65535u + 1u will evaluate to 0u if the target system
has 16-bit int, 65536u otherwise.-a (I picked an example that
doesn't depend on UINT_MAX or any other macros defined in the
standard library.)
Any compiler, cross- or not, is required to evaluate constantso in
expressions correctly for the target system.-a Whether they do so
by some sort of emulation is an implementation detail.
Even a non-cross compiler might not be implemented in exactly
the same language and configuration as the code it's compiling,
so evaluating constant expressions locally might not work correctly.
So in theory it has to be the same result. This may be hard do achieve.