变量的变量使用是我想的 [英] a variable use of variables is what i'm after i think

查看:72
本文介绍了变量的变量使用是我想的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论如何在c中编写代码可以变化地使用

两个结构之一(一个具有32位val,另一个具有64位

vals)整个代码?我正在编写一些解析一些数据的代码。

有一些数据类型,其最大值为10 ^ 10,需要34

位,所以u_int64_t使用/操作时需要使用变量类型来保证它们是
。 (正如我写的那样,我想的就越多。

我觉得我想要的就是更多,而且回答是

否但无论如何..)。几乎总是,或者甚至总是(只有微小的,很小的,非常遥远的机会,他们不会)有问题的价值

适合32位值的罚款。在32位机器上处理64位

值在整个代码中会减慢我的想法,所以

我想知道的是,无论如何以某种方式可变地使用32位值和64

位值?我能想到的唯一方法就是拥有相当多的代码重复,一次用于32位处理,一次用于64位
位处理。我想要做的是设置两个结构(一个用于32

位值,另一个用于64)然后基于开头的if语句

代码确定应该使用哪个,使用该结构

。在一天结束时,唯一的方法是

重复处理结构的代码对吗?我只是

以为我会检查以防有一些很好的方式来获得像我不知道的这样的东西。


工会没有任何直接用途。函数指针可能是有用的但是

并没有解决必须复制大部分代码的问题,可能几乎所有代码都是b $ b。所以我想只需使用64位的val就可以了。

。确实看起来很遗憾,因为真正需要超过32位的价值将是如此罕见 - 虽然谁知道将来。

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.

推荐答案



ben写道:

ben wrote:
无论如何在c中写代码可以变化地使用
两个结构之一(一个代码中有32位的值,另一个有64位的值吗?我正在编写一些解析一些数据的代码。
有一些数据类型,其最大值为10 ^ 10,需要34位,因此需要一个u_int64_t类型的变量来保存他们在使用/操纵时使用。 (正如我写的那样,我想的越多,我想的就越多,我想要的就是根本不在,答案是否定的,但不管怎样......)。几乎总是,甚至总是(只是微小的,微小的,非常遥远的机会,他们不会)有问题的值将适合32位值。在整个代码中处理64位
值的32位机器上我认为会减慢速度,所以我想知道的是,无论如何都要改变使用32位值和64位
比特价值不知何故?我能想到的唯一方法就是拥有相当多的代码重复,一次用于32位处理,一次用于64位处理。我想要做的是设置两个结构(一个用于32位/另一个用于64位)然后根据代码的开始时确定哪个应该使用的if语句,使用整个结构。在一天结束时,唯一的方法是
重复处理结构的代码吗?我只是想知道我会检查是否有一些很好的方法来获得这样的东西,这是我不知道的。

工会不是任何直接使用。函数指针可能有用但是没有解决必须复制大多数代码的问题,可能几乎所有代码。所以我想只需使用64位的val就可以了。确实看起来很遗憾,因为超过32位值的真正需求将是如此罕见 - 虽然谁知道将来。
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.




对象类型可以大小各不相同,因此它们可以从平台到平台表示价值范围。在这方面,可以考虑对象类型的一定程度的可变性。但这超出/你的/

控制。


因此,我可能会创建自己的容器

表示某些[x,y]值的范围。


< OT>

您是否实际分析了您的程序以确定净效果
在32位系统上支持64位值?


从您的帖子看,您只是在猜测

它会减慢速度。但任何经历的缓慢可能实际上可以忽略不计。但是,这真的全部取决于

应用程序的性质。

< / OT>


-

aegis



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


文章< 11 ******************* *@g10g2000cwb.googlegroups.c om>,aegis

< ae *** @ mad.scientist.com>写道:
In article <11********************@g10g2000cwb.googlegroups.c om>, aegis
<ae***@mad.scientist.com> wrote:

对象类型的大小可能不同,因此它们可以从平台到平台表示值的范围。在这方面,人们可以考虑对象类型的一定程度的可变性。但这超出了/你的/
控制。

因此,我可能会创建自己的容器,它能够代表某些[x,y]范围的值。


对,好。

< OT>
您是否真实地描述过您的计划以确定支持64的净效果
32位系统上的位值?

从你的帖子中可以看出,你只是在猜测它会减慢速度。但任何经历的缓慢实际上都可以忽略不计。但是,这真的全部取决于应用程序的性质。
< / OT>

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.


文章< 04 ***************** *@x.x> ;, ben< x@x.x>写道:
In article <04******************@x.x>, ben <x@x.x> wrote:
无论如何在c中编写代码可以变化地使用两个结构之一(一个具有32位val,另一个具有64位
) vals)整个代码?


不是直接的,没有。


考虑一下这个事实,在机器级别,你经常 - 我会

说总是但是对于一切都有例外 - 获得不同的

指令序列,用于两个相同的 - 除了类型的源代码

构造。例如:


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; < br $>
struct foo32 y;

...

MUL(& x);

MUL(& y) );


这可能会发出如下代码:


ld%r1,x + 4 #fetch xb

ld%r2,x + 8 #fetch xc

umul%r1,%r2#compute(32位)产品

st%r1,x#store result


ldx%r1,y + 8#fetch yb

ldx%r2,y + 16 #fetch yc

mulx%r1, %r2#compute(64位)产品

stx%r1,y #store result


这里,C编译器使用了类型信息来源

代码 - x的成员是uint32_t,而

" y"是uint64_t - 生成不同的代码:y.b和y.c的偏移量

与x.b和x.c的偏差量不同;

的大小所有操作数都不同;和mul x的指令使用32位/ b $ b位算术,而y的指令使用32位/ b $ b位算术。使用64位算术。

我正在编写一些解析一些数据的代码。
有一些数据类型,其最大值为10 ^ 10,这需要34 位,因此在使用/操作时需要使用u_int64_t类型的变量来保存它们。
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.




在这种情况下,使用&b编写代码。 unsigned long long (或者uint64_t

如果你真的想把它限制在*正好* 64位,这似乎就像矫枉过正一样)。然后,当且仅当它变得太慢时,

描述代码,找到它花费的时间太多时间,并优化

一旦它确实有效。


请注意,您可以使用宏来生成等效代码,除了

类型。这是有道理的,因为预处理器阶段无知

的C语义:它只是扩展了令牌,而C'的类型信息

更深入嵌入,因此个别令牌将他们的

类型带入C编译器。上面的MUL示例是一个微不足道的

一个(如此微不足道,它可能只需要在线扩展

手动,但这应该是一个简单的例子)。在其他

字中,预处理器无法看到类型是一个缺点

(类型检查是不可能的)和一个功能(类型独立是

自然)。

-

In-Real-Life:风河系统Chris Torek

美国犹他州盐湖城(40°39.22''N,111°50.29''W)+ 1 801 277 2603

电子邮件:忘了它 http ://web.torek.net/torek/index.html

由于垃圾邮件发送者,阅读电子邮件就像在垃圾中搜索食物一样。



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.


这篇关于变量的变量使用是我想的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