在 x86 上将 float 转换为 int 的最快方法是什么 [英] What is the fastest way to convert float to int on x86

查看:24
本文介绍了在 x86 上将 float 转换为 int 的最快方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道在 x86 CPU 上将浮点数转换为 int 的最快方法是什么.对于以下任意组合,最好使用 C 或汇编(可以在 C 中内联):

What is the fastest way you know to convert a floating-point number to an int on an x86 CPU. Preferrably in C or assembly (that can be in-lined in C) for any combination of the following:

  • 32/64/80 位浮点数 -> 32/64 位整数

我正在寻找一种比让编译器来做更快的技术.

I'm looking for some technique that is faster than to just let the compiler do it.

推荐答案

这取决于您想要截断转换还是舍入转换以及精度.默认情况下,当您从 float 转换为 int 时,C 将执行截断转换.有 FPU 指令可以做到这一点,但它不是 ANSI C 转换,使用它有一些重要的警告(例如了解 FPU 舍入状态).由于您的问题的答案相当复杂,并且取决于您尚未表达的一些变量,因此我推荐这篇关于该问题的文章:

It depends on if you want a truncating conversion or a rounding one and at what precision. By default, C will perform a truncating conversion when you go from float to int. There are FPU instructions that do it but it's not an ANSI C conversion and there are significant caveats to using it (such as knowing the FPU rounding state). Since the answer to your problem is quite complex and depends on some variables you haven't expressed, I recommend this article on the issue:

http://www.stereopsis.com/FPU.html

这篇关于在 x86 上将 float 转换为 int 的最快方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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