为什么Math.sin()委托给StrictMath.sin()? [英] Why does Math.sin() delegate to StrictMath.sin()?

查看:144
本文介绍了为什么Math.sin()委托给StrictMath.sin()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,为什么 Math.sin(double)委托给 StrictMath.sin(double)我在 Reddit主题中发现了这个问题。提到的代码片段如下所示(JDK 7u25):

I was wondering, why does Math.sin(double) delegate to StrictMath.sin(double) when I've found the problem in a Reddit thread. The mentioned code fragment looks like this (JDK 7u25):

Math.java

public static double sin(double a) {
    return StrictMath.sin(a); // default impl. delegates to StrictMath
}

StrictMath.java

public static native double sin(double a);

第二个声明是 native 这是合理的为了我。 Math 的文档声明:

The second declaration is native which is reasonable for me. The doc of Math states that:


鼓励代码生成器使用平台 - 特定的本机库或微处理器指令,如果可用(...)

Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available (...)

问题是:isn实现 StrictMath 的本机库是否足够平台? JIT比安装的JRE更了解平台(请专注于这种情况)?换句话说,为什么不是 Math.sin()原生?

And the question is: isn't the native library that implements StrictMath platform-specific enough? What more can a JIT know about the platform than an installed JRE (please only concentrate on this very case)? In ther words, why isn't Math.sin() native already?

推荐答案

我会尝试在一篇文章中结束整个讨论..

I'll try to wrap up the entire discussion in a single post..

一般来说,数学委托 StrictMath 。显然,调用可以是内联,因此这不是性能问题。

Generally, Math delegates to StrictMath. Obviously, the call can be inlined so this is not a performance issue.

StrictMath 是一个由本地库支持的 native 方法的最终类。有人可能会认为, native意味着最佳,但这不一定是这种情况。查看 StrictMath javadoc可以阅读以下内容:

StrictMath is a final class with native methods backed by native libraries. One might think, that native means optimal, but this doesn't necessarily has to be the case. Looking through StrictMath javadoc one can read the following:


(...)此程序包中某些数字函数的定义要求它们产生与某些已发布算法相同的结果。这些算法可以从众所周知的网络库netlib获得,作为Freely Distributable Math Library包fdlibm。然后,这些以C编程语言编写的算法将被理解为遵循Java浮点算法规则的所有浮点运算执行。

(...) the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms. These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library," fdlibm. These algorithms, which are written in the C programming language, are then to be understood as executed with all floating-point operations following the rules of Java floating-point arithmetic.

我如何理解这个文档是实现 StrictMath 的本机库是用 fdlibm 库实现的,这是多平台,已知可产生可预测的结果。因为它是多平台的,所以不能期望它是每个平台上的最佳实现,并且我认为这是智能JIT可以微调实际性能的地方,例如通过输入范围的统计分析并相应地调整算法/实现。

How I understand this doc is that the native library implementing StrictMath is implemented in terms of fdlibm library, which is multi-platform and known to produce predictable results. Because it's multi-platform, it can't be expected to be an optimal implementation on every platform and I believe that this is the place where a smart JIT can fine-tune the actual performance e.g. by statistical analysis of input ranges and adjusting the algorithms/implementation accordingly.

深入研究实现,很快就会发现,本机库备份 StrictMath 实际使用 fdlibm

Digging deeper into the implementation it quickly turns out, that the native library backing up StrictMath actually uses fdlibm:

StrictMath.c 源代码7看起来像这样:

StrictMath.c source in OpenJDK 7 looks like this:

   #include "fdlibm.h"
   ...
   JNIEXPORT jdouble JNICALL
   Java_java_lang_StrictMath_sin(JNIEnv *env, jclass unused, jdouble d)
   {
       return (jdouble) jsin((double)d);
   }

并且正弦函数在 fdlibm / src / s_sin.c 在几个地方引用 __ kernel_sin 函数直接来自标题 fdlibm.h

and the sine function is defined in fdlibm/src/s_sin.c refering in a few places to __kernel_sin function that comes directly from the header fdlibm.h.


虽然我暂时接受我自己的回答,但我很乐意接受当它出现时更有能力。

While I'm temporarily accepting my own answer, I'd be glad to accept a more competent one when it comes up.

这篇关于为什么Math.sin()委托给StrictMath.sin()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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