473,586 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a variable use of variables is what i'm after i think

ben
is there anyway in c to write code that can variably make use of one of
two structs (one that has 32 bit vals and the other that has 64 bit
vals) throughout the code? i'm writing some code that parses some data.
there's a few data types whose max values are 10^10 which requires 34
bits, so a u_int64_t type of variable would be needed to hold them
while using/manipulating. (as i write this the more i think about it
the more i think what i'm hoping for isn't on at all and the answer is
no but anyway..). nearly always, or even always (just with the tiny,
tiny, incredably remote chance they won't) the values in question will
fit into 32 bit values fine. on a 32 bit machine dealing with 64 bit
values throughout the code is going to slow things up i think, so what
i'm wondering is, is there anyway to variably use 32 bit values and 64
bit values somehow? the only way i can think of is just having pretty
much all the code duplicated, once for 32 bit handling and once for 64
bit handling. what i would like to do is set up two structs (one for 32
bit vals the other for 64) then based on an if statement at the start
of the code that determines which should be used, use that struct
throughout. at the end of the day the only way to do that is
duplication of the code that deals with the structs right? i just
thought i'd check in case there is some nifty way to get something like
this that i don't know about.

unions aren't any direct use. function pointers could be of use but
doesn't get round the problem of having to duplicate most, probably
nearly all, of the code. so i guess just use 64 bit vals and be done
with it. does seem a shame though because the real need for over 32 bit
values will be so rare -- although who knows in the future.
Jun 4 '06 #1
10 1802

ben wrote:
is there anyway in c to write code that can variably make use of one of
two structs (one that has 32 bit vals and the other that has 64 bit
vals) throughout the code? i'm writing some code that parses some data.
there's a few data types whose max values are 10^10 which requires 34
bits, so a u_int64_t type of variable would be needed to hold them
while using/manipulating. (as i write this the more i think about it
the more i think what i'm hoping for isn't on at all and the answer is
no but anyway..). nearly always, or even always (just with the tiny,
tiny, incredably remote chance they won't) the values in question will
fit into 32 bit values fine. on a 32 bit machine dealing with 64 bit
values throughout the code is going to slow things up i think, so what
i'm wondering is, is there anyway to variably use 32 bit values and 64
bit values somehow? the only way i can think of is just having pretty
much all the code duplicated, once for 32 bit handling and once for 64
bit handling. what i would like to do is set up two structs (one for 32
bit vals the other for 64) then based on an if statement at the start
of the code that determines which should be used, use that struct
throughout. at the end of the day the only way to do that is
duplication of the code that deals with the structs right? i just
thought i'd check in case there is some nifty way to get something like
this that i don't know about.

unions aren't any direct use. function pointers could be of use but
doesn't get round the problem of having to duplicate most, probably
nearly all, of the code. so i guess just use 64 bit vals and be done
with it. does seem a shame though because the real need for over 32 bit
values will be so rare -- although who knows in the future.


object types can vary in size and thus the range of values they can
represent from platform to platform. In this respect, one can consider
a degree of variability in object types. But this is beyond /your/
control.

As such, I'd probably create my own container
capable of representing some [x,y] range of values.

<OT>
Have you actually profiled your program to determine the net effect
of supporting 64-bit values on a 32-bit system?

It would seem from your post that you are merely speculating that
it would slow things up. But any slowness experienced may actually
be negligible. However, this really all depends on
the nature of the application.
</OT>

--
aegis

Jun 4 '06 #2
ben
In article <11************ ********@g10g20 00cwb.googlegro ups.com>, aegis
<ae***@mad.scie ntist.com> wrote:

object types can vary in size and thus the range of values they can
represent from platform to platform. In this respect, one can consider
a degree of variability in object types. But this is beyond /your/
control.

As such, I'd probably create my own container
capable of representing some [x,y] range of values.
right, ok.

<OT>
Have you actually profiled your program to determine the net effect
of supporting 64-bit values on a 32-bit system?

It would seem from your post that you are merely speculating that
it would slow things up. But any slowness experienced may actually
be negligible. However, this really all depends on
the nature of the application.
</OT>


no i haven't and yes it's all speculation -- it's just something i
thought was a bit wasteful.

ok thanks for the confirmation.

cheers, ben.
Jun 4 '06 #3
In article <04************ ******@x.x>, ben <x@x.x> wrote:
is there anyway in c to write code that can variably make use of one of
two structs (one that has 32 bit vals and the other that has 64 bit
vals) throughout the code?
Not directly, no.

Consider the fact that, at the machine level, you often -- I would
say "always" but there are exceptions to everything -- get different
instruction-sequences for two identical-except-for-type source code
constructs. For instance:

struct foo32 {
uint32_t a, b, c;
};
struct foo64 {
uint64_t a, b, c;
};

#define MUL(p) ((p)->a = (p)->b * (p)->c)

...
struct foo32 x;
struct foo32 y;
...
MUL(&x);
MUL(&y);

This might emit code like:

ld %r1,x+4 # fetch x.b
ld %r2,x+8 # fetch x.c
umul %r1,%r2 # compute (32-bit) product
st %r1,x # store result

ldx %r1,y+8 # fetch y.b
ldx %r2,y+16 # fetch y.c
mulx %r1,%r2 # compute (64-bit) product
stx %r1,y # store result

Here, the C compiler has used the type information in the source
code -- that the members of "x" are "uint32_t"s , while those in
"y" are "uint64_t"s -- to generate different code: the offsets
for y.b and y.c differ from those for x.b and x.c; the sizes of
all the operands differ; and the "mul" instruction for x uses 32
bit arithmetic while that for "y" uses 64-bit arithmetic.
i'm writing some code that parses some data.
there's a few data types whose max values are 10^10 which requires 34
bits, so a u_int64_t type of variable would be needed to hold them
while using/manipulating.


In that case, write the code using "unsigned long long" (or uint64_t
if you really want to restrict it to *exactly* 64 bits, which seems
like overkill). Then, if and only if it turns out to be "too slow",
profile the code, find where it spends "too much time", and optimize
it once it actually works.

Note that you can use macros to generate "equivalent code except
for types". This works becaues the preprocessor phase is ignorant
of C semantics: it merely expands tokens, and C's type-information
is more deeply embedded, so that individual tokens carry their
types on into the C compiler. The MUL example above is a trivial
one (so trivial that it probably should just be expanded in-line
"by hand", but this was meant to be a simple example). In other
words, the preprocessor's inability to "see" types is both a drawback
(type-checking is impossible) and a feature (type-independence is
natural).
--
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.
Jun 4 '06 #4
ben
In article <e5*********@ne ws3.newsguy.com >, Chris Torek
<no****@torek.n et> wrote:
In article <04************ ******@x.x>, ben <x@x.x> wrote:
is there anyway in c to write code that can variably make use of one of
two structs (one that has 32 bit vals and the other that has 64 bit
vals) throughout the code?
Not directly, no.

Consider the fact that, at the machine level, you often -- I would
say "always" but there are exceptions to everything -- get different
instruction-sequences for two identical-except-for-type source code
constructs. For instance:

struct foo32 {
uint32_t a, b, c;
};
struct foo64 {
uint64_t a, b, c;
};

#define MUL(p) ((p)->a = (p)->b * (p)->c)

...
struct foo32 x;
struct foo32 y;
...
MUL(&x);
MUL(&y);

This might emit code like:

ld %r1,x+4 # fetch x.b
ld %r2,x+8 # fetch x.c
umul %r1,%r2 # compute (32-bit) product
st %r1,x # store result

ldx %r1,y+8 # fetch y.b
ldx %r2,y+16 # fetch y.c
mulx %r1,%r2 # compute (64-bit) product
stx %r1,y # store result

Here, the C compiler has used the type information in the source
code -- that the members of "x" are "uint32_t"s , while those in
"y" are "uint64_t"s -- to generate different code: the offsets
for y.b and y.c differ from those for x.b and x.c; the sizes of
all the operands differ; and the "mul" instruction for x uses 32
bit arithmetic while that for "y" uses 64-bit arithmetic.


right, yes. i hadn't considered it in such detail but i had started to
think/realise that there was no possibility for flexibility in the way
i wanted -- the code is very much bound to the types, as you've
illustrated above.
i'm writing some code that parses some data.
there's a few data types whose max values are 10^10 which requires 34
bits, so a u_int64_t type of variable would be needed to hold them
while using/manipulating.


In that case, write the code using "unsigned long long" (or uint64_t
if you really want to restrict it to *exactly* 64 bits, which seems
like overkill). Then, if and only if it turns out to be "too slow",
profile the code, find where it spends "too much time", and optimize
it once it actually works.


don't understand why you think using uint64_t is overkill. the values
that i said have max values of 10^10 -- they really are never going to
be more than that -- the format of the data itself if fixed in that
way. i guess in the future long long might be 128 bits. that would be
overkill for a 34 bit value right? i'm just picking the smallest size
that the value fit into.

yes i'll just use a larger value. and profile when done. i'm sure it'll
be fine.
Note that you can use macros to generate "equivalent code except
for types". This works becaues the preprocessor phase is ignorant
of C semantics: it merely expands tokens, and C's type-information
is more deeply embedded, so that individual tokens carry their
types on into the C compiler. The MUL example above is a trivial
one (so trivial that it probably should just be expanded in-line
"by hand", but this was meant to be a simple example). In other
words, the preprocessor's inability to "see" types is both a drawback
(type-checking is impossible) and a feature (type-independence is
natural).


right ok.

thanks very much for that. 64 bit variables it is.
ben.
Jun 4 '06 #5
>In article <e5*********@ne ws3.newsguy.com >, Chris Torek
<no****@torek. net> wrote:
In that case, write the code using "unsigned long long" (or uint64_t
if you really want to restrict it to *exactly* 64 bits, which seems
like overkill).

In article <04************ ******@x.x> ben <x@x.x> wrote:don't understand why you think using uint64_t is overkill. the values
that i said have max values of 10^10 -- they really are never going to
be more than that -- the format of the data itself if fixed in that
way. i guess in the future long long might be 128 bits. that would be
overkill for a 34 bit value right? i'm just picking the smallest size
that the value fit into.


Exercise: The *smallest* size it fits into is uint34_t. Why not
use that? And if there are some reasons not to use that, do they
also apply to uint64_t?

Specifically, uintN_t (for any N) tells the (C99) compiler: "don't
you dare use anything other than an exactly-2-sup-N type, no matter
how <censored> slow it might be." The "native" types (char, short,
int, long, and long long, and their signed and unsigned variants)
are less "forceful" requests: "please get me a type that holds at
least 127, 32767, etc" and the compiler can use whatever makes them
go fastest / smallest-code / "best".

In other words, when you use "int" or "long" or similar, you are
cutting the compiler some slack.
--
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.
Jun 5 '06 #6
ben
In article <e5********@new s1.newsguy.com> , Chris Torek
<no****@torek.n et> wrote:
In article <04************ ******@x.x> ben <x@x.x> wrote:
don't understand why you think using uint64_t is overkill. the values
that i said have max values of 10^10 -- they really are never going to
be more than that -- the format of the data itself if fixed in that
way. i guess in the future long long might be 128 bits. that would be
overkill for a 34 bit value right? i'm just picking the smallest size
that the value fit into.
Exercise: The *smallest* size it fits into is uint34_t. Why not
use that?


because it doesn't exist :/
And if there are some reasons not to use that, do they
also apply to uint64_t?
no; because it exists.

Specifically, uintN_t (for any N) tells the (C99) compiler: "don't
you dare use anything other than an exactly-2-sup-N type, no matter
how <censored> slow it might be." The "native" types (char, short,
int, long, and long long, and their signed and unsigned variants)
are less "forceful" requests: "please get me a type that holds at
least 127, 32767, etc" and the compiler can use whatever makes them
go fastest / smallest-code / "best".

