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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 #if defined(ELFOBJ)
  31 #pragma weak hypotl = __hypotl
  32 #endif
  33 
  34 /*
  35  * hypotl(x,y)
  36  * Method :
  37  *      If z=x*x+y*y has error less than sqrt(2)/2 ulp than sqrt(z) has
  38  *      error less than 1 ulp.
  39  *      So, compute sqrt(x*x+y*y) with some care as follows:
  40  *      Assume x>y>0;
  41  *      1. save and set rounding to round-to-nearest
  42  *      2. if x > 2y  use
  43  *              x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
  44  *      where x1 = x with lower 32 bits cleared, x2 = x-x1; else
  45  *      3. if x <= 2y use
  46  *              t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
  47  *      where t1 = 2x with lower 64 bits cleared, t2 = 2x-t1, y1= y with
  48  *      lower 32 bits cleared, y2 = y-y1.
  49  *
  50  *      NOTE: DO NOT remove parenthsis!
  51  *
  52  * Special cases:
  53  *      hypot(x,y) is INF if x or y is +INF or -INF; else
  54  *      hypot(x,y) is NAN if x or y is NAN.
  55  *
  56  * Accuracy:
  57  *      hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
  58  *      in the last place)
  59  */
  60 
  61 #include "libm.h"
  62 
  63 #if defined(__x86)
  64 extern enum fp_direction_type __swap87RD(enum fp_direction_type);
  65 
  66 #define k       0x7fff
  67 
  68 long double
  69 hypotl(long double x, long double y) {
  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, nx, ny, nz;
  75 
  76         px[2] &= 0x7fff;    /* clear sign bit and padding bits of x and y */
  77         py[2] &= 0x7fff;
  78         nx = px[2];             /* biased exponent of x and y */
  79         ny = py[2];
  80         if (ny > nx) {
  81                 w = x;
  82                 x = y;
  83                 y = w;
  84                 nz = ny;
  85                 ny = nx;
  86                 nx = nz;
  87         }                       /* force nx >= ny */
  88         if (nx - ny >= 66)
  89                 return (x + y); /* x / y >= 2**65 */
  90         if (nx < 0x5ff3 && ny > 0x205b) { /* medium x,y */
  91                 /* save and set RD to Rounding to nearest */
  92                 rd = __swap87RD(fp_nearest);
  93                 w = x - y;
  94                 if (w > y) {
  95                         pt1[2] = px[2];
  96                         pt1[1] = px[1];
  97                         pt1[0] = 0;
  98                         t2 = x - t1;
  99                         x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
 100                 } else {
 101                         x += x;
 102                         py1[2] = py[2];
 103                         py1[1] = py[1];
 104                         py1[0] = 0;
 105                         y2 = y - y1;
 106                         pt1[2] = px[2];
 107                         pt1[1] = px[1];
 108                         pt1[0] = 0;
 109                         t2 = x - t1;
 110                         x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
 111                 }
 112                 if (rd != fp_nearest)
 113                         __swap87RD(rd); /* restore rounding mode */
 114                 return (x);
 115         } else {
 116                 if (nx == k || ny == k) {       /* x or y is INF or NaN */
 117                         /* since nx >= ny; nx is always k within this block */
 118                         if (px[1] == 0x80000000 && px[0] == 0)
 119                                 return (x);
 120                         else if (ny == k && py[1] == 0x80000000 && py[0] == 0)
 121                                 return (y);
 122                         else
 123                                 return (x + y);
 124                 }
 125                 if (ny == 0) {
 126                         if (y == 0.L || x == 0.L)
 127                                 return (x + y);
 128                         pt1[2] = 0x3fff + 16381;
 129                         pt1[1] = 0x80000000;
 130                         pt1[0] = 0;
 131                         py1[2] = 0x3fff - 16381;
 132                         py1[1] = 0x80000000;
 133                         py1[0] = 0;
 134                         x *= t1;
 135                         y *= t1;
 136                         return (y1 * hypotl(x, y));
 137                 }
 138                 j = nx - 0x3fff;
 139                 px[2] -= j;
 140                 py[2] -= j;
 141                 pt1[2] = nx;
 142                 pt1[1] = 0x80000000;
 143                 pt1[0] = 0;
 144                 return (t1 * hypotl(x, y));
 145         }
 146 }
 147 #endif