java - Java 数学类的 native 代码

标签 java

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

最佳答案

这是依赖于实现的。如 java.lang.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.

... 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 实现):

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 提供。看起来像

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]);
        }
    }
}

__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 - Java 数学类的 native 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14970750/

相关文章:

java - Java 8 CFG 中无法访问的规则?

java - MS Project Server 2010 Java API

java - 整数求和

java - 使用 Java 8 将带引号的 ArrayList<String> 转换为从外部 txt 文件读取的不带引号的 ArrayList<String>

java - Eclipse 中光标前的矩形

java - 我真的需要创建一个类来处理java中的每个事件吗?

java - 哪种分析技术最适合分析程序行为

java - 如何在 Swing 中绘制高分辨率?

java.lang.OutOfMemoryError : Java heap space when i tried to create a BufferedImage 错误

java - Spring集成总是将收到的消息标记为已读