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 __acos = acos 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis /* INDENT OFF */ 3325c28e83SPiotr Jasiukajtis /* 3425c28e83SPiotr Jasiukajtis * acos(x) 3525c28e83SPiotr Jasiukajtis * Method : 3625c28e83SPiotr Jasiukajtis * acos(x) = pi/2 - asin(x) 3725c28e83SPiotr Jasiukajtis * acos(-x) = pi/2 + asin(x) 3825c28e83SPiotr Jasiukajtis * For |x|<=0.5 3925c28e83SPiotr Jasiukajtis * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) 4025c28e83SPiotr Jasiukajtis * For x>0.5 4125c28e83SPiotr Jasiukajtis * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) 4225c28e83SPiotr Jasiukajtis * = 2asin(sqrt((1-x)/2)) 4325c28e83SPiotr Jasiukajtis * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) 4425c28e83SPiotr Jasiukajtis * = 2f + (2c + 2s*z*R(z)) 4525c28e83SPiotr Jasiukajtis * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term 4625c28e83SPiotr Jasiukajtis * for f so that f+c ~ sqrt(z). 4725c28e83SPiotr Jasiukajtis * For x<-0.5 4825c28e83SPiotr Jasiukajtis * acos(x) = pi - 2asin(sqrt((1-|x|)/2)) 4925c28e83SPiotr Jasiukajtis * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) 5025c28e83SPiotr Jasiukajtis * 5125c28e83SPiotr Jasiukajtis * Special cases: 5225c28e83SPiotr Jasiukajtis * if x is NaN, return x itself; 5325c28e83SPiotr Jasiukajtis * if |x|>1, return NaN with invalid signal. 5425c28e83SPiotr Jasiukajtis * 5525c28e83SPiotr Jasiukajtis * Function needed: sqrt 5625c28e83SPiotr Jasiukajtis */ 5725c28e83SPiotr Jasiukajtis /* INDENT ON */ 5825c28e83SPiotr Jasiukajtis 5925c28e83SPiotr Jasiukajtis #include "libm_protos.h" /* _SVID_libm_error */ 6025c28e83SPiotr Jasiukajtis #include "libm_macros.h" 6125c28e83SPiotr Jasiukajtis #include <math.h> 6225c28e83SPiotr Jasiukajtis 6325c28e83SPiotr Jasiukajtis /* INDENT OFF */ 6425c28e83SPiotr Jasiukajtis static const double xxx[] = { 6525c28e83SPiotr Jasiukajtis /* one */ 1.00000000000000000000e+00, /* 3FF00000, 00000000 */ 6625c28e83SPiotr Jasiukajtis /* pi */ 3.14159265358979311600e+00, /* 400921FB, 54442D18 */ 6725c28e83SPiotr Jasiukajtis /* pio2_hi */ 1.57079632679489655800e+00, /* 3FF921FB, 54442D18 */ 6825c28e83SPiotr Jasiukajtis /* pio2_lo */ 6.12323399573676603587e-17, /* 3C91A626, 33145C07 */ 6925c28e83SPiotr Jasiukajtis /* pS0 */ 1.66666666666666657415e-01, /* 3FC55555, 55555555 */ 7025c28e83SPiotr Jasiukajtis /* pS1 */ -3.25565818622400915405e-01, /* BFD4D612, 03EB6F7D */ 7125c28e83SPiotr Jasiukajtis /* pS2 */ 2.01212532134862925881e-01, /* 3FC9C155, 0E884455 */ 7225c28e83SPiotr Jasiukajtis /* pS3 */ -4.00555345006794114027e-02, /* BFA48228, B5688F3B */ 7325c28e83SPiotr Jasiukajtis /* pS4 */ 7.91534994289814532176e-04, /* 3F49EFE0, 7501B288 */ 7425c28e83SPiotr Jasiukajtis /* pS5 */ 3.47933107596021167570e-05, /* 3F023DE1, 0DFDF709 */ 7525c28e83SPiotr Jasiukajtis /* qS1 */ -2.40339491173441421878e+00, /* C0033A27, 1C8A2D4B */ 7625c28e83SPiotr Jasiukajtis /* qS2 */ 2.02094576023350569471e+00, /* 40002AE5, 9C598AC8 */ 7725c28e83SPiotr Jasiukajtis /* qS3 */ -6.88283971605453293030e-01, /* BFE6066C, 1B8D0159 */ 7825c28e83SPiotr Jasiukajtis /* qS4 */ 7.70381505559019352791e-02 /* 3FB3B8C5, B12E9282 */ 7925c28e83SPiotr Jasiukajtis }; 8025c28e83SPiotr Jasiukajtis #define one xxx[0] 8125c28e83SPiotr Jasiukajtis #define pi xxx[1] 8225c28e83SPiotr Jasiukajtis #define pio2_hi xxx[2] 8325c28e83SPiotr Jasiukajtis #define pio2_lo xxx[3] 8425c28e83SPiotr Jasiukajtis #define pS0 xxx[4] 8525c28e83SPiotr Jasiukajtis #define pS1 xxx[5] 8625c28e83SPiotr Jasiukajtis #define pS2 xxx[6] 8725c28e83SPiotr Jasiukajtis #define pS3 xxx[7] 8825c28e83SPiotr Jasiukajtis #define pS4 xxx[8] 8925c28e83SPiotr Jasiukajtis #define pS5 xxx[9] 9025c28e83SPiotr Jasiukajtis #define qS1 xxx[10] 9125c28e83SPiotr Jasiukajtis #define qS2 xxx[11] 9225c28e83SPiotr Jasiukajtis #define qS3 xxx[12] 9325c28e83SPiotr Jasiukajtis #define qS4 xxx[13] 9425c28e83SPiotr Jasiukajtis /* INDENT ON */ 9525c28e83SPiotr Jasiukajtis 9625c28e83SPiotr Jasiukajtis double 9725c28e83SPiotr Jasiukajtis acos(double x) { 9825c28e83SPiotr Jasiukajtis double z, p, q, r, w, s, c, df; 9925c28e83SPiotr Jasiukajtis int hx, ix; 10025c28e83SPiotr Jasiukajtis 10125c28e83SPiotr Jasiukajtis hx = ((int *) &x)[HIWORD]; 10225c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff; 10325c28e83SPiotr Jasiukajtis if (ix >= 0x3ff00000) { /* |x| >= 1 */ 10425c28e83SPiotr Jasiukajtis if (((ix - 0x3ff00000) | ((int *) &x)[LOWORD]) == 0) { 10525c28e83SPiotr Jasiukajtis /* |x| == 1 */ 10625c28e83SPiotr Jasiukajtis if (hx > 0) /* acos(1) = 0 */ 10725c28e83SPiotr Jasiukajtis return (0.0); 10825c28e83SPiotr Jasiukajtis else /* acos(-1) = pi */ 10925c28e83SPiotr Jasiukajtis return (pi + 2.0 * pio2_lo); 11025c28e83SPiotr Jasiukajtis } else if (isnan(x)) 11125c28e83SPiotr Jasiukajtis #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN) 11225c28e83SPiotr Jasiukajtis return (ix >= 0x7ff80000 ? x : (x - x) / (x - x)); 11325c28e83SPiotr Jasiukajtis /* assumes sparc-like QNaN */ 11425c28e83SPiotr Jasiukajtis #else 11525c28e83SPiotr Jasiukajtis return (x - x) / (x - x); /* acos(|x|>1) is NaN */ 11625c28e83SPiotr Jasiukajtis #endif 11725c28e83SPiotr Jasiukajtis else 11825c28e83SPiotr Jasiukajtis return (_SVID_libm_err(x, x, 1)); 11925c28e83SPiotr Jasiukajtis } 12025c28e83SPiotr Jasiukajtis if (ix < 0x3fe00000) { /* |x| < 0.5 */ 12125c28e83SPiotr Jasiukajtis if (ix <= 0x3c600000) 12225c28e83SPiotr Jasiukajtis return (pio2_hi + pio2_lo); /* if |x| < 2**-57 */ 12325c28e83SPiotr Jasiukajtis z = x * x; 12425c28e83SPiotr Jasiukajtis p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + 12525c28e83SPiotr Jasiukajtis z * (pS4 + z * pS5))))); 12625c28e83SPiotr Jasiukajtis q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); 12725c28e83SPiotr Jasiukajtis r = p / q; 12825c28e83SPiotr Jasiukajtis return (pio2_hi - (x - (pio2_lo - x * r))); 12925c28e83SPiotr Jasiukajtis } else if (hx < 0) { 13025c28e83SPiotr Jasiukajtis /* x < -0.5 */ 13125c28e83SPiotr Jasiukajtis z = (one + x) * 0.5; 13225c28e83SPiotr Jasiukajtis p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + 13325c28e83SPiotr Jasiukajtis z * (pS4 + z * pS5))))); 13425c28e83SPiotr Jasiukajtis q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); 13525c28e83SPiotr Jasiukajtis s = sqrt(z); 13625c28e83SPiotr Jasiukajtis r = p / q; 13725c28e83SPiotr Jasiukajtis w = r * s - pio2_lo; 13825c28e83SPiotr Jasiukajtis return (pi - 2.0 * (s + w)); 13925c28e83SPiotr Jasiukajtis } else { 14025c28e83SPiotr Jasiukajtis /* x > 0.5 */ 14125c28e83SPiotr Jasiukajtis z = (one - x) * 0.5; 14225c28e83SPiotr Jasiukajtis s = sqrt(z); 14325c28e83SPiotr Jasiukajtis df = s; 14425c28e83SPiotr Jasiukajtis ((int *) &df)[LOWORD] = 0; 14525c28e83SPiotr Jasiukajtis c = (z - df * df) / (s + df); 14625c28e83SPiotr Jasiukajtis p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + 14725c28e83SPiotr Jasiukajtis z * (pS4 + z * pS5))))); 14825c28e83SPiotr Jasiukajtis q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); 14925c28e83SPiotr Jasiukajtis r = p / q; 15025c28e83SPiotr Jasiukajtis w = r * s + c; 15125c28e83SPiotr Jasiukajtis return (2.0 * (df + w)); 15225c28e83SPiotr Jasiukajtis } 15325c28e83SPiotr Jasiukajtis } 154