In other words, when you use "int" or "long" or similar, you are
cutting the compiler some slack.


oh right, i see. ok i'll definetely use an unsigned long long then --
thanks very much for all the info -- much appreciated.

ben.
Jun 7 '06 #7
ben <x@x.x> writes:
In article <e5********@new s1.newsguy.com> , Chris Torek
<no****@torek.n et> wrote:
In article <04************ ******@x.x> ben <x@x.x> wrote:
>don't understand why you think using uint64_t is overkill. the values
>that i said have max values of 10^10 -- they really are never going to
>be more than that -- the format of the data itself if fixed in that
>way. i guess in the future long long might be 128 bits. that would be
>overkill for a 34 bit value right? i'm just picking the smallest size
>that the value fit into.


Exercise: The *smallest* size it fits into is uint34_t. Why not
use that?


because it doesn't exist :/
And if there are some reasons not to use that, do they
also apply to uint64_t?


no; because it exists.


Correction: it *probably* exists (on any C99 conforming
implementation) . If there is no 64-bit unsigned integer type with no
padding bits, it won't define uint64_t. All C99 implementations are
required to support unsigned long long, which is required to have a
width of at least 64 bits, so the only ways uint64_t will be defined
*unless* unsigned long long either is bigger than 64 bits or has
padding bits.

