/*
@@ lua_number2int is a macro to convert lua_Number to int.
@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.
** CHANGE them if you know a faster way to convert a lua_Number to
** int (with any rounding method and without throwing errors) in your
** system. In Pentium machines, a naive typecast from double to int
** in C is extremely slow, so any alternative is worth trying.
*/
/* On a Pentium, resort to a trick */
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \
(defined(__i386) || defined (_M_IX86) || defined(__i386__))
/* On a Microsoft compiler, use assembler */
#if defined(_MSC_VER)
#define lua_number2int(i,d) __asm fld d __asm fistp i
#define lua_number2integer(i,n) lua_number2int(i, n)
/* the next trick should work on any Pentium, but sometimes clashes
with a DirectX idiosyncrasy */
#else
union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) \
{ volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
#define lua_number2integer(i,n) lua_number2int(i, n)
#endif
/* this option always works, but may be slow */
#else
#define lua_number2int(i,d) ((i)=(int)(d))
#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
#endif
17
我假设需要截断,就像在"C"中写入 i = (int)f 一样 .
如果你有SSE3,你可以使用:
int convert(float x)
{
int n;
__asm {
fld x
fisttp n // the extra 't' means truncate
}
return n;
}
或者,使用SSE2(或在x64中,内联汇编可能不可用),您可以使用几乎同样快:
#include <xmmintrin.h>
int convert(float x)
{
return _mm_cvtt_ss2si(_mm_load_ss(&x)); // extra 't' means truncate
}
void Set_Trunc()
{
// cw is a 16-bit register [_ _ _ ic rc1 rc0 pc1 pc0 iem _ pm um om zm dm im]
__asm {
push ax // use stack to store the control word
fnstcw word ptr [esp]
fwait // needed to make sure the control word is there
mov ax, word ptr [esp] // or pop ax ...
or ax, 0xc00 // set both rc bits (alternately "or ah, 0xc")
mov word ptr [esp], ax // ... and push ax
fldcw word ptr [esp]
pop ax
}
}
void convertArray(int *dest, const float *src, int n)
{
Set_Trunc();
__asm {
mov eax, src
mov edx, dest
mov ecx, n // load loop variables
cmp ecx, 0
je bottom // handle zero-length arrays
top:
fld dword ptr [eax]
fistp dword ptr [edx]
loop top // decrement ecx, jump to top
bottom:
}
}
10 回答
这取决于您是否需要截断转换或舍入转换以及精确度 . 默认情况下,当您从float转到int时,C将执行截断转换 . 有FPU指令可以做到这一点,但它不是ANSI C转换,并且使用它有很多警告(例如了解FPU舍入状态) . 由于你的问题的答案非常复杂,并且取决于你没有表达的一些变量,我推荐这篇文章:
http://www.stereopsis.com/FPU.html
使用SSE的打包转换是迄今为止最快的方法,因为您可以在同一指令中转换多个值 . ffmpeg为此有很多组装(主要用于将音频的解码输出转换为整数样本);检查它的一些例子 .
普通x86 / x87代码的常用技巧是强制浮点的尾数部分表示int . 随后是32位版本 .
64位版本是类比的 . 上面发布的Lua版本更快,但依赖于截断double到32位结果,因此它需要将x87单位设置为双精度,并且不能适用于双到64位int转换 .
这个代码的好处是它对于符合IEEE 754的所有平台都是完全可移植的,唯一的假设是将浮点舍入模式设置为最接近 . 注意:便携式的编译和工作 . 如果有的话,x86以外的平台通常不会从这种技术中受益很多 .
如果可以保证运行代码的CPU与SSE3兼容(即使是Pentium 5,JBB),也可以允许编译器使用其FISTTP指令(即-msse3用于gcc) . 它似乎做了应该总是这样做的事情:
http://software.intel.com/en-us/articles/how-to-implement-the-fisttp-streaming-simd-extensions-3-instruction/
请注意,FISTTP与FISTP不同(它有问题,导致速度缓慢) . 它是SSE3的一部分,但实际上是(唯一的)X87端的改进 .
除此之外,X86 CPU可能会很好地进行转换 . :)
Processors with SSE3 support
在汇编中有一条指令将浮点转换为int:使用FISTP指令 . 它将浮点堆栈中的值弹出,将其转换为整数,然后将其存储在指定的地址处 . 我认为不会有更快的方式(除非你使用像我不熟悉的MMX或SSE这样的扩展指令集) .
另一条指令FIST将值保留在FP堆栈上,但我不确定它是否适用于四字大小的目的地 .
Lua代码库有以下代码片段(请访问www.lua.org查看src / luaconf.h) . 如果你发现(SO发现)更快的方式,我相信他们会很激动 .
哦,
lua_Number
意味着双倍 . :)我假设需要截断,就像在"C"中写入
i = (int)f
一样 .如果你有SSE3,你可以使用:
或者,使用SSE2(或在x64中,内联汇编可能不可用),您可以使用几乎同样快:
在较旧的计算机上,可以选择手动设置舍入模式并使用普通的
fistp
指令执行转换 . 这可能只适用于浮点数组,否则必须注意不要使用任何会使编译器改变舍入模式的构造(例如转换) . 它是这样完成的:请注意,内联汇编仅适用于Microsoft的Visual Studio编译器(也许是Borland),它必须重写为GNU程序集才能使用gcc进行编译 . 然而,具有内在函数的SSE2解决方案应该是非常便携的 .
其他舍入模式可以通过不同的SSE2内在函数或通过手动将FPU控制字设置为不同的舍入模式来实现 .
如果你真的关心它的速度,请确保你的编译器正在生成FIST指令 . 在MSVC中,您可以使用/ QIfist,see this MSDN overview执行此操作
您也可以考虑使用SSE内在函数为您完成工作,请参阅英特尔的这篇文章:http://softwarecommunity.intel.com/articles/eng/2076.htm
由于MS让我们摆脱了X64中的内联汇编并迫使我们使用内在函数,所以我查找了哪些内容使用 . MSDN doc给出了一个例子
_mm_cvtsd_si64x
.这个例子有效,但效率非常低,使用2个双倍的未对齐加载,我们只需要一个加载,因此摆脱了额外的对齐要求 . 然后产生了许多不必要的负载和重新加载,但它们可以如下消除:
结果:
可以在没有内联汇编的情况下设置舍入模式,例如,
舍入到最近的是默认值(无论如何) .
我想,是否要在每次通话中设置舍入模式或假设它将被恢复(第三方库)的问题必须通过经验来回答 . 您必须为
_control87()
和相关常量包含float.h
.并且,不,这不会在32位中工作,因此请继续使用FISTP指令:
通常,您可以信任编译器高效且正确 . 通常可以通过为编译器中已存在的东西滚动自己的函数来获得任何东西 .