473,941 Members | 10,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arithmetic for 32-bit and 64-bit machines

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)(1000 0 * 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
Jan 9 '08
10 4351

"Why Tea" <yt****@gmail.c omwrote in message
news:a9******** *************** ***********@p69 g2000hsa.google groups.com...
>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)(1000 0 * 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...

/Why Tea
Jan 10 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
1817
by: Stephen Biggs | last post by:
Given this code: void f(void){} int main(void){return (int)f+5;} Is there anything wrong with this in terms of the standards? Is this legal C code? One compiler I'm working with compiles this quietly, even with the most stringent and pedantic ANSI and warning levels, but generates code that only loads the address of "f" and fails to make the addition before returning a value from "main".
5
1661
by: joshc | last post by:
After reading the Standard section 6.3 multiple times and consulting K&R I can't seem to figure out what would happen in the following case assuming longs are 32 bits(have typedefs for this): unsigned long int result; signed long int x, y; x = 0x7FFF8000; y = -0x8000;
43
26619
by: Mehta Shailendrakumar | last post by:
Hello, Can anyone suggest me operator to perform arithmetic shift in C? May it be for a perticular compiler. Thank you in advance. Regards, Shailendra
6
2313
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
7
528
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
3112
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
2
1682
by: jbe3d | last post by:
Here is a snippet of code that returns 0 (c = any number) d = 5 / 9 * (c-32); here it works as I expect it should, resulting in the expected numeric value d = (c-32) * 5 / 9; I'm spending time reading though MSDN but have only come up with an indication that that (c-32) in the first example might be an empty expression. Thanks in advance for helping me to understand why the expression gives two different results when the two sides...
7
3141
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
27
2077
by: jacob navia | last post by:
As Richard Bos rightly pointed out, I had left in my classification of types the C99 types Complex and boolean. Here is a new classification. Those are not mentioned in the classification of Plauger and Brody, probably because their work predates C99. Since there are no examples of this in the literature (known to me) please take a look. Thanks
7
3819
by: GCRhoads | last post by:
I'm looking for a very basic high-precision arithmetic library. I need to be able to specify a fixed number of bits or decimal digits (32 decimal digits should be all I need). The only arithmetic functions I need are addition, subtraction, multiplication, division, square-root, and comparison (i.e. less-than, greater-than, equal). Preferably this should be in C++ but I could use C by putting everything into a single class. I looked at a...
0
11529
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11111
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3505
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.