< prev index next >

test/java/lang/StrictMath/FdlibmTranslit.java

Print this page




  56      */
  57     private static int __HI(double x) {
  58         long transducer = Double.doubleToRawLongBits(x);
  59         return (int)(transducer >> 32);
  60     }
  61 
  62     /**
  63      * Return a double with its high-order bits of the second argument
  64      * and the low-order bits of the first argument..
  65      */
  66     private static double __HI(double x, int high) {
  67         long transX = Double.doubleToRawLongBits(x);
  68         return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
  69     }
  70 
  71     public static double hypot(double x, double y) {
  72         return Hypot.compute(x, y);
  73     }
  74 
  75     /**





























































  76      * hypot(x,y)
  77      *
  78      * Method :
  79      *      If (assume round-to-nearest) z = x*x + y*y
  80      *      has error less than sqrt(2)/2 ulp, than
  81      *      sqrt(z) has error less than 1 ulp (exercise).
  82      *
  83      *      So, compute sqrt(x*x + y*y) with some care as
  84      *      follows to get the error below 1 ulp:
  85      *
  86      *      Assume x > y > 0;
  87      *      (if possible, set rounding to round-to-nearest)
  88      *      1. if x > 2y  use
  89      *              x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y
  90      *      where x1 = x with lower 32 bits cleared, x2 = x - x1; else
  91      *      2. if x <= 2y use
  92      *              t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y))
  93      *      where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1,
  94      *      y1= y with lower 32 bits chopped, y2 = y - y1.
  95      *




  56      */
  57     private static int __HI(double x) {
  58         long transducer = Double.doubleToRawLongBits(x);
  59         return (int)(transducer >> 32);
  60     }
  61 
  62     /**
  63      * Return a double with its high-order bits of the second argument
  64      * and the low-order bits of the first argument..
  65      */
  66     private static double __HI(double x, int high) {
  67         long transX = Double.doubleToRawLongBits(x);
  68         return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL)|( ((long)high)) << 32 );
  69     }
  70 
  71     public static double hypot(double x, double y) {
  72         return Hypot.compute(x, y);
  73     }
  74 
  75     /**
  76      * cbrt(x)
  77      * Return cube root of x
  78      */
  79     public static class Cbrt {
  80         // unsigned
  81         private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */
  82         private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
  83 
  84         private static final double C =  5.42857142857142815906e-01; /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
  85         private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */
  86         private static final double E =  1.41428571428571436819e+00; /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
  87         private static final double F =  1.60714285714285720630e+00; /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
  88         private static final double G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
  89 
  90         public static strictfp double compute(double x) {
  91             int     hx;
  92             double  r, s, t=0.0, w;
  93             int sign; // unsigned
  94 
  95             hx = __HI(x);           // high word of x
  96             sign = hx & 0x80000000;             // sign= sign(x)
  97             hx  ^= sign;
  98             if (hx >= 0x7ff00000)
  99                 return (x+x); // cbrt(NaN,INF) is itself
 100             if ((hx | __LO(x)) == 0)
 101                 return(x);          // cbrt(0) is itself
 102 
 103             x = __HI(x, hx);   // x <- |x|
 104             // rough cbrt to 5 bits
 105             if (hx < 0x00100000) {               // subnormal number
 106                 t = __HI(t, 0x43500000);          // set t= 2**54
 107                 t *= x;
 108                 t = __HI(t, __HI(t)/3+B2);
 109             } else {
 110                 t = __HI(t, hx/3+B1);
 111             }
 112 
 113             // new cbrt to 23 bits, may be implemented in single precision
 114             r = t * t/x;
 115             s = C + r*t;
 116             t *= G + F/(s + E + D/s);
 117 
 118             // chopped to 20 bits and make it larger than cbrt(x)
 119             t = __LO(t, 0);
 120             t = __HI(t, __HI(t)+0x00000001);
 121 
 122 
 123             // one step newton iteration to 53 bits with error less than 0.667 ulps
 124             s = t * t;          // t*t is exact
 125             r = x / s;
 126             w = t + t;
 127             r= (r - t)/(w + r);  // r-s is exact
 128             t= t + t*r;
 129 
 130             // retore the sign bit
 131             t = __HI(t, __HI(t) | sign);
 132             return(t);
 133         }
 134     }
 135 
 136     /**
 137      * hypot(x,y)
 138      *
 139      * Method :
 140      *      If (assume round-to-nearest) z = x*x + y*y
 141      *      has error less than sqrt(2)/2 ulp, than
 142      *      sqrt(z) has error less than 1 ulp (exercise).
 143      *
 144      *      So, compute sqrt(x*x + y*y) with some care as
 145      *      follows to get the error below 1 ulp:
 146      *
 147      *      Assume x > y > 0;
 148      *      (if possible, set rounding to round-to-nearest)
 149      *      1. if x > 2y  use
 150      *              x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y
 151      *      where x1 = x with lower 32 bits cleared, x2 = x - x1; else
 152      *      2. if x <= 2y use
 153      *              t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y))
 154      *      where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1,
 155      *      y1= y with lower 32 bits chopped, y2 = y - y1.
 156      *


< prev index next >