奇/偶的极致效率 [英] Extreme Efficiency for Odd/Even

查看:76
本文介绍了奇/偶的极致效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写极其高效,完全可移植的算法来确定整数是奇数还是偶数。最基本的算法是:


#define IS_ODD(x)((x)%2)


#define IS_EVEN(x) (!((x)%2))

试图让它们更有效率,我们可以将它们改为:


#define IS_ODD(x)((x)& 1)


#define IS_EVEN(x)(!((x)& 1))


(是的,有些编译器实际上可以将前者编译成后者,但是

我们在这里玩效率游戏。)


仅限但事情是: -

后两者在One''s Complement系统上出现故障时,

处理负数。因此,我有以下代码......我会

感谢任何意见或建议。 Sign-

幅度或Two''s Complement不需要进行任何更改,但我们必须即兴进行One's
补充。我们走了:


#define SIGNMAG 0

#define ONES 1

#define TWOS 2


#if -1& 3 == 1

#define NUMSYS SIGNMAG

#elif -1& 3 == 2

#define NUMSYS ONES

#else

#define NUMSYS TWOS

#endif


#if NUMSYS!= ONES

#define IS_ODD(x)((x)& 1)

#define IS_EVEN( x)(!((x)& 1))

#else

#define ONE_IF_NEGATIVE(x)((x)< 0)


#define IS_ODD(x)(!((x)& 1)== ONE_IF_NEGATIVE((x)))

#define IS_EVEN(x)(!(( x)& 1)!= ONE_IF_NEGATIVE((x)))

#endif


不幸的是,One's Complement版本评估论证

两次。


是的,我意识到聪明的事情可能只是写x%

2忘了它,但这比其他任何东西更多。


-


Frederick Gotham


I''m trying to write extremely efficient, fully portable algorithms to
determine if an integer is odd or even. The most basic algorithms would be:

#define IS_ODD(x) ((x) % 2)

#define IS_EVEN(x) (!((x) % 2))

Attempting to make them slightly more efficient, we could change them to:

#define IS_ODD(x) ((x) & 1)

#define IS_EVEN(x) (!((x) & 1))

(Yes, some compilers may actually compile the former into the latter, but
we''re playing the efficiency game here.)

Only thing though:-
The latter two will malfunction on One''s Complement systems when
dealing with negative numbers. Thus, I have the following code... I''d
appreciate any comments or suggestions. No changes are required for Sign-
magnitude or for Two''s Complement, but we must improvise for One''s
Complement. Here we go:

#define SIGNMAG 0
#define ONES 1
#define TWOS 2

#if -1 & 3 == 1
#define NUMSYS SIGNMAG
#elif -1 & 3 == 2
#define NUMSYS ONES
#else
#define NUMSYS TWOS
#endif

#if NUMSYS != ONES
#define IS_ODD(x) ((x) & 1)
#define IS_EVEN(x) (!((x) & 1))
#else
#define ONE_IF_NEGATIVE(x) ((x) < 0)

#define IS_ODD(x) ( !((x) & 1) == ONE_IF_NEGATIVE((x)) )
#define IS_EVEN(x) ( !((x) & 1) != ONE_IF_NEGATIVE((x)) )
#endif

Unfortunately though, the One''s Complement version evaluates the argument
twice.

Yes, I realise that the smart thing would probably just be to write "x %
2" and forget about it, but this is more for kicks than anything else.

--

Frederick Gotham

推荐答案

Frederick Gotham< fg ******* @ SPAM.com撰写:
Frederick Gotham <fg*******@SPAM.comwrites:

我''我试图编写极其高效,完全可移植的算法来确定整数是奇数还是偶数。
I''m trying to write extremely efficient, fully portable algorithms to
determine if an integer is odd or even.



[cut]

[cut]


#if -1& 3 == 1
#if -1 & 3 == 1



我不确定你是否可以假设预处理器使用相同的

算法,因为程序将使用本身。如果你正在为另一个架构编译

a程序怎么办 - 比如在某个补充系统上编译ix86

(这是两个补码)。


[一个人的强制版本:]

