快速fmo​​df(...,1.0f) [英] Fast fmodf(..., 1.0f)

查看:113
本文介绍了快速fmo​​df(...,1.0f)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个问题:


1.在现代32位Intel机器上,但不使用SSE,

最快的计算方法是什么?浮点的小数部分?例如,

" fmodf(num,1.0f)"或num - (float)(int)num等等。是什么东西

更快比fmodf?我不知道输入范围,所以我不能做伎俩

,例如if(num< 0.0f)num + = 1.0f;"。 br />

2.我知道这不是正确的新闻组,尽管我认为这里有人会有一些好的建议。它似乎比c.l.c ++或comp.unix.programmer更好的选择,这是我真正经常使用的另外两个
新闻组。在将来,对于特定于机器的优化问题,例如

这个问题,最好的

新闻组是什么,不一定是任何特定的编程语言?

谢谢,

Jason

Two questions:

1. On a modern 32-bit Intel machine, but not using SSE, what is the
fastest way to compute the fractional part of a float? E.g.,
"fmodf(num, 1.0f)", or "num - (float)(int)num", etc. Is the something
faster than fmodf? I don''t know the input range, so I can''t do tricks
like "if (num < 0.0f) num += 1.0f;", for example.

2. I know that this is not the correct newsgroup for this, although I
figured somebody here would have some good advice. It seemed like a
better option than c.l.c++ or comp.unix.programmer, the only other two
newsgroups I really frequent. In the future, what is the best
newsgroup for questions about machine-specific optimizations like
this, not necessarily in any specific programming language?

Thanks,
Jason

推荐答案

6月29日,3:57 * pm,jason .cipri ... @ gmail.com"

< jason.cipri ... @ gmail.comwrote:
On Jun 29, 3:57*pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:

1.开一台现代的32位英特尔机器,但不使用SSE,计算浮点数的小数部分的最快方法是什么?例如,

" fmodf(num,1.0f)"或num - (float)(int)num等等。是什么东西

更快比fmodf?我不知道输入范围,所以我不能做伎俩

就像if(num< 0.0f)num + = 1.0f;"。
1. On a modern 32-bit Intel machine, but not using SSE, what is the
fastest way to compute the fractional part of a float? E.g.,
"fmodf(num, 1.0f)", or "num - (float)(int)num", etc. Is the something
faster than fmodf? I don''t know the input range, so I can''t do tricks
like "if (num < 0.0f) num += 1.0f;", for example.



哎呀,我搞砸了这个问题。我正在寻找的不只是

找到一个数字的小数部分,我需要负数

来包裹,所以,不像fmodf,我是寻找:


断言(magic_function(-0.1f)== 0.9f);

断言(magic_function(-3.8f)== 0.2f) ;

断言(magic_function(0.1f)== 0.1f);

断言(magic_function(2.8f)== 0.8f);


所以它的速度相当于:


float magic_function(float f){

while(f <0.0f) )f + = 1.0f;

而(f 1.0f)f - = 1.0f;

返回f;

}


谢谢,

Jason

Oops, I screwed up that question. I am looking for more than just
finding the fractional component of a number, I need negative numbers
to wrap around, too, so, unlike fmodf, I''m looking for:

assert(magic_function(-0.1f) == 0.9f);
assert(magic_function(-3.8f) == 0.2f);
assert(magic_function( 0.1f) == 0.1f);
assert(magic_function( 2.8f) == 0.8f);

So it''s more the fastest equivalent of:

float magic_function (float f) {
while (f < 0.0f) f += 1.0f;
while (f 1.0f) f -= 1.0f;
return f;
}

Thanks,
Jason


ja ************ @ gmail.com 写道:

两个问题:


1.在现代32位Intel机器上,但不使用SSE,

计算小数部分的最快方法是什么?一个漂浮?例如,

" fmodf(num,1.0f)"或num - (float)(int)num等等。是什么东西

更快比fmodf?我不知道输入范围,所以我不能做伎俩

,例如if(num< 0.0f)num + = 1.0f;"。 br />

2.我知道这不是正确的新闻组,尽管我认为这里有人会有一些好的建议。它似乎比c.l.c ++或comp.unix.programmer更好的选择,这是我真正经常使用的另外两个
新闻组。将来,对于机器特定的优化问题,例如

这个问题,最好的

新闻组是什么,不一定是任何特定的编程语言?
Two questions:

1. On a modern 32-bit Intel machine, but not using SSE, what is the
fastest way to compute the fractional part of a float? E.g.,
"fmodf(num, 1.0f)", or "num - (float)(int)num", etc. Is the something
faster than fmodf? I don''t know the input range, so I can''t do tricks
like "if (num < 0.0f) num += 1.0f;", for example.

