1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __hypotl = hypotl
  32 
  33 /*
  34  * long double hypotl(long double x, long double y);
  35  * Method :
  36  *      If z=x*x+y*y has error less than sqrt(2)/2 ulp than sqrt(z) has
  37  *      error less than 1 ulp.
  38  *      So, compute sqrt(x*x+y*y) with some care as follows:
  39  *      Assume x>y>0;
  40  *      1. save and set rounding to round-to-nearest
  41  *      2. if x > 2y  use
  42  *              x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
  43  *      where x1 = x with lower 64 bits cleared, x2 = x-x1; else
  44  *      3. if x <= 2y use
  45  *              t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
  46  *      where t1 = 2x with lower 64 bits cleared, t2 = 2x-t1, y1= y with
  47  *      lower 64 bits chopped, y2 = y-y1.
  48  *
  49  *      NOTE: DO NOT remove parenthsis!
  50  *
  51  * Special cases:
  52  *      hypot(x,y) is INF if x or y is +INF or -INF; else
  53  *      hypot(x,y) is NAN if x or y is NAN.
  54  *
  55  * Accuracy:
  56  *      hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
  57  *      in the last place)
  58  */
  59 
  60 #include "libm.h"
  61 #include "longdouble.h"
  62 
  63 extern enum fp_direction_type __swapRD(enum fp_direction_type);
  64 
  65 static const long double zero = 0.0L, one = 1.0L;
  66 long double
  67 hypotl(long double x, long double y)
  68 {
  69         int n0, n1, n2, n3;
  70         long double t1, t2, y1, y2, w;
  71         int *px = (int *)&x, *py = (int *)&y;
  72         int *pt1 = (int *)&t1, *py1 = (int *)&y1;
  73         enum fp_direction_type rd;
  74         int j, k, nx, ny, nz;
  75 
  76         if ((*(int *)&one) != 0) {  /* determine word ordering */
  77                 n0 = 0;
  78                 n1 = 1;
  79                 n2 = 2;
  80                 n3 = 3;
  81         } else {
  82                 n0 = 3;
  83                 n1 = 2;
  84                 n2 = 1;
  85                 n3 = 0;
  86         }
  87 
  88         px[n0] &= 0x7fffffff;               /* clear sign bit of x and y */
  89         py[n0] &= 0x7fffffff;
  90         k = 0x7fff0000;
  91         nx = px[n0] & k;            /* exponent of x and y */
  92         ny = py[n0] & k;
  93 
  94         if (ny > nx) {
  95                 w = x;
  96                 x = y;
  97                 y = w;
  98                 nz = ny;
  99                 ny = nx;
 100                 nx = nz;
 101         }                               /* force x > y */
 102 
 103         if ((nx - ny) >= 0x00730000)
 104                 return (x + y); /* x/y >= 2**116 */
 105 
 106         if (nx < 0x5ff30000 && ny > 0x205b0000) { /* medium x,y */
 107                 /* save and set RD to Rounding to nearest */
 108                 rd = __swapRD(fp_nearest);
 109                 w = x - y;
 110 
 111                 if (w > y) {
 112                         pt1[n0] = px[n0];
 113                         pt1[n1] = px[n1];
 114                         pt1[n2] = pt1[n3] = 0;
 115                         t2 = x - t1;
 116                         x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
 117                 } else {
 118                         x = x + x;
 119                         py1[n0] = py[n0];
 120                         py1[n1] = py[n1];
 121                         py1[n2] = py1[n3] = 0;
 122                         y2 = y - y1;
 123                         pt1[n0] = px[n0];
 124                         pt1[n1] = px[n1];
 125                         pt1[n2] = pt1[n3] = 0;
 126                         t2 = x - t1;
 127                         x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
 128                 }
 129 
 130                 if (rd != fp_nearest)
 131                         (void) __swapRD(rd);    /* restore rounding mode */
 132 
 133                 return (x);
 134         } else {
 135                 if (nx == k || ny == k) {       /* x or y is INF or NaN */
 136                         if (isinfl(x))
 137                                 t2 = x;
 138                         else if (isinfl(y))
 139                                 t2 = y;
 140                         else
 141                                 t2 = x + y;     /* invalid if x or y is sNaN */
 142 
 143                         return (t2);
 144                 }
 145 
 146                 if (ny == 0) {
 147                         if (y == zero || x == zero)
 148                                 return (x + y);
 149 
 150                         t1 = scalbnl(one, 16381);
 151                         x *= t1;
 152                         y *= t1;
 153                         return (scalbnl(one, -16381) * hypotl(x, y));
 154                 }
 155 
 156                 j = nx - 0x3fff0000;
 157                 px[n0] -= j;
 158                 py[n0] -= j;
 159                 pt1[n0] = nx;
 160                 pt1[n1] = pt1[n2] = pt1[n3] = 0;
 161                 return (t1 * hypotl(x, y));
 162         }
 163 }