125c28e83SPiotr Jasiukajtis /* 225c28e83SPiotr Jasiukajtis * CDDL HEADER START 325c28e83SPiotr Jasiukajtis * 425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 725c28e83SPiotr Jasiukajtis * 825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 1125c28e83SPiotr Jasiukajtis * and limitations under the License. 1225c28e83SPiotr Jasiukajtis * 1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 1825c28e83SPiotr Jasiukajtis * 1925c28e83SPiotr Jasiukajtis * CDDL HEADER END 2025c28e83SPiotr Jasiukajtis */ 2125c28e83SPiotr Jasiukajtis 2225c28e83SPiotr Jasiukajtis /* 2325c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2425c28e83SPiotr Jasiukajtis */ 2525c28e83SPiotr Jasiukajtis /* 2625c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2725c28e83SPiotr Jasiukajtis * Use is subject to license terms. 2825c28e83SPiotr Jasiukajtis */ 2925c28e83SPiotr Jasiukajtis 30*ddc0e0b5SRichard Lowe #pragma weak __hypot = hypot 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis /* INDENT OFF */ 3325c28e83SPiotr Jasiukajtis /* 3425c28e83SPiotr Jasiukajtis * Hypot(x, y) 3525c28e83SPiotr Jasiukajtis * by K.C. Ng for SUN 4.0 libm, updated 3/11/2003. 3625c28e83SPiotr Jasiukajtis * Method : 3725c28e83SPiotr Jasiukajtis * A. When rounding is rounded-to-nearest: 3825c28e83SPiotr Jasiukajtis * If z = x * x + y * y has error less than sqrt(2) / 2 ulp than 3925c28e83SPiotr Jasiukajtis * sqrt(z) has error less than 1 ulp. 4025c28e83SPiotr Jasiukajtis * So, compute sqrt(x*x+y*y) with some care as follows: 4125c28e83SPiotr Jasiukajtis * Assume x > y > 0; 4225c28e83SPiotr Jasiukajtis * 1. Check whether save and set rounding to round-to-nearest 4325c28e83SPiotr Jasiukajtis * 2. if x > 2y use 4425c28e83SPiotr Jasiukajtis * xh*xh+(y*y+((x-xh)*(x+xh))) for x*x+y*y 4525c28e83SPiotr Jasiukajtis * where xh = x with lower 32 bits cleared; else 4625c28e83SPiotr Jasiukajtis * 3. if x <= 2y use 4725c28e83SPiotr Jasiukajtis * x2h*yh+((x-y)*(x-y)+(x2h*(y-yh)+(x2-x2h)*y)) 4825c28e83SPiotr Jasiukajtis * where x2 = 2*x, x2h = 2x with lower 32 bits cleared, yh = y with 4925c28e83SPiotr Jasiukajtis * lower 32 bits chopped. 5025c28e83SPiotr Jasiukajtis * 5125c28e83SPiotr Jasiukajtis * B. When rounding is not rounded-to-nearest: 5225c28e83SPiotr Jasiukajtis * The following (magic) formula will yield an error less than 1 ulp. 5325c28e83SPiotr Jasiukajtis * z = sqrt(x * x + y * y) 5425c28e83SPiotr Jasiukajtis * hypot(x, y) = x + (y / ((x + z) / y)) 5525c28e83SPiotr Jasiukajtis * 5625c28e83SPiotr Jasiukajtis * NOTE: DO NOT remove parenthsis! 5725c28e83SPiotr Jasiukajtis * 5825c28e83SPiotr Jasiukajtis * Special cases: 5925c28e83SPiotr Jasiukajtis * hypot(x, y) is INF if x or y is +INF or -INF; else 6025c28e83SPiotr Jasiukajtis * hypot(x, y) is NAN if x or y is NAN. 6125c28e83SPiotr Jasiukajtis * 6225c28e83SPiotr Jasiukajtis * Accuracy: 6325c28e83SPiotr Jasiukajtis * hypot(x, y) returns sqrt(x^2+y^2) with error less than 1 ulps 6425c28e83SPiotr Jasiukajtis * (units in the last place) 6525c28e83SPiotr Jasiukajtis */ 6625c28e83SPiotr Jasiukajtis 6725c28e83SPiotr Jasiukajtis #include "libm.h" 6825c28e83SPiotr Jasiukajtis 6925c28e83SPiotr Jasiukajtis static const double 7025c28e83SPiotr Jasiukajtis zero = 0.0, 7125c28e83SPiotr Jasiukajtis onep1u = 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */ 7225c28e83SPiotr Jasiukajtis twom53 = 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */ 7325c28e83SPiotr Jasiukajtis twom768 = 6.441148769597133308e-232, /* 2^-768 */ 7425c28e83SPiotr Jasiukajtis two768 = 1.552518092300708935e+231; /* 2^768 */ 7525c28e83SPiotr Jasiukajtis 7625c28e83SPiotr Jasiukajtis /* INDENT ON */ 7725c28e83SPiotr Jasiukajtis 7825c28e83SPiotr Jasiukajtis double 7925c28e83SPiotr Jasiukajtis hypot(double x, double y) { 8025c28e83SPiotr Jasiukajtis double xh, yh, w, ax, ay; 8125c28e83SPiotr Jasiukajtis int i, j, nx, ny, ix, iy, iscale = 0; 8225c28e83SPiotr Jasiukajtis unsigned lx, ly; 8325c28e83SPiotr Jasiukajtis 8425c28e83SPiotr Jasiukajtis ix = ((int *) &x)[HIWORD] & ~0x80000000; 8525c28e83SPiotr Jasiukajtis lx = ((int *) &x)[LOWORD]; 8625c28e83SPiotr Jasiukajtis iy = ((int *) &y)[HIWORD] & ~0x80000000; 8725c28e83SPiotr Jasiukajtis ly = ((int *) &y)[LOWORD]; 8825c28e83SPiotr Jasiukajtis /* 8925c28e83SPiotr Jasiukajtis * Force ax = |x| ~>~ ay = |y| 9025c28e83SPiotr Jasiukajtis */ 9125c28e83SPiotr Jasiukajtis if (iy > ix) { 9225c28e83SPiotr Jasiukajtis ax = fabs(y); 9325c28e83SPiotr Jasiukajtis ay = fabs(x); 9425c28e83SPiotr Jasiukajtis i = ix; 9525c28e83SPiotr Jasiukajtis ix = iy; 9625c28e83SPiotr Jasiukajtis iy = i; 9725c28e83SPiotr Jasiukajtis i = lx; 9825c28e83SPiotr Jasiukajtis lx = ly; 9925c28e83SPiotr Jasiukajtis ly = i; 10025c28e83SPiotr Jasiukajtis } else { 10125c28e83SPiotr Jasiukajtis ax = fabs(x); 10225c28e83SPiotr Jasiukajtis ay = fabs(y); 10325c28e83SPiotr Jasiukajtis } 10425c28e83SPiotr Jasiukajtis nx = ix >> 20; 10525c28e83SPiotr Jasiukajtis ny = iy >> 20; 10625c28e83SPiotr Jasiukajtis j = nx - ny; 10725c28e83SPiotr Jasiukajtis /* 10825c28e83SPiotr Jasiukajtis * x >= 2^500 (x*x or y*y may overflow) 10925c28e83SPiotr Jasiukajtis */ 11025c28e83SPiotr Jasiukajtis if (nx >= 0x5f3) { 11125c28e83SPiotr Jasiukajtis if (nx == 0x7ff) { /* inf or NaN, signal of sNaN */ 11225c28e83SPiotr Jasiukajtis if (((ix - 0x7ff00000) | lx) == 0) 11325c28e83SPiotr Jasiukajtis return (ax == ay ? ay : ax); 11425c28e83SPiotr Jasiukajtis else if (((iy - 0x7ff00000) | ly) == 0) 11525c28e83SPiotr Jasiukajtis return (ay == ax ? ax : ay); 11625c28e83SPiotr Jasiukajtis else 11725c28e83SPiotr Jasiukajtis return (ax * ay); /* + -> * for Cheetah */ 11825c28e83SPiotr Jasiukajtis } else if (j > 32) { /* x >> y */ 11925c28e83SPiotr Jasiukajtis if (j <= 53) 12025c28e83SPiotr Jasiukajtis ay *= twom53; 12125c28e83SPiotr Jasiukajtis ax += ay; 12225c28e83SPiotr Jasiukajtis if (((int *) &ax)[HIWORD] == 0x7ff00000) 12325c28e83SPiotr Jasiukajtis ax = _SVID_libm_err(x, y, 4); 12425c28e83SPiotr Jasiukajtis return (ax); 12525c28e83SPiotr Jasiukajtis } 12625c28e83SPiotr Jasiukajtis ax *= twom768; 12725c28e83SPiotr Jasiukajtis ay *= twom768; 12825c28e83SPiotr Jasiukajtis iscale = 2; 12925c28e83SPiotr Jasiukajtis ix -= 768 << 20; 13025c28e83SPiotr Jasiukajtis iy -= 768 << 20; 13125c28e83SPiotr Jasiukajtis } 13225c28e83SPiotr Jasiukajtis /* 13325c28e83SPiotr Jasiukajtis * y < 2^-450 (x*x or y*y may underflow) 13425c28e83SPiotr Jasiukajtis */ 13525c28e83SPiotr Jasiukajtis else if (ny < 0x23d) { 13625c28e83SPiotr Jasiukajtis if ((ix | lx) == 0) 13725c28e83SPiotr Jasiukajtis return (ay); 13825c28e83SPiotr Jasiukajtis if ((iy | ly) == 0) 13925c28e83SPiotr Jasiukajtis return (ax); 14025c28e83SPiotr Jasiukajtis if (j > 53) /* x >> y */ 14125c28e83SPiotr Jasiukajtis return (ax + ay); 14225c28e83SPiotr Jasiukajtis iscale = 1; 14325c28e83SPiotr Jasiukajtis ax *= two768; 14425c28e83SPiotr Jasiukajtis ay *= two768; 14525c28e83SPiotr Jasiukajtis if (nx == 0) { 14625c28e83SPiotr Jasiukajtis if (ax == zero) /* guard subnormal flush to zero */ 14725c28e83SPiotr Jasiukajtis return (ax); 14825c28e83SPiotr Jasiukajtis ix = ((int *) &ax)[HIWORD]; 14925c28e83SPiotr Jasiukajtis } else 15025c28e83SPiotr Jasiukajtis ix += 768 << 20; 15125c28e83SPiotr Jasiukajtis if (ny == 0) { 15225c28e83SPiotr Jasiukajtis if (ay == zero) /* guard subnormal flush to zero */ 15325c28e83SPiotr Jasiukajtis return (ax * twom768); 15425c28e83SPiotr Jasiukajtis iy = ((int *) &ay)[HIWORD]; 15525c28e83SPiotr Jasiukajtis } else 15625c28e83SPiotr Jasiukajtis iy += 768 << 20; 15725c28e83SPiotr Jasiukajtis j = (ix >> 20) - (iy >> 20); 15825c28e83SPiotr Jasiukajtis if (j > 32) { /* x >> y */ 15925c28e83SPiotr Jasiukajtis if (j <= 53) 16025c28e83SPiotr Jasiukajtis ay *= twom53; 16125c28e83SPiotr Jasiukajtis return ((ax + ay) * twom768); 16225c28e83SPiotr Jasiukajtis } 16325c28e83SPiotr Jasiukajtis } else if (j > 32) { /* x >> y */ 16425c28e83SPiotr Jasiukajtis if (j <= 53) 16525c28e83SPiotr Jasiukajtis ay *= twom53; 16625c28e83SPiotr Jasiukajtis return (ax + ay); 16725c28e83SPiotr Jasiukajtis } 16825c28e83SPiotr Jasiukajtis /* 16925c28e83SPiotr Jasiukajtis * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32 17025c28e83SPiotr Jasiukajtis * First check rounding mode by comparing onep1u*onep1u with onep1u+twom53. 17125c28e83SPiotr Jasiukajtis * Make sure the computation is done at run-time. 17225c28e83SPiotr Jasiukajtis */ 17325c28e83SPiotr Jasiukajtis if (((lx | ly) << 5) == 0) { 17425c28e83SPiotr Jasiukajtis ay = ay * ay; 17525c28e83SPiotr Jasiukajtis ax += ay / (ax + sqrt(ax * ax + ay)); 17625c28e83SPiotr Jasiukajtis } else 17725c28e83SPiotr Jasiukajtis if (onep1u * onep1u != onep1u + twom53) { 17825c28e83SPiotr Jasiukajtis /* round-to-zero, positive, negative mode */ 17925c28e83SPiotr Jasiukajtis /* magic formula with less than an ulp error */ 18025c28e83SPiotr Jasiukajtis w = sqrt(ax * ax + ay * ay); 18125c28e83SPiotr Jasiukajtis ax += ay / ((ax + w) / ay); 18225c28e83SPiotr Jasiukajtis } else { 18325c28e83SPiotr Jasiukajtis /* round-to-nearest mode */ 18425c28e83SPiotr Jasiukajtis w = ax - ay; 18525c28e83SPiotr Jasiukajtis if (w > ay) { 18625c28e83SPiotr Jasiukajtis ((int *) &xh)[HIWORD] = ix; 18725c28e83SPiotr Jasiukajtis ((int *) &xh)[LOWORD] = 0; 18825c28e83SPiotr Jasiukajtis ay = ay * ay + (ax - xh) * (ax + xh); 18925c28e83SPiotr Jasiukajtis ax = sqrt(xh * xh + ay); 19025c28e83SPiotr Jasiukajtis } else { 19125c28e83SPiotr Jasiukajtis ax = ax + ax; 19225c28e83SPiotr Jasiukajtis ((int *) &xh)[HIWORD] = ix + 0x00100000; 19325c28e83SPiotr Jasiukajtis ((int *) &xh)[LOWORD] = 0; 19425c28e83SPiotr Jasiukajtis ((int *) &yh)[HIWORD] = iy; 19525c28e83SPiotr Jasiukajtis ((int *) &yh)[LOWORD] = 0; 19625c28e83SPiotr Jasiukajtis ay = w * w + ((ax - xh) * yh + (ay - yh) * ax); 19725c28e83SPiotr Jasiukajtis ax = sqrt(xh * yh + ay); 19825c28e83SPiotr Jasiukajtis } 19925c28e83SPiotr Jasiukajtis } 20025c28e83SPiotr Jasiukajtis if (iscale > 0) { 20125c28e83SPiotr Jasiukajtis if (iscale == 1) 20225c28e83SPiotr Jasiukajtis ax *= twom768; 20325c28e83SPiotr Jasiukajtis else { 20425c28e83SPiotr Jasiukajtis ax *= two768; /* must generate side effect here */ 20525c28e83SPiotr Jasiukajtis if (((int *) &ax)[HIWORD] == 0x7ff00000) 20625c28e83SPiotr Jasiukajtis ax = _SVID_libm_err(x, y, 4); 20725c28e83SPiotr Jasiukajtis } 20825c28e83SPiotr Jasiukajtis } 20925c28e83SPiotr Jasiukajtis return (ax); 21025c28e83SPiotr Jasiukajtis } 211