Print this page
11210 libm should be cstyle(1ONBLD) clean


   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 #pragma weak __hypotl = hypotl
  31 
  32 /*
  33  * hypotl(x,y)
  34  * Method :
  35  *      If z=x*x+y*y has error less than sqrt(2)/2 ulp than sqrt(z) has
  36  *      error less than 1 ulp.
  37  *      So, compute sqrt(x*x+y*y) with some care as follows:
  38  *      Assume x>y>0;
  39  *      1. save and set rounding to round-to-nearest
  40  *      2. if x > 2y  use
  41  *              x1*x1+(y*y+(x2*(x+x2))) for x*x+y*y
  42  *      where x1 = x with lower 32 bits cleared, x2 = x-x1; else
  43  *      3. if x <= 2y use
  44  *              t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))


  47  *
  48  *      NOTE: DO NOT remove parenthsis!
  49  *
  50  * Special cases:
  51  *      hypot(x,y) is INF if x or y is +INF or -INF; else
  52  *      hypot(x,y) is NAN if x or y is NAN.
  53  *
  54  * Accuracy:
  55  *      hypot(x,y) returns sqrt(x^2+y^2) with error less than 1 ulps (units
  56  *      in the last place)
  57  */
  58 
  59 #include "libm.h"
  60 
  61 #if defined(__x86)
  62 extern enum fp_direction_type __swap87RD(enum fp_direction_type);
  63 
  64 #define k       0x7fff
  65 
  66 long double
  67 hypotl(long double x, long double y) {

  68         long double t1, t2, y1, y2, w;
  69         int *px = (int *) &x, *py = (int *) &y;
  70         int *pt1 = (int *) &t1, *py1 = (int *) &y1;
  71         enum fp_direction_type rd;
  72         int j, nx, ny, nz;
  73 
  74         px[2] &= 0x7fff;    /* clear sign bit and padding bits of x and y */
  75         py[2] &= 0x7fff;
  76         nx = px[2];             /* biased exponent of x and y */
  77         ny = py[2];

  78         if (ny > nx) {
  79                 w = x;
  80                 x = y;
  81                 y = w;
  82                 nz = ny;
  83                 ny = nx;
  84                 nx = nz;
  85         }                       /* force nx >= ny */

  86         if (nx - ny >= 66)
  87                 return (x + y); /* x / y >= 2**65 */

  88         if (nx < 0x5ff3 && ny > 0x205b) { /* medium x,y */
  89                 /* save and set RD to Rounding to nearest */
  90                 rd = __swap87RD(fp_nearest);
  91                 w = x - y;

  92                 if (w > y) {
  93                         pt1[2] = px[2];
  94                         pt1[1] = px[1];
  95                         pt1[0] = 0;
  96                         t2 = x - t1;
  97                         x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
  98                 } else {
  99                         x += x;
 100                         py1[2] = py[2];
 101                         py1[1] = py[1];
 102                         py1[0] = 0;
 103                         y2 = y - y1;
 104                         pt1[2] = px[2];
 105                         pt1[1] = px[1];
 106                         pt1[0] = 0;
 107                         t2 = x - t1;
 108                         x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
 109                 }

 110                 if (rd != fp_nearest)
 111                         __swap87RD(rd); /* restore rounding mode */

 112                 return (x);
 113         } else {
 114                 if (nx == k || ny == k) {       /* x or y is INF or NaN */
 115                         /* since nx >= ny; nx is always k within this block */
 116                         if (px[1] == 0x80000000 && px[0] == 0)
 117                                 return (x);
 118                         else if (ny == k && py[1] == 0x80000000 && py[0] == 0)
 119                                 return (y);
 120                         else
 121                                 return (x + y);
 122                 }

 123                 if (ny == 0) {
 124                         if (y == 0.L || x == 0.L)
 125                                 return (x + y);

 126                         pt1[2] = 0x3fff + 16381;
 127                         pt1[1] = 0x80000000;
 128                         pt1[0] = 0;
 129                         py1[2] = 0x3fff - 16381;
 130                         py1[1] = 0x80000000;
 131                         py1[0] = 0;
 132                         x *= t1;
 133                         y *= t1;
 134                         return (y1 * hypotl(x, y));
 135                 }

 136                 j = nx - 0x3fff;
 137                 px[2] -= j;
 138                 py[2] -= j;
 139                 pt1[2] = nx;
 140                 pt1[1] = 0x80000000;
 141                 pt1[0] = 0;
 142                 return (t1 * hypotl(x, y));
 143         }
 144 }
 145 #endif


   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  * hypotl(x,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 32 bits cleared, x2 = x-x1; else
  44  *      3. if x <= 2y use
  45  *              t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))


  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 
  62 #if defined(__x86)
  63 extern enum fp_direction_type __swap87RD(enum fp_direction_type);
  64 
  65 #define k       0x7fff
  66 
  67 long double
  68 hypotl(long double x, long double y)
  69 {
  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 
  81         if (ny > nx) {
  82                 w = x;
  83                 x = y;
  84                 y = w;
  85                 nz = ny;
  86                 ny = nx;
  87                 nx = nz;
  88         }                               /* force nx >= ny */
  89 
  90         if (nx - ny >= 66)
  91                 return (x + y); /* x / y >= 2**65 */
  92 
  93         if (nx < 0x5ff3 && ny > 0x205b) { /* medium x,y */
  94                 /* save and set RD to Rounding to nearest */
  95                 rd = __swap87RD(fp_nearest);
  96                 w = x - y;
  97 
  98                 if (w > y) {
  99                         pt1[2] = px[2];
 100                         pt1[1] = px[1];
 101                         pt1[0] = 0;
 102                         t2 = x - t1;
 103                         x = sqrtl(t1 * t1 - (y * (-y) - t2 * (x + t1)));
 104                 } else {
 105                         x += x;
 106                         py1[2] = py[2];
 107                         py1[1] = py[1];
 108                         py1[0] = 0;
 109                         y2 = y - y1;
 110                         pt1[2] = px[2];
 111                         pt1[1] = px[1];
 112                         pt1[0] = 0;
 113                         t2 = x - t1;
 114                         x = sqrtl(t1 * y1 - (w * (-w) - (t2 * y1 + y2 * x)));
 115                 }
 116 
 117                 if (rd != fp_nearest)
 118                         __swap87RD(rd);         /* restore rounding mode */
 119 
 120                 return (x);
 121         } else {
 122                 if (nx == k || ny == k) {       /* x or y is INF or NaN */
 123                         /* since nx >= ny; nx is always k within this block */
 124                         if (px[1] == 0x80000000 && px[0] == 0)
 125                                 return (x);
 126                         else if (ny == k && py[1] == 0x80000000 && py[0] == 0)
 127                                 return (y);
 128                         else
 129                                 return (x + y);
 130                 }
 131 
 132                 if (ny == 0) {
 133                         if (y == 0.L || x == 0.L)
 134                                 return (x + y);
 135 
 136                         pt1[2] = 0x3fff + 16381;
 137                         pt1[1] = 0x80000000;
 138                         pt1[0] = 0;
 139                         py1[2] = 0x3fff - 16381;
 140                         py1[1] = 0x80000000;
 141                         py1[0] = 0;
 142                         x *= t1;
 143                         y *= t1;
 144                         return (y1 * hypotl(x, y));
 145                 }
 146 
 147                 j = nx - 0x3fff;
 148                 px[2] -= j;
 149                 py[2] -= j;
 150                 pt1[2] = nx;
 151                 pt1[1] = 0x80000000;
 152                 pt1[0] = 0;
 153                 return (t1 * hypotl(x, y));
 154         }
 155 }
 156 #endif