"移"浮点数字 [英] "Shifting" floating point numbers

查看:66
本文介绍了"移"浮点数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道一种快速的方法来将浮点数乘以

两次幂?从概念上讲,您需要做的只是添加到

尾数。但是我可以编写C代码(或x86程序集)来完成这个

而不需要全面的乘法吗?


例如,我想要能够很快完成以下任务:


双x;

双倍;


x = 42.13;

y =(1<< 9)* x;


提前致谢,

Bill

Does anyone know of a fast way to multiply floating point numbers by
powers of two? Conceptually, all you need to do is add to the
mantissa. But can I write C code (or x86 assembly) to accomplish this
without a full-blown multiply?

For example, I''d like to be able to do the following very quickly:

double x;
double y;

x = 42.13;
y = (1 << 9) * x;

Thanks in advance,
Bill

推荐答案

woess ... @ gmail.com写道:
woess...@gmail.com wrote:
有没有人知道一种快速的方法来乘以浮点数通过
两个权力?从概念上讲,您需要做的就是添加到尾数。但是,我可以编写C代码(或x86程序集)来完成这个
而不需要完全繁殖吗?

例如,我希望能够很快完成以下任务:

双x;
双y;

x = 42.13;
y =(1<< 9)* x;
Does anyone know of a fast way to multiply floating point numbers by
powers of two? Conceptually, all you need to do is add to the
mantissa. But can I write C code (or x86 assembly) to accomplish this
without a full-blown multiply?

For example, I''d like to be able to do the following very quickly:

double x;
double y;

x = 42.13;
y = (1 << 9) * x;




如果你知道你的实现的浮点表示

和底层硬件,你可以将float值复制到unsiged

long变量,移动相应的位并将值复制回浮点变量
。所有这些意味着你必须知道关于你的实现的细节

,从而有可能失去一些便携性。


这些天,FPU是非常快,所以直接乘以浮动

应该足够有效,除非你的代码是时间关键的。



If you know the floating point representation of your implementation
and underlying hardware, you can copy the float value to an unsiged
long variable, shift the appropriate bits and copy the value back to
the float variable. All this implies that you''ll have to know details
about your implementation and thus risk losing some portability.

These days, FPU''s are quite fast, so a direct multiply on the float
should be efficient enough unless you''re code is time critical.


wo******@gmail.com 写道:
有谁知道一个快速的方法来将浮点数乘以两个幂?从概念上讲,您需要做的就是添加到尾数。但是我可以编写C代码(或x86程序集)来完成这个
而不需要完全繁殖吗?

我的建议:不要打扰。你将击败FPU

和编译器的组合的机会很小,当你想要它时,这样的伎俩往往会崩溃。


例如,如果你想以正确的方式做到这一点,你必须处理

无穷大,非规范化数字,NaNs以及最后但并非最不重要的零。但是,你不能这样做,因为这会让你的代码太慢。

例如,我希望能够做到以下几点很快:

双x;
双y;

x = 42.13;
y =(1<< 9)* x;
Does anyone know of a fast way to multiply floating point numbers by
powers of two? Conceptually, all you need to do is add to the
mantissa. But can I write C code (or x86 assembly) to accomplish this
without a full-blown multiply?
My advice: don''t bother. The chance that you''ll beat the combination of FPU
and compiler is small, and a trick like this tends to break down when you
least expect it.

For example, if you want to do this the right way, you have to deal with the
infinities, denormalized numbers, NaNs and last but not least, zero. But of
course you can''t, since that would make your code far too slow.
For example, I''d like to be able to do the following very quickly:

double x;
double y;

x = 42.13;
y = (1 << 9) * x;



确认你真的需要比现在更快地完成这项工作

。然后通过改变处理

数据的方式,看看是否有某种方法可以避免完全相乘(至少在紧密循环中)。最后,如果所有这些选项都用完了,你最好使用x86

汇编程序并读取双精度数字的格式。试试

comp.lang.asm.x86。当你在这一点上时,C(甚至是非便携式的C)

不太可能对你有所帮助。


S.


Verify that you really need to do this faster than you''re doing it now
first. Then see if there''s some way to avoid doing the multiplication
altogether (at least in tight loops) by changing the way you handle your
data. Finally, if all these options are exhausted, you''d best go with x86
assembler and read up on the format of double precision numbers. Try
comp.lang.asm.x86. When you''re at this point, C (even nonportable C) is
unlikely to help you enough.

S.




wo ****** @ gmail.com 写了03/21/06 13:45,:


wo******@gmail.com wrote On 03/21/06 13:45,:
有没有人知道一种快速的方法来将浮点数乘以2的幂?从概念上讲,您需要做的就是添加到尾数。


ITYM添加指数。

但是我可以编写C代码(或x86程序集)来完成这个
而不是完整的 - 吹嘘倍增?

例如,我希望能够很快地完成以下任务:

double x;
double y;

x = 42.13;
y =(1<< 9)* x;
Does anyone know of a fast way to multiply floating point numbers by
powers of two? Conceptually, all you need to do is add to the
mantissa.
ITYM "add the exponent."
But can I write C code (or x86 assembly) to accomplish this
without a full-blown multiply?

For example, I''d like to be able to do the following very quickly:

double x;
double y;

x = 42.13;
y = (1 << 9) * x;




#include< math.h> ;

...

y = ldexp(x,9);


不保证相对速度,但是:你'需要在感兴趣的平台上测量



-
Er ********* @sun.com



#include <math.h>
...
y = ldexp(x, 9);

No guarantees about relative speed, though: You''ll
need to measure on the platform(s) of interest.

--
Er*********@sun.com


这篇关于&QUOT;移&QUOT;浮点数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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