达夫的装置 [英] Duff's Device

查看:110
本文介绍了达夫的装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有时想知道:

任何人都知道为什么Duff的设备通常是这样写的:

I''ve been wondering sometimes:
Anyone know why Duff''s device is usually written like this:

展开 | 选择 | Wrap | 行号

推荐答案

我写道:

我有时想知道:

任何人都知道为什么Duff'的设备通常是这样写的:

(...)
I''ve been wondering sometimes:
Anyone know why Duff''s device is usually written like this:
(...)



呃,有点愚蠢的问题 - 这是'它的发布方式'当然。

我的意思是,是否有充分的理由以这种方式编写 - 在某些机器上生成更好的代码?


-

Hallvard

Er, a bit silly question - "that''s the way it''s published" of course.
I meant, is there a good reason to write it that way - produces
better code on some machine?

--
Hallvard


Hallvard B Furuseth写道:
Hallvard B Furuseth wrote:

I 有时候一直在想:

任何人都知道为什么Duff的设备通常是这样写的:


void duff(const char * str,int len){

int n =(len + 7)/ 8;

switch(len%8){

case 0:do { foo(* str ++);

案例7:foo(* str ++);

案例6:foo(* str ++);

.. 。

案例1:foo(* str ++);

} while(--n 0);

}

}


而不是这个?


void duff2(const char * str,int len){

开关(len%8){

案例0:while((len - = 8)> = 0){

foo(* str ++);

案例7:foo(* str ++);

案例6:foo(* str ++);

...

案例1:foo( * str ++);

}

}

}


原件有额外的'' +''并且不处理len = 0.

它也不需要除以8,但我知道n- = 1

可能比n便宜 - = 8在一些架构上。

人们现在有18年的时间注意到:-)
I''ve been wondering sometimes:
Anyone know why Duff''s device is usually written like this:

void duff(const char *str, int len) {
int n = (len + 7) / 8;
switch (len % 8) {
case 0: do{ foo(*str++);
case 7: foo(*str++);
case 6: foo(*str++);
...
case 1: foo(*str++);
} while (--n 0);
}
}

instead of this?

void duff2(const char *str, int len) {
switch (len % 8) {
case 0: while ((len -= 8) >= 0) {
foo(*str++);
case 7: foo(*str++);
case 6: foo(*str++);
...
case 1: foo(*str++);
}
}
}

The original has an extra ''+'' and doesn''t handle len=0.
Nor does it need the divide by 8, though I realize n-=1
may be cheaper than n-=8 on some architectures.
People have had 18 years to notice now:-)



前段时间我做了一些速度测试使用x86上的gcc和我能找到的最快的

版本是:


静态内联无效

ooc_memchr_copy(char *限制dst,

const char * restrict src,size_t cnt)

{

size_t rem = cnt%8;

cnt =(cnt / 8)+ 1;


开关(rem)

do {* dst ++ = * src ++;

case 7:* dst ++ = * src ++;

case 6:* dst ++ = * src ++;

case 5:* dst ++ = * src ++;

案例4:* dst ++ = * sr c ++;

案例3:* dst ++ = * src ++;

案例2:* dst ++ = * src ++;

案例1:* dst ++ = * src ++;

case 0:;

} while( - cnt);

}


不是已发布的,适用于cnt == 0以及

cnt ==(size_t)-1。为什么你认为orignal版本总是使用




a +,ld。

I did some speed test some time ago with gcc on x86 and the fastest
version I was able to find was:

static inline void
ooc_memchr_copy( char *restrict dst,
const char *restrict src, size_t cnt)
{
size_t rem = cnt % 8;
cnt = (cnt / 8) + 1;

switch (rem)
do { *dst++ = *src++;
case 7: *dst++ = *src++;
case 6: *dst++ = *src++;
case 5: *dst++ = *src++;
case 4: *dst++ = *src++;
case 3: *dst++ = *src++;
case 2: *dst++ = *src++;
case 1: *dst++ = *src++;
case 0: ;
} while(--cnt);
}

which is not the one published and works for cnt==0 as well as for
cnt==(size_t)-1. Why do you think that the orignal version is always the
one used?

a+, ld.




" Hallvard B Furuseth" < h。********** @ usit.uio.nowrote in message

news:hb ************** @ bombur。 uio.no ...

"Hallvard B Furuseth" <h.**********@usit.uio.nowrote in message
news:hb**************@bombur.uio.no...

任何人都知道为什么Duff'的设备通常是写的

这样(剪辑)而不是这个? (剪辑)
Anyone know why Duff''s device is usually written
like this (snip) instead of this? (snip)



是的。


这是他原来的帖子:
http://groups.google.com/group/net.l ...... e07aa94c?hl = en


这是他发的另一篇文章,其中澄清了来自clc的每个人的各种问题:
http:// groups。 google.com/group/comp....75c42411?hl=zh


从原始帖子中,他(间接)声明Duff的设计

C中的设备是他理解如何使用汇编语言为VAX生成高效展开循环的直接结果。至少,

是除了Duff的设备之外的一件事你应该从他的

消息中得到...

FYI ,其他人已经指出西蒙塔特姆的C中的Coroutines:
http://www.chiark.greenend.org.uk/~s...oroutines.html


Protothreads也是基于在类似的机制上:
http://www.sics.se / ~adam / pt /

Rod Pemberton

Yes.

This is his original post:
http://groups.google.com/group/net.l...e07aa94c?hl=en

This is another post from him with clarifications to various questions from
individuals on c.l.c:
http://groups.google.com/group/comp....75c42411?hl=en

From the original post, he (indirectly) states that the design of Duff''s
Device in C was the direct result of his understanding of how to generate
efficient unrolled loops in assembly language for the VAX. At least, that
is the one thing other than Duff''s Device that you should get from his
message...
FYI, others have pointed out Simon Tatham''s "Coroutines in C":
http://www.chiark.greenend.org.uk/~s...oroutines.html

Protothreads is also based on a similar mechanism:
http://www.sics.se/~adam/pt/
Rod Pemberton


这篇关于达夫的装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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