确认这是标准代码 [英] Confirm this is Standard code

查看:72
本文介绍了确认这是标准代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两位代码,我想要一些更有经验的人

来检查是否符合标准。我已尽力阅读

标准并搜索周围,我想并希望此代码包含

无需担心。

/ *取有符号整数的绝对值* /

int32 val;

uint32 abs_val;


val = -492;


if(val< 0)

abs_val = - (uint32)val;


我已经完成了对此标准的阅读并四处搜索并且

看来上面的标准看起来还不错。只是想确保

,因为我的lint警告预期签名类型。

另外请查看下面的代码并告诉我是否有任何内容

担心非标准。请注意,shiftAmt的绝对价值已经预先调整好并保证

不会溢出int16所以不要警告我可能

溢出并导致UB。


int32 foo(int16 x,int16 shiftAmt)

{

int32 ret;


...

/ *代码块处理x< 0,shiftAmt< 0 * /

ret = - ((int32)(( - (uint32)x)<<(-shiftAmt)));

}


谢谢。

解决方案

2005年4月2日14:09:21 -0800,joshc ; <乔******** @ gmail.com>写在

comp.lang.c:

我有两位代码,我想要一些更有经验的人
到检查是否符合标准。我已尽力阅读标准和搜索,我认为并希望此代码包含
无需担心。


让我感到困惑的是你想写这样的代码。

/ *取有符号整数的绝对值* /
int32 val;
uint32 abs_val;

val = -492;

if(val< 0)
abs_val = - (uint32)val;


为什么不只是:


abs_val = abs(val);


... .after包括< stdlib.h>?或者只是:


abs_val = val> 0? val:-val;


在任何情况下,如果val为负,则代码的结果是明确定义的

,除非它恰好是最小可能的价值并且大于

大于最大正值。

我已经完成了对此标准的阅读并搜索了它似乎似乎以上是标准的罚款。只是想确保
因为我的lint警告预期的签名类型。

另外请看下面的代码并告诉我是否有任何需要担心的事情至于非标准。只是注意,shiftAmt的绝对值已经预先调节并且保证不会溢出int16所以不要警告我可能会溢出并导致UB 。


预先条件是什么?由于你在转换之前转换为32位类型

,并返回32位值,可能会溢出一个

16位类型并没有什么区别。

int32 foo(int16 x,int16 shiftAmt)
{
int32 ret;

...
/ *代码块处理用x< 0,shiftAmt< 0 * /
ret = - ((int32)(( - (uint32)x)<<(-shiftAmt)));


如果shiftAmt介于0和-31之间,那么这已经定义好了

的结果,但看起来很奇怪。如果shiftAmt为正或小于

-32,则结果不确定。

}

谢谢。




你能用语言准确地描述你想要做什么吗?

这段代码?无论是什么,我都很确定写出它的方式要少得多。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


文章< 11 ********************** @ g14g2000cwa.googlegroups .com>,

joshc< jo ******** @ gmail.com>写道:

/ *取有符号整数的绝对值* /
int32 val;
uint32 abs_val;

val = -492;

if(val< 0)
abs_val = - (uint32)val;




在一元减去之前不会处理演员表?我可能是错的

但是代码 - 看起来 - 让我做以下事情:


1)取一个负数并使其变为无符号价值

(我必须重新阅读标准才能看到它的内容

到底会发生什么)

2)做了unary减去无符号值

(我必须重新阅读标准才能看到一元减去

表示当应用于无符号值时)

3)看到该值将被赋值给无符号值,因此

可能会再次转换该值。


这会更有意义对我来说,如果代码是


if(val< 0)

abs_val =(uint32)( - val);

此时演员阵容只是多余的,没有

三重转换的可能性。

-

你从这个男人那里买了一把二手?


Jack Klein写道:

4月2日2005 14:09:21 -0800,joshc <乔******** @ gmail.com>写在
comp.lang.c:

为什么不只是:

abs_val = abs(val);


这是嵌入式系统的代码,即独立实现。

预先条件是什么?由于你在转换之前转换为32位类型并返回32位值,因此可能溢出的16位类型没有任何区别。