2. I know that this is not the correct newsgroup for this, although I
figured somebody here would have some good advice. It seemed like a
better option than c.l.c++ or comp.unix.programmer, the only other two
newsgroups I really frequent. In the future, what is the best
newsgroup for questions about machine-specific optimizations like
this, not necessarily in any specific programming language?



我希望modff能够更快地将一个浮点数

分成整数和小数部分,只是因为,

不像fmodf,它不能用于其他任何东西。


N869

7.12.6.12 modf函数

概要

[#1]

#include< math.h>

double modf(double value,double * iptr);

浮点数modff(浮点值,浮点数* iptr);

long double modfl(long double value,long double * iptr);

描述

[#2] modf函数将参数值分解为

积分和小数部分,每个部分具有相同的

类型并且标记为争论。它们将积分部分

(浮点格式)存储在iptr指向的对象中。

返回

[#3] modf函数返回已签名的

小数部分值。


-

pete

I would expect modff to be faster at breaking a float
into integer and fractional parts, simply because,
unlike fmodf, it can''t be used for anything else.

N869
7.12.6.12 The modf functions
Synopsis
[#1]
#include <math.h>
double modf(double value, double *iptr);
float modff(float value, float *iptr);
long double modfl(long double value, long double *iptr);
Description
[#2] The modf functions break the argument value into
integral and fractional parts, each of which has the same
type and sign as the argument. They store the integral part
(in floating-point format) in the object pointed to by iptr.
Returns
[#3] The modf functions return the value of the signed
fractional part of value.

--
pete


On Sun,2008年6月29日12:57:15 -0700(PDT),ja ************ @ gmail.com

< ja ************ @ gmail.comwrote:
On Sun, 29 Jun 2008 12:57:15 -0700 (PDT), "ja************@gmail.com"
<ja************@gmail.comwrote:

>两个问题:

1。在现代的32位Intel机器上,但不使用SSE,计算浮点数的小数部分的最快方法是什么?例如,
>Two questions:

1. On a modern 32-bit Intel machine, but not using SSE, what is the
fastest way to compute the fractional part of a float? E.g.,



为什么要关心。过早优化是许多

的来源,否则可以避免问题。

Why do you care. Premature optimization is the source of many
otherwise avoidable problems.


>" fmodf(num,1.0f)",或者" num - (float)(int)num"等等。
>"fmodf(num, 1.0f)", or "num - (float)(int)num", etc. Is the something



无法保证num可以转换为int。它可能非常容易超过INT_MAX。

There is no guarantee that num can be converted to int. It could very
easily exceed INT_MAX.


>比fmodf更快?我不知道输入范围,所以我不能做例如if(num< 0.0f)num + = 1.0f;的技巧。
>faster than fmodf? I don''t know the input range, so I can''t do tricks
like "if (num < 0.0f) num += 1.0f;", for example.



当然可以。只需在while循环中包含此语句即可。请注意

该方法存在一些问题:1)可能需要很多次b / b
迭代(有解决方法),2)足够

大数,num + 1等于num(也有解决这个问题的方法),

和3)它可能会引入舍入错误。

Sure you can. Just include this statement in a while loop. Be aware
there are a few problems with the approach: 1) it could take very many
iterations (there are ways to code around this), 2) for sufficiently
large num, num+1 equals num (there are ways to code around this too),
and 3) it could introduce rounding errors.


>
2。我知道这不是正确的新闻组,虽然我认为这里有人会有一些好的建议。它似乎比clc ++或comp.unix.programmer更好的选择,唯一的另外两个
>
2. I know that this is not the correct newsgroup for this, although I
figured somebody here would have some good advice. It seemed like a
better option than c.l.c++ or comp.unix.programmer, the only other two



因为C和C ++是完全独立的语言,所以决定你想要编码哪一个

只在适当的组中询问。

Since C and C++ are completely separate languages, decide which one
you want to code in and ask in only the appropriate group.


> newsgroups我经常这么做。将来,对于机器特定的优化问题,最好的新闻组是什么?
这个,不一定是任何特定的编程语言?
>newsgroups I really frequent. In the future, what is the best
newsgroup for questions about machine-specific optimizations like
this, not necessarily in any specific programming language?



专门用于您系统的新闻组。可能有一个硬件

指令完全符合您的要求。

删除del电子邮件

A newsgroup devoted to your system. There may be a hardware
instruction that does exactly what you want.
Remove del for email


这篇关于快速fmo​​df(...,1.0f)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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