test/jdk/java/lang/StrictMath/FdlibmTranslit.java
author herrick
Mon, 14 Oct 2019 14:36:45 -0400
branchJDK-8200758-branch
changeset 58584 910b14f4fe3a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232042: [macos] Installation fails if application name contains spaces Submitted-by: almatvee Reviewed-by: aherrick, asemenyuk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     1
/*
42751
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
     2
 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     4
 *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    10
 *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    15
 * accompanied this code).
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    16
 *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    20
 *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    23
 * questions.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    24
 */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    25
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    26
/**
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    27
 * A transliteration of the "Freely Distributable Math Library"
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    28
 * algorithms from C into Java. That is, this port of the algorithms
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    29
 * is as close to the C originals as possible while still being
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    30
 * readable legal Java.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    31
 */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    32
public class FdlibmTranslit {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    33
    private FdlibmTranslit() {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    34
        throw new UnsupportedOperationException("No FdLibmTranslit instances for you.");
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    35
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    36
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    37
    /**
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    38
     * Return the low-order 32 bits of the double argument as an int.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    39
     */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    40
    private static int __LO(double x) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    41
        long transducer = Double.doubleToRawLongBits(x);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    42
        return (int)transducer;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    43
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    44
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    45
    /**
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    46
     * Return a double with its low-order bits of the second argument
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    47
     * and the high-order bits of the first argument..
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    48
     */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    49
    private static double __LO(double x, int low) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    50
        long transX = Double.doubleToRawLongBits(x);
42751
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
    51
        return Double.longBitsToDouble((transX & 0xFFFF_FFFF_0000_0000L) |
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
    52
                                       (low    & 0x0000_0000_FFFF_FFFFL));
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    53
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    54
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    55
    /**
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    56
     * Return the high-order 32 bits of the double argument as an int.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    57
     */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    58
    private static int __HI(double x) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    59
        long transducer = Double.doubleToRawLongBits(x);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    60
        return (int)(transducer >> 32);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    61
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    62
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    63
    /**
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    64
     * Return a double with its high-order bits of the second argument
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    65
     * and the low-order bits of the first argument..
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    66
     */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    67
    private static double __HI(double x, int high) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    68
        long transX = Double.doubleToRawLongBits(x);
42751
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
    69
        return Double.longBitsToDouble((transX & 0x0000_0000_FFFF_FFFFL) |
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
    70
                                       ( ((long)high)) << 32 );
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    71
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    72
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    73
    public static double hypot(double x, double y) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    74
        return Hypot.compute(x, y);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    75
    }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    76
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
    77
    /**
32992
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    78
     * cbrt(x)
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    79
     * Return cube root of x
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    80
     */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    81
    public static class Cbrt {
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    82
        // unsigned
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    83
        private static final int B1 = 715094163; /* B1 = (682-0.03306235651)*2**20 */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    84
        private static final int B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    85
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    86
        private static final double C =  5.42857142857142815906e-01; /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    87
        private static final double D = -7.05306122448979611050e-01; /* -864/1225 = 0xBFE691DE, 0x2532C834 */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    88
        private static final double E =  1.41428571428571436819e+00; /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    89
        private static final double F =  1.60714285714285720630e+00; /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    90
        private static final double G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    91
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    92
        public static strictfp double compute(double x) {
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    93
            int     hx;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    94
            double  r, s, t=0.0, w;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    95
            int sign; // unsigned
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    96
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    97
            hx = __HI(x);           // high word of x
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    98
            sign = hx & 0x80000000;             // sign= sign(x)
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
    99
            hx  ^= sign;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   100
            if (hx >= 0x7ff00000)
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   101
                return (x+x); // cbrt(NaN,INF) is itself
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   102
            if ((hx | __LO(x)) == 0)
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   103
                return(x);          // cbrt(0) is itself
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   104
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   105
            x = __HI(x, hx);   // x <- |x|
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   106
            // rough cbrt to 5 bits
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   107
            if (hx < 0x00100000) {               // subnormal number
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   108
                t = __HI(t, 0x43500000);          // set t= 2**54
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   109
                t *= x;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   110
                t = __HI(t, __HI(t)/3+B2);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   111
            } else {
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   112
                t = __HI(t, hx/3+B1);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   113
            }
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   114
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   115
            // new cbrt to 23 bits, may be implemented in single precision
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   116
            r = t * t/x;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   117
            s = C + r*t;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   118
            t *= G + F/(s + E + D/s);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   119
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   120
            // chopped to 20 bits and make it larger than cbrt(x)
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   121
            t = __LO(t, 0);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   122
            t = __HI(t, __HI(t)+0x00000001);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   123
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   124
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   125
            // one step newton iteration to 53 bits with error less than 0.667 ulps
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   126
            s = t * t;          // t*t is exact
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   127
            r = x / s;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   128
            w = t + t;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   129
            r= (r - t)/(w + r);  // r-s is exact
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   130
            t= t + t*r;
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   131
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   132
            // retore the sign bit
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   133
            t = __HI(t, __HI(t) | sign);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   134
            return(t);
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   135
        }
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   136
    }
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   137
19ed8781c2ba 8136799: Port fdlibm cbrt to Java
darcy
parents: 32928
diff changeset
   138
    /**
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   139
     * hypot(x,y)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   140
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   141
     * Method :
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   142
     *      If (assume round-to-nearest) z = x*x + y*y
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   143
     *      has error less than sqrt(2)/2 ulp, than
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   144
     *      sqrt(z) has error less than 1 ulp (exercise).
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   145
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   146
     *      So, compute sqrt(x*x + y*y) with some care as
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   147
     *      follows to get the error below 1 ulp:
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   148
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   149
     *      Assume x > y > 0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   150
     *      (if possible, set rounding to round-to-nearest)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   151
     *      1. if x > 2y  use
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   152
     *              x1*x1 + (y*y + (x2*(x + x1))) for x*x + y*y
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   153
     *      where x1 = x with lower 32 bits cleared, x2 = x - x1; else
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   154
     *      2. if x <= 2y use
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   155
     *              t1*y1 + ((x-y) * (x-y) + (t1*y2 + t2*y))
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   156
     *      where t1 = 2x with lower 32 bits cleared, t2 = 2x - t1,
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   157
     *      y1= y with lower 32 bits chopped, y2 = y - y1.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   158
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   159
     *      NOTE: scaling may be necessary if some argument is too
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   160
     *            large or too tiny
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   161
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   162
     * Special cases:
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   163
     *      hypot(x,y) is INF if x or y is +INF or -INF; else
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   164
     *      hypot(x,y) is NAN if x or y is NAN.
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   165
     *
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   166
     * Accuracy:
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   167
     *      hypot(x,y) returns sqrt(x^2 + y^2) with error less
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   168
     *      than 1 ulps (units in the last place)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   169
     */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   170
    static class Hypot {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   171
        public static double compute(double x, double y) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   172
            double a = x;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   173
            double b = y;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   174
            double t1, t2, y1, y2, w;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   175
            int j, k, ha, hb;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   176
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   177
            ha = __HI(x) & 0x7fffffff;        // high word of  x
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   178
            hb = __HI(y) & 0x7fffffff;        // high word of  y
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   179
            if(hb > ha) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   180
                a = y;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   181
                b = x;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   182
                j = ha;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   183
                ha = hb;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   184
                hb = j;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   185
            } else {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   186
                a = x;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   187
                b = y;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   188
            }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   189
            a = __HI(a, ha);   // a <- |a|
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   190
            b = __HI(b, hb);   // b <- |b|
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   191
            if ((ha - hb) > 0x3c00000) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   192
                return a + b;  // x / y > 2**60
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   193
            }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   194
            k=0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   195
            if (ha > 0x5f300000) {   // a>2**500
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   196
                if (ha >= 0x7ff00000) {       // Inf or NaN
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   197
                    w = a + b;                // for sNaN
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   198
                    if (((ha & 0xfffff) | __LO(a)) == 0)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   199
                        w = a;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   200
                    if (((hb ^ 0x7ff00000) | __LO(b)) == 0)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   201
                        w = b;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   202
                    return w;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   203
                }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   204
                // scale a and b by 2**-600
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   205
                ha -= 0x25800000;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   206
                hb -= 0x25800000;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   207
                k += 600;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   208
                a = __HI(a, ha);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   209
                b = __HI(b, hb);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   210
            }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   211
            if (hb < 0x20b00000) {   // b < 2**-500
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   212
                if (hb <= 0x000fffff) {      // subnormal b or 0 */
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   213
                    if ((hb | (__LO(b))) == 0)
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   214
                        return a;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   215
                    t1 = 0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   216
                    t1 = __HI(t1, 0x7fd00000);  // t1=2^1022
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   217
                    b *= t1;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   218
                    a *= t1;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   219
                    k -= 1022;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   220
                } else {            // scale a and b by 2^600
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   221
                    ha += 0x25800000;       // a *= 2^600
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   222
                    hb += 0x25800000;       // b *= 2^600
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   223
                    k -= 600;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   224
                    a = __HI(a, ha);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   225
                    b = __HI(b, hb);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   226
                }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   227
            }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   228
            // medium size a and b
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   229
            w = a - b;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   230
            if (w > b) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   231
                t1 = 0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   232
                t1 = __HI(t1, ha);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   233
                t2 = a - t1;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   234
                w  = Math.sqrt(t1*t1 - (b*(-b) - t2 * (a + t1)));
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   235
            } else {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   236
                a  = a + a;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   237
                y1 = 0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   238
                y1 = __HI(y1, hb);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   239
                y2 = b - y1;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   240
                t1 = 0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   241
                t1 = __HI(t1, ha + 0x00100000);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   242
                t2 = a - t1;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   243
                w  = Math.sqrt(t1*y1 - (w*(-w) - (t1*y2 + t2*b)));
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   244
            }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   245
            if (k != 0) {
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   246
                t1 = 1.0;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   247
                int t1_hi = __HI(t1);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   248
                t1_hi += (k << 20);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   249
                t1 = __HI(t1, t1_hi);
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   250
                return t1 * w;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   251
            } else
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   252
                return w;
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   253
        }
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   254
    }
