native code for Java Math class

前端 未结 1 2031
野性不改
野性不改 2021-01-04 10:36

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.

1条回答
  •  借酒劲吻你
    2021-01-04 11:22

    This is implementation-dependent. As stated in the documentation for 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.

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

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

    and the implementation of __kernel_sin looks like

    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 is similar.

    0 讨论(0)
提交回复
热议问题