473,509 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting intermediate results in 64 bits

Hi,

Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;

the result will be clobbered to 32 bits because that's the length of an
integer. How do I keep all the intermediate steps at 64 bits without
peppering my operands with (uint64_t) casts?

It would help if my default sizeof(int) were 8, I guess. How can I
obtain this?
Nov 14 '05 #1
14 2434
In article <d3**********@nnrp.waia.asn.au>,
Richard Cavell <ri***********@mail.com> wrote:
Using GCC on my G4, if I have a calculation like this: #include <stdint.h> uint64_t a = 0xffff * 0xffff ;
Integral values explicitly specified default to 'int' unless there
is a type modifier or the context provides reason to use a wider
type.

the result will be clobbered to 32 bits because that's the length of an
integer. How do I keep all the intermediate steps at 64 bits without
peppering my operands with (uint64_t) casts?
In terms of code changes, you could use:

uint64_t a = 0xffff * (unit64_t) 0xffff ;

or you could use

uint64_t a = 0xffffLU * 0xffffLU;

It would help if my default sizeof(int) were 8, I guess. How can I
obtain this?


That is implimentation specific. The version of gcc that I have handy
does not list options for the G4; I suggest you check your gcc
man page under the section "Configuration Dependent Options";
and if you do not find it there then try looking at the .info files
that a lot of the real documentation is in for FSF products.

As an example, on MIPS systems (e.g., SGI), there is -mint64

--
"I want to make sure [a user] can't get through ... an online
experience without hitting a Microsoft ad"
-- Steve Ballmer [Microsoft Chief Executive]
Nov 14 '05 #2
Richard Cavell <ri***********@mail.com> writes:
Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;

the result will be clobbered to 32 bits because that's the length of
an integer. How do I keep all the intermediate steps at 64 bits
without peppering my operands with (uint64_t) casts?