42751
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   255
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   256
    /**
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   257
     * Returns the exponential of x.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   258
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   259
     * Method
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   260
     *   1. Argument reduction:
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   261
     *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   262
     *      Given x, find r and integer k such that
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   263
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   264
     *               x = k*ln2 + r,  |r| <= 0.5*ln2.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   265
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   266
     *      Here r will be represented as r = hi-lo for better
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   267
     *      accuracy.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   268
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   269
     *   2. Approximation of exp(r) by a special rational function on
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   270
     *      the interval [0,0.34658]:
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   271
     *      Write
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   272
     *          R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   273
     *      We use a special Reme algorithm on [0,0.34658] to generate
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   274
     *      a polynomial of degree 5 to approximate R. The maximum error
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   275
     *      of this polynomial approximation is bounded by 2**-59. In
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   276
     *      other words,
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   277
     *          R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   278
     *      (where z=r*r, and the values of P1 to P5 are listed below)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   279
     *      and
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   280
     *          |                  5          |     -59
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   281
     *          | 2.0+P1*z+...+P5*z   -  R(z) | <= 2
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   282
     *          |                             |
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   283
     *      The computation of exp(r) thus becomes
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   284
     *                             2*r
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   285
     *              exp(r) = 1 + -------
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   286
     *                            R - r
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   287
     *                                 r*R1(r)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   288
     *                     = 1 + r + ----------- (for better accuracy)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   289
     *                                2 - R1(r)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   290
     *      where
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   291
     *                               2       4             10
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   292
     *              R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   293
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   294
     *   3. Scale back to obtain exp(x):
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   295
     *      From step 1, we have
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   296
     *         exp(x) = 2^k * exp(r)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   297
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   298
     * Special cases:
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   299
     *      exp(INF) is INF, exp(NaN) is NaN;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   300
     *      exp(-INF) is 0, and
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   301
     *      for finite argument, only exp(0)=1 is exact.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   302
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   303
     * Accuracy:
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   304
     *      according to an error analysis, the error is always less than
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   305
     *      1 ulp (unit in the last place).
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   306
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   307
     * Misc. info.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   308
     *      For IEEE double
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   309
     *          if x >  7.09782712893383973096e+02 then exp(x) overflow
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   310
     *          if x < -7.45133219101941108420e+02 then exp(x) underflow
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   311
     *
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   312
     * Constants:
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   313
     * The hexadecimal values are the intended ones for the following
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   314
     * constants. The decimal values may be used, provided that the
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   315
     * compiler will convert from decimal to binary accurately enough
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   316
     * to produce the hexadecimal values shown.
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   317
     */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   318
    static class Exp {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   319
        private static final double one     = 1.0;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   320
        private static final double[] halF = {0.5,-0.5,};
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   321
        private static final double huge    = 1.0e+300;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   322
        private static final double twom1000= 9.33263618503218878990e-302;      /* 2**-1000=0x01700000,0*/
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   323
        private static final double o_threshold=  7.09782712893383973096e+02;   /* 0x40862E42, 0xFEFA39EF */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   324
        private static final double u_threshold= -7.45133219101941108420e+02;   /* 0xc0874910, 0xD52D3051 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   325
        private static final double[] ln2HI   ={ 6.93147180369123816490e-01,    /* 0x3fe62e42, 0xfee00000 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   326
                                                 -6.93147180369123816490e-01};  /* 0xbfe62e42, 0xfee00000 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   327
        private static final double[] ln2LO   ={ 1.90821492927058770002e-10,    /* 0x3dea39ef, 0x35793c76 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   328
                                                 -1.90821492927058770002e-10,}; /* 0xbdea39ef, 0x35793c76 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   329
        private static final double invln2 =  1.44269504088896338700e+00;       /* 0x3ff71547, 0x652b82fe */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   330
        private static final double P1   =  1.66666666666666019037e-01;         /* 0x3FC55555, 0x5555553E */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   331
        private static final double P2   = -2.77777777770155933842e-03;         /* 0xBF66C16C, 0x16BEBD93 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   332
        private static final double P3   =  6.61375632143793436117e-05;         /* 0x3F11566A, 0xAF25DE2C */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   333
        private static final double P4   = -1.65339022054652515390e-06;         /* 0xBEBBBD41, 0xC5D26BF1 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   334
        private static final double P5   =  4.13813679705723846039e-08;         /* 0x3E663769, 0x72BEA4D0 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   335
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   336
        public static strictfp double compute(double x) {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   337
            double y,hi=0,lo=0,c,t;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   338
            int k=0,xsb;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   339
            /*unsigned*/ int hx;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   340
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   341
            hx  = __HI(x);  /* high word of x */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   342
            xsb = (hx>>31)&1;               /* sign bit of x */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   343
            hx &= 0x7fffffff;               /* high word of |x| */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   344
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   345
            /* filter out non-finite argument */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   346
            if(hx >= 0x40862E42) {                  /* if |x|>=709.78... */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   347
                if(hx>=0x7ff00000) {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   348
                    if(((hx&0xfffff)|__LO(x))!=0)
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   349
                        return x+x;                /* NaN */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   350
                    else return (xsb==0)? x:0.0;    /* exp(+-inf)={inf,0} */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   351
                }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   352
                if(x > o_threshold) return huge*huge; /* overflow */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   353
                if(x < u_threshold) return twom1000*twom1000; /* underflow */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   354
            }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   355
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   356
            /* argument reduction */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   357
            if(hx > 0x3fd62e42) {           /* if  |x| > 0.5 ln2 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   358
                if(hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   359
                    hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   360
                } else {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   361
                    k  = (int)(invln2*x+halF[xsb]);
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   362
                    t  = k;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   363
                    hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   364
                    lo = t*ln2LO[0];
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   365
                }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   366
                x  = hi - lo;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   367
            }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   368
            else if(hx < 0x3e300000)  {     /* when |x|<2**-28 */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   369
                if(huge+x>one) return one+x;/* trigger inexact */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   370
            }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   371
            else k = 0;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   372
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   373
            /* x is now in primary range */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   374
            t  = x*x;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   375
            c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   376
            if(k==0)        return one-((x*c)/(c-2.0)-x);
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   377
            else            y = one-((lo-(x*c)/(2.0-c))-hi);
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   378
            if(k >= -1021) {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   379
                y = __HI(y, __HI(y) + (k<<20)); /* add k to y's exponent */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   380
                return y;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   381
            } else {
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   382
                y = __HI(y, __HI(y) + ((k+1000)<<20));/* add k to y's exponent */
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   383
                return y*twom1000;
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   384
            }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   385
        }
38d28e784f44 8139688: Port fdlibm exp to Java
darcy
parents: 32992
diff changeset
   386
    }
32928
a3f03999ed62 8138823: Correct bug in port of fdlibm hypot to Java
darcy
parents:
diff changeset
   387
}