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 10 4329
Why Tea <yt****@gmail.c omwrites:
>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? */
Why not perform all arithmetic in 64bits?
uint64_t x = SOME_LARGE_NO;
uint64_t y;
y = 10000 * x;
y = x * 10000;
--
Chris.
On Jan 9, 3:43 pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
Why Tea <ytl...@gmail.c omwrites:
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?
*/
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)(1000 0 * 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?
*/
...
Why Tea <yt****@gmail.c omwrites:
>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)(1000 0 * 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).
--
Chris.
Why Tea wrote:
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
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 */
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique http://www.cs.virginia.edu/~lcc-win32
Why Tea wrote:
>
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? */
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.
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
On Jan 9, 12:08*am, CBFalconer <cbfalco...@yah oo.comwrote:
Why Tea wrote:
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? */
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.
Why Tea wrote:
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?
*/
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.
--
Thad
Why Tea wrote:
I understood that the CPU registers determine the size of the
machine.
[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.]
But what is the correct way to code an arithmetic
operation to avoid wrap-around
Do you mean unsigned modulo arithmetic?
when the code is to be run on both 32-
bit and 64-bit machines?
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.
Example:
uint32_t x = SOME_LARGE_NO;
uint64_t y;
Why are you using precise width types?
...
y = (uint64_t)(1000 0 * x); */* Will overflow occur before
the cast? */
Possibly.
y = (int64_t)(x) * 10000; * /* or, is it better to do the
cast first? */
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 <ja...@nospam.c omwrote:
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;
--
Peter
Spiros Bousbouras <spi...@gmail.c omwrote:
CBFalconer <cbfalco...@yah oo.comwrote:
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? */
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.
Not if (say) INT_MAX == 0x7FFFFFFFFF.
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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".
|
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;
|
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
|
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; }
|
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);
| |
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...
|
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...
|
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,
|
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
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |