cosl.c revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
ddc0e0b53c661f6e439e3b7072b3ef353eadb4afRichard Lowe#pragma weak __cosl = cosl
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* cosl(x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Table look-up algorithm by K.C. Ng, November, 1989.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * kernel function:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * __k_sinl ... sin function on [-pi/4,pi/4]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * __k_cosl ... cos function on [-pi/4,pi/4]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * __rem_pio2l ... argument reduction routine
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Method.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Let S and C denote the sin and cos respectively on [-PI/4, +PI/4].
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1. Assume the argument x is reduced to y1+y2 = x-k*pi/2 in
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [-pi/2 , +pi/2], and let n = k mod 4.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2. Let S=S(y1+y2), C=C(y1+y2). Depending on n, we have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * n sin(x) cos(x) tan(x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ----------------------------------------------------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 0 S C S/C
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 C -S -C/S
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2 -S -C S/C
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 3 -C S -C/S
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ----------------------------------------------------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Special cases:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Let trig be any of sin, cos, or tan.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * trig(+-INF) is NaN, with signals;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * trig(NaN) is that NaN;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Accuracy:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * computer TRIG(x) returns trig(x) nearly rounded.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "libm.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "longdouble.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include <sys/isa_defs.h>
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtislong double
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtiscosl(long double x) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis long double y[2], z = 0.0L;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int n, ix;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int *px = (int *) &x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* trig(Inf or NaN) is NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (!finitel(x))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return x - x;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* High word of x. */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#if defined(__i386) || defined(__amd64)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis XTOI(px, ix);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ix = px[0];
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#endif
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* |x| ~< pi/4 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ix &= 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix <= 0x3ffe9220)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return __k_cosl(x, z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* argument reduction needed */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis n = __rem_pio2l(x, y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis switch (n & 3) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis case 0:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return __k_cosl(y[0], y[1]);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis case 1:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return -__k_sinl(y[0], y[1]);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis case 2:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return -__k_cosl(y[0], y[1]);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis case 3:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return __k_sinl(y[0], y[1]);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* NOTREACHED */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return 0.0L;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis}