JolyJDIA

Untitled

Jul 9th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. public static strictfp double compute(double x, double y) {
  2.             double a = Math.abs(x);
  3.             double b = Math.abs(y);
  4.  
  5.             if (!Double.isFinite(a) || !Double.isFinite(b)) {
  6.                 if (a == INFINITY || b == INFINITY)
  7.                     return INFINITY;
  8.                 else
  9.                     return a + b; // Propagate NaN significand bits
  10.             }
  11.  
  12.             if (b > a) {
  13.                 double tmp = a;
  14.                 a = b;
  15.                 b = tmp;
  16.             }
  17.             assert a >= b;
  18.  
  19.             // Doing bitwise conversion after screening for NaN allows
  20.             // the code to not worry about the possibility of
  21.             // "negative" NaN values.
  22.  
  23.             // Note: the ha and hb variables are the high-order
  24.             // 32-bits of a and b stored as integer values. The ha and
  25.             // hb values are used first for a rough magnitude
  26.             // comparison of a and b and second for simulating higher
  27.             // precision by allowing a and b, respectively, to be
  28.             // decomposed into non-overlapping portions. Both of these
  29.             // uses could be eliminated. The magnitude comparison
  30.             // could be eliminated by extracting and comparing the
  31.             // exponents of a and b or just be performing a
  32.             // floating-point divide.  Splitting a floating-point
  33.             // number into non-overlapping portions can be
  34.             // accomplished by judicious use of multiplies and
  35.             // additions. For details see T. J. Dekker, A Floating
  36.             // Point Technique for Extending the Available Precision ,
  37.             // Numerische Mathematik, vol. 18, 1971, pp.224-242 and
  38.             // subsequent work.
  39.  
  40.             int ha = __HI(a);        // high word of a
  41.             int hb = __HI(b);        // high word of b
  42.  
  43.             if ((ha - hb) > 0x3c00000) {
  44.                 return a + b;  // x / y > 2**60
  45.             }
  46.  
  47.             int k = 0;
  48.             if (a > 0x1.00000_ffff_ffffp500) {   // a > ~2**500
  49.                 // scale a and b by 2**-600
  50.                 ha -= 0x25800000;
  51.                 hb -= 0x25800000;
  52.                 a = a * TWO_MINUS_600;
  53.                 b = b * TWO_MINUS_600;
  54.                 k += 600;
  55.             }
  56.             double t1, t2;
  57.             if (b < 0x1.0p-500) {   // b < 2**-500
  58.                 if (b < Double.MIN_NORMAL) {      // subnormal b or 0 */
  59.                     if (b == 0.0)
  60.                         return a;
  61.                     t1 = 0x1.0p1022;   // t1 = 2^1022
  62.                     b *= t1;
  63.                     a *= t1;
  64.                     k -= 1022;
  65.                 } else {            // scale a and b by 2^600
  66.                     ha += 0x25800000;       // a *= 2^600
  67.                     hb += 0x25800000;       // b *= 2^600
  68.                     a = a * TWO_PLUS_600;
  69.                     b = b * TWO_PLUS_600;
  70.                     k -= 600;
  71.                 }
  72.             }
  73.             // medium size a and b
  74.             double w = a - b;
  75.             if (w > b) {
  76.                 t1 = 0;
  77.                 t1 = __HI(t1, ha);
  78.                 t2 = a - t1;
  79.                 w  = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
  80.             } else {
  81.                 double y1, y2;
  82.                 a  = a + a;
  83.                 y1 = 0;
  84.                 y1 = __HI(y1, hb);
  85.                 y2 = b - y1;
  86.                 t1 = 0;
  87.                 t1 = __HI(t1, ha + 0x00100000);
  88.                 t2 = a - t1;
  89.                 w  = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
  90.             }
  91.             if (k != 0) {
  92.                 return Math.powerOfTwoD(k) * w;
  93.             } else
  94.                 return w;
  95.         }
Add Comment
Please, Sign In to add comment