Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Is addition/subtraction the same with constant added/removed
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mirhagk




PostPosted: Wed Sep 25, 2013 9:14 am   Post subject: Is addition/subtraction the same with constant added/removed

Basically is

code:
x = n;

The same as
code:
x = (n + k) - k;


For all values of n and k on computer. n + k might overflow, but assuming it is silent about overflow will subtracting k always fix it.

Furthermore will it work with any amount of add and subtract. Ie can a compiler arbitrarily add and subtract constants to a value so long as overflows don't have to throw exceptions?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Sep 25, 2013 10:27 am   Post subject: RE:Is addition/subtraction the same with constant added/removed

In cases like this, it's really up to the compiler. Dumb compilers will translate this as is, in which case it will take a few more instructions. Smart compilers like gcc will simplify a lot of expressions, in this case reducing it to a single memory assignment.
DemonWasp




PostPosted: Wed Sep 25, 2013 10:34 am   Post subject: RE:Is addition/subtraction the same with constant added/removed

Your answer might depend on when the compiler is doing this. If this is done at compile-time, I can't see any negative effects. If it's done at run-time, then there are a few obvious problems:

- obvious optimization problem
- concurrent contexts may require a mutex while x is being modified

Of course, all of this relies on the exact representation of X in hardware, the hardware semantics, and the semantics of the language in question. For example, in C, if X is unsigned (or maybe it's N and K that matter, it's hard to know with C) then that's valid. However, if X is signed, then overflows cause undefined behaviour (because don't we just love that phrase). Other systems will use saturating arithmetic or define overflow behaviour differently.

All of this assumes integers. With IEEE 754 floating-point numbers, that operation might cause significant loss of precision and other problems.

Why?
mirhagk




PostPosted: Wed Sep 25, 2013 9:58 pm   Post subject: RE:Is addition/subtraction the same with constant added/removed

okay, so let's assume that it's hand compiled into assembly, and it's MIPS, and it's use the non-trapping (no exceptions) addition command for integers.

From the looks of it, it wraps the values around from positive to negative, and back the same way, so I think that it should be fine.

I'm also trying to find any information on assembly language level hacks, specifically MIPS ones. For fun I'm trying to shave off as many instructions off of the code as I can, and I'm curious how short I can get my code
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: