duff的设备/循环unriolling [英] duff's device / loop unriolling

查看:46
本文介绍了duff的设备/循环unriolling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


下面的代码显示了DJB自己的strlen实现(str_len):


unsigned int str_len(char * s )

{

注册char * t;

t = s;

for(;;){

如果(!* t)返回t - s; ++ t;

如果(!* t)返回t - s; ++ t;

如果(!* t)返回t - s; ++ t;

如果(!* t)返回t - s; ++ t;

}

}

到目前为止我理解了代码,但我对循环有疑问。

对我来说,似乎他使用循环展开(aka duff'的设备)来优化代码执行时的代码。但他为什么要使用四个循环呢?当

调用该函数时,他不知道有多大s。是。或者我错了吗?

我一直以为,要展开一个循环,我需要知道循环是多少次使用




任何帮助将不胜感激,

JR

Hi there,

the Code below shows DJBs own implementation of strlen (str_len):

unsigned int str_len(char *s)
{
register char *t;
t = s;
for (;;) {
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff''s device) to optimize
the code while it is executed. But why did he use four loops? When
the function is invoked, he didn''t know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.

Any help would be appreciated,
JR

推荐答案

Jan Richter写道:
Jan Richter wrote:
您好,

下面的代码显示了DJB自己的strlen实现(str_len):

unsigned int str_len(char * s)

注册char * t;
t = s;
for(;;){
if(!* t)return t - s; ++ t;
if(!* t)返回t - s; ++ t;
if(!* t)返回t - s; ++ t;
if(!* t)返回t - s; ++ t;
}
}
我到目前为止理解了代码,但我对循环有疑问。
对我而言,似乎他使用了循环展开(aka duff) ('s)设备)在执行代码时优化代码。但他为什么要使用四个循环呢?当调用该函数时,他不知道有多大s。是。或者我错在这里?
我一直认为,要展开一个循环,我需要知道循环的使用频率。

任何帮助都将受到赞赏, JR
Hi there,

the Code below shows DJBs own implementation of strlen (str_len):

unsigned int str_len(char *s)
{
register char *t;
t = s;
for (;;) {
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
if (!*t) return t - s; ++t;
}
}
I understand the code so far, but I have a question about the loops.
To me, it seems he used loop unrolling (aka duff''s device) to optimize
the code while it is executed. But why did he use four loops? When
the function is invoked, he didn''t know how big "s" is. Or am I wrong here?
I always thought, to unroll a loop I need to know how often the loop is
used.

Any help would be appreciated,
JR




在我的linbox中,strlen()给出了更好的结果...


- -

一个人的自由停止在其他人开始的地方

Giannis Papadopoulos
http://dop.users.uth.gr/

塞萨利大学

计算机&通信工程部门



In my linbox, strlen() gives by far better results...

--
one''s freedom stops where others'' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.




" Giannis Papadopoulos"写道:


[...]

"Giannis Papadopoulos" wrote:

[...]

在我的linbox中,strlen()给出了更好的结果......

In my linbox, strlen() gives by far better results...




是的,对我来说一样,那么收益在哪里?


干杯,

JR



Yes, same for me, so where is the benefit?

Cheers,
JR


Jan Richter写道:
Jan Richter wrote:
" Giannis Papadopoulos"写道:

[...]

"Giannis Papadopoulos" wrote:

[...]

在我的linbox中,strlen()给出了更好的结果...
In my linbox, strlen() gives by far better results...



是的,对我来说一样,那么好处在哪里?

干杯,
JR


Yes, same for me, so where is the benefit?

Cheers,
JR



没有好处...也许它是为编译器编写的,不知道怎样

来展开循环...


和是,循环展开可以完美地工作,当你有一个提示这个循环将执行多少

次。


不是这个特殊的str_len的作者( )说什么?


-

一个人的自由停止在别人'开始的地方


Giannis Papadopoulos
http://dop.users.uth.gr/

塞萨利大学

电脑&通信工程部门



No benefit... Maybe it is written for a compiler that does not know how
to unroll loops...

And yes, loop unrolling works perfectly when you have a hint how many
times this loop will be executed.

Doesn''t the author of this peculiar str_len() say anything?

--
one''s freedom stops where others'' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.


这篇关于duff的设备/循环unriolling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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