int32 foo(int16 x,int16 shiftAmt)
{
int32 ret;

...
/ *代码块处理x< 0,shiftAmt< 0 * /
ret = - ((int32)(( - (uint32)x)<<(-shiftAmt)));
如果shiftAmt介于0和-31之间,那么这很好定义了结果,但看起来很奇怪。如果shiftAmt是正数或更少



比-32,它有未定义的结果。




是的,预先确定以确保你在上面描述的内容(0到-31)。


谢谢。


I''ve got two bits of code that I would like some more experienced folks
to check for conformance to the Standard. I''ve tried my best to read
the standard and search around and I think and hope this code contains
no cause for concern.

/* taking absolute value of signed integer */
int32 val;
uint32 abs_val;

val = -492;

if(val < 0)
abs_val = -(uint32)val;

I have done my reading of the standard on this and searching around and
it seems the above is fine by the standard. Just want to make sure
because my lint warns about "Expected signed type".
Also please look at the code below and tell me if there is anything to
worry about as far as being non-standard. Just a note, the absolute
value of shiftAmt has already been pre-conditioned and is guaranteed
not to overflow an int16 so don''t warn me about that possibly
overflowing and resulting in U.B..

int32 foo(int16 x, int16 shiftAmt)
{
int32 ret;

...
/* code block dealing with x < 0, shiftAmt < 0 */
ret = -((int32)((-(uint32)x) << (-shiftAmt)));
}

Thanks.

解决方案

On 2 Apr 2005 14:09:21 -0800, "joshc" <jo********@gmail.com> wrote in
comp.lang.c:

I''ve got two bits of code that I would like some more experienced folks
to check for conformance to the Standard. I''ve tried my best to read
the standard and search around and I think and hope this code contains
no cause for concern.
What puzzles me is WHY you want to write code like this.
/* taking absolute value of signed integer */
int32 val;
uint32 abs_val;

val = -492;

if(val < 0)
abs_val = -(uint32)val;
Why not just:

abs_val = abs(val);

....after including <stdlib.h>? Or just:

abs_val = val > 0 ? val : -val;

In any case, the result of the code is well-defined if val is negative
unless it happens to be the minimum possible value and has a magnitude
of on greater than the maximum positive value.
I have done my reading of the standard on this and searching around and
it seems the above is fine by the standard. Just want to make sure
because my lint warns about "Expected signed type".

Also please look at the code below and tell me if there is anything to
worry about as far as being non-standard. Just a note, the absolute
value of shiftAmt has already been pre-conditioned and is guaranteed
not to overflow an int16 so don''t warn me about that possibly
overflowing and resulting in U.B..
Pre-conditioned to be what? Since you are casting to a 32 bit type
before the shift, and returning a 32 bit value, what might overflow a
16 bit type doesn''t make any difference.
int32 foo(int16 x, int16 shiftAmt)
{
int32 ret;

...
/* code block dealing with x < 0, shiftAmt < 0 */
ret = -((int32)((-(uint32)x) << (-shiftAmt)));
If shiftAmt was between 0 and -31 inclusive, this has well defined
results but looks quite bizarre. If shiftAmt is positive or less than
-32, it has undefined results.
}

Thanks.



Can you describe in words exactly what it is you are trying to do with
this code? Whatever it is, I am pretty sure that there is a much less
convoluted way to write it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


In article <11**********************@g14g2000cwa.googlegroups .com>,
joshc <jo********@gmail.com> wrote:

/* taking absolute value of signed integer */
int32 val;
uint32 abs_val;

val = -492;

if(val < 0)
abs_val = -(uint32)val;



Would the cast not be processed before the unary minus? I could be wrong
but that code -looks- to me to be doing the following:

1) take a negative number and make it into an unsigned value
(I would have to re-read the standard to see what it says
about exactly what would happen)
2) does a unary minus on the unsigned value
(I would have to re-read the standard to see what unary minus
means when applied to an unsigned value)
3) sees that the value is to be assigned to an unsigned value, so
potentially converts the value again.

It would make more sense to me if the code were

if(val < 0)
abs_val = (uint32)(-val);

at which point the cast would merely be redundant, with no
possibility of a triple conversion.
--
Would you buy a used bit from this man??


Jack Klein wrote:

On 2 Apr 2005 14:09:21 -0800, "joshc" <jo********@gmail.com> wrote in
comp.lang.c:
Why not just:

abs_val = abs(val);
This is code for an embedded system, i.e a freestanding implementation.

Pre-conditioned to be what? Since you are casting to a 32 bit type
before the shift, and returning a 32 bit value, what might overflow a
16 bit type doesn''t make any difference.

int32 foo(int16 x, int16 shiftAmt)
{
int32 ret;

...
/* code block dealing with x < 0, shiftAmt < 0 */
ret = -((int32)((-(uint32)x) << (-shiftAmt)));
If shiftAmt was between 0 and -31 inclusive, this has well defined
results but looks quite bizarre. If shiftAmt is positive or less


than -32, it has undefined results.



Yeah, pre-conditioned to ensure what you describe above(0 to -31).

Thanks.


这篇关于确认这是标准代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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