--
Keith Thompson (The_Other_Keit h) 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.
Jun 7 '06 #8

"Chris Torek" <no****@torek.n et> wrote in message
news:e5******** @news1.newsguy. com...
In article <e5*********@ne ws3.newsguy.com >, Chris Torek
<no****@torek .net> wrote:
In that case, write the code using "unsigned long long" (or uint64_t
if you really want to restrict it to *exactly* 64 bits, which seems
like overkill).


In article <04************ ******@x.x> ben <x@x.x> wrote:
don't understand why you think using uint64_t is overkill. the values
that i said have max values of 10^10 -- they really are never going to
be more than that -- the format of the data itself if fixed in that
way. i guess in the future long long might be 128 bits. that would be
overkill for a 34 bit value right? i'm just picking the smallest size
that the value fit into.


Exercise: The *smallest* size it fits into is uint34_t. Why not
use that? And if there are some reasons not to use that, do they
also apply to uint64_t?

Specifically, uintN_t (for any N) tells the (C99) compiler: "don't
you dare use anything other than an exactly-2-sup-N type, no matter
how <censored> slow it might be."

It also tells the maintaining programmer that this type is, for some reason,
naturally an exact number of bits. For instance if an integer holds rgba
pixel values, with 256 levels for each channel, it makes sense to call it an
int32_t. If it just has to hold a big number, like the size of file in
bytes, make an int or a long.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm
Jun 8 '06 #9
ben
In article <ln************ @nuthaus.mib.or g>, Keith Thompson
<ks***@mib.or g> wrote:
ben <x@x.x> writes:
In article <e5********@new s1.newsguy.com> , Chris Torek
<no****@torek.n et> wrote:
In article <04************ ******@x.x> ben <x@x.x> wrote:
>don't understand why you think using uint64_t is overkill. the values
>that i said have max values of 10^10 -- they really are never going to
>be more than that -- the format of the data itself if fixed in that
>way. i guess in the future long long might be 128 bits. that would be
>overkill for a 34 bit value right? i'm just picking the smallest size
>that the value fit into.

