图形驱动程序函数中的浮点运算

如果图形驱动程序函数包含使用浮点单元 (FPU) 的代码,则代码前面必须调用 EngSaveFloatingPointState ,然后调用 EngRestoreFloatingPointState。 有关图形驱动程序函数的列表,请参阅 图形驱动程序函数

如果 FPU 可用,则任何向浮点变量赋值或执行涉及浮点数的计算的代码都将使用该 FPU。 例如,以下每一行代码都使用 FPU。

double myDouble = 5;
int myInt = 5 * 4.3;
int myInt = 50 * cos(2);

假设你正在编写使用 FPU 的 DrvAlphaBlend 函数。 以下示例演示了应如何保存和还原浮点状态。

#define DRIVER_TAG // Put your driver tag here, for example 'zyxD'

BOOL DrvAlphaBlend(...)
{
   ...
   ULONG result;
 double floatVal;
   VOID* pBuf;
   ULONG bufSize;

 // Determine the size of the required buffer.
   bufSize = EngSaveFloatingPointState(NULL, 0);

 if(bufSize > 0)
   {
 // Allocate a zeroed buffer in the nonpaged pool.
      pBuf = EngAllocMem(
         FL_NONPAGED_MEMORY|FL_ZERO_MEMORY, bufSize, DRIVER_TAG);

 if(pBuf != NULL)
      {
 // The buffer was allocated successfully.
 // Save the floating-point state.
         result = EngSaveFloatingPointState(pBuf, bufSize);

 if(TRUE == result)
         {
 // The floating-point state was saved successfully.
 // Use the FPU.
            floatVal = 0.8;
            ...
            EngRestoreFloatingPointState(pBuffer);
         }

         EngFreeMem(pBuf);
      }
   }
   ...
}

当转义为OPENGL_CMD、OPENGL_GETINFO或 MCDFUNCS 时,GDI 会自动保存对驱动程序 DrvEscape 函数的任何调用的浮点状态。 在这些情况下,可以在 DrvEscape 函数中使用 FPU,而无需调用 EngSaveFloatingPointStateEngRestoreFloatingPointState

执行浮点运算的大多数 DirectDraw 和 Direct3D 回调函数也应保存和还原浮点状态。 有关详细信息,请参阅 在 DirectDraw 中执行浮点操作在 Direct3D 中执行浮点操作

有关 GDI 提供的浮点服务的信息,请参阅 GDI Floating-Point服务