I''m not really sure if you can assume that preprocessor uses the same
arithmetic as the program will use itself. What if you are compiling
a program for one architecture on another - like compiling for ix86
(which is two''s complement) on some one''s complement system.

[one''s compelment version:]


#define ONE_IF_NEGATIVE(x)((x)< 0 )

#define IS_ODD(x)(!((x)& 1)== ONE_IF_NEGATIVE((x)))

#define IS_EVEN(x)(! ((x)& 1)!= ONE_IF_NEGATIVE((x)))
#define ONE_IF_NEGATIVE(x) ((x) < 0)
#define IS_ODD(x) ( !((x) & 1) == ONE_IF_NEGATIVE((x)) )
#define IS_EVEN(x) ( !((x) & 1) != ONE_IF_NEGATIVE((x)) )



#v +

#define IS_ODD(x)( (x)%2)

#define IS_EVEN(x)(!IS_ODD(x))

#v-


反正可能会更快。

#v+
#define IS_ODD(x) ((x) % 2)
#define IS_EVEN(x) (!IS_ODD(x))
#v-

Would probably be faster anyway.


是的,我意识到聪明的事情可能只是写x%

2并且忘了它,但这更像是踢腿而不是其他任何东西。
Yes, I realise that the smart thing would probably just be to write "x %
2" and forget about it, but this is more for kicks than anything else.



如果你真的想要可移植性,你应该坚持''%2''版本和

让编译器进行优化。


-

祝你好运,_ _

.o。 | Serenly Enlightened Majesty of o'',=。/`o

..o |计算机科学,Michalmina86 Nazarewicz(oo)

ooo + - < mina86 * tlen.pl> - < jid:mina86 * jabber.org> - ooO - (_) - Ooo -

If you really want portability you should stick to ''% 2'' version and
let compiler do the optimization.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o'' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--


Michal Nazarewicz写道:
Michal Nazarewicz wrote:

Frederick Gotham< fg ******* @ SPAM.comwrites :
Frederick Gotham <fg*******@SPAM.comwrites:


> #define ONE_IF_NEGATIVE(x)((x)< 0)
> #define ONE_IF_NEGATIVE(x) ((x) < 0)


#define IS_ODD(x)((x)%2 )
#define IS_ODD(x) ((x) % 2)


无论如何可能会更快。
Would probably be faster anyway.



很长一段时间我为速度优化了代码,但是没有


#define IS_ODD (x)((x)& 1)


甚至更快?不模数总是需要一个耗时的部门

(如果编译器不够聪明,不能以不同的方式处理模2)?


问候

Steffen

It''s been a long time I optimized code for speed, but wouldn''t

#define IS_ODD(x) ((x) & 1)

be even faster? Doesn''t modulo always require a time-consuming division
(if the compiler isn''t clever enough to handle modulo 2 differently)?

Regards
Steffen


Michal Nazarewicz写道:
Michal Nazarewicz wrote:

Frederick Gotham< fg * ****** @SPAM.com撰写:
Frederick Gotham <fg*******@SPAM.comwrites:

我正在尝试编写极其高效,完全可移植的算法来确定
确定如果整数是奇数或偶数。
I''m trying to write extremely efficient, fully portable algorithms to
determine if an integer is odd or even.



[cut]

[cut]


#if -1& 3 == 1
#if -1 & 3 == 1



我不确定你是否可以假设预处理器使用相同的

算法,因为程序将使用本身。如果你正在为一个架构编译另一个架构,比如编译ix86

(这是两个补码)在某个人的补充系统上怎么办?


I''m not really sure if you can assume that preprocessor uses the same
arithmetic as the program will use itself. What if you are compiling
a program for one architecture on another - like compiling for ix86
(which is two''s complement) on some one''s complement system.



在C99中,这是一个安全的假设。在目标'(u)intmax_t类型中完成预处理器算法(如

if)。 IIRC C99改变了

的规则,但这在C89中可能不安全。

In C99, that''s a safe assumption. Preprocessor arithmetic is done (as
if) in the target''s (u)intmax_t types. IIRC C99 changed the rules for
that, though, so it may not be safe in C89.


这篇关于奇/偶的极致效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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