Arithmetic for 32-bit and 64-bit machines

W

Why Tea

I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
....

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Please comment.

/Why Tea
 
C

Chris McDonald

Why Tea said:
I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

uint32_t x = SOME_LARGE_NO;
uint64_t y;
...
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Why not perform all arithmetic in 64bits?

uint64_t x = SOME_LARGE_NO;
uint64_t y;

y = 10000 * x;
y = x * 10000;
 
W

Why Tea

Why not perform all arithmetic in 64bits?

uint64_t x = SOME_LARGE_NO;
uint64_t y;

y = 10000 * x;
y = x * 10000;

Thanks Chris. But assuming the "x" is a 32-bit variable declared
somewhere else, and there is no easy way to change it to uint64_t; and
also it can be passed as a function parameter such as
some_func(1000*x,...), so what you mentioned would not work.

Let me rephrase the question:

void somefunc(uint64_t y, ....);
....

uint32_t x;
uint64_t y;
....
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/
....
somefunc(1000*x,....); /* Overflow can occur here on 32-bit machine?
*/
...
 
C

Chris McDonald

Why Tea said:
Thanks Chris. But assuming the "x" is a 32-bit variable declared
somewhere else, and there is no easy way to change it to uint64_t; and
also it can be passed as a function parameter such as
some_func(1000*x,...), so what you mentioned would not work.
Let me rephrase the question:
void somefunc(uint64_t y, ....);
...
uint32_t x;
uint64_t y;
...
y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/
...
somefunc(1000*x,....); /* Overflow can occur here on 32-bit machine?
*/
..


Then quickly use: int64_t x64 = x;

If somefunc() is correctly prototyped and visible at the point of calling
it, 10000*x should be promoted to 64bits as

(10000LL * (int64_t)x).
 
J

jacob navia

Why said:
I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
...

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Please comment.

/Why Tea

Do operations in 64 bit arithmetic
The second is better. Or even better
> y = (int64_t)(x) * 10000LL; /* put constant in 64 bits using LL
suffix */
 
C

CBFalconer

Why said:
I understood that the CPU registers determine the size of the
machine. But what is the correct way to code an arithmetic
operation to avoid wrap-around when the code is to be run on
both 32-bit and 64-bit machines?

Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
...

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first? */

Yes and yes. For the first, within the parentheses you are
operating with integers, so overflow occurs, and the results are
undefined. For the second x is cast to long long, so to perform
arithmetic 10000 is automaticall also converted to long long, and
the arithmetic is done in long long.
 
S

Spiros Bousbouras

Yes and yes.  For the first, within the parentheses you are
operating with integers, so overflow occurs, and the results are
undefined.

Since x has been declared as unsigned the results are perfectly
defined.
 
T

Thad Smith

Why said:
I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
...

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

If ints are at least 64 bits, they are the same. Otherwise, the first will
yield a product which is either uint32_t or int, which ever is larger. The
second will yield a product which is uint64_t or int, whichever is larger.
The tie breaker goes to the unsigned type.

Bottom line: do the cast first.
 
P

Peter Nilsson

[OT: If you say so. Some people use the size of the address
bus, e.g. the original Macs used an m68k and was considered
a 16-bit machine even though registers are 32-bits.]

Do you mean unsigned modulo arithmetic?

If you need a 64-bit result, then it doesn't matter whether
you're using a 32 or 64-bit machine, you use a 64-bit type,
if available.

Why are you using precise width types?
Possibly.

Did you really mean to cast with (uint64_t)? Your cast will
work, but it's not obvious why you're switching between
signednesss.

The 'factor' of 10000 is enough to avoid overflow in either
case, but a more robust solution would be to preserve the
signness in cases where the factor might be (say) 0xF0000000.

jacob navia said:
Do operations in 64  bit arithmetic
The second is better. Or even better
 > y = (int64_t)(x) * 10000LL;  /* put constant in 64 bits
using LL suffix */

How is this better? The presence of the cast on x means the
constant will be converted (if necessary) to a type that is
at least 64-bits anyway.

If the result is truly meant to be a 64-bit unsigned value
from a multiplication of two non-negative 32-bit values,
one of which is explicitly unsigned, then the following is
adequate...

y = x * 10000ull;

Even if unsigned long long is 128-bits (say), an optimiser
should be able to recognise that only 64-bits is significant
and a 64-bit calculation is all that is needed.

Alternatively...

y = x;
y *= 10000;
 
C

cr88192

Why Tea said:
I understood that the CPU registers determine the size of the machine.
But what is the correct way to code an arithmetic operation to avoid
wrap-around when the code is to be run on both 32-bit and 64-bit
machines?

it depends on who you ask...

it is perfectly possible to have a 32 bit machine with some 64 or 128 bit
registers (as is the case with MMX and SSE).

the issue is not quite as clear-cut as it may seem.


Example:

uint32_t x = SOME_LARGE_NO;
uint64_t y;
...

y = (uint64_t)(10000 * x); /* Will overflow occur before the cast? */
y = (int64_t)(x) * 10000; /* or, is it better to do the cast first?
*/

Please comment.

my answer:
y = ((int64_t)x) * 10000;

reason:
casts change the output type, not the input type.

so, yeah, in the first case, the overflow may well occur before cast to the
larger type. however, afaik, what happens, exactly, is up to the compiler
(for example, it may internally upcast when loading, for example because the
arch may have a load-and-extend instruction, wheras to load and then up-cast
would involve additional instructions).

and so on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top