Java Math类的本机代码 [英] native code for Java Math class

查看:81
本文介绍了Java Math类的本机代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以访问Math类的本机代码。更具体地说,我需要查看sin()方法的代码。

I was wondering if there is any way I could gain access to the native code for the Math class. More specifically I need to see the code for the sin() method.

推荐答案

这与实现有关。如 java的文档中所述.lang.Math

This is implementation-dependent. As stated in the documentation for java.lang.Math:


与类<$ c $的某些数值方法不同c> StrictMath ,类 Math 的等效函数的所有实现都未定义为返回逐位相同的结果。这种放宽允许在不需要严格再现性的情况下实现性能更好的实现。

Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

...鼓励代码生成器使用特定于平台的本机库或微处理器指令,如果可用,提供 Math 方法的更高性能实现。此类更高性能的实现仍必须符合 Math 的规范。

... Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations of Math methods. Such higher-performance implementations still must conform to the specification for Math.

对于Dalvik(Java的Android实现):

For Dalvik (the Android implementation of Java):

dalvik / vm / InlineNative.c

/*
 * public static double sin(double)
 */
static bool javaLangMath_sin(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
    JValue* pResult)
{
    Convert64 convert;
    convert.arg[0] = arg0;
    convert.arg[1] = arg1;
    pResult->d = sin(convert.dd);
    return true;
}

所以它调用 libm sin 功能,在Android上由bionic libc提供。看起来像

So it calls the libm sin function, which on Android is supplied by bionic libc. That looks like

bionic / libm / src / s_sin.c

double
sin(double x)
{
    double y[2],z=0.0;
    int32_t n, ix;

    /* High word of x. */
    GET_HIGH_WORD(ix,x);

    /* |x| ~< pi/4 */
    ix &= 0x7fffffff;
    if(ix <= 0x3fe921fb) {
        if(ix<0x3e400000)           /* |x| < 2**-27 */
           {if((int)x==0) return x;}    /* generate inexact */
        return __kernel_sin(x,z,0);
    }

    /* sin(Inf or NaN) is NaN */
    else if (ix>=0x7ff00000) return x-x;

    /* argument reduction needed */
    else {
        n = __ieee754_rem_pio2(x,y);
        switch(n&3) {
        case 0: return  __kernel_sin(y[0],y[1],1);
        case 1: return  __kernel_cos(y[0],y[1]);
        case 2: return -__kernel_sin(y[0],y[1],1);
        default:
            return -__kernel_cos(y[0],y[1]);
        }
    }
}

以及<$的实施c $ c> __ kernel_sin 看起来像

bionic / libm / src / k_sin.c

static const double
half =  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
S1  = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
S2  =  8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
S3  = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
S4  =  2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
S5  = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
S6  =  1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */

double
__kernel_sin(double x, double y, int iy)
{
    double z,r,v;

    z   =  x*x;
    v   =  z*x;
    r   =  S2+z*(S3+z*(S4+z*(S5+z*S6)));
    if(iy==0) return x+v*(S1+z*r);
    else      return x-((z*(half*y-v*r)-y)-v*S1);
}

__ kernel_cos 类似。

这篇关于Java Math类的本机代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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