Designate one operand as type `unsigned long long':
uint64_t a = 0xffffULL * 0xffff;
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #3
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <d3**********@nnrp.waia.asn.au>,
Richard Cavell <ri***********@mail.com> wrote:
Using GCC on my G4, if I have a calculation like this:

#include <stdint.h>

uint64_t a = 0xffff * 0xffff ;


Integral values explicitly specified default to 'int' unless there
is a type modifier or the context provides reason to use a wider
type.


The type of an expression is not affected by the context in which it
appears. (A null pointer constant in a pointer context is the only
exception I can think of.) If it were, 0xffff and the result of the
multiplication would inherit the uint64_t type provided by the
context.

Very often the result of an expression is implicitly converted to some
type imposed by its context. For example (assuming 32-bit int),
given:

uint64_t a = 0xffff;

the constant 0xffff is of type int, but the result is implicitly
converted to uint64_t before being used to initialize a. But these
context-driven implicit conversions happen only on a single level;
the uint64_t context in

uint64_t a = 0xffff * 0xffff;

affects only the result of the multiplication, not the operands.

This makes using the fixed-width types in <stdint.h> tricky. You can
declare everything to be of some fixed size, and never refer to the
predefined types short, int, long, et al, but the ranges of the
predefined types can still affect the semantics of your code. The
language provides suffixes for signed and unsigned long and long long,
but not for the fixed-width types. Casts are probably the best
approach (one of the rare cases where casts are useful in portable
code).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote:
The type of an expression is not affected by the context in which it
appears. (A null pointer constant in a pointer context is the only
exception I can think of.)
I think this is not such a good way to put it. It might be better
to say something like "contexts are extremely local".
If it were, 0xffff and the result of the multiplication would
inherit the uint64_t type provided by the [result] context [since this was "uint64_t result = expr1 * expr2"].
Very often the result of an expression is implicitly converted to some
type imposed by its context. For example (assuming 32-bit int),
given:

uint64_t a = 0xffff;

the constant 0xffff is of type int, but the result is implicitly
converted to uint64_t before being used to initialize a. But these
context-driven implicit conversions happen only on a single level;
the uint64_t context in

uint64_t a = 0xffff * 0xffff;

affects only the result of the multiplication, not the operands.
Just so: the context is as localized as compiler-ly possible, which
may often be "more local" than the programmer intends.
This makes using the fixed-width types in <stdint.h> tricky. You can
declare everything to be of some fixed size, and never refer to the
predefined types short, int, long, et al, but the ranges of the
predefined types can still affect the semantics of your code. ...


Indeed. Worse, the "value preserving" rules make the effects of
widening unpredictable in "questionably signed" cases, so that:

uint16_t a, b, c;
...
if (a >= (b - c))

gives different results on 16- and 32-bit machines: the former uses
an unsigned compare, and the latter a signed compare.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5
Richard Cavell wrote:
Using GCC on my G4, if I have a calculation like this:
Technically GCC and G4 are off topic here. But whatever.
#include <stdint.h>
Wow, lucky you -- you happen to have one of these lying around.
uint64_t a = 0xffff * 0xffff ;

the result will be clobbered to 32 bits because that's the length
of an integer. How do I keep all the intermediate steps at 64 bits
without peppering my operands with (uint64_t) casts?


Actually take a look into stdint.h more closely and you will see:

uint64_t a = UINT64_C(0xffff) * UINT64_C(0xffff);

That's the official way anyhow. Not much better than casting, but it
works. Of course if we were taking into account that you are using
gcc, then the answer is a little more simple:

unsigned long long a = 0xffffull * 0xffffull;

But then, this is not portable.

---
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 14 '05 #6
Chris Torek <no****@torek.net> writes:
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote:
The type of an expression is not affected by the context in which it
appears. (A null pointer constant in a pointer context is the only
exception I can think of.)


I think this is not such a good way to put it. It might be better
to say something like "contexts are extremely local".


Hmm. I still like the way I phrased it.

Given:

short a = 10;
int b = 20;
long c = 30;

all three integer constants are of type int. The constant 10 is then
implicitly converted to type short, and 30 is implicitly converted to
type long, but I think of the implicit conversions as separate
operations, not impositions of the context type on the expression.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Keith Thompson wrote:
Chris Torek <no****@torek.net> writes:
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote:
The type of an expression is not affected by the context in
which it appears. (A null pointer constant in a pointer

context is the only exception I can think of.)

I think this is not such a good way to put it. It might be better
to say something like "contexts are extremely local".


Hmm. I still like the way I phrased it.
Given:

short a = 10;
int b = 20;
long c = 30;

all three integer constants are of type int.


What did you have in mind with the "A null pointer constant..."
exception? In

void *p = 0;
or
memset(0, 0, 0);

the 0s are all ints (which then undergo conversion to pointers).

Nov 14 '05 #8
"Old Wolf" <ol*****@inspire.net.nz> writes:
Keith Thompson wrote:
Chris Torek <no****@torek.net> writes:
> In article <ln************@nuthaus.mib.org>
> Keith Thompson <ks***@mib.org> wrote:
>>The type of an expression is not affected by the context in
>>which it appears. (A null pointer constant in a pointer

context is the only exception I can think of.)
>
> I think this is not such a good way to put it. It might be better
> to say something like "contexts are extremely local".


Hmm. I still like the way I phrased it.
Given:

short a = 10;
int b = 20;
long c = 30;

all three integer constants are of type int.


What did you have in mind with the "A null pointer constant..."
exception? In

void *p = 0;
or
memset(0, 0, 0);

the 0s are all ints (which then undergo conversion to pointers).


What I had in mind was not entirely coherent. Null pointer constants
are not an exception.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
Keith Thompson <ks***@mib.org> writes:
"Old Wolf" <ol*****@inspire.net.nz> writes:
[snip]
> Keith Thompson <ks***@mib.org> wrote:
>>The type of an expression is not affected by the context in
>>which it appears. (A null pointer constant in a pointer
>>context is the only exception I can think of.)
[snip]


What did you have in mind with the "A null pointer constant..."
exception? [...]


What I had in mind was not entirely coherent. Null pointer constants
are not an exception.


So what you meant to say was, A null pointer constant in a pointer
context is the only exception you can think of, and even that's not an
exception? :)

On a related topic - would anyone like to opine as to whether

int (*pf)(int);
pf = (void*)(int*)(void*) 0;

is permitted by the standard or not? How about

pf = (void*)(int*) 0;

or

pf = (void*)(void*) 0;

The statements in 6.3.2.3 p3 and p4 seem to imply that the right hand
side expressions are not null pointer constants.
Nov 14 '05 #10
Tim Rentsch <tx*@alumnus.caltech.edu> writes:
Keith Thompson <ks***@mib.org> writes:
"Old Wolf" <ol*****@inspire.net.nz> writes: [snip]
> What did you have in mind with the "A null pointer constant..."
> exception? [...]


What I had in mind was not entirely coherent. Null pointer constants
are not an exception.


So what you meant to say was, A null pointer constant in a pointer
context is the only exception you can think of, and even that's not an
exception? :)


Exactly, except that I didn't think of the "and even that's not an
exception" part until later.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
Actually take a look into stdint.h more closely and you will see:

uint64_t a = UINT64_C(0xffff) * UINT64_C(0xffff);


Does it distribute across any arbitrary number of operands?

uint64_t a = UINT64_C ( b + c - d * e % f << 3 ) ;
Nov 14 '05 #12
Richard Cavell wrote:
Actually take a look into stdint.h more closely and you will see:

uint64_t a = UINT64_C(0xffff) * UINT64_C(0xffff);


Does it distribute across any arbitrary number of operands?

uint64_t a = UINT64_C ( b + c - d * e % f << 3 ) ;


It's only for constants.
Christian

Nov 14 '05 #13
Richard Cavell wrote:
Actually take a look into stdint.h more closely and you will see:

uint64_t a = UINT64_C(0xffff) * UINT64_C(0xffff);


Does it distribute across any arbitrary number of operands?

uint64_t a = UINT64_C ( b + c - d * e % f << 3 ) ;


It's only for constants.
Christian

Nov 14 '05 #14
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
uint64_t a = 0xffffLU * 0xffffLU;


This assumes that unsigned long is 64 bits wide.

DES
--
Dag-Erling Smørgrav - de*@des.no
Nov 14 '05 #15

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

Similar topics

303
17421
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
14
1797
by: Brad Tilley | last post by:
Hello, What is the proper way to limit the results of division to only a few spaces after the decimal? I don't need rocket-science like precision. Here's an example: 1.775 is as exact as I...
4
1450
by: Richard Cavell | last post by:
Hi, If I'm doing something like this: #include<stdint.h> uint64_t i = (0xffff * 0xffff ) ; Or indeed any arbitrarily long expression, how do I ensure the intermediate results won't be...
10
2399
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
3
9923
by: JerryK | last post by:
Hi, I have an ASP.net page, written in VB.net. In that code I want to access a validator web services that is secured via SSL. I have installed the certificate, via a .pfx file, on the system....
22
26673
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
4
374
by: tnowles00 | last post by:
Hi, Using GCC on my G4, if I have a calculation like this: #include <stdint.h> uint64_t a = 0xffff * 0xffff ; the result will be clobbered to 32 bits because that's the length of an...
16
2699
by: Jorge | last post by:
Webkit r34469 vs. Opera 9.50 : 3.00x as fast 6339.6ms(Opera) 2109.8ms (Webkit) ----- FF3.0 (final) vs. Opera 9.50 : 1.94x as fast 6339.6ms (Opera) 3269.6ms (FF3)
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7412
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...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5652
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,...
1
5060
isladogs
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...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
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...

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.