小编Cjd*_*coy的帖子

c - 定义的值比硬编码的数字慢

这个问题对你来说可能显得愚蠢,但我找不到答案,我想确保它按照我的想法运作.

最近我遇到了这段代码:

void RDP_G_SETBLENDCOLOR(void)
{
    Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * 0.0039215689f;
    Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * 0.0039215689f;
    Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * 0.0039215689f;
    Gfx.BlendColor.A = _SHIFTR(w1, 0, 8) * 0.0039215689f;

    if(OpenGL.Ext_FragmentProgram && (System.Options & BRDP_COMBINER)) {
        glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 2, Gfx.BlendColor.R, Gfx.BlendColor.G, Gfx.BlendColor.B, Gfx.BlendColor.A);
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,0.0039215689f(指的是1/255)由于优化原因而被硬编码.现在假设我想出于可读性原因来定义它(即使这里选择的名称不是更好,它只是用于示例).

#define PIXEL_VALUE 0.0039215689f

void RDP_G_SETBLENDCOLOR(void)
{
    Gfx.BlendColor.R = _SHIFTR(w1, 24, 8) * PIXEL_VALUE;
    Gfx.BlendColor.G = _SHIFTR(w1, 16, 8) * PIXEL_VALUE;
    Gfx.BlendColor.B = _SHIFTR(w1, 8, 8) * PIXEL_VALUE;
    Gfx.BlendColor.A = _SHIFTR(w1, 0, 8) …
Run Code Online (Sandbox Code Playgroud)

c macros optimization performance

2
推荐指数
1
解决办法
74
查看次数

c-if + else if + else是否在一行中?

我误解了朋友的陈述后才有一个疑问。

我的朋友告诉我:我刚刚教同事如何在c中一行执行if / else。

例:

int i = 0;
i < 0 ? printf("i is below 0") : printf("i is over or equal to 0");
Run Code Online (Sandbox Code Playgroud)

现在,没有什么新鲜的东西,它被称为三元数,大多数人都知道这种陈述,但是我首先了解到:

我刚刚教了一位同事如何在同一行中执行IF / ELSE IF / ELSE。因为我不知道这样做是不可能的,所以我尝试做类似的事情

int i = 0;
 i < 0 ? printf("i is below 0") : i == 0 ? printf("i equal 0") : printf("i is over 0");
Run Code Online (Sandbox Code Playgroud)

实际上是否可以执行if / else if / else“三元”。还是有一种方法可以在没有可怕的代码的情况下进行此类操作?

c if-statement

2
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×2

if-statement ×1

macros ×1

optimization ×1

performance ×1