Exercise: The *smallest* size it fits into is uint34_t. Why not
use that?


because it doesn't exist :/
And if there are some reasons not to use that, do they
also apply to uint64_t?


no; because it exists.


Correction: it *probably* exists (on any C99 conforming
implementation) . If there is no 64-bit unsigned integer type with no
padding bits, it won't define uint64_t. All C99 implementations are
required to support unsigned long long, which is required to have a
width of at least 64 bits, so the only ways uint64_t will be defined
*unless* unsigned long long either is bigger than 64 bits or has
padding bits.


righy hoe -- ok, thanks.
Jun 8 '06 #10

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

Similar topics

83
6459
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
134
7831
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you...
166
8573
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
24
2518
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think that I am using them appropriate in class in question. One typical example: This class initiates TCP session, keeps sending commands to the server...
7
2158
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've got is a decodeImage.aspx page that gets called to decode base64 encoded images and return them as displayable images to a Web page. The ASPX page is...
78
4927
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only problem is that my little helper function doesn't work! It claims that a variable doesn't exist. If I move the variable declaration, it finds the...
148
5471
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
17
5077
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any guidance appreciated. Regards
10
12785
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem involving variable scope in JavaScript.) I have a lot of code that generates HTML on the fly. This code has tags with id attributes derived from...
112
5392
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
7912
marktang
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...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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. ...
1
7959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8216
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...
1
5710
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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...
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.