常数 0.0039215689 代表什么? [英] What does the constant 0.0039215689 represent?

查看:23
本文介绍了常数 0.0039215689 代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在各种图形头文件中看到这个常量弹出

I keep seeing this constant pop up in various graphics header files

0.0039215689

好像和颜色有关?

这是第一个谷歌上的点击:

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

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);
    }
}

//...more like this

这个数字代表什么?为什么似乎没有人将其声明为 const?

What does this number represent? Why does no one seem to declare it as a const?

我在 Google 上找不到任何解释它的内容.

I couldn't find anything on Google that explained it.

推荐答案

0.0039215689约等于1/255.

看到这是 OpenGL,性能可能很重要.因此可以肯定地猜测这是出于性能原因.

Seeing that this is OpenGL, performance is probably important. So it's probably safe to guess that this was done for performance reasons.

乘以倒数比重复除以 255 更快.

Multiplying by the reciprocal is faster than repeatedly dividing by 255.

旁注:

如果您想知道为什么不将这种微优化留给编译器,那是因为它是不安全的浮点优化.换句话说:

If you're wondering why such a micro-optimization isn't left to the compiler, it's because it is an unsafe floating-point optimization. In other words:

x / 255  !=  x * (1. / 255)

由于浮点舍入错误.

因此,尽管现代编译器可能足够聪明地进行这种优化,但除非您通过编译器标志明确告诉它们,否则它们是不允许这样做的.

So while modern compilers may be smart enough to do this optimization, they are not allowed to do it unless you explicitly tell them to via a compiler flag.

相关: 为什么 GCC 不将 a*a*a*a*a*a 优化为(a*a*a)*(a*a*a)?

这篇关于常数 0.0039215689 代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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