Time travel would be fine if it were limited to things which have no causal relationship. For example, giventepples wrote:That and the general trend of newer versions of C and C++ compilers becoming more pedantic about treating signed overflow and other undefined behaviors as an excuse to make unexpectedly aggressive optimizations. Raymond Chen has referred to this aggression as "time travel". Unsigned addition, subtraction, and multiplication, on the other hand, are carefully defined to wrap around modulo [type]_MAX + 1.rainwarrior wrote:Though honestly, this entire problem only happens with division.
Code: Select all
#include <math.h>
volatile int x,y;
void test(long long temp)
{
while(temp & 1234)
temp = 123456789123456789*sin(temp);
if (x) y=temp;
}
Unfortunately, "modern" compilers use Undefined Behavior not only as an excuse to engage in time travel, but also to negate the laws of causality. For example, given:
Code: Select all
unsigned mul_mod_65536(unsigned short x, unsigned short y)
{ return x*y & 0xFFFF; }
volatile unsigned q;
unsigned test(unsigned short x)
{
unsigned sum=0;
x|=0x8000;
for (int i=0x8000; i<=x; i++)
{
q=mul_mod_65536(65535,i);
sum++;
}
return sum;
}