便携式更换 [英] Portable replacement

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

问题描述

您好,


我已经重写了一个函数(greater_or_equal),它依赖于

实现定义的行为和精确宽度的可用性

整数,目标是在任何平台上实现新的实现

(greater_or_equal2)。


你如何看待新的实施?

(欢迎提出意见和建议。)


int greater_or_equal(uint16_t u,uint16_t v)

{

return(int16_t)(uv)> = 0;

}


int greater_or_equal2(unsigned u,unsigned v)

{

return((uv)& 0xffffU)< = 0x7fffU;

}


< OT>

GCC似乎理解输出以下代码的来源。


greater_or_equal2:

movl 8(%esp),%edx

cmpw% dx,4(%esp)

setns%al

movzbl%al,%eax

ret

< ; / OT>


问候。

Hello,

I''ve rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.

What do you think of the new implementation?
(Suggestions and comments are welcome.)

int greater_or_equal(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}

int greater_or_equal2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}

<OT>
GCC seems to "understand" the source as it outputs the following code.

greater_or_equal2:
movl 8(%esp), %edx
cmpw %dx, 4(%esp)
setns %al
movzbl %al, %eax
ret
</OT>

Regards.

推荐答案

Noob写道:
Noob wrote:

您好,


我已经重写了一个依赖于实现定义的行为和可用性的函数(greater_or_equal)确切宽度

整数,目标是在任何平台上实现新的实现

(greater_or_equal2)。


什么你觉得新的实现吗?

(欢迎提出意见和建议。)


int greater_or_equal(uint16_t u,uint16_t v)

{

return(int16_t)(uv)> = 0;

}


int greater_or_equal2(unsigned u ,未签名的v)

{

return((u-v)& 0xffffU)< = 0x7fffU;

}


< OT>

GCC似乎理解输出以下代码的来源。


greater_or_equal2:

movl 8(%esp),%edx

cmpw% dx,4(%esp)

setns%al

movzbl%al,%eax

ret

< ; / OT>
Hello,

I''ve rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.

What do you think of the new implementation?
(Suggestions and comments are welcome.)

int greater_or_equal(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}

int greater_or_equal2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}

<OT>
GCC seems to "understand" the source as it outputs the following code.

greater_or_equal2:
movl 8(%esp), %edx
cmpw %dx, 4(%esp)
setns %al
movzbl %al, %eax
ret
</OT>



我不禁要问,它输出的是什么代码:


int greater_or_equal3(unsigned u, unsigned v)

{

return u> = v;

}





-

pete

I can''t help but wonder, what code does it output for:

int greater_or_equal3(unsigned u, unsigned v)
{
return u >= v;
}

?

--
pete


4月28日上午10:38 * am,Noob< root @ localhostwrote:
On Apr 28, 10:38*am, Noob <root@localhostwrote:

你好,


我已经重写了一个依赖于

实现定义的行为和精确宽度

整数的可用性,目标是使新实现在任何平台上都可移植.b $ b(big_or_equal2)。


您如何看待新的实施?

(欢迎提出意见和建议。)


int greater_or_equal (uint16_t u,uint16_t v)

{

* * return(int16_t)(uv)> = 0;


}


int greater_or_equal2(unsigned u,uns签署v)

{

* * return((u-v)& 0xffffU)< = 0x7fffU;


}


< OT>

GCC似乎理解 ;它输出以下代码来源。


greater_or_equal2:

* * * movl * * 8(%esp),%edx

* * * cmpw * *%dx,4(%esp)

* * * setns *%al

* * * movzbl *%al,%eax

* * * ret

< / OT>


问候。
Hello,

I''ve rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.

What do you think of the new implementation?
(Suggestions and comments are welcome.)

int greater_or_equal(uint16_t u, uint16_t v)
{
* *return (int16_t)(u-v) >= 0;

}

int greater_or_equal2(unsigned u, unsigned v)
{
* *return ((u-v) & 0xffffU) <= 0x7fffU;

}

<OT>
GCC seems to "understand" the source as it outputs the following code.

greater_or_equal2:
* * *movl * *8(%esp), %edx
* * *cmpw * *%dx, 4(%esp)
* * *setns * %al
* * *movzbl *%al, %eax
* * *ret
</OT>

Regards.



你仍然使用2字节掩码,如果unsigned u代表32位怎么办?


Suresh M. Shenoy

You still use a 2 byte mask, what if unsigned u represents 32 bits?

Suresh M. Shenoy


4月28日19:58,pete< pfil ... @ mindspring.comwrote:
On 28 Apr., 19:58, pete <pfil...@mindspring.comwrote:

Noob写道:
Noob wrote:

你好,
Hello,


我已经重写了一个函数(greater_or_equal )依赖于

实现定义的行为和精确宽度

整数的可用性,目标是实现新的实现

(greater_or_equal2 )可移植到任何平台。
I''ve rewritten a function (greater_or_equal) that relies on
implementation-defined behavior and availability of exact-width
integers, with the goal of making the new implementation
(greater_or_equal2) portable across any platform.


您如何看待新的实施?

(欢迎提出建议和意见。)
What do you think of the new implementation?
(Suggestions and comments are welcome.)


int greater_or_equal(uint16_t u,uint16_t v)

{

return(int16_t)(uv)> = 0;

}
int greater_or_equal(uint16_t u, uint16_t v)
{
return (int16_t)(u-v) >= 0;
}


int greater_or_equal2(unsigned u,unsigned v)

{

return((uv)& 0xffffU)< = 0x7fffU;

}
int greater_or_equal2(unsigned u, unsigned v)
{
return ((u-v) & 0xffffU) <= 0x7fffU;
}


< OT>

GCC似乎理解源它输出以下代码。
<OT>
GCC seems to "understand" the source as it outputs the following code.


greater_or_equal2:

movl 8(%esp),%edx

cmpw%dx ,4(%esp)

setns%al

movzbl%al,%eax

ret

< / OT>
greater_or_equal2:
movl 8(%esp), %edx
cmpw %dx, 4(%esp)
setns %al
movzbl %al, %eax
ret
</OT>



我不禁要问,它输出的是什么代码:


int greater_or_equal3(unsigned u, unsigned v)

{

return u> = v;


}


I can''t help but wonder, what code does it output for:

int greater_or_equal3(unsigned u, unsigned v)
{
return u >= v;

}



也许OP的代码将用于混淆的

C比赛...


问候Thomas Mertes


Seed7主页: http://seed7.sourceforge.net

Seed7 - 可扩展的编程语言:用户定义的语句

和运算符,抽象数据类型,没有特殊的模板
语法,带接口的OO和多个调度,静态类型,

解释或编译,可移植,在linux / unix / windows下运行。

Maybe the code of the OP will be used in a obfuscated
C contest...

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.


这篇关于便携式更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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